unit U_FileUp_SD;

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,
  cxCalendar, cxCheckBox, Menus;

type
  TfrmFileUp_SD = class(TForm)
    cxGrid7: TcxGrid;
    TV7: TcxGridDBTableView;
    vFileName: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    Panel1: TPanel;
    Label1: TLabel;
    Code: TEdit;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    ADOQueryFile: TADOQuery;
    DataSource1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ToolBar6: TToolBar;
    FileUp: TToolButton;
    FileDel: TToolButton;
    ToolButton1: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_SC: TClientDataSet;
    FileDown: TToolButton;
    ToolButton2: TToolButton;
    Panel3: TPanel;
    Label4: TLabel;
    FileName: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    ADOQueryCmd: TADOQuery;
    ADOQuery2: TADOQuery;
    ToolButton3: TToolButton;
    V7FileDate: TcxGridDBColumn;
    Label3: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Panel2: TPanel;
    Panel16: TPanel;
    cxStyle2: TcxStyle;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    V7Ssel: TcxGridDBColumn;
    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);
    procedure ToolButton3Click(Sender: TObject);
    procedure FileNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private
    lstPat: TStringList;
		AJpeg: TJPEGImage;
    Canshu1,FDEPT,FDPName:string;
    procedure ReadINIFile10();
    procedure InitGrid();
    { Private declarations }
  public
    CYID:String;
    { Public declarations }
  end;

var
  frmFileUp_SD: TfrmFileUp_SD;

implementation
uses
  U_DataLink,U_Fun, U_ZDYHelp, U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmFileUp_SD.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_SD.FileUpClick(Sender: TObject);
var
  i,j: Integer;
  PatFile,HZStr: String;
  FTPPath,FConNo,MaxNo:string;
begin
  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;
  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))  ) ;
      if IdFTP1.Connected then
      begin
        try
          if GetLSNo(ADOQueryCmd,MaxNo,'HY','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=2');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('XFID').Value:=Trim(MaxNo);
            FieldByName('CYID').Value:='公司海运费';
            FieldByName('CYNO').Value:=Trim(FDPName);
            FieldByName('filename').Value:=Trim(PatFile);
            FieldByName('FileDate').Value:=SGetServerDate(ADOQueryTemp);
            FieldByName('Filltime').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_SD.FormCreate(Sender: TObject);
begin
  lstPat := TStringList.Create;
  canShu1:=trim(DParameters1);
end;

procedure TfrmFileUp_SD.FileDelClick(Sender: TObject);
begin
  if CDS_SC.IsEmpty then Exit;
  if CDS_SC.Locate('Ssel',true,[])=False then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  if Application.MessageBox('确定要删除文件吗？','提示',32+4)<>IDYES then Exit;
  if CDS_SC.fieldbyname('filler').AsString<>trim(DName) then
  begin
    application.MessageBox('不能删除他人文件','提示');
    exit;
  end;
  with CDS_SC do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').Asboolean=true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add(' Delete XD_File where XFID='''+Trim(CDS_SC.fieldbyname('XFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add(' Delete OA_Chk where Mainid='''+Trim(CDS_SC.fieldbyname('XFID').AsString)+'''');
          ExecSQL;
        end;
      end;
      next;
    end;
  end;
  initGrid();
end;

procedure TfrmFileUp_SD.FormShow(Sender: TObject);
begin
  WriteCxGrid('正排表',TV7,'文件上传');
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  InitGrid();
  {if Trim(canShu1)='查询' then
  begin
    FileUp.Visible:=False;
    FileDel.Visible:=False;
    TTJ.Visible:=False;
  end
  else
  begin
    FileUp.Visible:=True;
    FileDel.Visible:=True;
    TTJ.Visible:=True;
  end;}
end;

procedure TfrmFileUp_SD.InitGrid();
begin
    with ADOQueryFile do
    begin
      Close;
      SQL.Clear;
      sql.add('select *, ');
      sql.add(' chkStatus=(case when isnull((select Chkstatus from OA_Chk where Mainid=XFID),'''')='''' then ''未提交'' else ''已提交'' end)');
      sql.add(' from XD_File where ');
      sql.Add(' FileType=''SC'' ');
      sql.add(' and FileDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and FileDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      sql.add(' and CYID=''公司海运费''');
      sql.add(' order by FileDate desc');
      Open;
    end;                                
    SCreateCDS20(ADOQueryFile,CDS_SC);
    SInitCDSData20(ADOQueryFile,CDS_SC);
end;

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

procedure TfrmFileUp_SD.FormDestroy(Sender: TObject);
begin
  frmFileUp_SD:=nil;
end;

procedure TfrmFileUp_SD.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_SD.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)<'HY1310280009' 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_SD.ToolButton2Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmFileUp_SD.ToolButton3Click(Sender: TObject);
begin
  SDofilter(ADOQueryFile,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryFile,CDS_SC);
  SInitCDSData20(ADOQueryFile,CDS_SC);
end;

procedure TfrmFileUp_SD.FileNameChange(Sender: TObject);
begin
  toolButton3.Click;
end;

procedure TfrmFileUp_SD.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_SC,True);
end;

procedure TfrmFileUp_SD.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_SC,False);
end;

end.
