unit U_CPDBList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit, cxPC,
  IniFiles, jpeg, U_SLT, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmCPDBList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    CPName: TEdit;
    DQFactoryName: TEdit;
    DHID: TEdit;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    Label6: TLabel;
    OrderNo: TEdit;
    IdHTTP1: TIdHTTP;
    CDS_WXTS: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    V1Column1: TcxGridDBColumn;
    V1Column2: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure DHIDChange(Sender: TObject);
    procedure DQFactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    canshu1, canshu2, canshu3: string;
    procedure InitGrid();
    procedure SetStatus();
    procedure ReadINIFile();
    procedure SendMessageQR(Mfsid, KeyWord2: string);
    procedure SendMessageBH(GdName, Mfsid, KeyWord2: string);
    { Private declarations }
  public
    { Public declarations }
  end;

//var
 // frmShaRKList: TfrmShaRKList;
var
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_RTFun, U_Fun, U_PBRKInPut, U_FileUp, U_WechatAuthority,
  U_SysLogHelp;

{$R *.dfm}

procedure TfrmCPDBList.SendMessageQR(Mfsid, KeyWord2: string);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select distinct A.* from SY_User A inner join SY_Purview B on A.UserID=B.UserID  where A.Valid=''Y'' ');
    SQL.Add('and isnull(A.wxid,'''')<>''''');
    SQL.Add(' and exists(select 1 from SY_ModuleSub X where X.ModuleID=B.ModuleID and X.ModuleSubID=B.ModuleSubID and X.FormName=''业务员确认'') ');
//    showmessage(sql.text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_WXTS);
  SInitCDSData20(ADOQueryTemp, CDS_WXTS);

  if CDS_WXTS.IsEmpty then
  begin
    Application.MessageBox('用户未绑定微信,提交失败！', '提示信息', 0);
    Exit;
  end;
  CDS_WXTS.First;
  while not CDS_WXTS.eof do
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from WeixinMessage where 1<>1');
      Open;
      Append;
      FieldByName('MessageType').Value := 'OrderProgress';
      FieldByName('MessageName').Value := '订单进度提醒';
      FieldByName('Title').Value := '您有新的坯布到货仓库已确认，请及时处理';
      FieldByName('URL').Value := 'pages/grayfabric/ywConfirm?fhid=' + Trim(Mfsid);
      FieldByName('OpenId').Value := Trim(CDS_WXTS.FieldByName('wxid').AsString);
      FieldByName('Status').Value := '0';
      FieldByName('CreateDate').Value := FormatDateTime('yyyymmdd', SGetServerDateTime(ADOQueryTemp));
      FieldByName('CreateTime').Value := FormatDateTime('HH:mm:ss', SGetServerDateTime(ADOQueryTemp));
      FieldByName('BusinessId').Value := Trim(Mfsid);
      FieldByName('KeyWord1').Value := Trim(Mfsid);
      FieldByName('KeyWord2').Value := Trim(KeyWord2);
      Post;
    end;
    CDS_WXTS.Next;
  end;

  GetHTTP(IdHTTP1, 'http://www.rightsoft.top/yifu/api/message/Send/OrderProgress');
end;

procedure TfrmCPDBList.SendMessageBH(GdName, Mfsid, KeyWord2: string);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.* from SY_User A  where A.Valid=''Y'' ');
    SQL.Add('and isnull(A.wxid,'''')<>''''');
    SQL.Add(' and UserName= ' + quotedstr(Trim(GdName)));
    Open;
  end;

  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('用户未绑定微信,提交失败！', '提示信息', 0);
    Exit;
  end;
  ADOQueryTemp.First;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from WeixinMessage where 1<>1');
    Open;
    Append;
    FieldByName('MessageType').Value := 'OrderProgress';
    FieldByName('MessageName').Value := '订单进度提醒';
    FieldByName('Title').Value := '您有新的坯布到货被驳回，请及时处理';
    FieldByName('URL').Value := 'pages/grayfabric/gdConfirm?fhid=' + Trim(Mfsid);
    FieldByName('OpenId').Value := Trim(ADOQueryTemp.FieldByName('wxid').AsString);
    FieldByName('Status').Value := '0';
    FieldByName('CreateDate').Value := FormatDateTime('yyyymmdd', SGetServerDateTime(ADOQueryTemp));
    FieldByName('CreateTime').Value := FormatDateTime('HH:mm:ss', SGetServerDateTime(ADOQueryTemp));
    FieldByName('BusinessId').Value := Trim(Mfsid);
    FieldByName('KeyWord1').Value := Trim(Mfsid);
    FieldByName('KeyWord2').Value := Trim(KeyWord2);
    Post;
  end;

end;

procedure TfrmCPDBList.SetStatus();
begin

end;

procedure TfrmCPDBList.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i, j: integer;
begin
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  Action := caFree;
end;

procedure TfrmCPDBList.ReadINIFile();
var
  programIni: Tinifile;  //配置文件名
  FileName: string;
begin
  FileName := ExtractFilePath(Paramstr(0)) + 'SYSTEMSET.INI';
  programIni := Tinifile.create(FileName);
  server := programIni.ReadString('SERVER', '服务器地址', '127.0.0.1');
  programIni.Free;
end;

procedure TfrmCPDBList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 30;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
  canshu3 := Trim(DParameters3);
end;

procedure TfrmCPDBList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.DHPS as RKPS,B.DHQty RKQty from XH_Cloth_DH A left join XH_Cloth_DH B   on A.DHId=B.FHID ');
      sql.Add('where (A.DHType=''调拨出库'') and A.DQFactoryName<>A.ToFactoryName and B.DHType=''调拨入库''  ');
      sql.add(' and A.DHDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' and A.DHDate<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPDBList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCPDBList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmCPDBList.TBCloseClick(Sender: TObject);
begin
  if Trim(DName)='ADMIN' then
  begin
    WriteCxGridAll(trim(self.Caption), Tv1,ADOQueryCmd,ADOQueryTemp);
  end;


  Close;
end;

procedure TfrmCPDBList.FormShow(Sender: TObject);
begin
  ReadCxGridAll(trim(self.Caption), Tv1);
  SetStatus();
end;

procedure TfrmCPDBList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;

end;

procedure TfrmCPDBList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmCPDBList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmCPDBList.DHIDChange(Sender: TObject);
begin

  TBFind.Click;
end;

procedure TfrmCPDBList.DQFactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

end.

