unit U_JCDJEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, DB, ADODB, cxControls, cxContainer, cxEdit,
  cxTextEdit, cxCurrencyEdit, BtnEdit, StdCtrls, ExtCtrls, cxStyles,
  cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxDBData,
  cxButtonEdit, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, DBClient, cxDropDownEdit, Menus, cxLookAndFeelPainters,
  strUtils, cxButtons, cxImage, cxDBEdit, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, ShellAPI, IdFTP, ImgList;

type
  TfrmJCDJEdit = class(TForm)
    ToolBar1: TToolBar;
    TSAVE: TToolButton;
    Tclose: TToolButton;
    ADOQueryTMP: TADOQuery;
    ADOQueryCMD: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_HZ: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DS_HZ: TDataSource;
    ADOQueryImage: TADOQuery;
    DataSource1: TDataSource;
    Panel1: TPanel;
    Label11: TLabel;
    Label5: TLabel;
    Label2: TLabel;
    Label10: TLabel;
    Label13: TLabel;
    Note: TMemo;
    CRTime: TDateTimePicker;
    C_CodeName: TEdit;
    KHNAME: TBtnEditA;
    OrderNo: TBtnEditA;
    FactoryName: TEdit;
    Label6: TLabel;
    C_CODE: TEdit;
    Label17: TLabel;
    Label1: TLabel;
    JCID: TEdit;
    ListView1: TListView;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ImageList1: TImageList;
    IdFTP1: TIdFTP;
    Panel2: TPanel;
    procedure TcloseClick(Sender: TObject);
    procedure TSAVEClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure QtyKeyPress(Sender: TObject; var Key: Char);

    procedure NoteDblClick(Sender: TObject);
    procedure KHNAMEBtnClick(Sender: TObject);
    procedure OrderNoBtnClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    procedure savedata();
    procedure initdata();
    procedure initfj();
    { Private declarations }
  public
    fkeyNo: string;
    fflag: integer;
    fSYRName: string;
    canshu1: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmJCDJEdit: TfrmJCDJEdit;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachGYS,
  U_ProductOrderListSel, U_CompressionFun;

{$R *.dfm}
procedure TfrmJCDJEdit.initfj();
var
  ListItem: TListItem;
  Flag: Cardinal;
  info: SHFILEINFOA;
  Icon: TIcon;
begin
  ListView1.Items.Clear;
  try

    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from TP_File ');
      sql.Add('where WBID=' + quotedstr(trim(JCID.Text)));
      sql.Add('and TFType=' + quotedstr(trim('检测报告')));
      open;
      if not IsEmpty then
      begin
        while not eof do
        begin
          with ListView1 do
          begin
            LargeImages := ImageList1;
            Icon := TIcon.Create;
            ListItem := Items.Add;
            ListItem.Caption := trim(fieldbyname('fileName').AsString);
       //    Listitem.SubItems.Add(OpenDiaLog.FileName);
            Flag := (SHGFI_SMALLICON or SHGFI_ICON or SHGFI_USEFILEATTRIBUTES);
            SHGetFileInfo(Pchar(trim(fieldbyname('fileName').AsString)), 0, info, Sizeof(info), Flag);
            Icon.Handle := info.hIcon;
            ImageList1.AddIcon(Icon);
            ListItem.ImageIndex := ImageList1.Count - 1;
          end;
          next;
        end;
      end;
    end;
  except
  end;
end;

procedure TfrmJCDJEdit.initdata();
begin
  with ADOQueryMain do
  begin
    close;
    sql.clear;
    sql.Add(' select * from JC_LIST ');
    sql.Add(' where JCID=' + quotedstr(trim(fkeyNo)));
    open;
    if not IsEmpty then
    begin
      SCSHDataNew(ADOQueryMain, Panel1, 0);
    end;
  end;
end;

procedure TfrmJCDJEdit.savedata();
begin
  try
    ADOQueryCMD.Connection.BeginTrans;

    with ADOQueryCMD do
    begin
      Close;
      sql.Clear;
      SQL.Add(' select * from JC_LIST where JCID=''' + Trim(fkeyNo) + ''' ');
      Open;
    end;
    if ADOQueryCMD.IsEmpty then
    begin
      ADOQueryCMD.Append;
      ADOQueryCMD.FieldByName('Filler').Value := Trim(Dname);

    end
    else
    begin
      ADOQueryCMD.Edit;
      ADOQueryCMD.FieldByName('Editer').Value := Trim(Dname);
      ADOQueryCMD.FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTMP);
    end;
    SSetsaveSqlNew(ADOQueryCMD, 'JC_LIST', Panel1, 0);

    ADOQueryCMD.Post;

    ADOquerycmd.Connection.CommitTrans;
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  except
    ADOquerycmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmJCDJEdit.TcloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmJCDJEdit.TSAVEClick(Sender: TObject);
begin
  savedata();
end;

procedure TfrmJCDJEdit.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cahide;
end;

procedure TfrmJCDJEdit.FormDestroy(Sender: TObject);
begin
  frmJCDJEdit := nil;
end;

procedure TfrmJCDJEdit.FormShow(Sender: TObject);
begin
  SClearData(Panel1, 0);
  CRTime.DateTime := SGetServerDate10(adoqueryTmp);
  if Trim(fkeyNo) = '' then
  begin
    if GetLSNo(ADOQueryCMD, fkeyNo, 'JC', 'JC_LIST', 3, 1) = False then
    begin
      ADOQueryCMD.Connection.RollbackTrans;
      Application.MessageBox('取流水号失败!', '提示');
      Exit;
    end;

  end;
  JCID.Text := fkeyNo;
  initData();
  initfj();
end;

procedure TfrmJCDJEdit.QtyKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', '.', '-', #8]) then
    Key := #0;
  if (Key = '.') and (Pos('.', Tedit(Sender).Text) > 0) then
    Key := #0;
end;

procedure TfrmJCDJEdit.NoteDblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      Flag := 'KSNote';
      flagname := '索赔事件及诉求';
      if ShowModal = 1 then
      begin
        Note.Text := Trim(ReturnStr);
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmJCDJEdit.KHNAMEBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        KHNAME.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmJCDJEdit.OrderNoBtnClick(Sender: TObject);
var
  MMaxno: string;
begin
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        with frmProductOrderListSel.Order_Main do
        begin
          C_Code.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
          C_CodeName.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
          SELF.OrderNo.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('orderno').AsString);

          KHNAME.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('CustomerNoName').AsString);
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmJCDJEdit.ToolButton1Click(Sender: TObject);
var
  OpenDiaLog: TOpenDialog;
  fFileName: string;
  fFilePath: string;
  maxNo: string;
//	myStream: TADOBlobStream;
//  FJStream : TMemoryStream;
  FJStream: TMemoryStream;
  mfileSize: integer;
  mCreationTime: TdateTime;
  mWriteTime: TdateTime;
begin

  try
    OpenDiaLog := TOpenDialog.Create(Self);
    if OpenDiaLog.Execute then
    begin
      fFilePath := OpenDiaLog.FileName;
      fFileName := ExtractFileName(OpenDiaLog.FileName);

      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select TFId from TP_File ');
        sql.Add('where WBID<>' + quotedstr(trim(JCID.Text)));
        sql.Add('and TFType=' + quotedstr(trim('检测报告')));
        sql.Add('and FileName=' + quotedstr(trim(fFileName)));
        open;
        if not adoqueryCmd.IsEmpty then
        begin
          application.MessageBox('此附件名称已存在，请修改文件名，继续上传！', '提示信息', MB_ICONERROR);
          exit;
        end;
      end;

      Panel2.Caption := '正在上传数据，请稍等...';
      Panel2.Visible := true;
      application.ProcessMessages;

      if GetLSNo(ADOQueryCmd, maxNo, 'FJ', 'TP_File', 4, 1) = False then
      begin
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

             //获取文件信息
      GetFileInfo(fFilePath, mfileSize, mCreationTime, mWriteTime);

      adoqueryCmd.Connection.BeginTrans;

      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('delete from TP_File ');
        sql.Add('where WBID=' + quotedstr(trim(JCID.Text)));
        sql.Add('and TFType=' + quotedstr(trim('检测报告')));
        sql.Add('and FileName=' + quotedstr(trim(fFileName)));
        execsql;
      end;

      try
        FJStream := TMemoryStream.Create;
        with adoqueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from TP_File ');
          sql.Add('where WBID=' + quotedstr(trim(JCID.Text)));
          sql.Add('and TFType=' + quotedstr(trim('检测报告')));
          sql.Add('and FileName=' + quotedstr(trim(fFileName)));
          open;
          append;
          fieldbyname('TFID').Value := trim(maxNo);
          fieldbyname('WBID').Value := trim(JCID.Text);
          fieldbyname('TFType').Value := '检测报告';
          fieldbyname('FileName').Value := trim(fFileName);
          FJStream.LoadFromFile(fFilePath);
          CompressionStream(FJStream);
          tblobfield(FieldByName('Filesother')).LoadFromStream(FJStream);
    //      tblobfield(FieldByName('Filesother')).LoadFromFile(fFilePath);
          post;
        end;

     {   if fFilePath <> '' then
        begin
          try
					  IdFTP1.Host := PicSvr;
					  IdFTP1.Username := 'three';
					  IdFTP1.Password := '641010';
					  IdFTP1.Connect();
            IdFTP1.Put(fFilePath, 'FJ\' + Trim(fFileName));
					  IdFTP1.Quit;
          except
					  IdFTP1.Quit;
					  Application.MessageBox('上传客户图样文件失败，请检查文件服务器！', '提示', MB_ICONWARNING);
          end;
        end;
        IdFTP1.Quit;  }

        Panel2.Visible := false;
        initfj();
      finally
       // FJStream.Free;
      end;
      adoqueryCmd.Connection.CommitTrans;
    end;

  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('附件保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmJCDJEdit.ListView1DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
  ff: TADOBlobStream;
  FJStream: TMemoryStream;
begin
  if ListView1.Items.Count < 1 then
    EXIT;

  if listView1.SelCount < 1 then
    exit;
  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);

  fileName := ListView1.Selected.Caption;

  sFieldName := sFieldName + '\' + trim(fileName);

{  try
    IdFTP1.Host := PicSvr;
    IdFTP1.Username := 'three';
		IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin

      Panel2.Caption:='正在下载数据，请稍等...';
      Panel2.Visible:=true;
      application.ProcessMessages;
    try
      IdFTP1.Get('FJ\'+ Trim(fileName), sFieldName,true, false);
    except
      Panel2.Visible:=false;
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Panel2.Visible:=false;
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  Panel2.Visible:=false;
   if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(sFieldName),'', '', SW_SHOWNORMAL);
   }

  try
    adoqueryTmp.Locate('FileName', fileName, []);
    ff := TADOBlobstream.Create(adoqueryTmp.fieldByName('FilesOther') as TblobField, bmRead);

    FJStream := TMemoryStream.Create;
    ff.SaveToStream(FJStream);
    UnCompressionStream(FJStream);
    FJStream.SaveToFile(sFieldName);
    ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
  finally
    FJStream.free;
    ff.Free;
  end;
end;

procedure TfrmJCDJEdit.ToolButton2Click(Sender: TObject);
var
  fFileName: string;
  fFilePath: string;
begin
  if listView1.SelCount < 1 then
    exit;

  try
    fFileName := ListView1.Selected.Caption;
   // ADOQueryTmp.Locate('fileName',fFileName,[]);

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete  from TP_File ');
      sql.Add('where WBID=' + quotedstr(trim(JCID.Text)));
      sql.Add('and TFType=' + quotedstr(trim('检测报告')));
      sql.Add('and FileName=' + quotedstr(trim(fFileName)));
      execsql;
    end;

    initfj();

  except
  end;
end;

end.

