unit U_DeviceJkTest;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
   cxContainer, cxEdit,  Vcl.Menus,
  Vcl.StdCtrls, cxButtons, cxTextEdit, cxGroupBox, cxStyles, cxCustomData,
  cxFilter, cxData, cxDataStorage, cxNavigator, dxDateRanges, ShellAPI,
  dxScrollbarAnnotations, Data.DB, cxDBData, Data.Win.ADODB, cxGridLevel,
  cxClasses, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxDropDownEdit, Vcl.ComCtrls, Vcl.ToolWin,
  Datasnap.DBClient, cxMaskEdit, Vcl.Buttons, U_BaseList, cxProgressBar,
  cxLookAndFeelPainters;

type
  TfrmDeviceJkTest = class(TfrmBaseList)
    cxGroupBox1: TcxGroupBox;
    InputLen: TcxTextEdit;
    cxButton1: TcxButton;
    DataSource1: TDataSource;
    ADOQueryList: TADOQuery;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1FtFileName: TcxGridDBColumn;
    tv1FileType: TcxGridDBColumn;
    tv1factory: TcxGridDBColumn;
    tv1remark: TcxGridDBColumn;
    tv1fileCreatedate: TcxGridDBColumn;
    tv1FileEditDate: TcxGridDBColumn;
    tv1fillTime: TcxGridDBColumn;
    tv1FileSize: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    edt_dllName: TcxTextEdit;
    cds_list: TClientDataSet;
    cxButton2: TcxButton;
    ADOQueryTmp: TADOQuery;
    fileType: TcxComboBox;
    ToolButton1: TToolButton;
    SpeedButton1: TSpeedButton;
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure cxComboBox1PropertiesChange(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    fIsCommopen: Boolean;
    fDllName:string;
    procedure InitGrid();
    procedure testCom();
    function  ExportFtErpFile(recid,mFileName:string;ADORead:TADOQuery):boolean;
    procedure On1201(var Message: Tmessage); message 1201; // 电子称
    procedure On1301(var Message: Tmessage); message 1301; // 码表
  public
    fFormID:Integer;

  end;

var
  frmDeviceJkTest: TfrmDeviceJkTest;

implementation
uses
  U_DataLink,U_globalVar,U_DeviceJkDll,U_RTFun;

{$R *.dfm}

procedure TfrmDeviceJkTest.cxButton1Click(Sender: TObject);
begin
  if cds_list.IsEmpty then
  begin
    exit;
  end;


  testCom();
end;

procedure TfrmDeviceJkTest.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  if fIsCommopen then
  begin
    CloseCom(fDllName);
  end;
  Action:=caFree;
end;

procedure TfrmDeviceJkTest.FormCreate(Sender: TObject);
begin
  inherited;
  cxGrid1.Align:=alClient;
end;

procedure TfrmDeviceJkTest.FormDestroy(Sender: TObject);
begin
    inherited;
  frmDeviceJkTest:=nil;
end;

procedure TfrmDeviceJkTest.FormShow(Sender: TObject);
begin
 // inherited;
  InitGrid();
end;

procedure TfrmDeviceJkTest.InitGrid();
begin
  try
    ADOQueryList.DisableControls ;
    with ADOQueryList do
    begin
      close;
      sql.clear;
      sql.Add('select recid, FileName,FileType,FileEditDate,fileCreateDate,FileSize,filltime,');
      sql.Add('FilePath,remark,factory');
      sql.Add('from RT_deviceDllFile');
      if Trim(fileType.Text)<>'' then
        sql.Add('where filetype='+QuotedStr(Trim(fileType.Text)));
      sql.Add('order by fillTime desc');
      Open;

     SCreateCDS(ADOQueryList, cds_list);
     SInitCDSData(ADOQueryList, cds_list);
    end;
  finally
    ADOQueryList.EnableControls ;
    tv1.ApplyBestFit() ;
  end;
end;

procedure TfrmDeviceJkTest.TBCloseClick(Sender: TObject);
begin
  inherited;
  close;
end;

procedure TfrmDeviceJkTest.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmDeviceJkTest.testCom();
var
  iniFileName:string;
  filePath:string;
begin
  try
   //  while True do
   //  begin
     fDllName:=Trim(cds_list.FieldByName('FileName').AsString) ;
     edt_dllName.Text:=fDllName ;
     if Trim(cds_list.FieldByName('FileType').AsString)<>'' then
       ExportFtErpFile(Trim(cds_list.FieldByName('recid').AsString),fDllName,ADOQueryTmp);
     iniFileName:=  StringReplace(fDllName,'.dll','.ini',[rfIgnoreCase]);
     filePath:= ExtractFilePath(Paramstr(0))+iniFileName  ;
     if not FileExists(filePath) then
     begin
       ExportFtErpFile('',iniFileName,ADOQueryTmp);
     end;



     if not FileExists(ExtractFilePath(Paramstr(0))+iniFileName) then
     begin
       application.MessageBox(PChar(iniFileName+'文件不存在!'),'警告信息',0);
       Exit;
     end;
     OpenCom(self.handle,fDllName,fIsCommopen);
        // Close;
   //  end;
  finally

  end;
end;

procedure TfrmDeviceJkTest.ToolButton1Click(Sender: TObject);
var
  iniFileName:String;
  filePath:string;
begin
  if cds_list.IsEmpty then   exit;
  iniFileName :=Trim( cds_list.FieldByName('fileName').AsString );
     iniFileName:=  StringReplace(iniFileName,'.dll','.ini',[rfIgnoreCase]);
     filePath:= ExtractFilePath(Paramstr(0))+iniFileName ;
     if not FileExists(filePath) then
     begin
       ExportFtErpFile('',iniFileName,ADOQueryTmp);
     end;

     if not FileExists(filePath) then
     begin
       application.MessageBox(PChar(iniFileName+'文件不存在!'),'警告信息',0);
       Exit;
     end;
     //
    ShellExecute(0, 'open', PChar('notepad.exe'), PChar(filePath), nil, SW_SHOWNORMAL);
end;

///////////////////////////////////////////////////////
  //函数功能：从服务器下载文件；
///////////////////////////////////////////////////////
procedure TfrmDeviceJkTest.cxButton2Click(Sender: TObject);
begin
  if fDllName<>'' then
    CloseCom(fDllName);
end;

procedure TfrmDeviceJkTest.cxComboBox1PropertiesChange(Sender: TObject);
begin
  TBRafresh.Click;
end;

function  TfrmDeviceJkTest.ExportFtErpFile(recid,mFileName:string;ADORead:TADOQuery):boolean;
var
  Stream : TMemoryStream;
  ff:TADOBlobstream;
  mfileSize:integer;
  mCreationTime:TdateTime;
  mWriteTime:TdateTime;
  IsFileHas:boolean;
  mChildPath:string;
  mFilePath:string;
begin
  try
    result:=false;
    mChildPath:='';    //test\
    ///////////////////////////////////////////////
    //如果产品存在
    mFilePath:= ExtractFilePath(Paramstr(0))+mChildPath;
    IsFileHas:= FileExists(mFilePath+fDllName);
    if IsFileHas then
    begin
      //////////////////////////
      //获取文件信息
      GetFileInfo(mFilePath+mFileName,mfileSize,mCreationTime,mWriteTime);
     //eleteFile(mFilePath+fDllName);
    end;
    //////////////////////////////////////////
    //存在文件
    if IsFileHas then
    begin
      with ADORead do
      begin
        close;
        sql.Clear ;
        sql.Add('select count(FileName) as cnt ');
        sql.Add('from RT_deviceDllFile');
        if recid<>'' then
          sql.Add('where recid='+quotedStr(recid))
        else
          sql.Add('where FileName='+quotedStr(mFileName));
        sql.Add(' and DATEDIFF(minute,'+ quotedStr(formatDateTime('yyyy-MM-dd hh:mm',mWriteTime))+',fileEditDate)>0');
        Open;


        //是否存在新的文件
        if fieldByName('cnt').AsInteger>0 then
        begin
          close;
          sql.Clear ;
          sql.Add('select * ');
          sql.Add('from RT_deviceDllFile');
          if recid<>'' then
            sql.Add('where recid='+quotedStr(recid))
          else
           sql.Add('where FileName='+quotedStr(mFileName));

          Open;

          ff := TADOBlobstream.Create(fieldByName('Files') as TblobField, bmRead);
        end
        else
        begin
          exit;
        end;

       if trim(fieldByName('FilePath').AsString)<>'' then
          mChildPath:=trim(fieldByName('FilePath').AsString)+'\';
      end;
    end
    //////////////////////////////////////
    //不存在
    else
    begin
      with ADORead do
      begin
        close;
        sql.Clear ;
        sql.Add('select * ');
        sql.Add('from RT_deviceDllFile');
        if recid<>'' then
          sql.Add('where recid='+quotedStr(recid))
        else
          sql.Add('where FileName='+quotedStr(mFileName));
        Open;

        if recordCount>0 then
        begin
          ff := TADOBlobstream.Create(fieldByName('Files') as TblobField, bmRead);
        end
        else
        begin
          application.MessageBox(pchar('未找到文件'+mfileName+'!'),'提示信息',0);
          exit;
        end;

       if trim(fieldByName('FilePath').AsString)<>'' then
          mChildPath:=trim(fieldByName('FilePath').AsString)+'\';
      end;

    end;

    if  ff<>nil then
    begin
      try
        mfileName:=trim(ADORead.fieldByName('FileName').asString);
        if not DirectoryExists(ExtractFileDir(mFilePath+mfileName)) then
        ForceDirectories(ExtractFileDir(mFilePath+mfileName));
        Stream:= TMemoryStream.Create ;
        ff.SaveToStream(Stream);
        Stream.SaveToFile(mFilePath+mfileName); //+'\tmpFile\'
      finally
        Stream.Free ;
           ff.free;
      end;
    end;
    UpdateFileTime(mFilePath+mfileName,ADORead.fieldByName('FileCreateDate').AsDateTime,ADORead.fieldByName('FileEditDate').AsDateTime,ADORead.fieldByName('FileEditDate').AsDateTime);
    Result:=true;
  except
    application.MessageBox(pchar('读取文件'+mfileName+'失败!'),'提示信息',0);
  end;
end;


procedure TfrmDeviceJkTest.On1301(var Message: Tmessage);
var
  i1, i2: Integer;
  unitname: string;
  fdata: double;
begin
  i1 := Message.WParam;
  i2 := Message.LParam;

 // if trim(cds_list.fieldbyName('filetype').AsString) = '码表' then
 // begin
    InputLen.Text := format('%.2f', [i1 / 100000]);

  //end;
end;

procedure TfrmDeviceJkTest.On1201(var Message: Tmessage);
var
  i1, i2: Integer;
  unitname: string;
  fdata: double;
begin
  i1 := Message.WParam;
  i2 := Message.LParam;

//  if trim(cds_list.fieldbyName('filetype').AsString) = '电子称' then
//  begin
    InputLen.Text := format('%.2f', [i1 / 100000]);
//  end;

end;
end.
