unit U_ZdyAttachCP_HX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  ComObj, Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB,
  ADODB, cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, Menus, cxLookAndFeelPainters, cxButtons, RM_BarCode,
  cxLookAndFeels, cxNavigator;

type
  FdDy = record
    inc: integer; //客户端套接字句柄
    FDdys: string[32]; //客户端套接字
    FdDysName: string[32]; //客户端套接字
  end;

  TfrmZdyAttachCP_HX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    ZdyName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TBADD: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    Label1: TLabel;
    ZdyCode: TEdit;
    TBedit: TToolButton;
    Label2: TLabel;
    DEFstr5: TComboBox;
    v2Column12: TcxGridDBColumn;
    OpenDialog1: TOpenDialog;
    ToolButton5: TToolButton;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    defNote1: TEdit;
    Label4: TLabel;
    v2Column2: TcxGridDBColumn;
    ToolButton6: TToolButton;
    defnote2: TEdit;
    Label5: TLabel;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    Label7: TLabel;
    defnote5: TEdit;
    ToolButton4: TToolButton;
    ComboBox1: TComboBox;
    Label8: TLabel;
    Edit1: TEdit;
    Label9: TLabel;
    v2ssel: TcxGridDBColumn;
    Label10: TLabel;
    defstr2: TEdit;
    RMBarCodeObject1: TRMBarCodeObject;
    v2Column9: TcxGridDBColumn;
    Label11: TLabel;
    DEFNote6: TEdit;
    Label12: TLabel;
    zjm: TEdit;
    v2Column10: TcxGridDBColumn;
    Label6: TLabel;
    DEFStr4: TEdit;
    Tv2Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ZdyNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBeditClick(Sender: TObject);
    procedure TBADDClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    fkhType: string;
    dFdDy: array[0..20] of FdDy; //客户端连接数组
    canshu1: string;
  end;

var
  frmZdyAttachCP_HX: TfrmZdyAttachCP_HX;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZdyAttInputCP_HX, U_FjList_RZ, U_ZdyAttInputXH;

{$R *.dfm}

procedure TfrmZdyAttachCP_HX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select  ATID,ZdyName,ZdyCode,DEFstr1,DEFstr2,defNote1,Mrate,defNote2,defNote5,defNote6,DEFstr3,DEFstr5,DEFstr4,');
      sql.Add(' DEFstr6,DEFstr7,DEFstr8,Note,DEFstr4,DEFstr5,defflt1,Filler,FillTime,  ');
      sql.Add(' zdyNameZ=(select Top 1 zdyNameZ from KH_Zdy_Attachment X where X.zdyName=A.zdyName and Type=''KHName''),');
      sql.Add(' zjm=dbo.getpinyin(ZdyName) ');
      sql.Add(' from KH_Zdy_Attachment A where Type=''CPHX'' ');
   //   sql.Add(' and isnull(khType,'''')='+quotedstr(trim(fkhType)));
      sql.Add(' order by zdyName');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmZdyAttachCP_HX.FormDestroy(Sender: TObject);
begin
  frmZdyAttachCP_HX := nil;
end;

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

procedure TfrmZdyAttachCP_HX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv2, '客户自定义');
  Close;
end;

procedure TfrmZdyAttachCP_HX.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(CDS_HZ.fieldbyname('ATID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete KH_Zdy_Attachment where ATID=''' + Trim(CDS_HZ.fieldbyname('ATID').AsString) + '''');
      sql.Add('delete KH_Zdy where ZdyNo=''' + Trim(CDS_HZ.fieldbyname('ATID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmZdyAttachCP_HX.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption, Tv2, '客户自定义');
  if trim(canshu1) = '查询' then
  begin
    TBADD.Visible := false;
    TBedit.Visible := false;
    TBdel.Visible := false;
  end;
  if trim(canshu1) = '高权限' then
  begin
    Tv2Column1.Visible := true;
  end;
  InitGrid();
end;

procedure TfrmZdyAttachCP_HX.TBRafreshClick(Sender: TObject);
begin

  InitGrid();
end;

procedure TfrmZdyAttachCP_HX.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmZdyAttachCP_HX.ZdyNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmZdyAttachCP_HX.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid2);
//  SelExportData(Tv2,ADOQueryMain,'产品资料列表');
end;

procedure TfrmZdyAttachCP_HX.TBeditClick(Sender: TObject);
begin
  try
    frmZdyAttInputXH := TfrmZdyAttInputXH.Create(Application);
    with frmZdyAttInputXH do
    begin
      FATID := Trim(Self.CDS_HZ.fieldbyname('ATID').AsString);
      fkhType := self.fkhType;
      fflag := 1;
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
        self.CDS_HZ.Locate('ATID', trim(FATID), []);
      end;
    end;
  finally
    frmZdyAttInputXH.Free;
  end;
end;

procedure TfrmZdyAttachCP_HX.TBADDClick(Sender: TObject);
begin
  try
    frmZdyAttInputXH := TfrmZdyAttInputXH.Create(Application);
    with frmZdyAttInputXH do
    begin
      FATID := '';
      fflag := 0;
      fkhType := self.fkhType;
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
        self.CDS_HZ.Locate('ATID', trim(FATID), []);
      end;
    end;
  finally
    frmZdyAttInputXH.Free;
  end;
end;

procedure TfrmZdyAttachCP_HX.ToolButton5Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, ExcelRowCount: integer;
  maxId: string;
begin

  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;

    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    k := 0;
    for i := 1 to 20 do
    begin
      if trim(excelApp.Cells[1, i].value) = '' then
        break;
      if trim(excelApp.Cells[1, i].value) = '产品编号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ZdyCode';
        dFdDy[k].FdDysName := '产品编号';
      end;

      if trim(excelApp.Cells[1, i].value) = '产品名称' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ZdyName';
        dFdDy[k].FdDysName := '产品名称';
      end;

      if trim(excelApp.Cells[1, i].value) = '名称' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'defNote1';
        dFdDy[k].FdDysName := '名称';
      end;

      if trim(excelApp.Cells[1, i].value) = '颜色' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'DEFstr3';
        dFdDy[k].FdDysName := '名称';
      end;

      if trim(excelApp.Cells[1, i].value) = '工艺' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'defNote2';
        dFdDy[k].FdDysName := '工艺';
      end;
      if trim(excelApp.Cells[1, i].value) = '类型' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'DEFstr5';
        dFdDy[k].FdDysName := '类型';
      end;
      if trim(excelApp.Cells[1, i].value) = '规格' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'DEFstr1';
        dFdDy[k].FdDysName := '规格';
      end;
      if trim(excelApp.Cells[1, i].value) = '成份' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'DEFstr2';
        dFdDy[k].FdDysName := '成份';
      end;
      k := k + 1;
    end;
  except
  end;

  ADOQueryCmd.Connection.BeginTrans;
  try
    for i := 2 to ExcelRowCount do
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'AT', 'KH_Zdy_Attachment', 4, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from KH_Zdy_Attachment where 1=2');
        open;
      end;
      ADOQueryCmd.Append;
      for j := 0 to k - 1 do
      begin
        ADOQueryCmd.fieldbyname(dFdDy[j].FDdys).Value := excelApp.Cells[i, dFdDy[j].inc].Value;
      end;
      ADOQueryCmd.FieldByName('ATID').Value := Trim(maxId);
      ADOQueryCmd.FieldByName('Type').Value := 'CP';
      ADOQueryCmd.FieldByName('Filler').Value := Trim(DName);
      ADOQueryCmd.Post;

      with ADOQueryTemp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from KH_Zdy_Attachment where ATID=''' + trim(maxId) + ''' ');
        open;
      end;

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from KH_Zdy where ZdyNo=''' + Trim(maxId) + '''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        if ADOQueryCmd.IsEmpty then
        begin
          Append;
        end
        else
        begin
          Edit;
        end;
        FieldByName('ZdyNo').Value := Trim(maxId);
        FieldByName('ZdyName').Value := Trim(ADOQueryTemp.fieldbyname('ZdyName').AsString);
        FieldByName('Type').Value := 'CP';
        FieldByName('MainType').Value := Trim(ADOQueryTemp.fieldbyname('DEFstr5').AsString);
        Post;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmZdyAttachCP_HX.ToolButton6Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  try
    frmFjList_RZ := TfrmFjList_RZ.Create(Application);
    with frmFjList_RZ do
    begin
     // cxButton1.Visible:=false;
    //  PState:=1;
      if Trim(canshu1) = '查询' then
        cxButton1.Visible := false;
      fkeyNO := Trim(Self.CDS_HZ.fieldbyname('ATID').AsString);
      fzdyCode := Trim(Self.CDS_HZ.fieldbyname('zdyCode').AsString);
      fType := '产品花型';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmFjList_RZ.Free;
  end;
end;

procedure TfrmZdyAttachCP_HX.FormCreate(Sender: TObject);
begin
  canshu1 := trim(DParameters1);
end;

procedure TfrmZdyAttachCP_HX.ToolButton4Click(Sender: TObject);
var
  fPrintFile, FFATID: string;
  DPS, i, j: Integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if CDS_HZ.IsEmpty then
    Exit;
  ToolBar1.SetFocus;
  if trim(ComboBox1.Text) = '' then
  begin
    Application.MessageBox('花型标签不能为空!', '提示', 0);
    exit;
  end;
  if Trim(Edit1.Text) <> '' then
  begin
    if TryStrToInt(Edit1.Text, i) = False then
    begin
      Application.MessageBox('打印份数录入错误!', '提示', 0);
      exit;
    end;
  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(ComboBox1.Text) + '.rmf';
  CDS_HZ.DisableControls;
  DPS := 0;
  FFATID := '';
  i := 1;
  if Trim(Edit1.Text) = '' then
  begin
    j := 1;
  end
  else
  begin
    j := StrToInt(Edit1.Text);
  end;
  with CDS_HZ do
  begin
    First;
    while not Eof do
    begin
      if CDS_HZ.FieldByName('SSel').AsBoolean = True then
      begin
        try
          Moudle := LoadLibrary('MakeQRBarcode.dll');
          @Makebar := GetProcAddress(Moudle, 'Make');
          @Mixtext := GetProcAddress(Moudle, 'MixText');
          Txt := Trim(CDS_HZ.fieldbyname('ATID').AsString);
          fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
          if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
            CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
          if FileExists(fImagePath) then
            DeleteFile(fImagePath);
          Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
        except
          application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
          exit;
        end;
        RMVariables['QRBARCODE'] := fImagePath;
        for i := 1 to j do
        begin
          if FileExists(fPrintFile) then
          begin
            RM1.LoadFromFile(fPrintFile);
            RM1.PrintReport;
          end
          else
          begin
            Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + trim(ComboBox1.Text) + '.rmf'), '提示', 0);
            Exit;
          end;
        end;

        if DPS = 0 then
        begin
          FFATID := Trim(CDS_HZ.fieldbyname('ATID').AsString);
        end;
      end;
      Next;
    end;
  end;
  CDS_HZ.EnableControls;
  CDS_HZ.Locate('ATID', FFATID, []);
  Edit1.Text := '1';

end;

end.

