unit U_ClList;

interface

uses
  Windows, Messages, SysUtils, StrUtils,Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, dxPSGlbl, dxPSUtl, dxPSEngn,
  dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, dxPSCore, dxPScxCommon, dxPScxGridLnk;

type
  TfrmClList = class(TForm)
    ToolBar1: TToolBar;
    tbselect: TToolButton;
    tbadd: TToolButton;
    tbupdate: TToolButton;
    tbdelete: TToolButton;
    tbclose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    begdate: TDateTimePicker;
    enddate: TDateTimePicker;
    Label2: TLabel;
    Label3: TLabel;
    customName: TEdit;
    Label4: TLabel;
    ChnName: TEdit;
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    V1operTime: TcxGridDBColumn;
    V1customname: TcxGridDBColumn;
    V1P_Code: TcxGridDBColumn;
    V1P_ChnName: TcxGridDBColumn;
    V1P_Spec: TcxGridDBColumn;
    V1packs: TcxGridDBColumn;
    V1P_Num: TcxGridDBColumn;
    V1P_unit: TcxGridDBColumn;
    V1operatorName: TcxGridDBColumn;
    V1note: TcxGridDBColumn;
    Panetime: TPanel;
    ADOQueryCmd: TADOQuery;
    V1planNo: TcxGridDBColumn;
    Label5: TLabel;
    planNo: TEdit;
    tbLook: TToolButton;
    V1colorname: TcxGridDBColumn;
    V1P_YHNO: TcxGridDBColumn;
    V1P_Colstd: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label6: TLabel;
    userID: TEdit;
    userName: TEdit;
    Label7: TLabel;
    tbPrintlb: TToolButton;
    ToolButton1: TToolButton;
    dxComponentPrinter1: TdxComponentPrinter;
    dxComponentPrinter1Link1: TdxGridReportLink;
    procedure tbcloseClick(Sender: TObject);
    procedure tbaddClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure tbselectClick(Sender: TObject);
    procedure tbupdateClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure tbdeleteClick(Sender: TObject);
    procedure tbLookClick(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure tbPrintlbClick(Sender: TObject);
  private
    procedure DoQuery();
    procedure FilterData();
    function  DeleteData():boolean;  //删除数据
    procedure initGrid();
    { Private declarations }
  public

    FType:string;
    { Public declarations }
  end;

var
  frmClList: TfrmClList;
implementation
uses
  U_DataLink,U_Fun;             //, U_ClEdit
{$R *.dfm}
procedure TfrmClList.initGrid();
var
  i:integer;
begin
  with adoqueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add(' select * from BD_ColumnConfig ');
    sql.Add(' where tableName=''WS_Register'' ');
    sql.Add(' and Type='''+trim(fType)+''' ');
    sql.Add(' and isVisibleLB=1 ');
    sql.Add(' order by orderNo ');
    open;
  end;
  TV1.ClearItems;
  if not adoqueryCmd.IsEmpty then
  begin
    for i:=0 to adoqueryCmd.RecordCount-1 do
    begin
      tv1.CreateColumn;
      tv1.Columns[i].DataBinding.FieldName:=trim(adoqueryCmd.fieldbyname('col').AsString);
      tv1.Columns[i].Caption:=trim(adoqueryCmd.fieldbyname('colName').AsString);
      tv1.Columns[i].Name:='tv1'+trim(adoqueryCmd.fieldbyname('col').AsString);
      tv1.Columns[i].HeaderAlignmentHorz:=tacenter;
      tv1.Columns[i].Width:=90;
      if LeftBStr(trim(adoqueryCmd.fieldbyname('col').AsString),2) ='fd' then
      tv1.Columns[i].Summary.FooterKind:=sksum;
   //   if  adoqueryCmd.fieldbyname('isreadonly').AsBoolean then
   //   tv1.Columns[i].Options.Editing:=false;
      adoqueryCmd.Next;
    end;
  end;
 //TV1.ApplyBestFit();
end;

//////////////////////////////////////////////
//////删除数据
//////////////////////////////////////////////
function  TfrmClList.DeleteData():boolean;
begin
  result:=false;
  try
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
    //  sql.Add('delete from JD_Inspect where InspectNo='+quotedstr(trim(ADOQueryTmp.fieldbyname('InspectNo').AsString)));
      sql.Add('delete from WS_Register where WRNo='+quotedstr(trim(ADOQueryTmp.fieldbyname('WRNo').AsString)));
      sql.Add('and WRID='+quotedstr(trim(ADOQueryTmp.fieldbyname('WRID').AsString)));
      execsql;
    end;
    result:=true;
  except
  end;
end;
procedure TfrmClList.DoQuery();
var
  strsql,sqlwhere:string;
begin
  begdate.SetFocus;
  screen.Cursor:=crsqlwait;
  Panetime.Visible:=true;
  Panetime.Left:=(Width-Panetime.Width) div 2;
  Panetime.top:=(Height-Panetime.Height-400) div 2;
  Application.ProcessMessages;

  sqlwhere:=' where WRDate>='''+formatdatetime('yyyy-MM-dd',begdate.Date)+''' '+
            ' and   WRDate<'''+formatdatetime('yyyy-MM-dd',enddate.Date+1)+''' ';
  sqlwhere:=sqlwhere+' and Type='''+trim(FType)+'''  ';
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      filtered:=false;
      sql.Add('select * from WS_Register A');
      sql.Add(sqlwhere);
      open;
    end;
    FilterData();
  finally
    Panetime.Visible:=false;
    screen.Cursor:=crdefault;
  end;
end;
procedure TfrmClList.FilterData();
var
  filterStr:string;
begin
  filterStr:='';
  if trim(userID.Text)<>'' then
  filterStr:=filterStr+' and userID like '+quotedstr('%'+trim(userID.Text)+'%');
  if trim(USERName.Text)<>'' then
  filterStr:=filterStr+' and USERName like '+quotedstr('%'+trim(USERName.Text)+'%');
  if trim(planNo.Text)<>'' then
  filterStr:=filterStr+' and planNo like '+quotedstr('%'+trim(planNo.Text)+'%');
  try
    ADOQueryTmp.DisableControls ;
    if trim(filterStr)='' then
    begin
      ADOQueryTmp.Filtered:=false;
      ADOQueryTmp.EnableControls;
      exit;
   end;
   filterStr:=trim(Copy(filterStr,5,length(filterStr)-4));
    with ADOQueryTmp do
    begin
      filtered:=false;
      filter:=filterStr;
      filtered:=true;
    end;
   finally
    ADOQueryTmp.EnableControls;
  end;
end;

procedure TfrmClList.tbcloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmClList.tbaddClick(Sender: TObject);
begin
//  frmclEdit:=TfrmclEdit.create(self);
//  with frmclEdit do
//  begin
//    fromid:=0;
//    fType:=trim(self.FType);
//    caption:=trim(self.Caption);
//    if showmodal=1 then
//    begin
//      //  DoQuery();
//     // self.ADOQueryTmp.Locate('inspectNo',fkeyNO,[]);
//    end;
//    DoQuery();
//    Release;
//  end;
end;

procedure TfrmClList.FormDestroy(Sender: TObject);
begin
  frmClList:=nil;
end;

procedure TfrmClList.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  writeCxGrid(self.Name+tv1.Name+Self.FType,tv1,'产量登记');
  action:=cafree;
end;

procedure TfrmClList.FormCreate(Sender: TObject);
begin
  begdate.Date:=dServerDate-6;
  enddate.Date:=dServerDate;
  cxgrid1.Align:=alClient;
end;

procedure TfrmClList.tbselectClick(Sender: TObject);
begin
  DoQuery();
end;

procedure TfrmClList.tbupdateClick(Sender: TObject);
begin
//  if ADOQueryTmp.IsEmpty then exit;
//  frmclEdit:=TfrmclEdit.create(self);
//  with frmclEdit do
//  begin
//    fromid:=1;
//    fType:=trim(self.FType);
//    caption:=trim(self.Caption);
//    fkeyNO:=trim(self.ADOQueryTmp.fieldbyname('WRNO').AsString);
//    if showmodal=1 then
//    begin
//      DoQuery();
//      self.ADOQueryTmp.Locate('WRNO',fkeyNO,[]);
//    end;
//    Release;
//  end; 
end;

procedure TfrmClList.FormShow(Sender: TObject);
begin
  initGrid();
  readCxGrid(self.Name+tv1.Name+Self.FType,tv1,'产量登记');
  DoQuery();
end;

procedure TfrmClList.tbdeleteClick(Sender: TObject);
begin
  if adoqueryTmp.IsEmpty then exit;
  if application.MessageBox('是否删除此信息？','提示信息',MB_YesNo+MB_DefButton2+MB_IconQuestion)=IdYes then
  begin
    if not DeleteData() then
    begin
      application.MessageBox('删除数据失败！','提示信息');
      exit;
    end;
    DoQuery();
  end;
  
end;

procedure TfrmClList.tbLookClick(Sender: TObject);
begin
//  if ADOQueryTmp.IsEmpty then exit;
//  frmclEdit:=TfrmclEdit.create(self);
//  with frmclEdit do
//  begin
//    fromid:=10;
//    fType:=trim(self.FType);
//    fkeyNO:=trim(self.ADOQueryTmp.fieldbyname('WRNO').AsString);
//    caption:=trim(self.Caption);
//    if showmodal=1 then
//    begin
//      DoQuery();
//      self.ADOQueryTmp.Locate('inspectNo',fkeyNO,[]);
//    end;
//    Release;
//  end;
end;

procedure TfrmClList.TV1DblClick(Sender: TObject);
begin
  tbLook.Click;
end;

procedure TfrmClList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryTmp.IsEmpty then exit;
  TcxGridToExcel(self.Caption,cxGrid1);
end;

procedure TfrmClList.tbPrintlbClick(Sender: TObject);
begin
  if ADOQueryTmp.IsEmpty then exit;
  dxComponentPrinter1.ReportLink[0].Preview;
end;

end.
