unit U_PlanDR;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB,
  DBClient, ExtCtrls, StdCtrls, BtnEdit, cxButtonEdit, cxTextEdit,
  cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCalendar,
  dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, ComObj,
  dxSkinBasic, 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, dxSkinOffice2016Colorful,
  dxSkinOffice2016Dark, dxSkinOffice2019Black, dxSkinOffice2019Colorful,
  dxSkinOffice2019DarkGray, dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringtime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinTheBezier, dxSkinValentine, dxSkinVisualStudio2013Blue,
  dxSkinVisualStudio2013Dark, dxSkinVisualStudio2013Light, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinWXI, dxSkinXmas2008Blue, dxScrollbarAnnotations;

type
  TfrmPlanDR = class(TForm)
    DS_1: TDataSource;
    CDS_Import: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TBClose: TToolButton;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    OpenDialog1: TOpenDialog;
    CDS_LM: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    v2FactoryName: TcxGridDBColumn;
    v2OrderNo: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOCmd: TADOQuery;
    CDS_1: TClientDataSet;
    Tv1Column1: TcxGridDBColumn;
    CDS_4: TClientDataSet;
    CDS_2: TClientDataSet;
    CDS_3: TClientDataSet;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1P_SpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    procedure initGrid();
    procedure SaveDate();
    { Private declarations }
  public
    FFFID: string;
    fInvoiceFlag: string;
    { Public declarations }
  end;

var
  frmPlanDR: TfrmPlanDR;

implementation

uses
  U_RTFun, U_ZDYHelp, U_CompanySel, U_dataLink;
{$R *.dfm}

procedure TfrmPlanDR.SaveDate();
var
  MaxNo, JMmaxno, JSmaxno: string;
begin

  ADOQueryCmd.Connection.BeginTrans;

  try
     /// 保存主表
    if GetLSNo(ADOQueryTmp, JMmaxno, 'JM', 'Trade_Plan_Main', 3, 1) = false then
      raise Exception.Create('取最大号失败!');

    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from Trade_Plan_Main where MainId=''' + trim(JMmaxno) + '''');
      open;
    end;
    with ADOCmd do
    begin
      if IsEmpty then
      begin
        Append;
        fieldbyname('FillId').Value := trim(DCode);
        fieldbyname('Filler').Value := trim(DName);
        fieldbyname('status').Value := '0';
      end;
//    LRTSetsavedata(ADOCmd, 'Trade_Plan_Main', dxLayoutControl_bzplan, 2);
      fieldbyname('MainId').Value := trim(JMmaxno);
      FieldByName('Orderno').Value := JMmaxno;
      Post;
    end;
   /// 保存子表
    if GetLSNo(ADOQueryTmp, JSmaxno, 'JS', 'Trade_Plan_Sub', 4, 1) = false then
      raise Exception.Create('取子流水号失败!');

    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from Trade_Plan_Sub where MainId=''' + trim(JMmaxno) + '''');
      sql.Add(' and SubId=''' + trim(JSmaxno) + '''');
      open;
    end;
    with ADOCmd do
    begin
      if IsEmpty then
      begin
        Append;

        fieldbyname('OrdQty').Value := '0';
      end;
//    LRTSetsavedata(ADOCmd, 'Trade_Plan_Main', dxLayoutControl_bzplan, 2);
      fieldbyname('C_Code').Value := Trim(CDS_Import.fieldbyname('C_Code').AsString);
      fieldbyname('C_Name').Value := Trim(CDS_Import.fieldbyname('C_Name').AsString);
      fieldbyname('batchNo').Value := Trim(CDS_Import.fieldbyname('BatchNo').AsString);
      fieldbyname('MainId').Value := trim(JMmaxno);
      fieldbyname('SubId').Value := trim(JSmaxno);
      Post;
    end;
    with CDS_Import do
    begin
      first;
      while not eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Trade_Cloth_Inspect where BCIOID=''' + Trim(CDS_Import.fieldbyname('BCIOID').AsString) + '''  ');
          open;
        end;
        if not ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox(PChar(Trim(CDS_Import.fieldbyname('BCIOID').AsString) + '已经存在'), '提示', 0);
          exit;
        end;

        if fieldbyname('CIID').AsString = '' then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, '', 'Trade_Cloth_Inspect', 4, 1) = False then
            raise Exception.create('获取条码失败！');
        end
        else
          MaxNo := CDS_Import.fieldbyname('CIID').AsString;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.add('select * from Trade_Cloth_Inspect where CIID=' + quotedstr(Trim(MaxNo)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.FieldByName('FillId').Value := Trim(DCode);
          ADOQueryCmd.FieldByName('Filler').Value := Trim(DName);
          ADOQueryCmd.FieldByName('status').Value := '0';
        end
        else
        begin
          ADOQueryCmd.edit;
          ADOQueryCmd.FieldByName('EditId').Value := Trim(DCode);
          ADOQueryCmd.FieldByName('Editer').Value := Trim(DName);
          ADOQueryCmd.FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTmp);
        end;
        RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Import, 'Trade_Cloth_Inspect', 0);
        ADOQueryCmd.fieldbyname('CIID').Value := MaxNo;
        ADOQueryCmd.fieldbyname('PCID').Value := MaxNo;
        ADOQueryCmd.FieldByName('Orderno').Value := JMmaxno;
        ADOQueryCmd.FieldByName('mainid').Value := JMmaxno;
        ADOQueryCmd.FieldByName('subid').Value := JSmaxno;
        ADOQueryCmd.fieldbyName('CIIOFlag').value := '未检验';
        ADOQueryCmd.fieldbyName('Coefficient').value := '0';
        ADOQueryCmd.fieldbyName('Yardage').value := '0';
        ADOQueryCmd.fieldbyName('Meter').value := '0';
        ADOQueryCmd.fieldbyName('NetWeight').value := '0';
        ADOQueryCmd.fieldbyName('Tare').value := '0';
        ADOQueryCmd.fieldbyName('GrossWeight').value := '0';
        ADOQueryCmd.fieldbyName('filltime').value := SGetServerDateTime(ADOQueryTmp);

        ADOQueryCmd.Post;
        next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('exec P_BS_Com_UP_AllTable @UPType=''应收款登记'' ');
//      ExecSQL;
//    end;
    application.MessageBox('保存成功！', '提示');
    Modalresult := 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmPlanDR.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select *,'''' as mjxh1 from Trade_Cloth_Inspect where CIID=''' + trim(FFFID) + '''');
//    sql.add(' and isnull(status,''0'')=''0''');
    open;
  end;

  SCreateCDS(ADOQueryTmp, CDS_Import);
  SInitCDSData(ADOQueryTmp, CDS_Import);
end;

procedure TfrmPlanDR.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmPlanDR.FormShow(Sender: TObject);
begin
  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;
  with CDS_1 do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;
  with CDS_2 do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;
  with CDS_3 do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;
  with CDS_4 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 TfrmPlanDR.TSaveClick(Sender: TObject);
begin
  if CDS_Import.Locate('BCIOID', null, []) then
  begin
    application.MessageBox('卷号不能为空', '提示');
    exit;
  end;
//  if CDS_Import.Locate('ROLLLNO', null, []) then
//  begin
//    application.MessageBox('ROLLLNO不能为空', '提示');
//    exit;
//  end;
//  if CDS_Import.Locate('ART', null, []) then
//  begin
//    application.MessageBox('ART不能为空', '提示');
//    exit;
//  end;

  SaveDate();
end;

procedure TfrmPlanDR.v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        Self.CDS_Import.Edit;
        Self.CDS_Import.FieldByName('P_CodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Import.FieldByName('P_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  //self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPlanDR.v1P_SpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'P_Spec';
      flagname := '成品规格';
      if ShowModal = 1 then
      begin
        Self.CDS_Import.Edit;
        Self.CDS_Import.FieldByName('P_Spec').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPlanDR.v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'QtyUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        Self.CDS_Import.Edit;
        Self.CDS_Import.FieldByName('QtyUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPlanDR.FormDestroy(Sender: TObject);
begin
  frmPlanDR := nil;
end;

procedure TfrmPlanDR.Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'InvoiceType';
      flagname := '发票类型';
      if ShowModal = 1 then
      begin
        Self.CDS_Import.Edit;
        Self.CDS_Import.FieldByName('IVType').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPlanDR.ToolButton2Click(Sender: TObject);
begin
  if CDS_Import.IsEmpty then
    exit;
  CDS_Import.delete;
end;

procedure TfrmPlanDR.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(Self.Caption, Tv1, '财务管理');
end;

procedure TfrmPlanDR.ToolButton4Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3, FFID: string;
  Parts, KeyValuePairs: TStringList;
  ColonPos: Integer;
  Key, Value, AData: 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;
  CDS_1.EmptyDataSet;
  CDS_2.EmptyDataSet;
  CDS_3.EmptyDataSet;
  CDS_4.EmptyDataSet;

  Parts := TStringList.Create;
  KeyValuePairs := TStringList.Create;

  AData := WorkBook.WorkSheets[1].Cells[3, 2].Value;
    // 按竖线分割
  ExtractStrings(['|'], [' '], PChar(AData), Parts);

    // 处理每个部分
  for i := 0 to Parts.Count - 1 do
  begin
      // 找到冒号位置
    ColonPos := Pos(':', Trim(Parts[i]));
    if ColonPos > 0 then
    begin
      Key := Trim(Copy(Parts[i], 1, ColonPos - 1));
      Value := Trim(Copy(Parts[i], ColonPos + 1, Length(Parts[i])));
      KeyValuePairs.Values[Key] := Value;
    end;
  end;

  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;
    with CDS_1 do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
    with CDS_2 do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
    with CDS_3 do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
    with CDS_4 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 := 2 to 3 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[6, i].Value) = '' then
        continue;
      t2 := trim(WorkBook.WorkSheets[1].Cells[1, i].Value);
      if CDS_LM.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[6, i].Value), []) then
      begin
        with CDS_LM do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
    for i := 4 to 5 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[6, i].Value) = '' then
        continue;
      t2 := trim(WorkBook.WorkSheets[1].Cells[6, i].Value);
      if CDS_1.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[6, i].Value), []) then
      begin
        t2 := trim(WorkBook.WorkSheets[1].Cells[6, i].Value);
        with CDS_1 do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
    for i := 6 to 7 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[6, i].Value) = '' then
        continue;
      if CDS_2.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[6, i].Value), []) then
      begin
        with CDS_2 do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
    for i := 8 to 9 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[6, i].Value) = '' then
        continue;
      if CDS_3.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[6, i].Value), []) then
      begin
        with CDS_3 do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
    for i := 11 to 12 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[6, i].Value) = '' then
        continue;
      t2 := trim(WorkBook.WorkSheets[1].Cells[1, i].Value);
      if CDS_4.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[6, i].Value), []) then
      begin
        with CDS_4 do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    for i := 7 to ExcelRowCount do
    begin
      with CDS_Import do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if CDS_LM.FieldByName('LXH').AsInteger > 0 then
          begin
            CDS_Import.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
            CDS_Import.FieldByName('C_Name').AsString := KeyValuePairs.Values['ART'];
            CDS_Import.FieldByName('BatchNo').AsString := KeyValuePairs.Values['LOT'];
            CDS_Import.FieldByName('C_Code').AsString := KeyValuePairs.Values['GUID'];
            CDS_Import.FieldByName('Filltime').AsDateTime := StrToDateTimeDef(KeyValuePairs.Values['DATETIME'], Now);
          end;
          CDS_LM.Next;
        end;
        Post;
      end;
    end;
    for i := 7 to ExcelRowCount do
    begin
      with CDS_Import do
      begin
        Append;
        CDS_1.First;
        while not CDS_1.Eof do
        begin
          if CDS_1.FieldByName('LXH').AsInteger > 0 then
          begin
            CDS_Import.fieldbyname(CDS_1.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_1.FieldByName('LXH').AsInteger].Value;
            CDS_Import.FieldByName('C_Name').AsString := KeyValuePairs.Values['ART'];
            CDS_Import.FieldByName('BatchNo').AsString := KeyValuePairs.Values['LOT'];
            CDS_Import.FieldByName('C_Code').AsString := KeyValuePairs.Values['GUID'];
            CDS_Import.FieldByName('Filltime').AsDateTime := StrToDateTimeDef(KeyValuePairs.Values['DATETIME'], Now);
          end;
          CDS_1.Next;
        end;
        Post;
      end;
    end;
    for i := 7 to ExcelRowCount do
    begin
      with CDS_Import do
      begin
        Append;
        CDS_2.First;
        while not CDS_2.Eof do
        begin
          if CDS_2.FieldByName('LXH').AsInteger > 0 then
          begin
            CDS_Import.fieldbyname(CDS_2.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_2.FieldByName('LXH').AsInteger].Value;
            CDS_Import.FieldByName('C_Name').AsString := KeyValuePairs.Values['ART'];
            CDS_Import.FieldByName('BatchNo').AsString := KeyValuePairs.Values['LOT'];
            CDS_Import.FieldByName('C_Code').AsString := KeyValuePairs.Values['GUID'];
            CDS_Import.FieldByName('Filltime').AsDateTime := StrToDateTimeDef(KeyValuePairs.Values['DATETIME'], Now);
          end;
          CDS_2.Next;
        end;
        Post;
      end;
    end;
    for i := 7 to ExcelRowCount do
    begin
      with CDS_Import do
      begin
        Append;
        CDS_3.First;
        while not CDS_3.Eof do
        begin
          if CDS_3.FieldByName('LXH').AsInteger > 0 then
          begin
            CDS_Import.fieldbyname(CDS_3.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_3.FieldByName('LXH').AsInteger].Value;
            CDS_Import.FieldByName('C_Name').AsString := KeyValuePairs.Values['ART'];
            CDS_Import.FieldByName('BatchNo').AsString := KeyValuePairs.Values['LOT'];
            CDS_Import.FieldByName('C_Code').AsString := KeyValuePairs.Values['GUID'];
            CDS_Import.FieldByName('Filltime').AsDateTime := StrToDateTimeDef(KeyValuePairs.Values['DATETIME'], Now);
          end;
          CDS_3.Next;
        end;
        Post;
      end;
    end;
    for i := 7 to ExcelRowCount do
    begin
      with CDS_Import do
      begin
        Append;
        CDS_4.First;
        while not CDS_4.Eof do
        begin
          if CDS_4.FieldByName('LXH').AsInteger > 0 then
          begin
            CDS_Import.fieldbyname(CDS_4.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_4.FieldByName('LXH').AsInteger].Value;
            CDS_Import.FieldByName('C_Name').AsString := KeyValuePairs.Values['ART'];
            CDS_Import.FieldByName('BatchNo').AsString := KeyValuePairs.Values['LOT'];
            CDS_Import.FieldByName('C_Code').AsString := KeyValuePairs.Values['GUID'];
            CDS_Import.FieldByName('Filltime').AsDateTime := StrToDateTimeDef(KeyValuePairs.Values['DATETIME'], Now);
          end;
          CDS_4.Next;
        end;
        Post;
      end;
    end;

    with CDS_Import do
    begin
      First; // 移动到第一条记录
      while not Eof do
      begin
        if Trim(FieldByName('bcioid').AsString) = '' then
        begin
          Edit;
          Delete;
        end
        else
        begin
          Next; // 移动到下一条记录
        end;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    exit;
  end;

  Parts.Free;
  KeyValuePairs.Free;

end;

end.

