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,
  dxDateRanges, dxBarBuiltInMenu, System.ImageList,
  Vcl.ImgList, U_BaseHelp, Vcl.Clipbrd,  dxScrollbarAnnotations, cxImageList;

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;
    LMName: TEdit;
    v2Column8: TcxGridDBColumn;
    Label1: TLabel;
    v2Column12: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Label2: TLabel;
    LMType: TEdit;
    v2Column1: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    ADOConnection1: TADOConnection;
    ImageList1: TImageList;
    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;
    cxImageList_bar: TcxImageList;
    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 CustomerChange(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 N3Click(Sender: TObject);
    procedure TextEdit(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    procedure Getfields(MSql: Integer);
  public
    IsSql1: Boolean;
    FLMType: string;
    FFiltration1, FFiltration2, FFiltration3: string;
    { Public declarations }
  end;

var
  frmLabelMapSet: TfrmLabelMapSet;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmLabelMapSet.Getfields(MSql: Integer);
var
  i:integer;
begin
  try
   // ADO_2.DisableControls;
    TV2.BeginUpdate();
  case MSql of
    1:
      begin
        if IsSql1 then
        begin
          with ADO_2 do
          begin
            Close;
            sql.Clear;
            sql.add('exec ' + trim(CDS_1.fieldbyname('LMSql1').AsString));
            sql.add(FFiltration1);
            Open;
          end;
        end
        else
        begin
          if trim(CDS_1.fieldbyname('LMSql1').AsString) <> '' then
          begin
            with ADO_2 do
            begin
              Close;
              sql.Clear;
              sql.add('exec ' + trim(CDS_1.fieldbyname('LMSql1').AsString) + ' @Filtration=' + quotedstr(Trim(FFiltration1)));
              Open;
            end;
          end;

        end;

        TV2.ClearItems; //清空数据
       // (TV2.DataController as IcxCustomGridDataController).DeleteAllItems;  //删除所有列
        (TV2.DataController as IcxCustomGridDataController).CreateAllItems(false); //创建数据源中的所有列

      end;
    2:
      begin
        if trim(CDS_1.fieldbyname('LMSql2').AsString) <> '' then
        begin
          with ADO_2 do
          begin
            Close;
            sql.Clear;
            sql.add('exec ' + trim(CDS_1.fieldbyname('LMSql2').AsString) + ' @Filtration=' + quotedstr(Trim(FFiltration2)));
            Open;
          end;
        end;

        TV2.ClearItems; //清空数据
       // (TV2.DataController as IcxCustomGridDataController).DeleteAllItems;  //删除所有列
        (TV2.DataController as IcxCustomGridDataController).CreateAllItems(False); //创建数据源中的所有列

      end;
    3:
      begin
        if trim(CDS_1.fieldbyname('LMSql3').AsString) <> '' then
        begin
          with ADO_2 do
          begin
            Close;
            sql.Clear;
            sql.add('exec ' + trim(CDS_1.fieldbyname('LMSql3').AsString) + ' @Filtration=' + quotedstr(Trim(FFiltration3)));
            Open;
          end;
        end;

        TV2.ClearItems; //清空数据
       // (TV2.DataController as IcxCustomGridDataController).DeleteAllItems;  //删除所有列
        (TV2.DataController as IcxCustomGridDataController).CreateAllItems(false); //创建数据源中的所有列

      end;
  end;
   finally
      for i := 0 to TV2.ColumnCount-1 do
        TV2.Columns[i].Width:=80;
      tv2.EndUpdate ;


  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;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmLabelMapSet.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(TV2.Controller.FocusedColumn.DataBinding.FilterFieldName));

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);
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(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.CustomerChange(Sender: TObject);
begin
  ToolButton2.Click;
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.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.

