unit U_KUANNOInfoImport;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  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, cxDropDownEdit, cxPC, U_BaseHelp, cxLookAndFeels,
  cxLookAndFeelPainters,   cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, ComObj, U_BaseList, System.ImageList,
  Vcl.ImgList,  dxScrollbarAnnotations;

type
  TfrmkuannoImport = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    OpenDialog1: TOpenDialog;
    ToolButton5: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1CYNo: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    CDS_LM: TClientDataSet;
    ToolButton3: TToolButton;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
    FCTID: string;
  end;

var
  frmkuannoImport: TfrmkuannoImport;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmkuannoImport.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from Trade_Plan_Sub_MX where mainid=' + quotedstr(fctid));
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmkuannoImport.FormDestroy(Sender: TObject);
begin
  inherited;
  frmkuannoImport := nil;
end;

procedure TfrmkuannoImport.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := cafree;

end;

procedure TfrmkuannoImport.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmkuannoImport.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption, Tv1, '样品管理');
  with CDS_LM do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;
  InitGrid();
end;

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

procedure TfrmkuannoImport.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '样品管理');
end;

procedure TfrmkuannoImport.ToolButton2Click(Sender: TObject);
var
  smaxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_1.DisableControls;
    with CDS_1 do
    begin
      First;
      while not eof do
      begin

        if Trim(CDS_1.fieldbyname('mxid').AsString) = '' then
        begin
          if GetLSNo(ADOQueryCmd, smaxno, 'MX', 'Trade_Plan_Sub_mx', 4, 1) = False then
            raise Exception.Create('取子流水号失败!');
        end
        else
        begin
          smaxno := Trim(CDS_1.fieldbyname('mxid').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Trade_Plan_Sub_mx where MainId=''' + Trim(FCTID) + '''');
          sql.Add(' and MXID=''' + Trim(smaxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_1.fieldbyname('mxid').AsString) = '' then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'Trade_Plan_Sub_MX', 0);

          FieldByName('mxid').Value := Trim(smaxno);
          FieldByName('mainid').Value := Trim(FCTID);

          Post;

        end;

        Next;
      end;
    end;
//    with ADOQueryCmd do
//    begin
//      Close;
//      SQL.Clear;
//      sql.Add('exec P_Plan_Sub_MXDR');
//
//      Open;
//    end;
    Application.MessageBox('保存成功!', '提示', 0);
    ADOQueryCmd.Connection.CommitTrans;
    CDS_1.EnableControls;
  except
    CDS_1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message + '编号' + Trim(CDS_1.fieldbyname('C_Code').AsString)), '提示信息', 0);

  end;
end;

procedure TfrmkuannoImport.ToolButton3Click(Sender: TObject);
begin

  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除选择的数据吗？', '提示', 32 + 4) <> IDYES then
    exit;
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while Locate('SSel', True, []) do
    begin

      if Trim(CDS_1.fieldbyname('MXID').AsString) <> '' then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;

//          sql.Add('insert into Trade_Plan_Sub_Del select * from Trade_Plan_Sub where SubId=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');

          sql.Add('delete Trade_Plan_Sub_MX where MXID=''' + Trim(CDS_1.fieldbyname('MXID').AsString) + '''');

          ExecSQL;
        end;
      end;
      Delete;
    end;
  end;
  CDS_1.EnableControls;

end;

procedure TfrmkuannoImport.ToolButton5Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3, FFID: 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;

  CDS_LM.EmptyDataSet;

  for j := 0 to Tv1.ColumnCount - 1 do
  begin
    with CDS_LM do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
  end;

  try
    for i := 1 to 50 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[1, i].value) = '' then
        continue;
      if CDS_LM.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[1, i].value), []) then
      begin
        with CDS_LM do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    for i := 2 to ExcelRowCount do
    begin
      with CDS_1 do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if CDS_LM.FieldByName('LXH').AsInteger > 0 then
            CDS_1.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
          CDS_LM.Next;
        end;

        FieldByName('drdate').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
        Post;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    application.MessageBox('写入数据异常，导入中断！', '错误信息', MB_ICONERROR);
    exit;
  end;

end;

end.

