unit U_SysLogList;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters,   cxStyles, cxCustomData,Vcl.Clipbrd,
  cxFilter, cxData, cxDataStorage, cxEdit, cxNavigator, dxDateRanges,
  dxScrollbarAnnotations, Data.DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid,
  Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls, Vcl.ToolWin, cxContainer, dxCore,
  cxDateUtils, cxTextEdit, cxMaskEdit, cxDropDownEdit, cxCalendar,System.StrUtils ,
  System.ImageList, Vcl.ImgList, cxImageList, Data.Win.ADODB, dxBarBuiltInMenu,
  cxGridCustomPopupMenu, cxGridPopupMenu, Datasnap.DBClient, cxCheckBox,
  Vcl.Menus;

type
  TfrmSysLogList = class(TForm)
    Panel1: TPanel;
    Label2: TLabel;
    Label1: TLabel;
    Label4: TLabel;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1UOperator: TcxGridDBColumn;
    tv1utime: TcxGridDBColumn;
    tv1Utype: TcxGridDBColumn;
    tv1UOperation: TcxGridDBColumn;
    tv1UDetails: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    begdate: TcxDateEdit;
    enddate: TcxDateEdit;
    edt_nr: TcxTextEdit;
    cxImageList_bar: TcxImageList;
    ADOQueryLog: TADOQuery;
    DataSource1: TDataSource;
    ADOConnection1: TADOConnection;
    ToolBar1: TToolBar;
    TQry: TToolButton;
    Tclose: TToolButton;
    tv1orderNo: TcxGridDBColumn;
    tv1pieceNo: TcxGridDBColumn;
    tv1Meter: TcxGridDBColumn;
    tv1GrossWeight: TcxGridDBColumn;
    tv1NetWeight: TcxGridDBColumn;
    tv1C_name: TcxGridDBColumn;
    tv1c_color: TcxGridDBColumn;
    tv1FtyPCId: TcxGridDBColumn;
    Label3: TLabel;
    orderNo: TcxTextEdit;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    tv1batchNo: TcxGridDBColumn;
    tv1CIID: TcxGridDBColumn;
    tv1C_pattern: TcxGridDBColumn;
    tv1C_colorNo: TcxGridDBColumn;
    Tundo: TToolButton;
    cds_loglist: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    tv1ssel: TcxGridDBColumn;
    PM_1: TPopupMenu;
    N2: TMenuItem;
    N3: TMenuItem;
    N1: TMenuItem;
    procedure TQryClick(Sender: TObject);
    procedure TcloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure edt_nrPropertiesChange(Sender: TObject);
    procedure TundoClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private
    procedure DoQuery();
    procedure DoFilter();
    function undoDel():Boolean;
  public
    FMainid:string;
    FSubId:string;
    fModel:string;
  end;

var
  frmSysLogList: TfrmSysLogList;

implementation
uses
  U_DataLink,U_globalVar,U_RTFun;
{$R *.dfm}

procedure TfrmSysLogList.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alClient;
  begDate.Date :=date-2;
  endDate.Date :=date;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=DConString;
    Connected:=true;
  end;
end;

procedure TfrmSysLogList.FormShow(Sender: TObject);
begin
    ReadCxGrid('包装日志列表' + 'Tv1', TV1, gDllFileCaption);
    if FMainid<>'' then
      TQry.Click;
end;

procedure TfrmSysLogList.N1Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(cds_logList.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmSysLogList.N2Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True); //全选
end;

procedure TfrmSysLogList.N3Click(Sender: TObject);
begin
   SelOKNoFiler(Tv1, false); //全选
end;

procedure TfrmSysLogList.TcloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmSysLogList.ToolButton1Click(Sender: TObject);
begin
    WriteCxGrid(trim('包装日志列表') + 'Tv1', TV1, gDllFileCaption);
end;

procedure TfrmSysLogList.TQryClick(Sender: TObject);
begin
    DoQuery();
end;

procedure TfrmSysLogList.TundoClick(Sender: TObject);
begin
  try
    begdate.SetFocus;
    if not gIsCanDesign then
    begin
      application.MessageBox('没有操作权限!','提示信息',0) ;
      exit;
    end;

    cds_logList.DisableControls;
    if not cds_logList.Locate('ssel;Utype',vararrayOf([true,'检验删除']),[]) then
    begin
       application.MessageBox('请先选择检验删除的记录','提示信息',0) ;
       exit;
    end;
    if undoDel() then
    begin
       tqry.Click;
    end;
  finally
    cds_logList.EnableControls ;
  end;
end;

procedure TfrmSysLogList.DoQuery();
var
  mbegdate,menddate:string;

begin
  mbegdate:=formatDatetime('yyyy-MM-dd',begDate.Date);  //
  menddate:=formatDatetime('yyyy-MM-dd',endDate.Date+1);
  try
    cds_logList.DisableControls;
    with ADOQueryLog do
    begin
      close;
      sql.clear;
      filtered:=false;
      sql.add('select B.*,A.utime,A.Utype,A.UOperation,A.UOperator,A.UDetails');
      sql.add(' from Trade_Need_Up A');
      sql.add(' inner join Trade_Cloth_Inspect B on A.udataId=B.ciid');

      if FMainid<>'' then
      begin
        sql.Add('where B.mainId='''+Trim(FMainid)+''' ');
      end
      else
      begin
        sql.Add('where UTime>='+quotedStr(mbegdate));
        sql.Add('and UTime<'+quotedStr(menddate));
      end;
      if FSubId<>'' then
        sql.Add('and B.subId='''+Trim(FSubId)+''' ');

      sql.add('union all');

      sql.add('select B.*,A.utime,A.Utype,A.UOperation,A.UOperator,A.UDetails');
      sql.add(' from Trade_Need_Up A');
      sql.add(' inner join Trade_Cloth_Inspect_del B on A.udataId  like   ''%''+rtrim(B.ciid)+'',%''');

      if FMainid<>'' then
      begin
        sql.Add('where B.mainId='''+Trim(FMainid)+''' ');
      end
      else
      begin
        sql.Add('where UTime>='+quotedStr(mbegdate));
        sql.Add('and UTime<'+quotedStr(menddate));
      end;

      if FSubId<>'' then
        sql.Add('and B.subId='''+Trim(FSubId)+''' ');
     // sql.Add('order by UOperator,UTime');
      Open;
    end;
    SCreateCDS(ADOQueryLog, cds_logList);
    SInitCDSData(ADOQueryLog, cds_logList);
  finally
    cds_logList.EnableControls ;
  end;
end;

procedure TfrmSysLogList.edt_nrPropertiesChange(Sender: TObject);
begin
  DoFilter();
end;

///////////////////////////////////////////////////////////
  //
///////////////////////////////////////////////////////////
procedure TfrmSysLogList.DoFilter();
var
  filterStr:string;
begin
  if not ADOQueryLog.Active then  exit;

  filterStr:='';
 //
  if trim(orderNo.text)<>'' then
  begin
    filterStr:=' and orderNo like '+quotedStr('%'+trim(orderNo.text)+'%');
  end;
  if trim(edt_nr.text)<>'' then
  begin
    filterStr:=' and UDetails like '+quotedStr('%'+trim(edt_nr.text)+'%');
  end;

  try
    cds_logList.DisableControls ;
    if trim(filterStr)='' then
    begin
      ADOQueryLog.Filtered:=false;
      exit;
    end;
    filterStr:=RightBStr(filterStr,length(filterStr)-4);
    with ADOQueryLog do
    begin
      filtered:=false;
      filter:=filterStr;
      filtered:=true;
    end;
    cds_logList.EmptyDataSet;
    SInitCDSData(ADOQueryLog, cds_logList);
  finally
    cds_logList.EnableControls;
  end;

end;

//////////////////////////////////////////////////////////////////
///
function TfrmSysLogList.undoDel():Boolean;
var
  MCIIDS:string;
begin
  result:=false;
  with cds_loglist do
  begin
    First;
    while not Eof do
    begin
      if cds_loglist.FieldByName('SSel').AsBoolean = True then
      begin
        MCIIDS := MCIIDS + Trim(cds_loglist.fieldbyname('CIID').AsString) + ',';
      end;
        Next;
    end;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Trade_Need_Up(UOperation,UType,UDataId) values(''检验'',''恢复检验删除'',' + quotedstr(MCIIDS) + ')  ');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Trade_Insp_Del_undo ');
      Sql.Add('@CIIDS=' + quotedstr(Trim(MCIIDS)));
      Sql.Add(',@DCode=' + Quotedstr(DCode));
      Sql.Add(',@DName=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;


    ADOQueryCmd.Connection.CommitTrans;
    result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('恢复数据删除记录发生错误!','提示信息',0);
  end;
end;

end.
