unit U_LabelList;

interface

uses
  Windows, Messages, SysUtils, StrUtils, 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, DBClient,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, 
   dxDateRanges;

type
  TfrmLabelList = class(TForm)
    ADOQueryCmd: TADOQuery;
    OpenDialog1: TOpenDialog;
    RMGridReport1: TRMGridReport;
    ADOQueryTmp: TADOQuery;
    RMPreview1: TRMPreview;
    ADOQuery1: TADOQuery;
    DS_Label: TDataSource;
    ADOQueryLabel10: TADOQuery;
    ToolBar2: TToolBar;
    Tadd: TToolButton;
    Tupd: TToolButton;
    Tdel: TToolButton;
    TOK: TToolButton;
    Tclose: TToolButton;
    Panel4: TPanel;
    ToolButton1: TToolButton;
    Label9: TLabel;
    Label10: TLabel;
    LabelCaption: TEdit;
    LabelType: TRTComboBox;
    ADOQueryLabel: TClientDataSet;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1labelId: TcxGridDBColumn;
    tv1labeltype: TcxGridDBColumn;
    tv1labelCaption: TcxGridDBColumn;
    tv1labelFile: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    procedure TcloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtOpenClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RMPreview1DblClick(Sender: TObject);
    procedure TaddClick(Sender: TObject);
    procedure TupdClick(Sender: TObject);
    procedure tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TdelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure LabelTypeChange(Sender: TObject);
    procedure tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private
    isLoad: Boolean;
    function DeleteData(): Boolean;
    procedure InitWinData();
    procedure InitVarDictionary();
    procedure InitDataSetDictionary();
    procedure InitGrid();
    procedure OpenLabel();
    procedure DoFilter();
  public
    fSelLabelId, LBName, LBInt, SLBName: string;
    fKeyNo: string;
    fchg: Boolean;
    fIsShowModal: Boolean;
    fWinStatus: integer;
  end;

var
  frmLabelList: TfrmLabelList;

implementation

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

procedure TfrmLabelList.DoFilter();
var
  filterStr: string;
begin
  filterStr := '';

  if trim(LabelType.Text) <> '' then
  begin
    filterStr := ' and LabelType like ' + quotedStr('%' + trim(LabelType.Text) + '%');
  end;
  //名称
  if trim(LabelCaption.Text) <> '' then
  begin
    filterStr := filterStr + ' and LabelCaption like ' + quotedStr('%' + trim(LabelCaption.Text) + '%');
  end;
  try
    ADOQueryLabel10.DisableControls;
    if trim(filterStr) = '' then
    begin
      ADOQueryLabel.Filtered := false;
      ADOQueryLabel.EnableControls;
      exit;
    end;
    filterStr := trim(RightBStr(filterStr, length(filterStr) - 4));
    with ADOQueryLabel do
    begin
      filtered := false;
      filter := filterStr;
      filtered := true;
    end;
    SDofilter(ADOQueryLabel10, filterStr);
    SCreateCDS(ADOQueryLabel10, ADOQueryLabel);
    SInitCDSData(ADOQueryLabel10, ADOQueryLabel);
  finally
    ADOQueryLabel10.EnableControls;
  end;
end;

procedure TfrmLabelList.TcloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmLabelList.FormCreate(Sender: TObject);
begin

 // ClearWinData(panel3);
  fSelLabelId := '';

end;

procedure TfrmLabelList.BtOpenClick(Sender: TObject);
begin

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

procedure TfrmLabelList.InitWinData();
begin
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* ,B.customName as customNoName');
      sql.Add('from JD_Label A');
      sql.Add('INNER JOIN BC_customer B ON A.customNO=B.customNo');
      sql.Add('WHERE B.customNo=''' + fkeyNo + '''');
      Open;
      if isEmpty then
      begin
        close;
        exit;
      end;

     // SetWinData(ADOQueryTmp,panel3);
      {
      RMGridReport1.LoadFromBlobField(tblobfield(fieldbyname('labelFile')));
      RMGridReport1.Preview :=RMPreview1;
      RMGridReport1.PrepareReport;
      RMGridReport1.ShowReport ;
      }
    end;
  except
  end;
end;

procedure TfrmLabelList.FormShow(Sender: TObject);
begin
  if fWinStatus = 1 then
    tok.Visible := false;
  InitGrid();
  if Trim(SLBName) <> '' then
  begin
    ADOQueryLabel.Locate('labelCaption', SLBName, []);
  end;
end;

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

procedure TfrmLabelList.InitVarDictionary();
var
  TmpList: Tstrings;
  mm: string;
  i: integer;
begin
  try
    TmpList := TstringList.Create();
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select distinct ItemType from JC_LabelSetItems ');
      sql.Add('where valid=''Y''');
      Open;
      TmpList.Clear;
      while not Eof do
      begin
        TmpList.Add(trim(fieldByName('ItemType').AsString));
        Next;
      end;
    end;
  finally
    TmpList.Free;
  end;
end;
/////////////////////////////////////////////////
  //
/////////////////////////////////////////////////

procedure TfrmLabelList.InitDataSetDictionary();
begin
{  with ADOQuery1 do
  begin
    close;
    sql.Clear ;
    sql.Add('exec P_Get_LabelPrintData');
    sql.Add(quotedStr(''));
    sql.Add(','+quotedStr(''));
    sql.Add(','+quotedStr(''));
    OPen;
  end; }
end;

procedure TfrmLabelList.TaddClick(Sender: TObject);
begin
  try
    frmLabelAdd := TfrmLabelAdd.create(self);
    with frmLabelAdd do
    begin
      if showModal = 1 then
      begin
        fchg := true;
        InitGrid();
      end;
    end;
  finally
    frmLabelAdd.Free;
  end;
end;

procedure TfrmLabelList.TupdClick(Sender: TObject);
begin
  if ADOQueryLabel.IsEmpty then
    exit;
  try
    frmLabelAdd := TfrmLabelAdd.create(self);
    with frmLabelAdd do
    begin
      fKeyNo := ADOQueryLabel.fieldByName('LabelId').AsString;
      fWinstatus := 1;
      if showModal = 1 then
      begin
        fchg := true;
        InitGrid();
      end;
    end;
  finally
    frmLabelAdd.Free;
  end;

end;
/////////////////////////////////////////////
  //
/////////////////////////////////////////////

procedure TfrmLabelList.InitGrid();
begin
  try
    isLoad := false;
    ADOQueryLabel10.DisableControls;
    with ADOQueryLabel10 do
    begin
      close;
      sql.Clear;
      sql.Add('select * from P_Label');
      sql.Add('where valid=''Y''');
      sql.Add(' order by labelCaption');
      Open;
    end;
    SCreateCDS(ADOQueryLabel10, ADOQueryLabel);
    SInitCDSData(ADOQueryLabel10, ADOQueryLabel);
  finally
    ADOQueryLabel10.EnableControls;
    isLoad := true;
    //DoFilter();
    //OpenLabel();
  end;
end;
////////////////////////////////////////////////////////
  //函数功能：打开标签文件
////////////////////////////////////////////////////////

procedure TfrmLabelList.OpenLabel();
begin
  if ADOQueryLabel.IsEmpty then
    exit;
  with RMGridReport1 do
  begin
    LoadFromBlobField(tblobfield(ADOQueryLabel.fieldbyname('labelFile')));
    //Preview :=RMPreview1;
    ShowReport;
  end;
end;

procedure TfrmLabelList.tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin

end;

/////////////////////////////////////////////////////////////

function TfrmLabelList.DeleteData(): Boolean;
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.Add('delete P_Label');
      sql.Add('where labelId=' + ADOQueryLabel.fieldByName('LabelID').asString);

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('标签删除')));
      sql.Add(',' + quotedstr(trim('标签名称：' + trim(ADOQueryLabel.fieldByName('labelCaption').asString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execSql;
    end;
    result := true;
  except
    result := false;
    application.MessageBox('删除失败!', '警告信息', 0);
  end;
end;

procedure TfrmLabelList.TdelClick(Sender: TObject);
begin
  if ADOQueryLabel.IsEmpty then
    exit;
  if application.MessageBox('确定要删除此标签吗？', '警告信息', 1) = 2 then
    exit;
  if DeleteData() then
  begin
    fchg := true;
    InitGrid();
  end;
end;

procedure TfrmLabelList.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Action := caFree;
end;

procedure TfrmLabelList.FormDestroy(Sender: TObject);
begin
  frmLabelList := nil;
end;

procedure TfrmLabelList.TOkClick(Sender: TObject);
begin
  if ADOQueryLabel.IsEmpty then
    exit;
  LBName := Trim(ADOQueryLabel.fieldbyname('labelCaption').AsString);
  ModalResult := 1;
end;

procedure TfrmLabelList.tv1DblClick(Sender: TObject);
begin
  TOk.Click;
end;

procedure TfrmLabelList.ToolButton1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmLabelList.LabelTypeChange(Sender: TObject);
begin
  DoFilter();
end;

procedure TfrmLabelList.tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if isLoad then
    OpenLabel();
end;

end.

