unit U_AttachmentUpload;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, Menus, cxLookAndFeelPainters, StdCtrls, cxButtons,
  DB, ADODB, ImgList, shellapi, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGrid, cxLookAndFeels,  cxNavigator,
  dxDateRanges, IdExplicitTLSClientServerBase, System.ImageList, U_BaseHelp,
  Vcl.ToolWin, dxScrollbarAnnotations;

type
  TfrmAttachmentUpload = class(TfrmBaseHelp)
    ListView1: TListView;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel2: TPanel;
    IdFTP1: TIdFTP;
    ADOConnection1: TADOConnection;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    DataSource1: TDataSource;
    v1Column4: TcxGridDBColumn;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    TBDel: TToolButton;
    btnDown: TToolButton;
    ImageList1: TImageList;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Panel2DblClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure btnDownClick(Sender: TObject);
  private
    procedure InitData();
    { Private declarations }
  public
    fkeyNO: string;
    fType: string;
    fId: integer;
    FEditAuthority: Boolean;

    { Public declarations }
  end;

var
  frmAttachmentUpload: TfrmAttachmentUpload;

implementation

uses
  U_DataLink, U_RTFun, U_CompressionFun;
{$R *.dfm}

procedure TfrmAttachmentUpload.InitData();
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 FJ_File ');
      sql.Add('where WBID=' + quotedstr(trim(fkeyNO)));
      sql.Add('and TFType=' + quotedstr(trim(fType)));
      open;
    end;
  except
  end;
end;

procedure TfrmAttachmentUpload.FormDestroy(Sender: TObject);
begin
  frmAttachmentUpload := nil;
end;

procedure TfrmAttachmentUpload.FormCreate(Sender: TObject);
begin
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
end;

procedure TfrmAttachmentUpload.FormShow(Sender: TObject);
begin
  if FEditAuthority then
  begin
    btnDown.Visible := True;
    TBDel.Visible := True;
    TBAdd.Visible := True;
  end
  else
  begin
    btnDown.Visible := false;
    TBDel.Visible := false;
    TBAdd.Visible := false;
  end;

  initdata();
end;

procedure TfrmAttachmentUpload.ListView1DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
begin
  if ListView1.Items.Count < 1 then
    EXIT;

  if listView1.SelCount < 1 then
    exit;
  sFieldName := 'D:\附件查看';

  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, false, true);
    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);

end;

procedure TfrmAttachmentUpload.btnDownClick(Sender: TObject);
var
  SaveDialog: TSaveDialog;
  fFileName: string;
  fFilePath: string;
  ff: TADOBlobStream;
  FJStream: TMemoryStream;
begin
  if adoqueryTmp.IsEmpty then
    exit;

  try

    fFileName := adoqueryTmp.fieldbyname('FileName').AsString;

    SaveDialog := TSaveDialog.Create(Self);

    SaveDialog.FileName := fFileName;
    if SaveDialog.Execute then
    begin
      Panel2.Caption := '正在保存数据，请稍等...';
      Panel2.Visible := true;
      application.ProcessMessages;
      fFilePath := SaveDialog.FileName;

      try
        ff := TADOBlobstream.Create(adoqueryTmp.fieldByName('FilesOther') as TblobField, bmRead);

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

      Panel2.Visible := false;
  //   if IdFTP1.Connected then IdFTP1.Quit;
    end;
  except
    Panel2.Visible := false;
  end;
end;

procedure TfrmAttachmentUpload.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if fId = 10 then
    Action := cafree
  else
    Action := cahide;
end;

procedure TfrmAttachmentUpload.Panel2DblClick(Sender: TObject);
begin
  Panel2.Visible := false;
end;

procedure TfrmAttachmentUpload.TBAddClick(Sender: TObject);
var
  OpenDiaLog: TOpenDialog;
  fFileName: string;
  fFilePath: string;
  maxNo: string;
  FJStream: TMemoryStream;
  mfileSize: integer;
  mCreationTime: TdateTime;
  mWriteTime: TdateTime;
begin

  try
    adoqueryCmd.Connection.BeginTrans;
    OpenDiaLog := TOpenDialog.Create(Self);
    //OpenDiaLog.Filter := 'Bitmap Files (*.bmp)|*.bmp|All Files (*.*)|*.*';
    if OpenDiaLog.Execute then
    begin
      fFilePath := OpenDiaLog.FileName;
      fFileName := ExtractFileName(OpenDiaLog.FileName);
      Panel2.Caption := '正在上传数据，请稍等...';
      Panel2.Visible := true;
      application.ProcessMessages;

      if GetLSNo(ADOQueryCmd, maxNo, 'FJ', 'FJ_File', 4, 1) = False then
      begin
        adoqueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
      //获取文件信息
      GetFileInfo(fFilePath, mfileSize, mCreationTime, mWriteTime);
      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('delete from FJ_File ');
        sql.Add('where TFID=' + quotedstr(trim(maxNo)));
        execsql;
      end;
      try
        FJStream := TMemoryStream.Create;
        with adoqueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from FJ_File ');
          sql.Add('where TFID=' + quotedstr(trim(maxNo)));
          open;
          append;
          fieldbyname('TFID').Value := trim(maxNo);
          fieldbyname('WBID').Value := trim(fkeyNO);
          fieldbyname('TFType').Value := trim(fType);
          fieldbyname('Filler').Value := trim(DName);
          fieldbyname('FileName').Value := trim(fFileName);
          fieldbyname('TFDate').Value := mWriteTime;
          FJStream.LoadFromFile(fFilePath);
          CompressionStream(FJStream);
          tblobfield(FieldByName('Filesother')).LoadFromStream(FJStream);
          post;
        end;
        Panel2.Visible := false;
        initdata();
      finally
        FJStream.Free;
      end;

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

procedure TfrmAttachmentUpload.TBCloseClick(Sender: TObject);
begin
  ADOQueryTmp.Close;
  ADOQuerycmd.Close;
  ListView1.Free;
  ModalResult := 1;
end;

procedure TfrmAttachmentUpload.TBDelClick(Sender: TObject);
var
  fFileName: string;
  fFilePath: string;
begin

  if ADOQueryTmp.IsEmpty then
    exit;
  if trim(ADOQueryTmp.fieldbyname('Filler').AsString) <> trim(DName) then
  begin
    Application.MessageBox('权限不足，上传账户可删除!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete  from FJ_File ');
    sql.Add('where TFID=' + quotedstr(trim(ADOQueryTmp.fieldbyname('TFID').AsString)));
    execsql;
  end;
  initData();

end;

procedure TfrmAttachmentUpload.TBRafreshClick(Sender: TObject);
begin
  initData();
end;

procedure TfrmAttachmentUpload.Tv1DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
  ff: TADOBlobStream;
  FJStream: TMemoryStream;
begin

  if adoqueryTmp.IsEmpty then
    exit;

  sFieldName := 'D:\附件查看';

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

  fileName := adoqueryTmp.fieldbyname('FileName').AsString;

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

  try
    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;

end.

