unit U_BPZdy_TJHXK;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ToolWin, ComCtrls,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, ADODB, ImgList,
  StdCtrls, ExtCtrls, cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxButtonEdit, BtnEdit, cxTL, cxMaskEdit, cxInplaceContainer, cxDBTL,U_SLT_TJHX,U_CPType_TJTP,
  cxTLData, cxContainer, cxCurrencyEdit, cxCheckBox, cxSplitter,jpeg,U_SLT_PBSK,U_SLT,Math, Menus, cxCalendar;

type
  TfrmBPZDY_TJHXK = class(TForm)
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    Ord_TJ: TClientDataSet;
    ADOConnection1: TADOConnection;
    ThreeImgList: TImageList;
    DataSource2: TDataSource;
    Panel2: TPanel;
    Panel4: TPanel;
    DataSource4: TDataSource;
    ADOQueryTree: TADOQuery;
    Order_Tree: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    cxSplitter1: TcxSplitter;
    ToolBar2: TToolBar;
    TSH: TToolButton;
    ToolBar1: TToolBar;
    adoqueryPicture: TADOQuery;
    ScrollBox2: TScrollBox;
    THB: TToolButton;
    ToolButton1: TToolButton;
    TGB: TToolButton;
    ADOQuery1: TADOQuery;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TPBLBClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
    procedure TSHClick(Sender: TObject);
    procedure THBClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TGBClick(Sender: TObject);
  private
    FBPID,FCPID,FCPNo,FCPName:string;
    procedure InitGrid();
    procedure SaveData();
    procedure initTree();
    { Private declarations }
  public
    flag,flagname,snote,MainType,FGYName,Fint,canshu1:string;
    fnote,fnote1,forderno,fZdyFlag,ViewFlag,fHelpType:Boolean;
    PPSTE:integer;
    procedure initimageSH();
    { Public declarations }
  end;
var
  frmBPZDY_TJHXK: TfrmBPZDY_TJHXK;
  Mach: array of TfrmSLT_PBSK;
  Mach1: array of TfrmSLT_TJHX;
implementation
uses
  U_DataLink,U_Fun10,U_BPZdy_SH, U_BPZdy_HXK,U_CPTypePB,U_FileUp_TP,U_FileUp,U_FileUp_PB,U_FileUp_TPSH,U_SLT_BF1;

{$R *.dfm}
procedure TfrmBPZDY_TJHXK.initTree();
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select *');
    sql.Add(', Case when isnull(CPNo,'''')<>'''' then RTrim(CPNo)+''/''+CPName else CPName end as CP ');
    SQL.Add('from CP_TypeTJ order by CPlevel,CPOrder,CPName');
    Open;
  end;
  SCreateCDS20(ADOQueryTree,Order_Tree);
  SInitCDSData20(ADOQueryTree,Order_Tree);
  cxDBTreeList1.Items[0].Expand(true);
end;

procedure TfrmBPZDY_TJHXK.initimageSH();
var
  i,j,p,x:integer;
  jpg:TJpegImage;
  myStream: TADOBlobStream;
  FZGao:string;
begin
  j:=length(Mach1);
  if j>0 then
  begin
    for i:=0 to j-1 do
    begin
      Mach1[i].free;
    end;
    //Finalize(Mach1);
  end;
  SetLength(Mach1, 0);
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName,C.* from TJ_Base C ');
      sql.Add(' inner join XD_File B on B.XFID=C.XFID');
      SQL.Add(' inner join TP_File A on B.XFID=A.TFID');
      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(Mach1, j);
    with adoqueryPicture do
    begin
      First;
      i:=0;
      x:=0;
      p:=0;
      while not eof do
      begin
        jpg:=TJpegImage.Create();
        myStream:=tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')),bmread);
        jpg.LoadFromStream(myStream);
        if i<3*(p+1) then
        begin

        end
        else
        begin
          p:=p+1;
        end;
        Mach1[i] := TfrmSLT_TJHX.Create(Self);
        Mach1[i].Name:=trim(adoqueryPicture.fieldbyname('TFID').AsString);
        Mach1[i].Parent := ScrollBox2;
        Mach1[I].Left:=0+(x-3*p)*185;
        Mach1[I].Top:=p*143;
        if adoqueryPicture.fieldbyname('TFID').AsString='' then
        begin
          Mach1[I].ColorNo.Text:=inttostr(i+1);
          Mach1[i].XFID.Text:=trim(adoqueryPicture.fieldbyname('XFID').AsString);
        end
        else
        begin
          SCSHDataNew(adoqueryPicture,Mach1[I].Panel1,2);
        end;
        Mach1[I].SH.Caption:=Trim(adoqueryPicture.fieldbyname('ColorNo').AsString);
        Mach1[I].ZW.Caption:=Trim(adoqueryPicture.fieldbyname('ColorName').AsString);
        Mach1[I].YW.Caption:=Trim(adoqueryPicture.fieldbyname('ColorNameEng').AsString);
        if triM(adoqueryPicture.fieldbyname('TFID').AsString)<>'' then
        begin
          Mach1[i].Init(adoqueryPicture.fieldbyname('TFID').AsString,adoqueryPicture.fieldbyname('FileName').AsString,jpg);
        end;
        i:=i+1;
        x:=x+1;
        Next;
      end;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
  end;
end;

procedure TfrmBPZDY_TJHXK.SaveData();
begin
  
end;

procedure TfrmBPZDY_TJHXK.FormCreate(Sender: TObject);
begin
  try
    with ADOConnection1 do
    begin
      Connected:=false;
      ConnectionString:=DConString;
      Connected:=true;
    end;
  except;
    frmBPZDY_TJHXK.Free;
  end;
  canshu1:=Trim(DParameters1);
end;

procedure TfrmBPZDY_TJHXK.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ToolBar1.SetFocus;
  Finalize(Mach1);
  Finalize(Mach);
  Action:=caFree;
end;

procedure TfrmBPZDY_TJHXK.InitGrid();
begin
  
end;

procedure TfrmBPZDY_TJHXK.FormShow(Sender: TObject);
begin
  initTree();
  initimageSH();
  ToolBar1.Visible:=false;
  ToolButton1.Visible:=False;
  TSH.Visible:=false;
  if canshu1<>'查询' then
  begin
    ToolBar1.Visible:=true;
    ToolButton1.Visible:=true;
    TSH.Visible:=true;
  end;
end;

procedure TfrmBPZDY_TJHXK.TPBLBClick(Sender: TObject);
begin
  frmCPTypePB:=TfrmCPTypePB.Create(self);
  with frmCPTypePB do
  begin
    if ShowModal=1 then
    begin

    end;
    free;
  end;
  initTree();
end;

procedure TfrmBPZDY_TJHXK.ToolButton1Click(Sender: TObject);
var
  maxno:string;
  j,i:Integer;
begin
  ToolBar1.SetFocus;
  with ScrollBox2 do
  begin
    for i:=0 to ScrollBox2.ControlCount-1 do
    begin
      if Controls[i] is TfrmSLT_TJHX then
      begin
        with TfrmSLT_TJHX(Controls[i]).Panel1 do
        begin
          for j:=0 to ControlCount-1 do
          begin
            if Controls[j] is TButton then
            begin
              TButton(Controls[j]).Click;
            end;
          end;
        end;
      end;
    end;
  end;
  Application.MessageBox('保存成功','提示');
  initimageSH();
end;

procedure TfrmBPZDY_TJHXK.cxDBTreeList1Click(Sender: TObject);
begin
  initimageSH();
end;

procedure TfrmBPZDY_TJHXK.TSHClick(Sender: TObject);
var FSHID:string;
begin
  try
    frmFileUp_TPSH:=TfrmFileUp_TPSH.Create(Application);
    with frmFileUp_TPSH do
    begin
      Code.Text:=Trim(Self.Order_Tree.fieldbyname('CPID').AsString);   //色卡的SKID
      //CYID:=Trim(Self.Order_Tree.fieldbyname('CPID').AsString);
      FSKID:=Trim(Self.Order_Tree.fieldbyname('CPID').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmFileUp_TPSH.Free;
  end;
  with self.ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from XD_File A ');
    sql.Add('where A.SKID='''+trim(Self.Order_Tree.fieldbyname('CPID').AsString)+'''');
    SQL.Add(' and not exists (select B.XFID from TJ_Base B where B.XFID=A.XFID)');
    //ShowMessage(sql.text);
    Open;
  end;
  with self.ADOQueryCmd do
  begin
    First;
    while not eof do
    begin
      if GetLSNo(ADOQuery1,FSHID,'TJ','TJ_Base',4,1)=False then
      begin
        Application.MessageBox('取图片最大号失败！','提示',0);
        Exit;
      end;
      with self.ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('select * from TJ_Base A ');
        sql.Add('where 1=2');
        Open;
      end;
      with self.ADOQuery1 do
      begin
        append;
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('SKID').Value:=Trim(FSHID);
        FieldByName('SHID').Value:=Trim(FSHID);
        FieldByName('Valid').Value:='Y';
        FieldByName('XFID').Value:=Trim(Self.ADOQueryCmd.fieldbyname('XFID').AsString);
        FieldByName('CPID').Value:=Trim(Self.Order_Tree.fieldbyname('CPID').AsString);
        FieldByName('WBID').Value:=Trim(Self.Order_Tree.fieldbyname('CPID').AsString);
        post;
      end;
      next;
    end;
  end;
  initimageSH();
end;

procedure TfrmBPZDY_TJHXK.THBClick(Sender: TObject);
begin
  frmCPType_TJTP:=TfrmCPType_TJTP.Create(self);
  with frmCPType_TJTP do
  begin
    if ShowModal=1 then
    begin
      InitTree();
    end;
    free;
  end;
end;

procedure TfrmBPZDY_TJHXK.FormDestroy(Sender: TObject);
begin
  frmBPZDY_TJHXK:=nil;
end;

procedure TfrmBPZDY_TJHXK.TGBClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  Close;
end;

end.
