unit U_MLXXList;

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, cxLookAndFeels,
  cxNavigator, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter;

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

  TfrmMLXXList = 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;
    MLName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    Label1: TLabel;
    MLBH: TEdit;
    ToolButton4: TToolButton;
    Label2: TLabel;
    DEFstr5: TComboBox;
    v2Column7: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    OpenDialog1: TOpenDialog;
    ToolButton5: TToolButton;
    v2Mrate: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    Tv2Column5: TcxGridDBColumn;
    Tv2Column6: TcxGridDBColumn;
    Tv2Column7: TcxGridDBColumn;
    Tv2Column8: 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 MLNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    fkhType: string;
    dFdDy: array[0..200] of FdDy; //客户端连接数组
  end;

var
  frmMLXXList: TfrmMLXXList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_MLXXinput;

{$R *.dfm}

procedure TfrmMLXXList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select  *');
      sql.Add(' from KH_Zdy_MLXX     ');
      sql.Add(' order by filltime ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMLXXList.FormDestroy(Sender: TObject);
begin
  frmMLXXList := nil;
end;

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

procedure TfrmMLXXList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('产品资料13', Tv2, '客户自定义');
  Close;
end;

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

procedure TfrmMLXXList.FormShow(Sender: TObject);
begin
  ReadCxGrid('产品资料13', Tv2, '客户自定义');
  InitGrid();
end;

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

procedure TfrmMLXXList.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 TfrmMLXXList.MLNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

procedure TfrmMLXXList.ToolButton4Click(Sender: TObject);
begin
  try
    frmMLXXinput := TfrmMLXXinput.Create(Application);
    with frmMLXXinput do
    begin
      FMLID := Trim(Self.CDS_HZ.fieldbyname('MLID').AsString);
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmMLXXinput.Free;
  end;
end;

procedure TfrmMLXXList.ToolButton3Click(Sender: TObject);
begin
  try
    frmMLXXinput := TfrmMLXXinput.Create(Application);
    with frmMLXXinput do
    begin
      FMLID := '';
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmMLXXinput.Free;
  end;
end;

procedure TfrmMLXXList.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 := 'MLName';
        dFdDy[k].FdDysName := '辅料名称';
      end;

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

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

      if trim(excelApp.Cells[1, i].value) = '类型' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'Type';
        dFdDy[k].FdDysName := '类型';
      end;
      if trim(excelApp.Cells[1, i].value) = '供应商' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'GYS';
        dFdDy[k].FdDysName := '供应商';
      end;
      if trim(excelApp.Cells[1, i].value) = '辅料标签' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'MLBH';
        dFdDy[k].FdDysName := '辅料标签';
      end;
      if trim(excelApp.Cells[1, i].value) = '规格' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'MLgg';
        dFdDy[k].FdDysName := '规格';
      end;
      if trim(excelApp.Cells[1, i].value) = '成人用量' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'Adultamount';
        dFdDy[k].FdDysName := '成人用量';
      end;
      if trim(excelApp.Cells[1, i].value) = '儿童用量' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'childamount';
        dFdDy[k].FdDysName := '儿童用量';
      end;
      if trim(excelApp.Cells[1, i].value) = '单价' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'Price';
        dFdDy[k].FdDysName := '单价';
      end;
      if trim(excelApp.Cells[1, i].value) = '合计' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'Total';
        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, 'ML', 'KH_Zdy_MLXX', 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_MLXX 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('MLID').Value := Trim(maxId);
//      ADOQueryCmd.FieldByName('zdyCode').Value := Trim(maxId);
//      ADOQueryCmd.FieldByName('khType').Value := Trim(fkhType);
      ADOQueryCmd.FieldByName('Type').Value := '辅料';
      ADOQueryCmd.FieldByName('Filler').Value := Trim(DName);
      ADOQueryCmd.FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
      ADOQueryCmd.Post;

    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;

end.

