unit rpgBillsDm;

interface

uses
  SysUtils, Classes, sdDB, sdProvider, ADODB, ProjGatherTree;

type
  TrpgBillsData = class(TDataModule)
    sdpBills: TsdADOProvider;
    sddBills: TsdDataSet;
  private
    procedure SaveBillsTreeNode(ANode: TProjGatherTreeNode);
    procedure SaveBillsTree(ATree: TProjGatherTree);
  public
    constructor Create(AConnection: TADOConnection);

    procedure SaveDataTo(ATree: TProjGatherTree; const ATableName: string);
  end;

implementation

uses CacheTree;

{$R *.dfm}

{ TrpgBillsData }

constructor TrpgBillsData.Create(AConnection: TADOConnection);
begin
  inherited Create(nil);
  sdpBills.Connection := AConnection;
end;

procedure TrpgBillsData.SaveBillsTree(ATree: TProjGatherTree);
var
  iNode: Integer;
begin
  for iNode := 0 to ATree.CacheNodes.Count - 1 do
    SaveBillsTreeNode(TProjGatherTreeNode(ATree.CacheNodes.Items[iNode]));
end;

procedure TrpgBillsData.SaveBillsTreeNode(ANode: TProjGatherTreeNode);
var
  Rec: TsdDataRecord;
begin
  Rec := sddBills.Add;
  Rec.ValueByName('ID').AsInteger := ANode.ID;
  Rec.ValueByName('ParentID').AsInteger := ANode.ParentID;
  Rec.ValueByName('NextSiblingID').AsInteger := ANode.NextSiblingID;
  Rec.ValueByName('Code').AsString := ANode.Code;
  Rec.ValueByName('B_Code').AsString := ANode.B_Code;
  Rec.ValueByName('Name').AsString := ANode.Name;
  Rec.ValueByName('Units').AsString := ANode.Units;
  Rec.ValueByName('Price').AsFloat := ANode.Price;

  Rec.ValueByName('SerialNo').AsInteger := ANode.SerialNo;
  Rec.ValueByName('IsLeaf').AsBoolean := ANode.Children.Count = 0;
  Rec.ValueByName('XiangCode').AsString := ANode.XiangCode;
  Rec.ValueByName('MuCode').AsString := ANode.MuCode;
  Rec.ValueByName('JieCode').AsString := ANode.JieCode;
  Rec.ValueByName('XimuCode').AsString := ANode.XimuCode;
  Rec.ValueByName('IndexCode').AsString := ANode.IndexCode;
  Rec.ValueByName('ChapterParentID').AsInteger := ANode.ChapterParentID;
  Rec.ValueByName('B_CodeChapter').AsInteger := ANode.B_CodeChapter;
end;

procedure TrpgBillsData.SaveDataTo(ATree: TProjGatherTree; const ATableName: string);
begin
  sdpBills.TableName := ATableName;
  sddBills.Open;
  sddBills.BeginUpdate;
  try
    SaveBillsTree(ATree);
  finally
    sddBills.EndUpdate;
    sddBills.Save;
  end;
end;

end.
