unit U_LabelMapSet;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxPC, cxCheckComboBox, cxDropDownEdit, Menus, RM_e_Xls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu,
  dxSkinsCore, dxSkinsDefaultPainters, cxContainer, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter, ImgList;

type
  TfrmLabelMapSet = class(Tform)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    Panel1: TPanel;
    v2Column8: TcxGridDBColumn;
    Label1: TLabel;
    v2Column12: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Label2: TLabel;
    v2Column1: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    ADOConnection1: TADOConnection;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    DS_2: TDataSource;
    ToolBar2: TToolBar;
    ToolButton12: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton3: TToolButton;
    ADO_2: TADOQuery;
    PM_2: TPopupMenu;
    N3: TMenuItem;
    Tv1Column3: TcxGridDBColumn;
    LMName: TcxTextEdit;
    LMType: TcxTextEdit;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ThreeImgList: TImageList;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TextEdit(Sender: TObject);
    procedure LMNamePropertiesChange(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    procedure Getfields(MSql: Integer);
    procedure InitAdo(Ado: TADOQuery; IsSql: Boolean; LMSql, FFiltration: string);
  public
    IsSql1, IsSql2, IsSql3, IsSql4, IsSql5: Boolean;
    FLMType: string;
    FFiltration1, FFiltration2, FFiltration3, FFiltration4, FFiltration5: string;
    { Public declarations }
  end;

var
  frmLabelMapSet: TfrmLabelMapSet;

implementation

uses
  U_DataLink, U_RTFun, superobject;

{$R *.dfm}
procedure TfrmLabelMapSet.InitAdo(Ado: TADOQuery; IsSql: Boolean; LMSql, FFiltration: string);
begin
  with Ado do
  begin
    Close;
    sql.Clear;
    if IsSql then
    begin
      sql.add('exec ' + trim(CDS_1.fieldbyname(LMSql).AsString));
      sql.add(FFiltration);
    end
    else
    begin
      sql.add('exec ' + trim(CDS_1.fieldbyname(LMSql).AsString) + '  ' + quotedstr(Trim(FFiltration)));
    end;
    Open;
  end;
  TV2.BeginUpdate();
  TV2.ClearItems; //清空数据
  (TV2.DataController as IcxCustomGridDataController).DeleteAllItems;  //删除所有列
  (TV2.DataController as IcxCustomGridDataController).CreateAllItems(false); //创建数据源中的所有列
  TV2.EndUpdate;
  TV2.ApplyBestFit; //让列宽自适应 .BestFitMaxWidth;
end;

procedure TfrmLabelMapSet.Getfields(MSql: Integer);
begin

  case MSql of
    1:
      begin
        if trim(CDS_1.fieldbyname('LMSql1').AsString) <> '' then
          InitAdo(ADO_2, IsSql1, 'LMSql1', FFiltration1);
      end;
    2:
      begin
        if trim(CDS_1.fieldbyname('LMSql2').AsString) <> '' then
          InitAdo(ADO_2, IsSql2, 'LMSql2', FFiltration2);
      end;
    3:
      begin
        if trim(CDS_1.fieldbyname('LMSql3').AsString) <> '' then
          InitAdo(ADO_2, IsSql3, 'LMSql3', FFiltration3);
      end;
    4:
      begin
        if trim(CDS_1.fieldbyname('LMSql4').AsString) <> '' then
          InitAdo(ADO_2, IsSql4, 'LMSql4', FFiltration4);
      end;
    5:
      begin
        if trim(CDS_1.fieldbyname('LMSql5').AsString) <> '' then
          InitAdo(ADO_2, IsSql5, 'LMSql5', FFiltration5);
      end;
  end;

end;

procedure TfrmLabelMapSet.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* from  BS_Label_Map A  ');
      sql.Add(' where LMType=' + quotedstr(FLMType));
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_1);
    SInitCDSData20(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmLabelMapSet.LMNamePropertiesChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmLabelMapSet.FormDestroy(Sender: TObject);
begin

  frmLabelMapSet := nil;
end;

procedure TfrmLabelMapSet.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Action := caFree;
end;

procedure TfrmLabelMapSet.FormCreate(Sender: TObject);
begin
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
//    ADOQueryBaseCmd.Connection := ADOConnection1;
//    ADOQueryBaseTemp.Connection := ADOConnection1;
  except

  end;
end;

procedure TfrmLabelMapSet.TBCloseClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmLabelMapSet.FormShow(Sender: TObject);
begin
  ReadCxGrid(Trim(Self.Caption), Tv1, '标签管理');
  InitGrid();
end;

procedure TfrmLabelMapSet.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmLabelMapSet.ToolButton12Click(Sender: TObject);
begin
  Getfields(1);
end;

procedure TfrmLabelMapSet.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption), Tv1, '标签管理');

end;

procedure TfrmLabelMapSet.ToolButton2Click(Sender: TObject);
var
  sql: string;
begin
  if ADOQueryMain.Active then
  begin
    sql := SGetFilters(Panel1, 1, 2);
    SDofilter(ADOQueryMain, sql);
    SCreateCDS20(ADOQueryMain, CDS_1);
    SInitCDSData20(ADOQueryMain, CDS_1);
  end;
end;

procedure TfrmLabelMapSet.ToolButton3Click(Sender: TObject);
begin
  TcxGridToExcel('sql字段', cxgrid2);
end;

procedure TfrmLabelMapSet.cxTabControl1Change(Sender: TObject);
begin
  InitGrid;
end;

procedure TfrmLabelMapSet.v2Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate BS_Label_Map ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' , Editer=''' + Trim(DName) + '''');
      sql.Add(' , Edittime=getdate()');
      sql.Add(' where LMID=' + quotedstr(CDS_1.fieldbyname('LMID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();
  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmLabelMapSet.ToolButton4Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into BS_Label_Map(LMType,Filler) values(' + quotedstr(Trim(FLMType)) + ',' + quotedstr(Trim(dname)) + ')');
    ExecSQL;
  end;
  InitGrid();
end;

procedure TfrmLabelMapSet.ToolButton5Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete BS_Label_Map where LMID=' + QuotedStr(CDS_1.FieldByName('LMID').AsString));
    ExecSQL;
  end;
  CDS_1.Delete;
end;

procedure TfrmLabelMapSet.ToolButton6Click(Sender: TObject);
begin
  Getfields(2);
end;

procedure TfrmLabelMapSet.ToolButton7Click(Sender: TObject);
begin
  Getfields(3);
end;

procedure TfrmLabelMapSet.ToolButton8Click(Sender: TObject);
begin
  Getfields(4);
end;

procedure TfrmLabelMapSet.ToolButton9Click(Sender: TObject);
begin
  Getfields(5);
end;

procedure TfrmLabelMapSet.TextEdit(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate BS_Label_Map ');
      sql.Add(' Set ' + FFieldName + '=' + Trim(mvalue));
      sql.Add(' , Editer=''' + Trim(DName) + '''');
      sql.Add(' , Edittime=getdate()');
      sql.Add(' where LMID=' + quotedstr(CDS_1.fieldbyname('LMID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();
  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

end.

