unit U_FileUp;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ComCtrls, ToolWin, ExtCtrls,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdFTP, StdCtrls, ADODB,jpeg,
  BtnEdit,IniFiles, cxGridCustomPopupMenu, cxGridPopupMenu, DBClient,ShellAPI;

type
  TfrmFileUp = class(TForm)
    cxGrid7: TcxGrid;
    TV7: TcxGridDBTableView;
    FileName: TcxGridDBColumn;
    FileDate: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    Panel16: TPanel;
    Panel1: TPanel;
    Label1: TLabel;
    Code: TEdit;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    ADOQueryFile: TADOQuery;
    DataSource1: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ToolBar6: TToolBar;
    FileUp: TToolButton;
    FileDel: TToolButton;
    V7Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_SC: TClientDataSet;
    FileDown: TToolButton;
    Panel2: TPanel;
    ToolButton2: TToolButton;
    Panel3: TPanel;
    Label2: TLabel;
    Label5: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    procedure FileUpClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FileDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FileDownClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    lstPat: TStringList;
		AJpeg: TJPEGImage;
    procedure ReadINIFile10();
    procedure InitGrid();
    { Private declarations }
  public
    CYID:String;
    { Public declarations }
  end;

var
  frmFileUp: TfrmFileUp;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmFileUp.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 TfrmFileUp.FileUpClick(Sender: TObject);
var
  i,j: Integer;
  PatFile,HZStr: String;
  FTPPath,FConNo,MaxNo:string;
begin
  {if Trim(Code.Text)='' then
  begin
    Application.MessageBox('编号不能为空！','提示',0);
    Exit;
  end;}
  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 else
  begin
    Exit;
  end;
  Panel16.Visible:=True;
  Panel16.Refresh;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select isnull(Count(*),0) MM from XD_File where  ');
    SQL.Add(' filetype=''SC''');
    Open;
    j:=fieldbyname('MM').AsInteger;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    for i := 0 to lstPat.Count - 1 do
    begin
      PatFile := ExtractFileName(lstPat[i]);
      HZStr:=Copy(PatFile,(Pos('.',PatFile)+1),(Length(PatFile)-Pos('.',PatFile))  ) ;
     { PatFile:=Copy(PatFile,(Pos('.',PatFile)+1),(Length(PatFile)-Pos('.',PatFile))  ) ;
      FConNo:='生产正排表';
      while  Pos('/',FConNo)>0 do
      begin
        Delete(FConNo,Pos('/',FConNo),1);
      end;
      PatFile:=Trim(FConNo)+'-'+Inttostr(j+i+1)+'.'+PatFile; }
      if IdFTP1.Connected then
      begin
        try
          {if not DirectoryExists('D:\图片\'+Trim(gDef1)) then
           ForceDirectories('D:\图片\'+Trim(gDef1)); }
         // IdFTP1.Put(lstPat[i], Trim('\SC')+'\'+Trim(PatFile));
          if GetLSNo(ADOQueryCmd,MaxNo,'SC','XD_File',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取文件最大号失败！','提示',0);
            Exit;
          end;
          IdFTP1.Put(lstPat[i], Trim('\SC')+'\'+Trim(MaxNo)+'.'+Trim(HZStr));
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            SQL.Add('select * from XD_File where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('XFID').Value:=Trim(MaxNo);
            FieldByName('CYID').Value:=Trim(MaxNo);
            FieldByName('CYNO').Value:=Trim(MaxNo);
            FieldByName('filename').Value:=Trim(PatFile);
            FieldByName('FileDate').Value:=SGetServerDate(ADOQueryTemp);
            fieldbyname('FileType').value:=Trim('SC');
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('HZStr').Value:='.'+Trim(HZStr);
            Post;
          end;
        except
          //ADOQueryCmd.Connection.RollbackTrans;
          //Application.MessageBox('图片上传失败！','提示',0);
        end;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    if IdFTP1.Connected then IdFTP1.Quit;
    Panel16.Visible:=False;
    InitGrid();
    Application.MessageBox('上传成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('上传失败！','提示',0);
  end;
end;

procedure TfrmFileUp.FormCreate(Sender: TObject);
begin
  lstPat := TStringList.Create;
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-7;
end;

procedure TfrmFileUp.FileDelClick(Sender: TObject);
begin
  if CDS_SC.IsEmpty then Exit;
  if Application.MessageBox('确定要删除文件吗？','提示',32+4)<>IDYES then Exit;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add(' Delete XD_File where XFID='''+Trim(CDS_SC.fieldbyname('XFID').AsString)+'''');
    ExecSQL;
  end;
  CDS_SC.Delete;

end;

procedure TfrmFileUp.FormShow(Sender: TObject);
begin
  WriteCxGrid('正排表',TV7,'文件上传');
  InitGrid();
  if Trim(DParameters1)='下载' then
  begin
    FileUp.Visible:=False;
    FileDel.Visible:=False;
  end;
end;
procedure TfrmFileUp.InitGrid();
begin
  with ADOQueryFile do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from XD_File where ');
    sql.Add(' Filltime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
    sql.Add(' and Filltime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
    SQL.Add(' and FileType=''SC''');
    sql.add(' and CYID=CYNO');
    SQL.Add(' and CYID not like ''HX%'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryFile,CDS_SC);
  SInitCDSData20(ADOQueryFile,CDS_SC);
end;

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

procedure TfrmFileUp.FormDestroy(Sender: TObject);
begin
  frmFileUp:=nil;
end;

procedure TfrmFileUp.ToolButton1Click(Sender: TObject);
var
  FPath:String;
begin
  Close;
  WriteCxGrid('正排表',TV7,'文件上传');
  FPath:='D:\Right1209\';
  if  DirectoryExists(ExtractFileDir(FPath)) then
    winexec('cmd /c rd /s /q D:\Right1209\',sw_hide);
end;

procedure TfrmFileUp.FileDownClick(Sender: TObject);
var
  fHandle:THandle;
  FInt:Integer;
  FFName,FPath:String;
begin
  if CDS_SC.IsEmpty then Exit;
  Panel2.Visible:=True;
  Panel2.Refresh;
 { }
  try
    ReadINIFile10();
    server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    if Length(server)<6 then
    begin
      server:='127.0.0.1';
    end;
    IdFTP1.Host :=server;//PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath:='D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  if Trim(CDS_SC.fieldbyname('XFID').AsString)<'SC1310280002' then
  begin
    FFName:=Trim(CDS_SC.fieldbyname('FileName').AsString);

    FFName:=FPath+FFName;
    if FileExists(FFName)=false then
    begin
      IdFTP1.Get('SC\'+Trim(CDS_SC.fieldbyname('FileName').AsString),
                     FPath+Trim(CDS_SC.fieldbyname('FileName').AsString)
                      );
    end;
    Panel2.Visible:=False;
    if IdFTP1.Connected then IdFTP1.Quit;
    ShellExecute(Handle, 'open',PChar(FPath+Trim(CDS_SC.fieldbyname('FileName').AsString)),'', '', SW_SHOWNORMAL);
  end else
  begin
    FFName:=Trim(CDS_SC.fieldbyname('XFID').AsString)+Trim(CDS_SC.fieldbyname('HZStr').AsString);
    if FileExists(FPath+FFName)=false then
    begin
      IdFTP1.Get('SC\'+FFName,FPath+FFName);
    end;
    Panel2.Visible:=False;
    if IdFTP1.Connected then IdFTP1.Quit;
    if FileExists(FPath+FFName) then
    begin
      ShellExecute(Handle, 'open',PChar(FPath+FFName),'', '', SW_SHOWNORMAL);
    end;
  end;
end;

procedure TfrmFileUp.ToolButton2Click(Sender: TObject);
begin
  InitGrid();
end;

end.
