unit U_DetailBaseList;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ToolWin, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore,U_WindowFormdesign,
  dxSkinsDefaultPainters, dxSkinWXI, cxStyles, cxCustomData, cxFilter, cxData,
  cxDataStorage, cxEdit, cxNavigator, dxDateRanges, dxScrollbarAnnotations,
  Data.DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid, Data.Win.ADODB,math,
  dxBarBuiltInMenu, cxGridCustomPopupMenu, cxGridPopupMenu, cxCheckBox,
  cxContainer, dxCore, cxDateUtils, dxLayoutcxEditAdapters, dxLayoutContainer,
  cxMaskEdit, cxDropDownEdit, cxCalendar, cxTextEdit, dxLayoutControl,
  FrameDateSel, Datasnap.DBClient, cxMemo, Vcl.StdCtrls, Vcl.ExtCtrls,
  cxGeometry, dxFramedControl, dxPanel, cxSplitter, cxButtonEdit, FrameDateSel10,
  MovePanel;

type
  TfrmDetailBaseList = class(TForm)
    ToolBar3: TToolBar;
    Trefresh: TToolButton;
    Tprint: TToolButton;
    TprintGrid: TToolButton;
    TsaveGrid: TToolButton;
    Tclose: TToolButton;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    DataSource1: TDataSource;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryBaseCmd: TADOQuery;
    ADOQueryBaseTemp: TADOQuery;
    dxLayoutControl_query: TdxLayoutControl;
    dxLayoutControl_queryGroup_Root: TdxLayoutGroup;
    CDS_Main: TClientDataSet;
    Panel_page: TPanel;
    BTNP: TButton;
    LBCPAP: TLabel;
    BTLP: TButton;
    TCBNOR: TcxComboBox;
    Label31: TLabel;
    cxSplitter1: TcxSplitter;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Sub: TClientDataSet;
    TgridLiSet: TToolButton;
    frmFrameDateSel1: TfrmFrameDateSel10;
    cxProgressBar1: TMovePanel;
    procedure TrefreshClick(Sender: TObject);
    procedure TprintClick(Sender: TObject);
    procedure TprintGridClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TcloseClick(Sender: TObject);
    procedure TsaveGridClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure TCBNORPropertiesChange(Sender: TObject);
    procedure tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure TgridLiSetClick(Sender: TObject);
    procedure dxLayoutControl_queryDblClick(Sender: TObject);
  private
    fWindowDesign: TWindowFormdesign;
    fDesignCode:string;
    CurrentPage, RecordsNumber: Integer;
    procedure doQuery();
    procedure doQueryByCriteria();
    procedure initGrid();
    procedure InitSub();
  protected
    fParameters1: string;
    fParameters2: string;
    fParameters3: string;
    fParameters4: string;
    fParameters5: string;
    fParameters10: string;
  public
    fFormId: integer;
    fProcedureName:string; //存储过程名称
    fProcedureSubName:string; //子存储过程名称
    FLMType:string;   //报表文件类型标志
    ftimeType:string; //日期类型
    fQueryType:String; //查询类型
    FFiltration1:string; //打印参数条件
    fCriteria:string;   //条件参数
    constructor Create(AOwner: TComponent; ACaption: string=''; Parameters1: string=''; Parameters2: string=''; Parameters3: string=''; Parameters4: string=''; Parameters5: string=''; Parameters10: string='';FormID:Integer=0);

  end;

var
  frmDetailBaseList: TfrmDetailBaseList;

implementation
uses
  U_RTFun, U_globalVar,U_cxGridCustomSet, U_dataLink,U_FormLayOutDesign, U_LabelPrint;
{$R *.dfm}

procedure TfrmDetailBaseList.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  doQuery();
end;

procedure TfrmDetailBaseList.BTNPClick(Sender: TObject);
begin
  if CurrentPage < cds_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  doQuery();
end;

constructor TfrmDetailBaseList.Create(AOwner: TComponent; ACaption: string=''; Parameters1: string=''; Parameters2: string=''; Parameters3: string=''; Parameters4: string=''; Parameters5: string=''; Parameters10: string='';FormID:Integer=0);
begin
  inherited Create(AOwner);
  if ACaption <> '' then
    Caption := ACaption;

  fParameters1 := Parameters1;
  fParameters2 := Parameters2;
  fParameters3 := Parameters3;
  fParameters4 := Parameters4;
  fParameters5 := Parameters5;
  fParameters10 := Parameters10;
  fFormId:=   FormID ;
end;

procedure TfrmDetailBaseList.TCBNORPropertiesChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  initGrid();
end;

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

procedure TfrmDetailBaseList.TgridLiSetClick(Sender: TObject);
begin
  if cxgrid2.IsFocused then
    fWindowDesign.OpenGridDesignWin10(fDesignCode+'|'+'cxgrid2','cxgrid2',tv2)
  else
    fWindowDesign.OpenGridDesignWin10(fDesignCode+'|'+'cxgrid1','cxgrid1',tv1);
end;

procedure TfrmDetailBaseList.TprintClick(Sender: TObject);
begin
  if cds_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmDetailBaseList.TprintGridClick(Sender: TObject);
begin
  if cds_Main.IsEmpty then
    Exit;
  if trim(self.FLMType)='' then
  begin
    application.MessageBox('未设置打印报表类型,请先设置!','提示信息',0);
    exit;
  end;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := self.FLMType;
      FFiltration1 := self.FFiltration1;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmDetailBaseList.TrefreshClick(Sender: TObject);
begin
  if frmFrameDateSel1.BegDate.Visible then
  begin
    frmFrameDateSel1.BegDate.SetFocus;
  end;
  CurrentPage := 1;
  initGrid();
end;

procedure TfrmDetailBaseList.TsaveGridClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
  if gIsCanDesign then
  begin
    saveLayOut(application, dxLayoutControl_query, ADOQueryBaseCmd,PWideChar( fDllFileName + '|' + Self.Name + '|' + dxLayoutControl_query.Name + '.ini'));
  end;
end;

procedure TfrmDetailBaseList.tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
end;

////////////////////////////////////
procedure TfrmDetailBaseList.doQuery();
var
  strwhere: string;
begin
  if fProcedureName='' then
  begin
    application.MessageBox('存储过程名称参数不能为空!','警告信息',0);
    exit;
  end;
  strwhere := GetProcedureParam(dxLayoutControl_query);

   try
     Tv1.BeginUpdate();
     cds_Main.DisableControls;
     with ADOQueryMain do
     begin
       close;
       Filtered := false;
       sql.Clear;
       sql.Add('exec '+fProcedureName);
       sql.Add('@timeType='+quotedstr(fTimeType));
       sql.Add(',@begDate='+quotedstr(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.begdate.Date)));
       sql.Add(',@endDate='+quotedstr(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date+1)));
       if frmFrameDateSel1.jqModel.Checked then
         sql.Add(',@jqModel=1')
       else
         sql.Add(',@jqModel=0');
       if Panel_page.Visible then
       begin
         sql.Add(',@pageIndex=' + inttostr(CurrentPage));
         sql.Add(',@pageSize=' + inttostr(RecordsNumber));
       end;
       sql.Add(',@criteria='+ quotedstr(fcriteria));
       if strwhere<>'' then
         sql.Add(','+strwhere);
        //  showMessage(sql.Text);
       //cxMemo1.Text:=  sql.Text ;
       open;
     end;
     SCreateCDS(ADOQueryMain, cds_Main);
     SInitCDSData(ADOQueryMain, cds_Main);
     if Panel_page.Visible then
     begin
       LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(cds_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
     end;
   finally
      cds_Main.EnableControls;
      Tv1.EndUpdate;
   end;
end;

////////////////////////////////////
procedure TfrmDetailBaseList.doQueryByCriteria();
var
  mSqlWhere: string;
begin
  if fProcedureName='' then
  begin
    application.MessageBox('存储过程名称参数不能为空!','警告信息',0);
    exit;
  end;

   mSqlWhere := SLGetFilters(dxLayoutControl_query, 1, 2);
   if trim(mSqlWhere) <> '' then
   begin
      mSqlWhere := ' and ' + trim(mSqlWhere);
   end;
   mSqlWhere:=fCriteria + ' ' + mSqlWhere   ;
   try
     Tv1.BeginUpdate();
     CDS_Main.DisableControls;
     with ADOQueryMain do
     begin
       close;
       Filtered := false;
       sql.Clear;
       sql.Add('exec '+fProcedureName);
       sql.Add('@timeType='+quotedstr(fTimeType));
       sql.Add(',@begDate='+quotedstr(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.begdate.Date)));
       sql.Add(',@endDate='+quotedstr(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date+1)));
       if Panel_page.Visible then
       begin
         sql.Add(',@pageIndex=' + inttostr(CurrentPage));
         sql.Add(',@pageSize=' + inttostr(RecordsNumber));
       end;
       sql.Add(',@criteria='+ quotedstr(mSqlWhere));

       //cxMemo1.Text:=  sql.Text ;
       open;
     end;
     SCreateCDS(ADOQueryMain, CDS_Main);
     SInitCDSData(ADOQueryMain, CDS_Main);
     if Panel_page.Visible then
     begin
       LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
     end;
   finally
      CDS_Main.EnableControls;
      Tv1.EndUpdate;
   end;
end;
procedure TfrmDetailBaseList.dxLayoutControl_queryDblClick(Sender: TObject);
begin
  layoutDesign(TdxLayoutControl(Sender),ADOQueryBaseCmd,PWideChar(dcode));
end;

///
procedure TfrmDetailBaseList.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Sendmessage(application.MainForm.Handle, WM_CloseForm, 4, 0);

  Action:=cafree;
end;

procedure TfrmDetailBaseList.FormCreate(Sender: TObject);
var
  mFontSize:integer;
begin
   getSystemIni();
    if gFontSize<9 then
    begin
      mFontSize := 12;
    end
    else
    begin
      mFontSize:=gFontSize ;
    end;

  self.Font.Size := gFontSize;
  if trim(gFontName)<>'' then
    self.Font.Name:=gFontName;
  cxgrid1.Align:=alClient;
  CurrentPage := 1;
  RecordsNumber := 500;
  fWindowDesign := TWindowFormdesign.Create();
  frmFrameDateSel1.begdate.Date:=SGetServerDate(ADOQueryBaseTemp);
  frmFrameDateSel1.enddate.Date:=frmFrameDateSel1.begdate.Date;
end;

procedure TfrmDetailBaseList.FormDestroy(Sender: TObject);
begin
  fWindowDesign.free;
end;

procedure TfrmDetailBaseList.FormShow(Sender: TObject);
begin
  if DParameters8<>'1' then
  begin
    fWindowDesign.FormStyleInit10(self, fFormId, ADOQueryBaseTemp, ADOQueryBaseCmd, '', fParameters10);
  end;
  //
  if dxLayoutControl_query.Tag<>999 then
    readLayOut(dxLayoutControl_query,ADOQueryBaseTemp,PWideChar( fDllFileName+'|'+self.name+'|'+dxLayoutControl_query.Name+'.ini'));
  fWindowDesign.fProcedureName:=fProcedureName;
  TgridLiSet.Visible:=gIsCanDesign;
  fdesignCode := fDllFileName +'|'+self.name+ '|' + intTostr(fformId) ;
  CreateCxGridColumn( Tv1,ADOQueryBaseTemp,PWideChar(fdesignCode+'|cxgrid1'));
  CreateCxGridColumn( Tv1,ADOQueryBaseTemp,PWideChar(fdesignCode+'|cxgrid2'));
  //增加动态条件
  addQryContionByLay(ADOQueryBaseTemp,fformId,'cxgrid1',dxLayoutControl_query,7);
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
  initGrid();
end;

///////////////////////////////
///
procedure TfrmDetailBaseList.initGrid();
begin
  if fProcedureName='' then exit;
  try
     cxProgressBar1.Visible:=true;
     cxProgressBar1.Refresh ;

    if fQueryType='criteria' then
    begin
       doQueryByCriteria() ;
    end
    else
    begin
      frmFrameDateSel1.jqModel.Checked:=true;
      doQuery() ;
    end;

  finally
      cxProgressBar1.Visible:=false;
  end;

end;

/////////////////////////
procedure TfrmDetailBaseList.InitSub();
begin ADOQuerySub.close;
  if fProcedureSubName='' then
  begin
    application.MessageBox('您还未设置子存储过程名称!','提示信息',0);
    exit;
  end;
  if CDS_Main.IsEmpty then
    Exit;
  try
    TV2.BeginUpdate();
    CDS_SUB.DisableControls;
    with ADOQuerySub do begin close;
      sql.Clear;
      sql.Add('exec  '+fProcedureSubName);
      sql.Add('@mainId =' + quotedstr((CDS_Main.fieldbyname('mainID')
        .AsString)));
      Open;
    end;

    SCreateCDS(ADOQuerySub, CDS_SUB);
    SInitCDSData(ADOQuerySub, CDS_SUB);
  finally
    CDS_SUB.EnableControls;
    TV2.EndUpdate;
  end;

  //
end;
end.
