unit U_SketchManageListSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxContainer,
  cxImage, cxDBEdit, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, StrUtils, cxCheckBox, cxPC, cxMemo, Menus, Math, Clipbrd,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmSketchManageListSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBOK: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    M_HXCODE: TEdit;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    RMDBPRT: TRMDBDataSet;
    RMDBDataSet1: TRMDBDataSet;
    ClientDataSet1: TClientDataSet;
    v1Column31: TcxGridDBColumn;
    IdFTP1: TIdFTP;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    ClientDataSet2: TClientDataSet;
    RMDBDataSet2: TRMDBDataSet;
    Label7: TLabel;
    XGOrderno: TEdit;
    v1Column10: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    Panel6: TPanel;
    Label9: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    cxTabControl1: TcxTabControl;
    CheckBox1: TCheckBox;
    N3: TMenuItem;
    TCBNOR: TEdit;
    lbl1: TLabel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure M_HXCODEChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Picture4DblClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure FormDestroy(Sender: TObject);
    procedure cxTabControl1CanClose(Sender: TObject; var ACanClose: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure cbbISSavedChange(Sender: TObject);
    procedure chkYDDClick(Sender: TObject);
    procedure chkwzbClick(Sender: TObject);
    procedure chkWDDClick(Sender: TObject);
    procedure v1Column15PropertiesCustomClick(Sender: TObject);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure M_HXCODEKeyPress(Sender: TObject; var Key: Char);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TCBNORKeyPress(Sender: TObject; var Key: Char);
    procedure TBOKClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    FInt, PFInt: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure InitForm();
    procedure SetStatus();
    function DelData(): Boolean;
    procedure InitButton();
    procedure LookImage(FileName: string);
    procedure InitImage();
    { Private declarations }
  public
    canshu1, FCheJian: string;
    FKHName: string;
    FChtType: string;
    fFlileFlag: string;
    CXTJ1, CXZ1: string;
    CXTJ2, CXZ2: string;
    { Public declarations }
  end;

var
  frmSketchManageListSel: TfrmSketchManageListSel;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_FjList10, U_SketchManage, U_QXGL, U_OrderInPut; //U_ShuttleClothSCInPut2

{$R *.dfm}

procedure TfrmSketchManageListSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  WriteCxGrid(self.Caption, Tv1, '分析单管理');

end;

procedure TfrmSketchManageListSel.SetStatus();
begin

end;

procedure TfrmSketchManageListSel.InitButton();
begin

end;

procedure TfrmSketchManageListSel.TBCloseClick(Sender: TObject);
begin

  WriteCxGrid('印花描稿管理6', Tv1, '坯布生产管理');
  Close;
end;

procedure TfrmSketchManageListSel.InitGrid();
var
  fwhere, Pwhere: string;
begin
  fwhere := '';
  if SGetFilters(Panel1, 1, 2) <> '' then
    fwhere := fwhere + ' and ' + SGetFilters(Panel1, 1, 2);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select top ' + inttostr(RecordsNumber) + ' temp_row.*     ');
      sql.Add(',ISSD=[dbo].[F_Mg_Judge_SD](' + quotedstr(FKHName) + ',temp_row.M_HXCODE) ');
      sql.Add('from (select row_number() over(order by AA.FILLTIME DESC  ) as rownumber '); //--序号

      sql.Add(',COUNT(1) OVER() AS TotalCount '); //总记录数
      sql.Add(',* from (  ');
      sql.Add(' select A.*   ');
      sql.Add(' ,XGOrderno=cast((select distinct X.OrderNO +'' '' from JYOrder_Main X inner join JYOrder_Sub Y on X.MainId=Y.MainId  AND  Y.MGMainId=A.MgId for xml path('''') ) as varchar(1000))     ');
      sql.Add(' ,SLT=(select top 1 FilesOther from TP_File X where X.WBID=A.MGID and  X.TFType=''MGSLT'' )          ');
      SQL.Add(' FROM Sketch_MAIN A');
      sql.Add(' where 1=1 ');
      if CheckBox1.Checked then
      begin
        sql.Add('AND A.FILLTIME>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
        sql.Add('and A.FILLTIME<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      end;
      sql.Add(' )  AA where 1=1 ');
      sql.Add(fwhere);
      sql.Add(') temp_row');
      sql.Add(' where rownumber> ' + inttostr((CurrentPage - 1) * RecordsNumber));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);

    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    lbl1.Caption := '记录条数:' + Order_Main.FieldByName('TotalCount').AsString;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSketchManageListSel.InitForm();
begin
// InitButton();
  CurrentPage := 1;
  RecordsNumber := 30;
  TCBNOR.Text := '30';
  fFlileFlag := UserDataFlag + 'HX';
  ReadCxGrid('印花描稿管理6', Tv1, '坯布生产管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 30;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  SetStatus();
  InitGrid();
end;

function TfrmSketchManageListSel.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Sketch_main where MGID=''' + Trim(Order_Main.fieldbyname('MGID').AsString) + '''');

      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmSketchManageListSel.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  SetStatus();
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmSketchManageListSel.M_HXCODEChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmSketchManageListSel.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmSketchManageListSel.Picture4DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;

  LookImage(ADOQueryImage.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

procedure TfrmSketchManageListSel.LookImage(FileName: string);
var
  sFieldName: string;
begin

  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);

  sFieldName := sFieldName + '\' + trim(FileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin

//    Panel2.Caption := '正在下载数据，请稍等...';
//    Panel2.Visible := true;
    application.ProcessMessages;
    try
//      ShowMessage(PChar(fFlileFlag + '\' + Trim(FileName)));
      IdFTP1.Get(fFlileFlag + '\' + Trim(FileName), sFieldName, true, false);
    except
//      Panel2.Visible := false;
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
//    Panel2.Visible := false;
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
//  Panel2.Visible := false;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmSketchManageListSel.InitImage();
begin

  ADOQueryImage.Close;
  if Order_Main.IsEmpty then
    exit;
//  with ADOQueryImage do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from TP_File A');
//    sql.Add('where mgid=' + quotedstr(trim(Order_Main.fieldbyname('mgid').AsString)));
//    sql.Add('and TFType=' + quotedstr(trim('寻样')));
//    open;
//  end;
end;

procedure TfrmSketchManageListSel.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if TV1.Controller.FocusedColumn.DataBinding.FilterFieldName = 'DTP' then
  begin

    if ACellViewInfo.Value = '设计稿' then
      LookImage(Trim(Self.Order_Main.fieldbyname('IMG_SJT').AsString));
  end;
  if TV1.Controller.FocusedColumn.DataBinding.FilterFieldName = 'BQSD' then
  begin
    if ACellViewInfo.Value = '查看' then
    begin

      try
        frmQXGL := TfrmQXGL.Create(Application);
        with frmQXGL do
        begin
          FM_HXCODE := Trim(Self.Order_Main.fieldbyname('M_HXCODE').AsString);

          if ShowModal = 1 then
          begin

          end;
        end;
      finally
        frmQXGL.Free;
      end;
    end;
  end;
end;

procedure TfrmSketchManageListSel.FormDestroy(Sender: TObject);
begin
  frmSketchManageListSel := nil;
end;

procedure TfrmSketchManageListSel.cxTabControl1CanClose(Sender: TObject; var ACanClose: Boolean);
begin
  SetStatus();
  InitGrid();
end;

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

procedure TfrmSketchManageListSel.cbbISSavedChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmSketchManageListSel.chkYDDClick(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmSketchManageListSel.chkwzbClick(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmSketchManageListSel.chkWDDClick(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmSketchManageListSel.v1Column15PropertiesCustomClick(Sender: TObject);
begin
  Application.MessageBox('已经有制版日期，不能删除！', '提示', 0);
end;

procedure TfrmSketchManageListSel.Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  FBounds := AViewInfo.Bounds;
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);
    InflateRect(FBounds, -1, -1);    //Platform   specific.   May   not   work   on   Linux.
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TfrmSketchManageListSel.M_HXCODEKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

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

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

procedure TfrmSketchManageListSel.N3Click(Sender: TObject);
begin

  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmSketchManageListSel.TCBNORKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    RecordsNumber := StrToIntDef(TCBNOR.Text, 10);
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmSketchManageListSel.TBOKClick(Sender: TObject);
begin
  if Trim(Order_Main.fieldbyname('ISSD').AsString) = '已锁定' then
  begin
    if Application.MessageBox('已锁定，确定要选择吗？', '提示', 32 + 4) <> IDYES then
      exit;
  end;
  ModalResult := 1;
end;

procedure TfrmSketchManageListSel.Tv1DblClick(Sender: TObject);
begin
  TBOK.Click;
end;

end.

