unit U_BPZdy_ColTJ;

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_ColTJ = class(TForm)
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ThreeImgList: TImageList;
    DataSource2: TDataSource;
    Panel4: TPanel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    ADOQuery1: TADOQuery;
    SaveDialog1: TSaveDialog;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    Panel6: TPanel;
    TJGYName: TBtnEditA;
    Panel10: TPanel;
    Label14: TLabel;
    Label15: TLabel;
    Image2: TImage;
    Image11: TImage;
    TJNote: TMemo;
    HXType: TBtnEditA;
    TJColNo: TEdit;
    TJCol: TBtnEditA;
    TJNo: TEdit;
    TJPTID: TEdit;
    TJCPID: TEdit;
    TJTPID: TEdit;
    Button2: TButton;
    Button3: TButton;
    PopupMenu4: TPopupMenu;
    N4: TMenuItem;
    ADOCmd: TADOQuery;
    Panel16: TPanel;
    adoqueryPicture: TADOQuery;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TGBClick(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure HXTypeBtnClick(Sender: TObject);
    procedure TJGYNameBtnClick(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    lstPat2: TStringList;
    FBPID,FCPID1,FCPID,FCPNo,FCPName:string;
    JS:integer;
    lstPat: TStringList;
		AJpeg: TJPEGImage;
    FangXiang1,FangXiang2:String;
    procedure InitGrid();
    procedure SaveData();
    procedure showimage();
    procedure CreThumb(AJPeg:TJPEGImage;Image1:TImage;Width, Height: Integer);
    procedure ReadINIFile10();
    { Private declarations }
  public
    flag,flagname,snote,MainType,FGYName,Fint,canshu1,CYID,FTJML,FMLXXNO,FIMID,FileName1:string;
    fnote,fnote1,forderno,fZdyFlag,ViewFlag,fHelpType:Boolean;
    PPSTE,my,mz:integer;
    procedure SaveImage1(maxnoWB:string);
    { Public declarations }
  end;
var
  frmBPZDY_ColTJ: TfrmBPZDY_ColTJ;
  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_BPZdy_LRTJ1
  ,U_FileUp,U_FileUp_PB,U_FileUp_TPSH,U_SLT_BF1,U_SCPerson,U_BPZdy_LRTJ;

{$R *.dfm}

procedure TfrmBPZDY_ColTJ.SaveImage1(maxnoWB:string);
var
  ImagePath,ImagName,MaxNo,MaxNoFile:String;
  AJpeg:TJPEGImage;
  myStream:TADOBlobStream;
begin
  try
    ImagePath:=Trim(maxnoWB);
    ImagName:=Trim(maxnoWB);
    AJpeg:=TJpegImage.Create();
    {with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate Image_File Set Valid=''N'' ');
      sql.Add(' where IMID=(select IMID from Image_Info');
      Sql.Add('where WBID='''+Trim(maxnoWB)+''' and Valid=''Y'' )');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate Image_Info Set Editer='''+Trim(DName)+''',EditerCode='''+Trim(DCode)+''',EditTime=getdate()');
      sql.Add(',Valid=''N''  where WBID='''+Trim(maxnoWB)+''' and Valid=''Y''  ');
      ExecSQL;
    end;}
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from Image_Info ');
      sql.Add('where WBID='''+trim(maxnoWB)+''' and Valid=''Y''');
      Open;
    end;
    if ADOCmd.IsEmpty then
    begin
      if GetLSNo(ADOQueryTemp,MaxNo,'IM','Image_Info',4,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取图片信息最大号失败！','提示',0);
        Exit;
      end;
    end
    else
    begin
      MaxNo:=Trim(ADOCmd.fieldbyname('IMID').AsString);
    end;
    with ADOCmd do
    begin
      if IsEmpty then
      begin
        Append;
        FieldByName('IMID').Value:=Trim(MaxNo);
        FieldByName('WBID').Value:=Trim(maxnoWB);
        FieldByName('TJML').Value:=Trim(FTJML);
        FieldByName('ImageName').Value:=Trim(ImagName);
        FieldByName('ImagePath').Value:=Trim(MaxNo)+Trim(ImagName);
        FieldByName('ImageDate').Value:=SGetServerDate(ADOQueryTemp);
        Fieldbyname('ImageType').value:=Trim('TJ');
        Fieldbyname('Valid').value:=Trim('Y');
      end
      else
        edit;
      FieldByName('CPID').Value:=Trim(TJCPID.Text);
      FieldByName('TJHX').Value:=Trim(TJNo.Text);
      FieldByName('TJCol').Value:=Trim(TJCol.Text);
      FieldByName('TJColNo').Value:=Trim(TJColNo.Text);
      FieldByName('TJHXType').Value:=Trim(HXType.Text);
      FieldByName('TJMLNo').Value:=Trim(FMLXXNO);
      FieldByName('TJGYName').Value:=Trim(TJGYName.Text);
      FieldByName('TJPTID').Value:=Trim(TJPTID.Text);
      FieldByName('TJCPID').Value:=Trim(TJCPID.Text);
      FieldByName('TJTPID').Value:=Trim(TJTPID.Text);
      FieldByName('TJNote').Value:=Trim(TJNote.Text);
      Post;
    end;
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select *  from Image_File ');
      sql.Add('where IMID='''+trim(MaxNo)+''' and Valid=''Y''');
      open;
    end;
    if ADOCmd.IsEmpty then
    begin
      if GetLSNo(ADOQuery1,MaxNoFile,'IF','Image_File',4,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取图片文件最大号失败！','提示',0);
        Exit;
      end;
    end
    else
    begin
      MaxNoFile:=Trim(ADOCmd.fieldbyname('IFID').AsString);
    end;
    with ADOCmd do
    begin
      if IsEmpty then
      begin
        Append;
        FieldByName('IMID').Value:=Trim(MaxNo);
        FieldByName('IFID').Value:=Trim(MaxNoFile);
        Fieldbyname('Valid').value:=Trim('Y');
        FieldByName('ImageWidth').Value:=Image11.Width;
        FieldByName('ImageHeight').Value:=Image11.Height;
        FieldByName('FangXiang').Value:=FangXiang1;
        FieldByName('IFType').Value:='小';
      end
      else
        edit;
      FieldByName('CPID').Value:=Trim(TJCPID.Text);
      myStream := TADOBlobStream.Create(TBlobField(ADOCmd.FieldByName('ImageFile')), bmWrite);
      AJpeg.Assign(Image11.Picture.Graphic);
      AJpeg.SaveToStream(myStream);
      myStream.Free;
      Post;
    end;
    if lstPat2.Count=1 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;
      if IdFTP1.Connected then
      begin
        IdFTP1.Put(lstPat2[0], 'TJ'+'\'+Trim(MaxNo)+Trim(ImagName));
      end;
      if IdFTP1.Connected then IdFTP1.Quit;
    end;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('图片保存失败！','提示',0);
  end;
end;

procedure TfrmBPZDY_ColTJ.showimage();
var
  jpg:TJpegImage;
  myStream: TADOBlobStream;
begin
  with adoqueryPicture do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Image_Info A');
    sql.Add(' inner join Image_File B on A.IMID=B.IMID');
    sql.Add(' and A.WBID='''+Trim(TJTPID.Text)+'''');
    sql.Add(' and A.Valid=''Y'' ');
    //ShowMessage(sql.Text);
    Open;
  end;
  if adoqueryPicture.IsEmpty=False then
  begin
    FileName1:=Trim(adoqueryPicture.fieldbyname('ImagePath').AsString);
    jpg:=TJpegImage.Create();
    myStream:=tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('ImageFile')),bmread);
    jpg.LoadFromStream(myStream);
    Image11.Picture.Assign(jpg);
    Image11.Width:=adoqueryPicture.fieldbyname('ImageWidth').Value;
    Image11.Height:=adoqueryPicture.fieldbyname('ImageHeight').Value;
    if Trim(adoqueryPicture.fieldbyname('FangXiang').AsString)='垂直' then
    begin
      Image11.Top:=1;
      Image11.Left:=Round((Panel6.Width-Image11.Width)/2);
    end else
    begin
      Image11.Left:=1;
      Image11.Top:=Round((Panel6.Height-Image11.Height)/2);
    end;
    Image11.Visible:=True;
  end;
end;

procedure TfrmBPZDY_ColTJ.SaveData();
begin

end;

procedure TfrmBPZDY_ColTJ.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  lstPat2 := TStringList.Create;
end;

procedure TfrmBPZDY_ColTJ.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmBPZDY_ColTJ.InitGrid();
begin
  with adoqueryPicture do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from Image_Info ');
    sql.Add('where IMID='''+trim(FIMID)+''' and Valid=''Y''');
    Open;
  end;
  with adoqueryPicture do
  begin
    if adoqueryPicture.IsEmpty=False then
    begin
      FTJML:=Trim(FieldByName('TJML').asstring);
      TJCPID.Text:=Trim(FieldByName('CPID').asstring);
      TJNo.Text:=Trim(FieldByName('TJHX').asstring);
      TJCol.Text:=Trim(FieldByName('TJCol').asstring);
      TJColNo.Text:=Trim(FieldByName('TJColNo').asstring);
      HXType.Text:=Trim(FieldByName('TJHXType').asstring);
      FMLXXNO:=Trim(FieldByName('TJMLNo').asstring);
      TJGYName.Text:=Trim(FieldByName('TJGYName').asstring);
      TJPTID.Text:=Trim(FieldByName('TJPTID').asstring);
      TJCPID.Text:=Trim(FieldByName('TJCPID').asstring);
      TJTPID.Text:=Trim(FieldByName('TJTPID').asstring);
      TJNote.Text:=Trim(FieldByName('TJNote').asstring);
      showimage();
    end;
  end;
end;

procedure TfrmBPZDY_ColTJ.FormShow(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmBPZDY_ColTJ.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
    AWidth :=Width;
    AWidthOffset := 0;
    AHeight := Height;
    AHeightOffset := 0;

    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;
  {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 := 450;
    Bitmap.Height := 619;
    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_ColTJ.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_ColTJ.TGBClick(Sender: TObject);
begin
  Close;
end;

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

procedure TfrmBPZDY_ColTJ.HXTypeBtnClick(Sender: TObject);
begin
  frmSCPerson:=TfrmSCPerson.Create(Application);
  with frmSCPerson do
  begin
    FlagStr:='HXZDYType';
    if ShowModal=1 then
    begin
      HXType.Text:=Trim(FSDPerson);
      TJCPID.Text:='CP1910290001';
    end;
    free;
  end;
end;

procedure TfrmBPZDY_ColTJ.TJGYNameBtnClick(Sender: TObject);
var FGYName:string;
begin
  frmBPZDY_LRTJ1:=TfrmBPZDY_LRTJ1.Create(Application);
  with frmBPZDY_LRTJ1 do
  begin
    flag:='TJName';
    flagName:='烫金';
    cxGrid3.Visible:=true;
    ViewFlag:=True;
    Panel4.Visible:=true;
    if ShowModal=1 then
    begin
      with ML_GX do
      begin
        first;
        while not eof do
        begin
          if FGYName='' then
          begin
            FGYName:=Trim(ML_GX.fieldbyname('GYName').AsString);
          end
          else
          begin
            FGYName:=FGYName+'+'+Trim(ML_GX.fieldbyname('GYName').AsString)
          end;
          next;
        end;
      end;
      TJGYName.Text:=FGYName;
    end;
    free;
  end;
end;

procedure TfrmBPZDY_ColTJ.N4Click(Sender: TObject);
var
  AJpeg:TJPEGImage;
  Bitmap,Bitmap11: TBitmap;
  ARect,ARect11: TRect;
  TPHeight, AHeightOffset: Integer;
  TPWidth, AWidthOffset: Integer;
  WZShuiPing,WZChuiZhi:Integer;
  WZFlag:String;
  FHFStr:String;
begin
  lstPat2.Clear;
  if ODPat.Execute then
  begin
    lstPat2.AddStrings(ODPat.Files);
  end else
  begin
    Exit;
  end;
  if lstPat2.Count>1 then
  begin
    lstPat2.Clear;
    Application.MessageBox('不能上传多个图片!','提示',0);
    Exit;
  end else
  if lstPat2.Count<1 then
  begin
    Exit;
  end;
  AJpeg:=TJpegImage.Create();
  AJpeg.LoadFromFile(ExtractFileName(ODPat.FileName));
  Image2.Hint:=ExtractFileName(ODPat.FileName);
  TJTPID.Text:=FormatDateTime('yyMMddhhmmss',now);
  FileName1:=Trim(ExtractFileName(ODPat.FileName));
  TPWidth:=AJpeg.Width;
  TPHeight:=AJpeg.Height;
  Bitmap := TBitmap.Create;
  Bitmap11 := TBitmap.Create;
  Bitmap.Width :=Panel10.Width-2;
  Bitmap.Height :=Round(Panel10.Width/TPWidth*TPHeight);
  if Bitmap.Height>Panel10.Height then
  begin
    WZFlag:='垂直';
    Bitmap.Height:=Panel10.Height-2;
    Bitmap.Width :=Round(Panel10.Height/TPHeight*TPWidth);
  end;
  Bitmap11.Width:=Bitmap.Width*2;
  Bitmap11.Height:=Bitmap.Height*2;
  Bitmap.Canvas.FillRect(Rect(0, 0, Bitmap.Width, Bitmap.Height));
  ARect := Rect(0, 0, Bitmap.Width, Bitmap.Height);
  Bitmap.Canvas.StretchDraw(ARect, AJPeg);
  Image2.Height:=Bitmap.Height;
  Image2.Width:=Bitmap.Width;
  Image2.Picture.Assign(BitMap);
  if Trim(WZFlag)='垂直' then
  begin
    Image2.Top:=1;
    Image2.Left:=Round((Panel10.Width-Image2.Width)/2);
  end else
  begin
    Image2.Left:=1;
    Image2.Top:=Round((Panel10.Height-Image2.Height)/2);
  end;
  FangXiang1:=WZFlag;
  Image2.Visible:=True;
  Bitmap11.Canvas.FillRect(Rect(0, 0, Bitmap11.Width, Bitmap11.Height));
  ARect11 := Rect(0, 0, Bitmap11.Width, Bitmap11.Height);
  Bitmap11.Canvas.StretchDraw(ARect11, AJPeg);
  Image11.Height:=Bitmap11.Height;
  Image11.Width:=Bitmap11.Width;
  Image11.Picture.Assign(BitMap11);
  TJPTID.Text:='';
  Panel10.Color:=clBtnFace;
end;

procedure TfrmBPZDY_ColTJ.Button2Click(Sender: TObject);
begin
  if TJTPID.Text<>'' then
  begin
    if lstPat2.Count=1 then
    begin
      SaveImage1(Trim(TJTPID.Text));
      with adoqueryPicture do
      begin
        close;
        sql.Clear;
        sql.Add('update Image_Info set TJHXType='''+trim(HXType.Text)+'''');
        sql.add(',TJGYName='''+trim(TJGYName.Text)+'''');
        sql.Add(' where TJML='''+Trim(FTJML)+'''');
        ExecSQL;
      end;
    end;
  end;
  ModalResult:=1;
end;

procedure TfrmBPZDY_ColTJ.Button3Click(Sender: TObject);
begin
  close;
end;

end.
