unit U_FjList;

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,strutils;

type
  TfrmFjList = class(TForm)
    ListView1: TListView;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ImageList1: TImageList;
    Panel2: TPanel;
    IdFTP1: TIdFTP;
    ADOConnection1: TADOConnection;
    Panel3: TPanel;
    Label10: TLabel;
    Button1: TButton;
    Panel10: TPanel;
    Image2: TImage;
    WJName: TEdit;
    Button2: TButton;
    WJPach: TEdit;
    Button3: TButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    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 Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    procedure InitData();
    { Private declarations }
  public
    fkeyNO:string;
    fType:string;
    fId:integer;
    fstatus:integer;

    { Public declarations }
  end;

var
  frmFjList: TfrmFjList;

implementation
 uses
  U_DataLink,U_RTFun;
{$R *.dfm}
procedure TfrmFjList.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 fileName from FJ_File ');
    sql.Add('where WBID='+quotedstr(trim(fkeyNO)));
    sql.Add('and TFType='+quotedstr(trim(fType)));
    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);
           Flag := (SHGFI_LARGEICON 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 TfrmFjList.FormDestroy(Sender: TObject);
begin
  frmFjList:=nil;
end;

procedure TfrmFjList.FormCreate(Sender: TObject);
begin
    with ADOConnection1 do
    begin
      Connected:=false;
      ConnectionString:=DConString;
      //ConnectionString:='';
      Connected:=true;
    end;
  ListView1.Align:=alclient;
end;

procedure TfrmFjList.FormShow(Sender: TObject);
begin
  IF fstatus<>0 then
  begin
    N1.Visible:=False;
    N2.Visible:=False;
    N3.Visible:=False;
  end;
  initdata();
end;

procedure TfrmFjList.ListView1DblClick(Sender: TObject);
var
  sFieldName:string;
  fileName:string;
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 := ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    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);

end;

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

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

procedure TfrmFjList.Panel10MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel3).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmFjList.Image2Click(Sender: TObject);
begin
  Panel3.Visible:=False;
end;

procedure TfrmFjList.Button1Click(Sender: TObject);
var
  fFileName:string;
  fFilePath:string;
  maxNo:string;
begin
  try
    adoqueryCmd.Connection.BeginTrans;
    begin
      fFilePath:=WJPach.Text;
      fFileName:=WJName.Text;

      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select TFId from FJ_File ');
        sql.Add('where WBID='+quotedstr(trim(fkeyNO)));
        sql.Add('and TFType='+quotedstr(trim(fType)));
        sql.Add('and FileName='+quotedstr(trim(fFileName)));
        open;

      end;
      IF ADOQueryCmd.IsEmpty=False  then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('此附件名称已存在，请修改文件名，继续上传！','提示信息',MB_ICONERROR);
        exit;
      end;
      Panel3.Visible:=False;
      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;

      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('delete from FJ_File ');
        sql.Add('where WBID='+quotedstr(trim(fkeyNO)));
        sql.Add('and TFType='+quotedstr(trim(fType)));
        sql.Add('and FileName='+quotedstr(trim(fFileName)));
        execsql;
      end;
      with adoqueryCmd 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)));
        sql.Add('and FileName='+quotedstr(trim(fFileName)));
        open;
        append;
        fieldbyname('TFID').Value:=trim(maxNO);
        fieldbyname('WBID').Value:=trim(fkeyNO);
        fieldbyname('TFType').Value:=trim(fType);
        fieldbyname('FileName').Value:=trim(fFileName);
        post;
      end;
      if fFilePath <> '' then
      begin
        try
          IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
          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;
      initdata();
    end;
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！','提示信息',0);
  end;
end;

procedure TfrmFjList.Button2Click(Sender: TObject);
var
  fFileName:string;
  fFilePath:string;
  maxNo:string;
begin
  try
    adoqueryCmd.Connection.BeginTrans;
    fFilePath:=WJPach.Text;
    fFileName:=WJName.Hint;
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select TFId from FJ_File ');
      sql.Add('where WBID='+quotedstr(trim(fkeyNO)));
      sql.Add('and TFType='+quotedstr(trim(fType)));
      sql.Add('and FileName='+quotedstr(trim(fFileName)));
      open;
      IF not adoqueryCmd.IsEmpty then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('此附件名称已存在，请修改文件名，继续上传！','提示信息',MB_ICONERROR);
        exit;
      end;
    end;
    Panel3.Visible:=False;
    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;

    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from FJ_File ');
      sql.Add('where WBID='+quotedstr(trim(fkeyNO)));
      sql.Add('and TFType='+quotedstr(trim(fType)));
      sql.Add('and FileName='+quotedstr(trim(fFileName)));
      execsql;
    end;
    with adoqueryCmd 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)));
      sql.Add('and FileName='+quotedstr(trim(fFileName)));
      open;
      append;
      fieldbyname('TFID').Value:=trim(maxNO);
      fieldbyname('WBID').Value:=trim(fkeyNO);
      fieldbyname('TFType').Value:=trim(fType);
      fieldbyname('FileName').Value:=trim(fFileName);
      post;
    end;
    if fFilePath <> '' then
    begin
      try
        IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
        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;
    initdata();
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！','提示信息',0);
  end;
end;

procedure TfrmFjList.N2Click(Sender: TObject);
var
  OpenDiaLog: TOpenDialog;
  fFileName:string;
  fFilePath:string;
begin
  OpenDiaLog := TOpenDialog.Create(Self);
  if OpenDiaLog.Execute then
  begin
    fFilePath:=OpenDiaLog.FileName;
    fFileName:=ExtractFileName(OpenDiaLog.FileName);
    Panel3.Visible:=True;
    WJName.Text:=Trim(fFileName);
    WJName.Hint:=Trim(fFileName);
    WJPach.Text:=fFilePath;
    Button3.Visible:=False;
    Button1.Visible:=True;
    Button2.Visible:=True;
    Panel3.Refresh;
  end;
end;

procedure TfrmFjList.N3Click(Sender: TObject);
var
  fFileName:string;
begin
  if listView1.SelCount<1 then exit;
  try
    fFileName:=ListView1.Selected.Caption;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete  from FJ_File ');
      sql.Add('where WBID='+quotedstr(trim(fkeyNO)));
      sql.Add('and TFType='+quotedstr(trim(fType)));
      sql.Add('and FileName='+quotedstr(trim(fFileName)));
      execsql;
    end;
    initData();
  except
  end;
end;

procedure TfrmFjList.N4Click(Sender: TObject);
var
  SaveDialog: TSaveDialog;
  fFileName:string;
  fFilePath:string;
begin
  if listView1.SelCount<1 then exit;
  try
    fFileName:=ListView1.Selected.Caption;
    SaveDialog := TSaveDialog.Create(Self);
    SaveDialog.FileName:=fFileName;
    if SaveDialog.Execute then
    begin
      Panel2.Caption:='正在保存数据，请稍等...';
      Panel2.Visible:=true;
      application.ProcessMessages;
      fFilePath:=SaveDialog.FileName;
      try
        IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI','SERVER','FTP地址','127.0.0.1');;
        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(fFileName), fFilePath,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;
    end;
  except
    Panel2.Visible:=false;
  end;
end;

procedure TfrmFjList.N1Click(Sender: TObject);
begin
  if listView1.SelCount<1 then exit;
  Panel3.Visible:=True;
  Button1.Visible:=False;
  Button2.Visible:=False;
  Button3.Visible:=True;
  Panel3.Refresh;
  WJName.Text:=Trim(ListView1.Selected.Caption);
  WJName.Hint:=Trim(ListView1.Selected.Caption);
end;

procedure TfrmFjList.Button3Click(Sender: TObject);
var
  fFileName:String;
begin
  fFileName:=WJName.Hint;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('UPdate  FJ_File Set FileName='''+Trim(WJName.Text)+'''');
    sql.Add('where WBID='+quotedstr(trim(fkeyNO)));
    sql.Add('and TFType='+quotedstr(trim(fType)));
    sql.Add('and FileName='+quotedstr(trim(fFileName)));
    execsql;
  end;
  Panel3.Visible:=False;
  initData();
end;

end.
