unit ExcelImport_GclBills;
// 导入工程量清单至项目节

interface

uses
  Classes, DetailExcelImport, MCacheTree, sdDB, OExport;

type
  TDEI_GclBills = class(TDetailExcelImport)
  private
    FParentID: Integer;
    FSelectSheets: TList;
    FCacheTree: TGclCacheTree;
    FCurRow: Integer;

    FB_CodeCol: Integer;
    FNameCol: Integer;
    FUnitsCol: Integer;
    FPriceCol: Integer;
    FQuantityCol: Integer;
  protected
    procedure BeginImport; override;
    procedure EndImport; override;

    procedure Import; override;

    procedure ImportSheet(ASheet: TExportWorkSheet);

    procedure WriteNode(ADataSet: TsdDataSet; ANode: TGclCacheNode);
    procedure WriteNodes(ADataSet: TsdDataSet);
  public
    procedure ImportToXmj(const AFileName: string; AParentID: Integer);
  end;

implementation

uses
  Forms, mDataRecord, Controls, ProgressHintFrm, UtilMethods, SysUtils;

{ TDEI_GclBills }

procedure TDEI_GclBills.BeginImport;
begin
  Screen.Cursor := crHourGlass;
  ShowProgressHint('导入Excel数据', 100);

  FCacheTree := TGclCacheTree.Create;
  FCacheTree.NewNodeID := ProjectData.BillsData.GetMaxBillsID + 1;

  ProjectData.DisConnectTree;
  ProjectData.BillsData.DisableEvents;

  FSelectSheets := TList.Create;

  FB_CodeCol := 0;
  FNameCol := 1;
  FUnitsCol := 2;
  FQuantityCol := 3;
  FPriceCol :=  4;
end;

procedure TDEI_GclBills.EndImport;
var
  ParentRec: TsdDataRecord;
begin
  FSelectSheets.Free;

  FCacheTree.Free;

  ProjectData.BillsData.EnableEvents;
  ProjectData.ReConnectTree;

  ParentRec := ProjectData.BillsData.sddBills.FindKey('idxID', FParentID);
  ProjectData.BillsCompileData.sdvBillsCompile.LocateInControl(ParentRec);

  ProjectData.BillsCompileData.CalculateAll;

  CloseProgressHint;
  Screen.Cursor := crDefault;
end;

procedure TDEI_GclBills.Import;
var
  i: Integer;
begin
  {if SelectSheets(FMSExcel, FSelectSheets) then
  begin
    for i := 0 to FSelectSheets.Count - 1 do
    begin
      UpdateProgressHint(Format('导入Excel数据--工作表[%s]', [FMSExcel.SheetNames.Strings[i]]));
      UpdateProgressPosition(0);
      ImportSheet(FSelectSheets.Items[i]);
    end;
  end;}
  ImportSheet(OExport.OpenWorkSheet);
  WriteNodes(ProjectData.BillsData.sddBills);
end;

procedure TDEI_GclBills.ImportSheet(ASheet: TExportWorkSheet);
var
  vRow: TExportRow;
  iPos, iRow: Integer;
  sB_Code, sName: string;
  Node: TGclCacheNode;
begin
  for iRow := 1 to ASheet.Rows.Count do
  begin
    vRow := ASheet.Rows[iRow];
    sB_Code := GetCellTrimStr(vRow, FB_CodeCol);
    sName := GetCellTrimStr(vRow, FNameCol);

    if (sB_Code <> '') or (sName <> '') then
    begin
      Node := FCacheTree.AddNodeByData(sB_Code, sName);
      Node.B_Code := sB_Code;
      Node.Name := sName;
      Node.Units := GetCellTrimStr(vRow, FUnitsCol);
      Node.Price := GetCellFloat(vRow, FPriceCol);
      Node.Quantity := GetCellFloat(vRow, FQuantityCol);
    end;

    iPos := (iRow + 1) * 100 div ASheet.Rows.Count;
    UpdateProgressPosition(iPos);
  end;
end;

procedure TDEI_GclBills.ImportToXmj(const AFileName: string;
  AParentID: Integer);
begin
  FParentID := AParentID;
  ImportFile(AFileName);
end;

procedure TDEI_GclBills.WriteNode(ADataSet: TsdDataSet;
  ANode: TGclCacheNode);
var
  Rec: TBillsRecord;
begin
  if ANode.B_Code <> '' then
    UpdateProgressHint('写入读取的Excel数据 ' + ANode.B_Code)
  else
    UpdateProgressHint('写入读取的Excel数据 ' + ANode.Name);

  Rec := TBillsRecord(ADataSet.Add);
  Rec.ID.AsInteger := ANode.ID;
  if ANode.ParentID = -1 then
    Rec.ParentID.AsInteger := FParentID
  else
    Rec.ParentID.AsInteger := ANode.ParentID;
  Rec.NextSiblingID.AsInteger := ANode.NextSiblingID;
  Rec.B_Code.AsString := ANode.B_Code;
  Rec.Name.AsString := ANode.Name;
  Rec.Units.AsString := ANode.Units;
  Rec.Price.AsFloat := PriceRoundTo(ANode.Price);
  Rec.OrgQuantity.AsFloat := QuantityRoundTo(ANode.Quantity);
end;

procedure TDEI_GclBills.WriteNodes(ADataSet: TsdDataSet);
var
  i, iPos: Integer;
begin
  UpdateProgressHint('写入读取的Excel数据');
  UpdateProgressPosition(0);
  for i := 0 to FCacheTree.CacheNodes.Count - 1 do
  begin
    WriteNode(ADataSet, TGclCacheNode(FCacheTree.CacheNodes[i]));
    iPos := i*100 div FCacheTree.CacheNodes.Count;
    UpdateProgressPosition(iPos);
  end;
  UpdateProgressPosition(100);
end;

end.
