unit U_WeChatBinding;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxPC,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmWeChatBinding = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    UserID: TEdit;
    Label5: TLabel;
    UserName: TEdit;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    Udept: TEdit;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    UType: TEdit;
    Label6: TLabel;
    Label1: TLabel;
    wxNickName: TEdit;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure UserIDChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    FInt, PFInt: Integer;
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    FType: string;
    { Public declarations }
  end;

var
  frmWeChatBinding: TfrmWeChatBinding;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_WeiXinHelp; //,U_ClothContractInPutPB   ,U_ProductOrderList

{$R *.dfm}

procedure TfrmWeChatBinding.FormDestroy(Sender: TObject);
begin
  frmWeChatBinding := nil;
end;

procedure TfrmWeChatBinding.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmWeChatBinding.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption + tv1.Name, Tv1);
end;

procedure TfrmWeChatBinding.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select * from SY_User');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmWeChatBinding.InitForm();
begin
  ReadCxGrid(self.Caption + tv1.Name, Tv1);
  InitGrid();
end;

procedure TfrmWeChatBinding.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

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

procedure TfrmWeChatBinding.UserIDChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmWeChatBinding.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmWeChatBinding.TBAddClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmWeiXinHelp := TfrmWeiXinHelp.Create(Self);
    with frmWeiXinHelp do
    begin
      if ShowModal = 1 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add(' update SY_User set wxid=' + QuotedStr(Trim(frmWeiXinHelp.CDS_Help.FieldByName('OPENID').AsString)));
          SQL.Add(',wxNickName=' + QuotedStr(Trim(frmWeiXinHelp.CDS_Help.FieldByName('NICKNAME').AsString)));
          SQL.Add(' where UserID=' + QuotedStr(Trim(Self.Order_Main.FieldByName('UserID').AsString)));
          SQL.Add(' and UserName=' + QuotedStr(Trim(Self.Order_Main.FieldByName('UserName').AsString)));
          ExecSQL;
        end;
        with Self.Order_Main do
        begin
          Edit;
          FieldByName('wxid').Value := Trim(frmWeiXinHelp.CDS_Help.FieldByName('OPENID').AsString);
          FieldByName('wxNickName').Value := Trim(frmWeiXinHelp.CDS_Help.FieldByName('NICKNAME').AsString);
        end;
      end;
    end;
  finally
    frmWeiXinHelp.Free;
  end;
end;

procedure TfrmWeChatBinding.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要执行此操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add(' update SY_User set wxid=NULL,wxNickName=NULL');
    SQL.Add(' where UserID=' + QuotedStr(Trim(Self.Order_Main.FieldByName('UserID').AsString)));
    SQL.Add(' and UserName=' + QuotedStr(Trim(Self.Order_Main.FieldByName('UserName').AsString)));
    ExecSQL;
  end;
  with Self.Order_Main do
  begin
    Edit;
    FieldByName('wxid').Value := '';
    FieldByName('wxNickName').Value := '';
  end;
end;

end.

