unit U_LabelAdd;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, BtnEdit, ExtCtrls, DB, ADODB, RM_System,
  RM_Common, RM_Class, RM_GridReport, Buttons, RTComboBox, RM_Preview, RM_e_Xls,
  RM_e_Graphic, RM_e_bmp, RM_BarCode, RM_DsgGridReport, RM_Dataset, cxStyles,
  cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid;

type
  TfrmLabelAdd = class(TForm)
    Panel1: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    LabelFileName: TBtnEditA;
    beizhu: TMemo;
    ToolBar1: TToolBar;
    Tsave: TToolButton;
    Tclose: TToolButton;
    ADOQueryCmd: TADOQuery;
    OpenDialog1: TOpenDialog;
    RMGridReport1: TRMGridReport;
    Label9: TLabel;
    LabelCaption: TEdit;
    Label10: TLabel;
    LabelType: TRTComboBox;
    ADOQueryTmp: TADOQuery;
    RMPreview1: TRMPreview;
    RMGridReportDesigner1: TRMGridReportDesigner;
    RMBarCodeObject1: TRMBarCodeObject;
    RMBMPExport1: TRMBMPExport;
    RMXLSExport1: TRMXLSExport;
    RMDS_Main: TRMDBDataSet;
    RMDataDictionary1: TRMDataDictionary;
    ADOQuery1: TADOQuery;
    RMGridReport2: TRMGridReport;
    ADOQueryCmdSC: TADOQuery;
    ADOQueryCmdFileContent: TBlobField;
    ADOQueryCmdFtFileName: TStringField;
    ADOQueryCmdFileEditDate: TDateTimeField;
    ADOQueryCmdFileSize: TFloatField;
    ADOQueryCmdFiller: TStringField;
    ADOQueryCmdLastEditTime: TDateTimeField;
    ADOQueryCmdLastEditer: TStringField;
    ADOQueryCmdFileCreateDate: TDateTimeField;
    ADOQueryCmdchildPath: TStringField;
    ADOQueryCmdFileType: TStringField;
    procedure TcloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TsaveClick(Sender: TObject);
    procedure LabelFileNameBtnClick(Sender: TObject);
    procedure BtOpenClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RMPreview1DblClick(Sender: TObject);
  private
    fIsChg: Boolean;
    function SaveData(): Boolean;
    function EditData(): Boolean;
    procedure InitWinData();
    procedure InitVarDictionary();
    procedure InitDataSetDictionary();
    function PostFileToData(): boolean;
    procedure GetFileInfo(mFile: string; var mfileSize: integer; var CreationTime: tdatetime; var WriteTime: tdatetime);
    function CovFileDate(Fd: _FileTime): TDateTime;
  public
    fcustomNo: string;
    fKeyNo: string;
    fWinStatus: integer;
  end;

var
  frmLabelAdd: TfrmLabelAdd;

implementation

uses
  U_DataLink, U_RTFun;
{$R *.dfm}

procedure TfrmLabelAdd.TcloseClick(Sender: TObject);
begin
  if fIsChg then
  begin
    if application.MessageBox('标签设计过，是否要保存?', '提示信息', 1) = 1 then
    begin
      Tsave.Click;
    end
    else
      close;
  end
  else
    close;
end;

procedure TfrmLabelAdd.FormCreate(Sender: TObject);
begin
  panel1.Align := alClient;
  fIsChg := false;
//  ClearWinData(panel1);
 // InitVarDictionary();
end;

procedure TfrmLabelAdd.TsaveClick(Sender: TObject);
begin
  if trim(labelCaption.Text) = '' then
  begin
    application.MessageBox('标签名称不能为空！', '提示');
    labelCaption.SetFocus;
    exit;
  end;
  if trim(LabelFileName.Text) = '' then
  begin
    application.MessageBox('标签文件不能为空，请选择标签！', '提示');
    LabelFileName.SetFocus;
    exit;
  end;
  PostFileToData();
  if fWinStatus = 0 then
  begin
    if SaveData() then
    begin
      ModalResult := 1;
    end;
  end
  else
  begin
    if EditData() then
    begin
      ModalResult := 1;
    end;
  end;

end;

function TfrmLabelAdd.PostFileToData(): boolean;
var
  mFileName, fFileName, fpathFileName: string;
  Stream: TMemoryStream;
  mfileSize: integer;
  mCreationTime: TdateTime;
  mWriteTime: TdateTime;
begin
  result := false;
  fFileName := Trim(LabelCaption.Text) + '.rmf';
  fpathFileName := Trim(LabelFileName.Text);
  try
    ADOQueryCmdSC.Connection.BeginTrans;
    try
      with ADOQueryCmdSC do
      begin
        close;
        sql.Clear;
        sql.Add('delete  from RT_FileUpdate');
        sql.Add('where FileName=' + quotedStr(trim(fFileName)));
        execsql;
      end;
      with ADOQueryCmdSC do
      begin
        close;
        sql.Clear;
        sql.Add('select * from RT_FileUpdate');
        sql.Add('where FileName=' + quotedStr(trim(fFileName)));
        Open;
        //////////////////////////
        //获取文件信息
        GetFileInfo(Trim(fpathFileName), mfileSize, mCreationTime, mWriteTime);

        if RecordCount <= 0 then
        begin
          Append;
          fieldByName('FileName').AsString := trim(fFileName);
        end
        else
        begin
          edit;
        end;

        fieldByName('FileEditDate').Value := mWriteTime;
        fieldByName('FileCreateDate').Value := mCreationTime;
        fieldByName('FileSize').Value := mfileSize;
        fieldByName('Filler').Value := Dname;
        fieldByName('LastEditer').Value := Dname;
        fieldByName('LastEditTime').Value := SGetServerDateTime(ADOQueryTmp);
//        if pos('.rmf',fFileName)>0 then
        begin
          fieldByName('FilePath').Value := 'report';
          fieldByName('FileType').Value := '公用';
        end;
//        else if pos('.dll',fFileName)>0 then
//        begin
//          fieldByName('FilePath').Value :='';
//          fieldByName('FileType').Value :='一般';
//        end
//        else
//        begin
//          fieldByName('FilePath').Value :='';
//          fieldByName('FileType').Value :='公用';
//        end;
        //将OLE数据存入数据库
        ADOQueryCmdFileContent.LoadFromFile(fpathFileName);
        //ADOQueryCmdFileContent.LoadFromStream(Stream);

        post;
      end;
    finally
    end;
    result := true;
    ADOQueryCmdSC.Connection.CommitTrans;
  except
    ADOQueryCmdSC.Connection.RollbackTrans;
    Result := False;
    application.MessageBox(pchar('提交文件[' + trim(fFileName) + ']失败!'), '提示信息', MB_ICONERROR);
  end;

end;

procedure TfrmLabelAdd.GetFileInfo(mFile: string; var mfileSize: integer; var CreationTime: tdatetime; var WriteTime: tdatetime);
var
  vSearchRec: TSearchRec;
begin
  FindFirst(mFile, faAnyFile, vSearchRec);
  mfileSize := vSearchRec.Size;
  CreationTime := CovFileDate(vSearchRec.FindData.ftCreationTime); //创建时间
   //vSearchRec.FindData.ftLastAccessTime//访问时间
  WriteTime := CovFileDate(vSearchRec.FindData.ftLastWriteTime); //修改时间
  FindClose(vSearchRec);
end;

function TfrmLabelAdd.CovFileDate(Fd: _FileTime): TDateTime;
var
  Tct: _SystemTime;
  Temp: _FileTime;
begin
  FileTimeToLocalFileTime(Fd, Temp);
  FileTimeToSystemTime(Temp, Tct);
  CovFileDate := SystemTimeToDateTime(Tct);
end;

/////////////////////////////////////////////////////////////////////
  //函数功能：保存数据
/////////////////////////////////////////////////////////////////////
function TfrmLabelAdd.SaveData(): Boolean;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from P_Label');
      sql.Add('where 1<>1');
      Open;

      Append;
      fieldByName('filler').value := DName;
      fieldByName('filltime').value := DServerDate;
      fieldByName('beizhu').value := trim(beizhu.text);
      fieldByName('LabelCaption').value := trim(LabelCaption.text);
      fieldByName('LabelType').value := trim(LabelType.text);
      fieldByName('LabelFileName').value := trim(LabelFileName.text);
        //TBlobField(FieldByName('LabelFile')).LoadFromStream(fStream);
      RMGridReport1.SaveToBlobField(TBlobField(FieldByName('LabelFile')));
      Post;
    end;
    with ADOQueryTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from P_Label where LabelCaption=''' + Trim(LabelCaption.text) + '''');
      Open;
    end;
    if ADOQueryTmp.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Result := False;
      Application.MessageBox('标签名称重复!', '提示', 0);
      Exit;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := false;
    application.MessageBox('保存标签模板出错!', '警告信息', 0);
  end;
end;

/////////////////////////////////////////////////////////////////////
  //函数功能：保存数据
/////////////////////////////////////////////////////////////////////
function TfrmLabelAdd.EditData(): Boolean;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from P_Label');
      sql.Add('where labelId=' + fkeyNo);
      Open;
      Edit;
      fieldByName('LabelCaption').value := trim(LabelCaption.text);
      fieldByName('LabelType').value := trim(LabelType.text);
      fieldByName('LabelFileName').value := trim(LabelFileName.text);
      RMGridReport1.SaveToBlobField(TBlobField(FieldByName('LabelFile')));
      fieldByName('Editer').value := DName;
      fieldByName('EditTime').value := DServerDate;
      fieldByName('beizhu').value := trim(beizhu.text);
      Post;
    end;
    with ADOQueryTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from P_Label where LabelCaption=''' + Trim(LabelCaption.text) + '''');
      Open;
    end;
    if ADOQueryTmp.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Result := False;
      Application.MessageBox('标签名称重复!', '提示', 0);
      Exit;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := false;
    application.MessageBox('保存标签模板出错!', '警告信息', 0);
  end;
end;

procedure TfrmLabelAdd.LabelFileNameBtnClick(Sender: TObject);
var
  str: string;
begin
  if OpenDialog1.Execute() then
  begin
    LabelFileName.Text := OpenDialog1.FileName;
    str := Trim(ExtractFileName(OpenDialog1.FileName));
    LabelCaption.Text := copy(str, 0, Length(str) - 4);
    RMGridReport1.LoadFromFile(LabelFileName.Text);
    RMGridReport1.Preview := RMPreview1;
    RMGridReport1.ShowReport;
  end;
end;

procedure TfrmLabelAdd.BtOpenClick(Sender: TObject);
begin

end;
////////////////////////////////////////////////////////////
  //初始化窗口数据
////////////////////////////////////////////////////////////

procedure TfrmLabelAdd.InitWinData();
begin
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*');
    //  sql.Add('customNoName=isnull((select customName from BC_customer where customNO=A.customNo),A.customNo)');
      sql.Add('from P_Label A');
      sql.Add('WHERE LabelId=' + fkeyNo);
      Open;
      if isEmpty then
      begin
        close;
        exit;
      end;

      SSetWinData(ADOQueryTmp, panel1);
      RMGridReport1.LoadFromBlobField(tblobfield(fieldbyname('labelFile')));
      RMGridReport2.FileName := trim(fieldByName('labelFileName').AsString);
      RMGridReport2.LoadFromBlobField(tblobfield(fieldbyname('labelFile')));
      RMGridReport1.Preview := RMPreview1;
      //RMGridReport1.PrepareReport;
      RMGridReport1.ShowReport;
    end;
  except
  end;
end;

procedure TfrmLabelAdd.FormShow(Sender: TObject);
begin
  if fWinStatus > 0 then
    InitWinData();
end;

procedure TfrmLabelAdd.RMPreview1DblClick(Sender: TObject);
begin
  //btOpen.Click ;
end;
////////////////////////////////////////////////////////////
  //
////////////////////////////////////////////////////////////

procedure TfrmLabelAdd.InitVarDictionary();
var
  i: integer;
begin
{  try
    with RMGridReport2 do
    begin
        Dictionary.Variables.Clear ;
        Dictionary.Variables.AddCategory('客户单位信息');
        with ADOQueryTmp do
        begin
          close;
          sql.clear;
          sql.Add('exec P_Label_CustPrintData');
          sql.Add(quotedStr(fCustomNo));
          Open;
          for i:=0 to FieldCount-1 do
          begin

            Dictionary.Variables.Add(trim(fields[i].FieldName)
                                ,'');
            Dictionary.Variables.AsString[trim(fields[i].FieldName)]:=trim(fields[i].AsString);
          end;
        end;
    end;
  finally

  end;  }
end;
/////////////////////////////////////////////////
  //
/////////////////////////////////////////////////

procedure TfrmLabelAdd.InitDataSetDictionary();
begin
{  with ADOQuery1 do
  begin
    close;
    sql.Clear ;
    sql.Add('exec P_Label_PrintSet');
    sql.Add(quotedStr(''));
    //sql.Add(','+quotedStr(''));
    //sql.Add(','+quotedStr(''));
    //sql.Add(','+quotedStr(''));
    //sql.Add(','+quotedStr(''));
    OPen;
  end;
  with RMGridReport2 do
  begin
    Dictionary.FieldAliases.Clear;
    Dictionary.FieldAliases['RMDS_Main']:= '标签数据';
    Dictionary.FieldAliases['RMDS_Main."barcode"']:='标签条码';
  end;     }
end;

end.

