unit U_CPManageSel;

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,
  cxCurrencyEdit;

type
  TfrmCPManageSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    DataSource1: TDataSource;
    ADOQueryTree: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxSplitter1: TcxSplitter;
    Panel1: TPanel;
    Label2: TLabel;
    CYID: TEdit;
    ToolButton2: TToolButton;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    TOK: TToolButton;
    Panel2: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    CYName: TEdit;
    CYEName: TEdit;
    CYSpec: TEdit;
    CYNO: TEdit;
    Edit1: TEdit;
    Label7: TLabel;
    CYKZ: TEdit;
    defstr1: TEdit;
    Label13: TLabel;
    Panel3: TPanel;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    adoqueryPicture: TADOQuery;
    DefStr6: TEdit;
    Label8: TLabel;
    Label10: TLabel;
    CYMF: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column19: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column41: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column43: TcxGridDBColumn;
    v1Column44: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label9: TLabel;
    XTNo: TEdit;
    v1XTNo: TcxGridDBColumn;
    Label11: TLabel;
    CYHX: TEdit;
    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 ToolButton2Click(Sender: TObject);
    procedure CYIDKeyPress(Sender: TObject; var Key: Char);
    procedure CYNoChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure CYSpecChange(Sender: TObject);
    procedure TOKClick(Sender: TObject);
    procedure CYNOKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    CPID:string;
    PState:Integer;
    FCPID,FTopID:String;
    procedure InitTree();
    procedure InitGrid();
    procedure ReadINIFile();
    procedure InitImage();

  public
    { Public declarations }
  end;

var
  frmCPManageSel: TfrmCPManageSel;
  Mach: array of TfrmSlt;

implementation
uses
  U_DataLink,U_Fun10;

{$R *.dfm}

procedure TfrmCPManageSel.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);
  
  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  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 TfrmCPManageSel.InitTree();
var
  i:Integer;
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_Type order by CPlevel,CPOrder,CPName');
    Open;
  end;
  cxDBTreeList1.Items[0].Expand(false);
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmCPManageSel.InitGrid();
begin
  Panel2.Visible:=True;
  Panel2.Refresh;
  {try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      SQL.Add('exec P_Select_CP_YDang :Code,:PState,:CYType');
      Parameters.ParamByName('Code').Value:='';
      Parameters.ParamByName('PState').Value:=0;
      Parameters.ParamByName('CYType').Value:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;}
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
//    SQL.Add('select A.*,JG.HXNo,JG.HXName from CP_YDang A');
//    SQL.Add('left join CP_YDang_ML ML on ML.CYID=A.CYID and ML.CengJi=6');
//    SQL.Add('left join CP_YDang_JG JG on JG.CYID=A.CYID and JG.JGID=ML.MLID');
    SQL.Add('select * from CP_YDang');
    SQL.Add('where 1=2');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDS_Main);
  SInitCDSData20(ADOQueryMain,CDS_Main);
  Panel2.Visible:=False;
  //ToolButton2.Click;
end;

procedure TfrmCPManageSel.FormDestroy(Sender: TObject);
begin
  frmCPManageSel:=nil;
end;

procedure TfrmCPManageSel.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);
  Action:=caFree;
end;

procedure TfrmCPManageSel.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 TfrmCPManageSel.FormShow(Sender: TObject);
begin
  Panel3.Align:=alclient;
  InitTree();
  ReadCxGrid(self.Caption,Tv1,'样品管理');
  //InitGrid();
end;

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

procedure TfrmCPManageSel.TBRafreshClick(Sender: TObject);
begin
  inittree();
  InitGrid();
end;

procedure TfrmCPManageSel.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManageSel.CYIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYID='''+Trim(CYID.Text)+'''');
      Open;
    end;
    CPID:=Trim(ADOQueryTemp.fieldbyname('CYType').AsString);
    if Trim(CYID.Text)='' then Exit;
    SDofilter(ADOQueryMain,' CYID='''+Trim(CYID.Text)+''' ');
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
    CYID.Text:='';
    ADOQueryTree.Locate('CPID',CPID,[]);
  end;
end;

procedure TfrmCPManageSel.CYNoChange(Sender: TObject);
begin
//  if Length(Trim(CYNO.Text))<4 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManageSel.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel('样品档案',cxGrid1);
end;

procedure TfrmCPManageSel.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 TfrmCPManageSel.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty then Exit;
  initimage();
end;

procedure TfrmCPManageSel.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  {if Key= then
  begin
    if CDS_Main.IsEmpty then Exit;
    if Application.MessageBox('确定6666要删除数据吗？','提示',32+4)<>IDYES then Exit;
    if Trim(CDS_Main.fieldbyname('CYID').AsString)<>'' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('delete CP_YDang where CYID='''+Trim(CDS_Main.fieldbyname('CYID').AsString)+'''');
        ExecSQL;
      end;
    end;

    CDS_Main.Delete;
  end;  }
end;

procedure TfrmCPManageSel.CYSpecChange(Sender: TObject);
begin
//  if Length(Trim(CYSpec.Text))<3 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManageSel.TOKClick(Sender: TObject);
begin
  IF CDS_main.IsEmpty then exit;
  ModalResult:=1;
end;

procedure TfrmCPManageSel.CYNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      SQL.Add('select * from CP_YDang');
//      SQL.Add('select A.*,JG.HXNo,JG.HXName from CP_YDang A');
//      SQL.Add('left join CP_YDang_ML ML on ML.CYID=A.CYID and ML.CengJi=6');
//      SQL.Add('left join CP_YDang_JG JG on JG.CYID=A.CYID and JG.JGID=ML.MLID');
      SQL.Add('where ' +Tedit(sender).Name+' like '+Quotedstr('%'+trim(Tedit(sender).Text)+'%'));
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

end.
