unit U_FileUp_GSWJ;

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, cxContainer, cxTextEdit, cxCurrencyEdit, cxCheckBox;

type
  TfrmFileUp_GSWJ = 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;
    YWYName: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    ADOQueryCmd: TADOQuery;
    ADOQuery2: TADOQuery;
    ToolButton3: TToolButton;
    V7FileDate: TcxGridDBColumn;
    V7YWYName: TcxGridDBColumn;
    V7CYNO: TcxGridDBColumn;
    Label3: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label2: TLabel;
    CYNO: TEdit;
    Panel4: TPanel;
    Label6: TLabel;
    Label5: TLabel;
    Panel5: TPanel;
    Image1: TImage;
    TSure: TButton;
    ETaiTou: TEdit;
    FileDate: TDateTimePicker;
    Label7: TLabel;
    Label8: TLabel;
    FCYNO: TEdit;
    FYWYName: TBtnEditA;
    Panel2: TPanel;
    Panel16: TPanel;
    Label9: TLabel;
    Label10: TLabel;
    CConNo: TEdit;
    V7ConNo: TcxGridDBColumn;
    V7SBQty: TcxGridDBColumn;
    V7Filltime: TcxGridDBColumn;
    Label11: TLabel;
    Label12: TLabel;
    Conno: TEdit;
    V7SBBFB: TcxGridDBColumn;
    cxStyle2: TcxStyle;
    SBQty: TcxCurrencyEdit;
    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 YWYNameChange(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure TSureClick(Sender: TObject);
    procedure FYWYNameBtnClick(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_GSWJ: TfrmFileUp_GSWJ;

implementation
uses
  U_DataLink,U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmFileUp_GSWJ.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_GSWJ.FileUpClick(Sender: TObject);
begin
  Panel4.Visible:=True;
  fileDate.DateTime:=SGetServerDate(ADOQuery2);
end;

procedure TfrmFileUp_GSWJ.FormCreate(Sender: TObject);
begin
  lstPat := TStringList.Create;
  canShu1:=trim(DParameters1);
end;

procedure TfrmFileUp_GSWJ.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;
  with CDS_SC do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').Asboolean=true then
      begin
        if CDS_SC.fieldbyname('filler').AsString<>trim(DName) then
        begin
          application.MessageBox('不能删除他人文件','提示');
          exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add(' Delete XD_File where XFID='''+Trim(CDS_SC.fieldbyname('XFID').AsString)+'''');
          ExecSQL;
        end;
      end;
      next;
    end;
  end;
  initGrid();
end;

procedure TfrmFileUp_GSWJ.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;
  end
  else
  begin
    FileUp.Visible:=True;
    FileDel.Visible:=True;
  end;
end;

procedure TfrmFileUp_GSWJ.InitGrid();
var Fint:integer;
begin
  with ADOQuery2 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.* from OA_YG_DangAn A ');
    sql.Add('inner join SY_Dept B on A.DPID=B.DPID');
    SQL.Add('where isnull(A.YGEName,YGName)='''+trim(DName)+'''');
    Open;
  end;
  FDEPT:=ADOQuery2.fieldbyname('DPParent').AsString;
  for Fint:=4 to ADOQuery2.fieldbyname('DPlevel').AsInteger-1 do
  begin
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from SY_Dept where DPID='''+trim(FDEPT)+'''');
      open;
    end;
    FDEPT:=Trim(ADOQueryCmd.fieldbyname('DPParent').AsString);
  end;
  if FDEPT<>'' then
  begin
    FDPName:=ADOQueryCmd.fieldbyname('DPName').AsString;
  end
  else
  begin
    FDPName:='';
  end;
  with ADOQueryFile do
  begin
    Close;
    SQL.Clear;
    sql.add('select AA.*,SBBFB=(case when SumSBQty=0 then 0 else (SBQty/SumSBQty)*100 end) from (select *, ');
    SQL.Add(' SumSBQty=(select Sum(isnull(SBQty,0))');
    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(' 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=''公司档案'') AA where CYID=''公司档案''');
    if trim(CanShu1)='部门' then
    sql.add(' and (isnull(CYNo,'''')='''+trim(FDPName)+'''');
    if trim(CanShu1)='查询' then
    sql.add(' and YWYName='''+trim(DName)+'''');
    if trim(CanShu1)='登记' then
    sql.add(' and filler='''+trim(DName)+'''');
    Open;
  end;                                
  SCreateCDS20(ADOQueryFile,CDS_SC);
  SInitCDSData20(ADOQueryFile,CDS_SC);
end;

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

procedure TfrmFileUp_GSWJ.FormDestroy(Sender: TObject);
begin
  frmFileUp_GSWJ:=nil;
end;

procedure TfrmFileUp_GSWJ.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_GSWJ.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)<'DA1310280009' 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_GSWJ.ToolButton2Click(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmFileUp_GSWJ.YWYNameChange(Sender: TObject);
begin
  toolButton3.Click;
end;

procedure TfrmFileUp_GSWJ.Image1Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmFileUp_GSWJ.TSureClick(Sender: TObject);
var
  i,j: Integer;
  PatFile,HZStr: String;
  FTPPath,FConNo,MaxNo:string;
begin
  if FYWYName.Text='' then
  begin
    application.MessageBox('业务员不能为空','提示');
    exit;
  end;
  if CConNo.Text='' then
  begin
    application.MessageBox('合同号不能为空','提示');
    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;
  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,'DA','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(FCYNo.Text);
            FieldByName('filename').Value:=Trim(PatFile);
            FieldByName('FileDate').Value:=fileDate.DateTime;
            FieldByName('Filltime').Value:=SGetServerDate(ADOQueryTemp);
            fieldbyname('FileType').value:=Trim('SC');
            FieldByName('Filler').Value:=Trim(DName);
            fieldbyname('YWYName').Value:=trim(FYWYName.Text);
            fieldbyname('ConNo').Value:=trim(CConNo.Text);
            if SBQty.Text='' then
            fieldbyname('SBQty').Value:=0
            else
            fieldbyname('SBQty').Value:=trim(SBQty.Text);
            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;
    Panel4.Visible:=False;
    InitGrid();
    Application.MessageBox('上传成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('上传失败！','提示',0);
  end;
end;

procedure TfrmFileUp_GSWJ.FYWYNameBtnClick(Sender: TObject);
begin
 frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='YWYName';
    flagname:='业务员';
    MainType:=trim(DName);
    if showModal=1 then
    begin
      FYWYName.Text:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      with ADOQuery2 do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.* from OA_YG_DangAn A ');
        SQL.Add('where isnull(A.YGEName,YGName)='''+trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
        Open;
      end;
      FCYNO.Text:=ADOQuery2.fieldbyname('Dept').AsString;
    end;
    free;
  end;
end;

end.
