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,IniFiles,
  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,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP;

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;
    TGB: TToolButton;
    ADOQuery1: TADOQuery;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    ScrollBox3: TScrollBox;
    LBCPAP: TLabel;
    Label553: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    Edit1: TEdit;
    Image2: TImage;
    Panel16: TPanel;
    SaveDialog1: TSaveDialog;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TPBLBClick(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
    procedure TSHClick(Sender: TObject);
    procedure THBClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TGBClick(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
  private
    FBPID,FCPID1,FCPID,FCPNo,FCPName:string;
    JS:integer;
    lstPat: TStringList;
		AJpeg: TJPEGImage;
    procedure InitGrid();
    procedure SaveData();
    procedure initTree();
    procedure CreThumb(AJPeg:TJPEGImage;Image1:TImage;Width, Height: Integer);
    procedure ReadINIFile10();
    { Private declarations }
  public
    flag,flagname,snote,MainType,FGYName,Fint,canshu1,CYID:string;
    fnote,fnote1,forderno,fZdyFlag,ViewFlag,fHelpType:Boolean;
    PPSTE,my,mz: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(',JS=(select Max(A.CPlevel) from CP_TypeTJ A)');
    sql.Add(', Case when isnull(CPNo,'''')<>'''' and CPlevel=''2'' 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);
  JS:=Order_Tree.fieldbyname('JS').AsInteger;
  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;
  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 like '''+'%'+trim(FCPID1)+'%'+'''');
      sql.Add(' order by cast(C.ColorNo as int)');
      //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
        if (i<30*my) and (i>=30*(my-1)) then
        begin
          jpg:=TJpegImage.Create();
          myStream:=tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')),bmread);
          jpg.LoadFromStream(myStream);
          if (i-30*(my-1))<10*(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].SH.Caption:=trim(adoqueryPicture.fieldbyname('CPType').AsString);
          Mach1[i].ZW.Caption:=trim(adoqueryPicture.fieldbyname('SHID').AsString);
          Mach1[i].Parent:=ScrollBox2;
          Mach1[I].Left:=0+(x-10*p)*136;
          Mach1[I].Top:=p*176;
          if adoqueryPicture.fieldbyname('TFID').AsString='' then
          begin
            Mach1[i].XFID.Text:=trim(adoqueryPicture.fieldbyname('XFID').AsString);
          end
          else
          begin
            SCSHDataNew(adoqueryPicture,Mach1[I].Panel1,2);
          end;
          if triM(adoqueryPicture.fieldbyname('TFID').AsString)<>'' then
          begin
            Mach1[i].Init(adoqueryPicture.fieldbyname('TFID').AsString,adoqueryPicture.fieldbyname('FileName').AsString,jpg);
          end;
          x:=x+1;
        end;
        i:=i+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;
  lstPat := TStringList.Create;
  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();
  ToolBar1.Visible:=false;
  TSH.Visible:=false;
  if canshu1<>'查询' then
  begin
    ToolBar1.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.CreThumb(AJPeg:TJPEGImage;Image1:TImage;Width, Height: Integer);
var
  Bitmap: TBitmap;
  Ratio: Double;
  ARect: TRect;
  AHeight, AHeightOffset: Integer;
  AWidth, AWidthOffset: Integer;
begin
  Bitmap := TBitmap.Create;
  try
    Ratio := AJPeg.Width /AJPeg.Height;
    if Ratio > 1.333 then
    begin
      AHeight := Round(Width / Ratio);
      AHeightOffset := (Height - AHeight) div 2;
      AWidth := Width;
      AWidthOffset := 0;
    end
    else
    begin
      AWidth := Round(Height * Ratio);
      AWidthOffset := (Width - AWidth) div 2;
      AHeight := Height;
      AHeightOffset := 0;
    end;
    Bitmap.Width := Width;
    Bitmap.Height := Height;
    Bitmap.Canvas.Brush.Color := clBtnFace;
    Bitmap.Canvas.FillRect(Rect(0, 0, Width, Height));
    ARect := Rect(AWidthOffset, AHeightOffset, AWidth + AWidthOffset, AHeight + AHeightOffset);
    Bitmap.Canvas.StretchDraw(ARect, AJPeg);
    Image1.Picture.Assign(BitMap);
  finally
    Bitmap.Free;
  end;
end;

procedure TfrmBPZDY_TJHXK.ReadINIFile10();
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 TfrmBPZDY_TJHXK.cxDBTreeList1Click(Sender: TObject);
var FLevel:integer;

begin
  FCPID1:=trim(Order_Tree.fieldbyname('CPID').AsString);
  for FLevel:=Order_Tree.fieldbyname('CPLevel').AsInteger to JS do
  begin
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CP_TypeTJ');
      sql.Add(' where CPParent like '''+'%'+trim(FCPID1)+'%'+'''');
      sql.Add(' and CPLevel='''+inttostr(FLevel+1)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      first;
      while not eof do
      begin
        FCPID1:=FCPID1+trim(ADOQueryCmd.fieldbyname('CPID').AsString);
        next;
      end;
    end;
  end;
  my:=1;
  if Order_Tree.fieldbyname('CPlevel').AsInteger<>3 then
  begin
    TSH.Visible:=false;
  end
  else
  TSH.Visible:=True;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select Count(C.XFID) GS 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 like '''+'%'+trim(FCPID1)+'%'+'''');
    //ShowMessage(sql.Text);
    Open;
  end;
  //ShowMessage(Order_Tree.fieldbyname('CPID').AsString);
  mz:=Ceil(ADOQueryCmd.fieldbyname('GS').AsInteger/30);
  LBCPAP.Caption:=IntToStr(my)+'/'+IntToStr(mz);
  initimageSH();
end;

procedure TfrmBPZDY_TJHXK.TSHClick(Sender: TObject);
var
  i,j: Integer;
  PatFile,FSHID,FCPNo: String;
  FTPPath,FConNo,MaxNo:string;
  AJpeg: TJPEGImage;
  myStream: TADOBlobStream;
begin
  lstPat.Clear;
  if ODPat.Execute then
  begin
    lstPat.AddStrings(ODPat.Files);
  end;
  if lstPat.Count > 0 then
  begin
    try
      ReadINIFile10();
      server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
      IdFTP1.Host :=server;//PicSvr;
      IdFTP1.Username := 'three';
      IdFTP1.Password := '641010';
      IdFTP1.Connect();
    except
      IdFTP1.Quit;
      Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
      Exit;
    end;
  end;
  Panel16.Visible:=True;
  Panel16.Refresh;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select Count(*) MM from XD_File where CYNO='''+Trim(Self.Order_Tree.fieldbyname('CPID').AsString)+'''');
    SQL.Add('and filetype=''YP''');
    Open;
    j:=fieldbyname('MM').AsInteger;
  end;
  try
    AJpeg:=TJpegImage.Create();
    ADOQueryCmd.Connection.BeginTrans;
    for i := 0 to lstPat.Count - 1 do
    begin
      PatFile := ExtractFileName(lstPat[i]);
      AJpeg.LoadFromFile(ExtractFileName(lstPat[i]));
      CreThumb(AJpeg,Image2,170, 130);
      if IdFTP1.Connected then
      begin
        IdFTP1.Put(lstPat[i], 'YP'+'\'+Trim(PatFile));
        if GetLSNo(ADOQueryCmd,CYID,'H','XD_File',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取图片最大号失败！','提示',0);
          Exit;
        end;
        with ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          SQL.Add('select * from XD_File where CYID='''+Trim(CYID)+'''');
          SQL.Add(' and filename='''+Trim(PatFile)+'''');
          SQL.Add(' and filetype=''YP''');
          Open;
          if not IsEmpty then
          begin
            Panel16.Visible:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox(PChar('文件<'+Trim(PatFile)+'>重复,'+inttostr(i)+'个文件上传成功！'),'提示',0);
            Exit;
          end;
        end;
        if GetLSNo(ADOQueryCmd,MaxNo,'SS','XD_File',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取图片最大号失败！','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add('select * from XD_File where 1=2');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('XFID').Value:=Trim(MaxNo);
          FieldByName('CYID').Value:=Trim(CYID);
          FieldByName('CYNO').Value:=Trim(Self.Order_Tree.fieldbyname('CPID').AsString);
          FieldByName('SKID').Value:=Trim(Self.Order_Tree.fieldbyname('CPID').AsString);
          FieldByName('filename').Value:=Trim(PatFile);
          FieldByName('FileDate').Value:=SGetServerDate(ADOQueryTemp);
          fieldbyname('FileType').value:=Trim('YP');
          Post;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add(' select *  from TP_File where TFID='''+Trim(MaxNo)+'''');
          //ShowMessage(sql.Text);
          open;
        end;
        with ADOQueryCmd do
        begin
          if ADOQueryCmd.IsEmpty then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('TFID').Value:=Trim(MaxNo);
          FieldByName('WBID').Value:=Trim(CYID);
          FieldByName('TFType').Value:='样品';
          myStream := TADOBlobStream.Create(TBlobField(ADOQueryCmd.FieldByName('FilesOther')), bmWrite);
          AJpeg.Assign(Image2.Picture.Graphic);
          AJpeg.SaveToStream(myStream);
          myStream.Free;
          Post;
        end;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('图片上传失败！','提示',0);
  end;
  if IdFTP1.Connected then IdFTP1.Quit;
  Panel16.Visible:=False;
  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;
  FCPNo:=Self.Order_Tree.fieldbyname('CPNo').AsString;
  with self.ADOQueryCmd do
  begin
    First;
    while not eof do
    begin
      if GetLSNo(ADOQuery1,FSHID,FCPNo,'TJ_Base',4,0)=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('CPType').Value:=Trim(Self.Order_Tree.fieldbyname('CPName').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

    end;
    free;
  end;
  InitTree();
end;

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

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

procedure TfrmBPZDY_TJHXK.cxDBTreeList1DblClick(Sender: TObject);
begin
  ModalResult:=1;
end;

procedure TfrmBPZDY_TJHXK.BTLPClick(Sender: TObject);
begin
  if my<=1 then exit;
  my:=my-1;
  LBCPAP.Caption:=IntToStr(my)+'/'+inttostr(mz);
  initimageSH();
end;

procedure TfrmBPZDY_TJHXK.BTNPClick(Sender: TObject);
begin
  if my>=mz then exit;
  my:=my+1;
  LBCPAP.Caption:=IntToStr(my)+'/'+inttostr(mz);
  initimageSH();
end;

end.
