unit U_CPManage_TP;

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, RM_BarCode,
  cxButtonEdit, cxTextEdit,jpeg,U_SLT_TP,U_SLT,Math, Menus, cxCalendar,
  cxLookAndFeels, cxLookAndFeelPainters, cxTLdxBarBuiltInMenu;

type
  TfrmCPManage_TP = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxSplitter1: TcxSplitter;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    DSCYNO: TDataSource;
    CDS_CYNO: TClientDataSet;
    RMBarCodeObject1: TRMBarCodeObject;
    Panel3: TPanel;
    adoqueryPicture: TADOQuery;
    Panel6: TPanel;
    Label23: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    Label16: TLabel;
    RMDB_Sub: TRMDBDataSet;
    RM2: TRMGridReport;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    ADOQuery4: TADOQuery;
    ADOQuery5: TADOQuery;
    ADOQuery6: TADOQuery;
    ADOQuery7: TADOQuery;
    ADOQuery8: TADOQuery;
    Panel8: TPanel;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQuery9: TADOQuery;
    DataSource1: TDataSource;
    ADOQueryTree: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    ToolButton5: TToolButton;
    Order_Tree: TClientDataSet;
    ToolBar2: TToolBar;
    SKLB: TToolButton;
    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 ToolButton5Click(Sender: TObject);
    procedure CYMFChange(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton13Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SKLBClick(Sender: TObject);
  private
    { Private declarations }
    CPID:string;
    LP,CP,NP,AP,RN:Integer;
    PState:Integer;
    FCPID,FTopID:String;
    canshu1:string;
    FPrint:string;
    FZGao:integer;
    procedure InitTree();
//    procedure InitGrid();
    procedure ReadINIFile();
    procedure InitImage();
    procedure initsub();
    procedure InitImage1();
    procedure initImage10(k:Integer;FPanel:TScrollbox;fstr1,FSKID:string);
    procedure InitGrid(CP,NOR:Integer);
    procedure setBtn(CP:Integer);
    procedure setstatus();
    procedure Printdata();
    procedure MXPrintdata();
  public
    { Public declarations }
  end;

var
  frmCPManage_TP: TfrmCPManage_TP;
  Mach: array of TfrmSlt_TP;
  Mach1: array of array of TfrmSlt;
implementation
uses
  U_DataLink,U_Fun,U_CPAdd,U_FileUp, U_YPTypeHelp,U_CPAdd_ZLR,U_RTFun,U_FileUp_TP,U_CPType_TP,U_SKTP_DJ;

{$R *.dfm}
procedure TfrmCPManage_TP.initsub();
begin
  
end;

procedure TfrmCPManage_TP.initImage10(k:Integer; FPanel:TScrollbox;fstr1,FSKID:string);
var
  i,j,x,p:integer;
  jpg:TJpegImage;
  myStream: TADOBlobStream;
begin
  try
    with ADOQuery7 do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName,C.* from TP_File A ');
      sql.Add(' inner join XD_File B on A.WBID=B.CYID ');
      sql.Add(' left join SH_Base C on A.WBID=C.WBID ');
      SQL.Add('where B.CYNO='+quotedstr(trim(fstr1)));
      //ShowMessage(SQL.Text);
      open;
    end;
    j:=ADOQuery7.RecordCount;
    if j<1 then exit;
    jpg:=TJpegImage.Create();
    ADOQuery7.DisableControls;
    ADOQuery7.First;
   // SetLength(Mach1,k,j);
    x:=0; //行
    p:=0; //列
    for i:=0 to j-1 do
    begin
      if trim(ADOQuery7.fieldbyname('FilesOther').AsString)<>'' then
      begin
        myStream:=tadoblobstream.Create(tblobfield(ADOQuery7.fieldbyname('FilesOther')),bmread);
        jpg.LoadFromStream(myStream);
        if i<5*(p+1) then
        begin

        end
        else
        begin
          p:=p+1;
          Mach[k].Height:=Mach[k].Height+130;
          FZGao:=FZGao+130;
        end;
        Mach1[K,i]:=TfrmSlt.Create(Self);
        Mach1[k,i].Name:=trim(ADOQuery7.fieldbyname('TFID').AsString)+inttostr(k)+inttostr(i);
        Mach1[k,i].Parent :=FPanel;
        Mach1[k,I].Left:=0+(x-5*p)*185;
        Mach1[k,I].Top:=p*143;
        Mach1[k,i].SKID:=trim(FSKID);
        if ADOQuery7.fieldbyname('SHID').AsString='' then
        begin
          Mach1[k,I].ColorNo.Text:=inttostr(i+1);
          Mach1[k,i].WBID.Text:=trim(ADOQuery7.fieldbyname('WBID').AsString);
        end
        else
        begin
          SCSHDataNew(ADOQuery7,Mach1[k,I].Panel1,2);
        end;
        Mach1[k,i].Init(ADOQuery7.fieldbyname('TFID').AsString,ADOQuery7.fieldbyname('FileName').AsString,jpg);
      end;
      x:=x+1;
      ADOQuery7.Next;
    end;
     ADOQuery7.EnableControls;
  finally
    jpg.free;
  end;
end;

procedure TfrmCPManage_TP.InitImage1();
var
  i,j:integer;
  jpg:TJpegImage;
  myStream: TADOBlobStream;
begin
  j:=length(Mach1);
  IF j>0 then
  begin
    for i:=0 to 98 do
    begin
       for j:=0 to 98 do
       Mach1[i,j].free;
    end;
    //Finalize(Mach1);
  end;
  j:=length(Mach);
  if j>0 then
  begin
    for i:=0 to j-1 do
    begin
      Mach[i].free;
    end;
    //Finalize(Mach);
  end;
  SetLength(Mach, 0);
  SetLength(Mach1,99,99);
  {}
  IF Order_Tree.IsEmpty then exit;
  FZGao:=0;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName,C.* from SK_Base C ');
      SQL.Add(' left join TP_File_TP A on C.WBID=A.WBID ');
      sql.Add(' left join XD_File_TP B on A.WBID =B.CYID');
      sql.Add(' where C.CPID='+quotedstr(trim(Order_Tree.fieldbyname('CPID').AsString)));
      //ShowMessage(sql.Text);
      open;
    end;
    j:=adoqueryPicture.RecordCount;
    if j<1 then exit;
    adoqueryPicture.DisableControls;
    adoqueryPicture.First;
    SetLength(Mach, j);
    with adoqueryPicture do
    begin
      First;
      i:=0;
      while not eof do
      begin
        if triM(adoqueryPicture.fieldbyname('TFID').AsString)<>'' then
        begin
          jpg:=TJpegImage.Create();
          myStream:=tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')),bmread);
          jpg.LoadFromStream(myStream);
        end;
        Mach[i] := TfrmSlt_TP.Create(Self);
        Mach[i].Name:=trim(adoqueryPicture.fieldbyname('SKID').AsString);
        Mach[i].Parent := ScrollBox1;
        Mach[I].Left:=0;
        Mach[I].Top:=350*i+FZGao;
        //  Mach[I].Panel1.Name:='Panel'+inttostr(i+1);
        SCSHDataNew(adoqueryPicture,Mach[I].Panel0,2);
        if triM(adoqueryPicture.fieldbyname('TFID').AsString)<>'' then
        begin
          Mach[I].FMXCPID:=trim(adoqueryPicture.fieldbyname('TFID').AsString);
          Mach[i].Init(adoqueryPicture.fieldbyname('TFID').AsString,adoqueryPicture.fieldbyname('FileName').AsString,jpg);
        end;
        initImage10(i,Mach[i].ScrollBox2,adoqueryPicture.fieldbyname('SKID').AsString,adoqueryPicture.fieldbyname('SKID').AsString);
        i:=i+1;
        Next;
      end;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    //application.ProcessMessages;
  end;
end;


procedure TfrmCPManage_TP.setstatus();
begin
  
end;

procedure TfrmCPManage_TP.InitImage();
begin

end;

procedure TfrmCPManage_TP.InitTree();
var
  i:Integer;
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_TypeSK order by CPlevel,CPOrder,CPName');
    Open;
  end;
  SCreateCDS20(ADOQueryTree,Order_Tree);
  SInitCDSData20(ADOQueryTree,Order_Tree);
  cxDBTreeList1.Items[0].Expand(True);
end;

procedure TfrmCPManage_TP.InitGrid(CP,NOR:Integer);
begin
  BTLP.Enabled:=False;
  BTNP.Enabled:=False;
  try
    ADOQueryMain.DisableControls;
    LP:= CP-1;
    NP:= CP+1;
    With ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Select_YPTP :CYType,:CP,:RON,:WSql,:Stats');
      Parameters.ParamByName('CYType').Value:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
      Parameters.ParamByName('CP').Value:=CP;
      Parameters.ParamByName('RON').Value:=NOR;
      Parameters.ParamByName('WSql').Value:='';
      Parameters.ParamByName('Stats').Value:=1;
      Open;
    end;
    LBCPAP.Caption:= IntToStr(CP)+'/'+ IntToStr(Ceil(ADOQueryMain.FieldByName('AR').AsInteger/RN));
    AP:= Ceil(ADOQueryMain.FieldByName('AR').AsInteger/RN);
    Label16.Caption:='共计 '+IntToStr(ADOQueryMain.FieldByName('AR').AsInteger)+' 条记录';
    if CP>1 then
    begin
      BTLP.Enabled:=true;
    end;
    if CP<Ceil(ADOQueryMain.FieldByName('AR').AsInteger/RN) then
    begin
      BTNP.Enabled:=true;
    end;
    with ADOQueryMain do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      sql.Add(' exec P_Select_YPTP :CYType,:CP,:RON,:WSql,:Stats ');
      Parameters.ParamByName('CYType').Value:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
      Parameters.ParamByName('CP').Value:=CP;
      Parameters.ParamByName('RON').Value:=NOR;
      Parameters.ParamByName('WSql').Value:='';
      Parameters.ParamByName('Stats').Value:=2;
      Open;
    end;
    InitImage1();
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPManage_TP.FormDestroy(Sender: TObject);
begin
  frmCPManage_TP:=nil;
  
end;

procedure TfrmCPManage_TP.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i,j:integer;
  jpg:TJpegImage;
  myStream: TADOBlobStream;
begin
  Finalize(Mach1);
  Finalize(Mach);
  Action:=caFree;
end;

procedure TfrmCPManage_TP.TBCloseClick(Sender: TObject);

begin
  if  DirectoryExists(ExtractFileDir('D:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209',sw_hide);
  Close;
end;

procedure TfrmCPManage_TP.FormShow(Sender: TObject);
begin
  InitTree();
  Label16.Caption:='';
  RN:=StrToInt(Trim(TCBNOR.Text));
  setstatus();
  //InitGrid();
end;

procedure TfrmCPManage_TP.cxDBTreeList1DblClick(Sender: TObject);
begin
  CP:=1;
  //InitGrid(1,RN);
  //Finalize(Mach1);
  //Finalize(Mach);
  InitImage1();
end;

procedure TfrmCPManage_TP.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  TBRafresh.Enabled:=false;
  //InitTree();
  //InitGrid(1,RN);
  InitImage1();
  TBRafresh.Enabled:=True;
end;

procedure TfrmCPManage_TP.ToolButton2Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid(1,RN);
end;

procedure TfrmCPManage_TP.ToolButton5Click(Sender: TObject);
begin
  FrmSKTP_DJ:=TFrmSKTP_DJ.create(self);
  with FrmSKTP_DJ do
  begin                        
    FCPID:=Trim(Order_Tree.Fieldbyname('CPID').AsString);
    if ShowModal=1 then
    begin
      InitImage1();
    end;
    Free;
  end;
end;

procedure TfrmCPManage_TP.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 TfrmCPManage_TP.CYMFChange(Sender: TObject);
begin
  if Length(Trim(TEdit(Sender).Text))<2 then Exit;
  InitGrid(1,RN);
end;

procedure TfrmCPManage_TP.BTLPClick(Sender: TObject);
begin
  CP:=LP;
  InitGrid(CP,RN);
end;

procedure TfrmCPManage_TP.BTNPClick(Sender: TObject);
begin
  CP:=NP;
  InitGrid(CP,RN);
end;

procedure TfrmCPManage_TP.setBtn(CP:Integer);
begin
  if (CP=null) or (CP=1)  then
  begin
    CP:=1;
    BTLP.Enabled:=False;
  end else
  begin
   BTLP.Enabled:=True;
  end;
  if NP>AP then
  begin
    BTNP.Enabled:=False;
  end
  else
  begin
    BTNP.Enabled:=True;
  end;
end;

procedure TfrmCPManage_TP.TCBNORChange(Sender: TObject);
begin
  RN:=StrToInt(TCBNOR.Text);
  InitGrid(1,RN);
end;

procedure TfrmCPManage_TP.Printdata();
begin

end;

procedure TfrmCPManage_TP.MXPrintdata();
begin
  
end;

procedure TfrmCPManage_TP.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
end;

procedure TfrmCPManage_TP.ToolButton12Click(Sender: TObject);
begin
  if Order_Tree.IsEmpty then exit;
  try
    frmFileUp:=TfrmFileUp.Create(Application);
    with frmFileUp do
    begin
      Code.Text:=Trim(Self.Order_Tree.fieldbyname('CPID').AsString);
      CYID:=Trim(Self.Order_Tree.fieldbyname('CPID').AsString);
      if ShowModal=1 then
      begin
        Self.InitGrid(NP-1,RN);
        Self.Order_Tree.Locate('CPID',CYID,[]);
      end;
    end;
  finally
    frmFileUp.Free;
  end;
end;

procedure TfrmCPManage_TP.ToolButton13Click(Sender: TObject);
var
  fHandle:THandle;
  FInt:Integer;
  FFName,FPath:String;
begin
  if Order_Tree.IsEmpty then exit;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from XD_File where CYNO='''+Trim(Order_Tree.fieldbyname('CYNO').AsString)+'''');
    Open;
    if IsEmpty then
    begin
      Application.MessageBox('样品图片未上传！','提示',0);
      Exit;
    end;
  end;
  try
    ReadINIFile();
    server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    if Length(server)<6 then
    begin
      server:='127.0.0.1';
    end;
    IdFTP1.Host :=server;//PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath:='D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  FFName:=Trim(ADOQueryTemp.fieldbyname('FileName').AsString);
  FFName:=FPath+FFName;
  if FileExists(FFName) then
  begin
    FInt:=1;
  end;
  if FInt<>1 then
    IdFTP1.Get(UserDataFlag+'YP\'+Trim(ADOQueryTemp.fieldbyname('FileName').AsString),
                   FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString)
                    );
  if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString)),'', '', SW_SHOWNORMAL);
end;

procedure TfrmCPManage_TP.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,true);
end;

procedure TfrmCPManage_TP.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,false);
end;

procedure TfrmCPManage_TP.SKLBClick(Sender: TObject);
begin
  frmCPType_TP:=TfrmCPType_TP.Create(self);
  with frmCPType_TP do
  begin
    if ShowModal=1 then
    begin
      InitTree();
    end;
    free;
  end;
end;

end.
