StandardBillsDm.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. unit StandardBillsDm;
  2. interface
  3. uses
  4. SysUtils, Classes, ADODB, DB, DBClient, Provider, sdIDTree,
  5. UtilMethods, sdDB, sdProvider;
  6. type
  7. TStandardBillsData = class(TDataModule)
  8. sdpStdBills: TsdADOProvider;
  9. sddStdBills: TsdDataSet;
  10. sdvStdBills: TsdDataView;
  11. private
  12. FBillsTree: TsdIDTree;
  13. procedure CreateBillsTree;
  14. procedure OpenConnetion(AConnection: TADOConnection);
  15. procedure ExpandFirstNode;
  16. public
  17. constructor Create(AOwner: TComponent); override;
  18. destructor Destroy; override;
  19. procedure Open(AConnection: TADOConnection);
  20. procedure CalcCacheData(ANode:TsdIDTreeNode; AOrder: Integer; AParentPath: String);
  21. procedure RecursiveExportBillsJson(const AFileName: string);
  22. property BillsTree: TsdIDTree read FBillsTree;
  23. end;
  24. implementation
  25. {$R *.dfm}
  26. { TStandardBillsData }
  27. procedure TStandardBillsData.CalcCacheData(ANode: TsdIDTreeNode;
  28. AOrder: Integer; AParentPath: String);
  29. var
  30. sNodePath: string;
  31. begin
  32. if not Assigned(ANode) then Exit;
  33. ANode.Rec.ValueByName('BillsLevel').AsInteger := ANode.Level + 1;
  34. ANode.Rec.ValueByName('BillsOrder').AsInteger := AOrder;
  35. if AParentPath = '' then
  36. sNodePath := IntToStr(ANode.ID)
  37. else
  38. sNodePath := AParentPath + '.' + IntToStr(ANode.ID);
  39. ANode.Rec.ValueByName('FullPath').AsString := sNodePath;
  40. ANode.Rec.ValueByName('IsLeaf').AsBoolean := not ANode.HasChildren;
  41. CalcCacheData(ANode.FirstChild, 1, sNodePath);
  42. CalcCacheData(ANode.NextSibling, AOrder + 1, AParentPath);
  43. end;
  44. constructor TStandardBillsData.Create(AOwner: TComponent);
  45. begin
  46. inherited;
  47. CreateBillsTree;
  48. end;
  49. procedure TStandardBillsData.CreateBillsTree;
  50. begin
  51. FBillsTree := TsdIDTree.Create;
  52. FBillsTree.KeyFieldName := 'ID';
  53. FBillsTree.ParentFieldName := 'ParentID';
  54. FBillsTree.NextSiblingFieldName := 'NextID';
  55. FBillsTree.AutoExpand := False;
  56. end;
  57. destructor TStandardBillsData.Destroy;
  58. begin
  59. FBillsTree.Free;
  60. inherited;
  61. end;
  62. procedure TStandardBillsData.ExpandFirstNode;
  63. begin
  64. if FBillsTree.FirstNode <> nil then
  65. FBillsTree.FirstNode.Expand;
  66. end;
  67. procedure TStandardBillsData.Open(AConnection: TADOConnection);
  68. begin
  69. OpenConnetion(AConnection);
  70. FBillsTree.DataView := sdvStdBills;
  71. ExpandFirstNode;
  72. end;
  73. procedure TStandardBillsData.OpenConnetion(AConnection: TADOConnection);
  74. begin
  75. sdpStdBills.Connection := AConnection;
  76. sdpStdBills.TableName := 'BillsTree';
  77. sddStdBills.Open;
  78. sdvStdBills.Open;
  79. sddStdBills.AddIndex('idxID', 'ID');
  80. sddStdBills.AddIndex('sdxCode', 'Code');
  81. sddStdBills.AddIndex('sddBCode', 'B_Code');
  82. end;
  83. procedure TStandardBillsData.RecursiveExportBillsJson(
  84. const AFileName: string);
  85. var
  86. sgs: TStrings;
  87. function GetNodeData(ANode: TsdIDTreeNode; AOrder: Integer; AFullPath: string): string;
  88. const
  89. sBillsJson = '"id": %d, "pid": %d, "order": %d, "full_path": "%s", "level": %d, "is_leaf" : %d, ' +
  90. '"code": "%s", "b_code": "%s", "name": "%s", "unit": "%s"';
  91. begin
  92. Result := Format(sBillsJson, [ANode.ID, ANode.Rec.ValueByName('ParentID').AsInteger, AOrder, AFullPath,
  93. ANode.Rec.ValueByName('BillsLevel').AsInteger, Integer(not ANode.HasChildren),
  94. ANode.Rec.ValueByName('Code').AsString, ANode.Rec.ValueByName('B_Code').AsString,
  95. ANode.Rec.ValueByName('Name').AsString, ANode.Rec.ValueByName('Unit').AsString]);
  96. end;
  97. procedure ExportNode(ANode: TsdIDTreeNode; AOrder: Integer; AParentPath: string);
  98. var
  99. sNodePath: string;
  100. begin
  101. if not Assigned(ANode) then Exit;
  102. if AParentPath = '' then
  103. sNodePath := IntToStr(ANode.ID)
  104. else
  105. sNodePath := AParentPath + '.' + IntToStr(ANode.ID);
  106. sgs.Strings[sgs.Count - 1] := sgs.Strings[sgs.Count - 1] + '{';
  107. sgs.Add(Format(' %s', [AnsiToUtf8(GetNodeData(ANode, AOrder, sNodePath))]));
  108. sgs.Add('}');
  109. if Assigned(ANode.NextNode) then
  110. sgs.Strings[sgs.Count - 1] := sgs.Strings[sgs.Count - 1] + ',';
  111. ExportNode(ANode.FirstChild, 1, sNodePath);
  112. ExportNode(ANode.NextSibling, AOrder + 1, AParentPath);
  113. end;
  114. begin
  115. sgs := TStringList.Create;
  116. try
  117. sgs.Add('[');
  118. ExportNode(FBillsTree.FirstNode, 1, '');
  119. sgs.Strings[sgs.Count - 1] := sgs.Strings[sgs.Count - 1] + ']';
  120. sgs.SaveToFile(AFileName);
  121. finally
  122. sgs.Free;
  123. end;
  124. end;
  125. end.