unit U_SysLogOrder;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel,strUtils,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, ADODB, StdCtrls, ExtCtrls, ImgList,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxScrollbarAnnotations, System.ImageList;

type
  TfrmSysLogOrder = class(TForm)
    ToolBar1: TToolBar;
    TQry: TToolButton;
    Tclose: TToolButton;
    ADOQueryLog: TADOQuery;
    DataSource1: TDataSource;
    ADOConnection1: TADOConnection;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1OperMan: TcxGridDBColumn;
    tv1jopertime: TcxGridDBColumn;
    tv1Model: TcxGridDBColumn;
    tv1acction: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    tv1Opevent: TcxGridDBColumn;
    tv1Result: TcxGridDBColumn;
    Panel1: TPanel;
    Label2: TLabel;
    Label1: TLabel;
    begDate: TDateTimePicker;
    endDate: TDateTimePicker;
    Label3: TLabel;
    edt_model: TEdit;
    CheckBox1: TCheckBox;
    Label4: TLabel;
    edt_nr: TEdit;
    ThreeImgList: TImageList;
    procedure TcloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TQryClick(Sender: TObject);
    procedure edt_modelChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private

    procedure DoQuery();
    procedure DoFilter();
  public
    fModel,facction:string;
    fOtherWhere:string;
    FMainid:String;
  end;

var
  frmSysLogOrder: TfrmSysLogOrder;

implementation
uses
  U_DataLink;
{$R *.dfm}

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

procedure TfrmSysLogOrder.FormCreate(Sender: TObject);
begin
  cxGrid1.Align :=alClient;
  begDate.DateTime :=date-31;
  endDate.DateTime :=date;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=DConString;
    Connected:=true;
  end;    
end;
/////////////////////////////////////////////////////////
  //
////////////////////////////////////////////////////////
procedure TfrmSysLogOrder.DoQuery();
var
  mbegdate,menddate:string;

begin
  mbegdate:=formatDatetime('yyyy-MM-dd',begDate.Date);  //
  menddate:=formatDatetime('yyyy-MM-dd',endDate.Date+1);
  try
    with ADOQueryLog do
    begin
      close;
      sql.clear;
      filtered:=false;
      sql.add('select A.* ');
      sql.add('from SY_sysLog A');
      if CheckBox1.Checked then
      begin
        sql.Add('where OperTime>='+quotedStr(mbegdate));
        sql.Add('and OperTime<'+quotedStr(menddate));
      end
      else
      begin
        sql.Add('where 1=1');
      end;
      sql.Add(' and isnull(Mainid,'''')='''+Trim(FMainid)+''' and isnull(Mainid,'''')<>'''' ');
      if trim(facction)<>'' then
        sql.add('and acction='+quotedStr(facction));
      if trim(fOtherWhere)<>'' then
      begin
        sql.add(fOtherWhere);
      end;
      sql.Add('order by operOr,Opertime');
      Open;
    end;
  finally
  end;
end;
///////////////////////////////////////////////////////////
  //
///////////////////////////////////////////////////////////
procedure TfrmSysLogOrder.DoFilter();
var
  filterStr:string;
begin
  filterStr:='';
 //
  if trim(edt_model.text)<>'' then
  begin
    filterStr:=' and model ='+quotedStr(trim(edt_model.text));
  end;
  if trim(edt_nr.text)<>'' then
  begin
    filterStr:=' and OpEvent like '+quotedStr('%'+trim(edt_nr.text)+'%');
  end;

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

end;


procedure TfrmSysLogOrder.FormShow(Sender: TObject);
begin
  DoQuery();
end;

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

procedure TfrmSysLogOrder.edt_modelChange(Sender: TObject);
begin
  DoFilter();
end;

procedure TfrmSysLogOrder.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  action:=cafree;
end;

end.
