unit U_CPSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, jpeg, U_SLT, ComObj, Menus,
  cxPC, Math;

type
  FdDy = record
    inc: integer; //客户端套接字句柄
    FDdys: string[32]; //客户端套接字
    FdDysName: string[32]; //客户端套接字
  end;

  TfrmCPSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryTree20: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxSplitter1: TcxSplitter;
    Panel1: TPanel;
    v1CYNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    v1Column12: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    tbOK: TToolButton;
    Label3: TLabel;
    CYNO: TEdit;
    Panel2: TPanel;
    v1Column19: TcxGridDBColumn;
    DSCYNO: TDataSource;
    ADOQueryTree: TClientDataSet;
    Panel5: TPanel;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    Label11: TLabel;
    Label12: TLabel;
    Panel3: TPanel;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    adoqueryPicture: TADOQuery;
    v1Column39: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label4: TLabel;
    CYName: TEdit;
    v1Column6: TcxGridDBColumn;
    Label1: TLabel;
    CYColor: TEdit;
    Label5: TLabel;
    CYSpec: TEdit;
    Label6: TLabel;
    CYCF: TEdit;
    Label7: TLabel;
    CYNote: TEdit;
    OpenDialog1: TOpenDialog;
    Label9: TLabel;
    Label10: TLabel;
    CYKZ: TEdit;
    CarType: TEdit;
    Label8: TLabel;
    CYMF: TEdit;
    Label13: TLabel;
    ZhenXing: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column11: TcxGridDBColumn;
    Panel7: TPanel;
    Panel8: TPanel;
    cxTabControl1: TcxTabControl;
    Panel4: TPanel;
    Label14: TLabel;
    LBCPAP1: TLabel;
    Button1: TButton;
    Button2: TButton;
    TCBNOR1: TComboBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TCBNOR1Change(Sender: TObject);
    procedure CYNOKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure tbOKClick(Sender: TObject);
  private
    { Private declarations }
    CurrentPage, RecordsNumber: Integer;
    CPID: string;
    PState: Integer;
    FCPID, FTopID: string;
    procedure InitTree();
    procedure InitGrid();
    procedure ReadINIFile();
    procedure InitImage();
  public
    dFdDy: array[0..20] of FdDy; //客户端连接数组
    canshu1: string;
    { Public declarations }
  end;

var
  frmCPSel: TfrmCPSel;
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_Fun, U_FileUp;

{$R *.dfm}

procedure TfrmCPSel.InitTree();
var
  i: Integer;
begin
  with ADOQueryTree20 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_Type order by CPlevel,CPOrder,CPName');
    Open;
  end;
  SCreateCDS20(ADOQueryTree20, ADOQueryTree);
  SInitCDSData20(ADOQueryTree20, ADOQueryTree);
  cxDBTreeList1.Items[0].Expand(false);
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmCPSel.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Panel2.Visible := True;
  Panel2.Refresh;

  case cxTabControl1.TabIndex of
    0:
      begin
        fwhere := 'where status=''0'' ';
      end;
    1:
      begin
        fwhere := 'where status=''1'' ';
      end;
  end;
  Pwhere := SGetFilters(Panel1, 1, 2);
  if CYNo.TexT <> '' then
  begin
    if POS('*', CYNo.TexT) > 0 then
    begin
      CYNO.Tag := 9;
      if Pwhere = '' then
        Pwhere := ' CYNo  like ' + quotedstr(StringReplace(CYNo.TexT, '*', '%', [rfReplaceAll]));
    end
    else
    begin
      CYNO.Tag := 2;
      Pwhere := SGetFilters(Panel1, 1, 2);
    end;

  end;

  if trim(Pwhere) <> '' then
  begin
    if fwhere <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere)
    else
      fwhere := ' where ' + trim(Pwhere);
  end;
  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add(' exec P_Page_YP  ');
      sql.Add('@CYType=' + quotedstr(Trim(ADOQueryTree.fieldbyname('CPID').AsString)));
      sql.Add(',@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

    LBCPAP1.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));

  finally
    ADOQueryMain.EnableControls;
  end;
  Panel2.Visible := False;
end;

procedure TfrmCPSel.FormDestroy(Sender: TObject);
begin
  frmCPSel := nil;
end;

procedure TfrmCPSel.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i, j: integer;
begin
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
//  writeCxGrid(self.Caption+tv1.Name,tv1);
 // Application := mainApplication;
  Action := cafree;
end;

procedure TfrmCPSel.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '样品管理');
  if DirectoryExists(ExtractFileDir('D:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209', sw_hide);
  Close;
end;

procedure TfrmCPSel.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption, Tv1, '样品管理');
  RecordsNumber := 100;
  Panel3.Align := alclient;
  CurrentPage := 1;
  InitTree();

  InitGrid();
end;

procedure TfrmCPSel.cxDBTreeList1DblClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCPSel.TBRafreshClick(Sender: TObject);
begin
  InitTree();
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCPSel.ReadINIFile();
var
  programIni: Tinifile;  //配置文件名
  FileName: string;
begin
  FileName := ExtractFilePath(Paramstr(0)) + 'SYSTEMSET.INI';
  programIni := Tinifile.create(FileName);
  server := programIni.ReadString('SERVER', '服务器地址', '127.0.0.1');
  programIni.Free;
end;

procedure TfrmCPSel.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty then
    Exit;
{  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_YDang where CYID='''+Trim(CDS_Main.fieldbyname('CYID').asstring)+'''');
    Open;
  end;
  CPID:=Trim(ADOQueryTemp.fieldbyname('CYType').AsString);
  ADOQueryTree.Locate('CPID',CPID,[]);  }
  InitImage();
end;

procedure TfrmCPSel.InitImage();
var
  i, j: integer;
  jpg: TJpegImage;
  myStream: TADOBlobStream;
begin
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  if CDS_Main.IsEmpty then
    exit;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName from TP_File A,XD_File B ');
      sql.add('where A.TFID =B.XFID and A.WBID=' + quotedstr(trim(CDS_Main.fieldbyname('CYID').AsString)));
      open;
    end;
    j := adoqueryPicture.RecordCount;
    if j < 1 then
      exit;
    adoqueryPicture.DisableControls;
    adoqueryPicture.First;
    SetLength(Mach, j);
    jpg := TJpegImage.Create();
    for i := 0 to j - 1 do
    begin
      if triM(adoqueryPicture.fieldbyname('FilesOther').AsString) <> '' then
      begin
        myStream := tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')), bmread);
        jpg.LoadFromStream(myStream);
        Mach[i] := TfrmSlt.Create(Self);
        Mach[i].Name := trim(adoqueryPicture.fieldbyname('TFID').AsString);
        Mach[i].Parent := ScrollBox1;
        Mach[i].Left := 0 + i * 165;
        Mach[i].Init(adoqueryPicture.fieldbyname('TFID').AsString, adoqueryPicture.fieldbyname('FileName').AsString, jpg);
      end;
      adoqueryPicture.Next;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

procedure TfrmCPSel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmCPSel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

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

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

procedure TfrmCPSel.TCBNOR1Change(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR1.Text);
  CurrentPage := 1;
  InitGrid();
end;

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

procedure TfrmCPSel.cxTabControl1Change(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCPSel.tbOKClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

end.

