MCacheTree.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. unit MCacheTree;
  2. // CacheTree For Measure, Inherit From CacheTree
  3. interface
  4. uses
  5. Classes, CacheTree, Math, ZhAPI, sdIDTree;
  6. type
  7. // For Import Temp Excel
  8. TBillsCacheNode = class(TCacheNode)
  9. private
  10. FLevelCode: string;
  11. FCode: string;
  12. FB_Code: string;
  13. FName: string;
  14. FUnits: string;
  15. FCanDelete: Boolean;
  16. FQuantity: Double;
  17. FDgnQuantity1: Double;
  18. FDgnQuantity2: Double;
  19. FMemoStr: string;
  20. FPrice: Double;
  21. FDrawingCode: string;
  22. public
  23. property LevelCode: string read FLevelCode write FLevelCode;
  24. property Code: string read FCode write FCode;
  25. property B_Code: string read FB_Code write FB_Code;
  26. property Name: string read FName write FName;
  27. property Units: string read FUnits write FUnits;
  28. property CanDelete: Boolean read FCanDelete write FCanDelete;
  29. property Price: Double read FPrice write FPrice;
  30. property Quantity: Double read FQuantity write FQuantity;
  31. property DgnQuantity1: Double read FDgnQuantity1 write FDgnQuantity1;
  32. property DgnQuantity2: Double read FDgnQuantity2 write FDgnQuantity2;
  33. property DrawingCode: string read FDrawingCode write FDrawingCode;
  34. property MemoStr: string read FMemoStr write FMemoStr;
  35. end;
  36. TBillsCacheTree = class(TCacheTree)
  37. private
  38. FLastNode: TCacheNode;
  39. FSeparateChar: Char;
  40. FAutoSort: Boolean;
  41. function GetNewNode(AID: Integer = -1): TBillsCacheNode; overload;
  42. function FindNode(const ACode: string): TBillsCacheNode; overload;
  43. function FindNode(AParent: TBillsCacheNode; const ACode: string): TBillsCacheNode; overload;
  44. function FindParent(const ACode: string): TBillsCacheNode;
  45. function FindNextSibling(const ACode: string): TBillsCacheNode;
  46. procedure SetSeparateChar(const Value: Char);
  47. public
  48. function AddNode(AParent: TCacheNode; ANextSibling: TCacheNode = nil; AFixedID: Integer = -1): TBillsCacheNode;
  49. function AddNodeByCode(const ACode: string; AFixedID: Integer = -1): TBillsCacheNode;
  50. function AddLeafBillsNode(const AB_Code: string): TBillsCacheNode;
  51. // Only for Debugging lot of Data
  52. procedure SaveTreeToFile(const AFileName: string);
  53. property SeparateChar: Char read FSeparateChar write SetSeparateChar;
  54. property AutoSort: Boolean read FAutoSort write FAutoSort;
  55. end;
  56. // 此树仅用于导入工程量清单,禁止作为它用
  57. // 如须使用应遵守以下两点:1.完全清楚相关的导入方法类及需求 2.派生子类。
  58. TGclCacheNode = class(TCacheNode)
  59. private
  60. FB_Code: string;
  61. FName: string;
  62. FUnits: string;
  63. FPrice: Double;
  64. FQuantity: Double;
  65. public
  66. property B_Code: string read FB_Code write FB_Code;
  67. property Name: string read FName write FName;
  68. property Units: string read FUnits write FUnits;
  69. property Price: Double read FPrice write FPrice;
  70. property Quantity: Double read FQuantity write FQuantity;
  71. end;
  72. TGclCacheTree = class(TCacheTree)
  73. private
  74. FLastBlank1: TGclCacheNode;
  75. FLastNode: TGclCacheNode;
  76. function AddNodeByName(const AName: string): TGclCacheNode;
  77. function AddNodeByB_Code(const AB_Code: string): TGclCacheNode;
  78. protected
  79. function GetNewNode: TCacheNode; override;
  80. public
  81. function AddNodeByData(const AB_Code, AName: string): TGclCacheNode;
  82. procedure SaveTreeToFile(const AFileName: string);
  83. end;
  84. {For Report Memory(Gather or Compare Projects)}
  85. TDoubleArray = array of Double;
  86. TReportCacheNode = class(TCacheNode)
  87. private
  88. FCode: string;
  89. FB_Code: string;
  90. FName: string;
  91. FUnits: string;
  92. FMemoStr: string;
  93. FXiangCode: string;
  94. FMuCode: string;
  95. FJieCode: string;
  96. FXiMuCode: string;
  97. FPrice: Double;
  98. FQuantity: Double;
  99. FTotalPrice: Double;
  100. FRatioPercent: Double; // 0号台账 - 各项费用所占比例
  101. FDesignQuantity1: Double;
  102. FDesignQuantity2: Double;
  103. FAddQcQuantity: Double;
  104. FAddPcTotalPrice: Double;
  105. FAddQcTotalPrice: Double;
  106. FAddDealQuantity: Double;
  107. FAddDealTotalPrice: Double;
  108. FAddPcQuantity: Double;
  109. FAddRatioPercent: Double; // 决算 - 各项费用所占比例
  110. FDealDesignQuantity1: Double;
  111. FDealDesignQuantity2: Double;
  112. FCDesignQuantity1: Double;
  113. FCDesignQuantity2: Double;
  114. FPDQuantity: Double;
  115. FPDTotalPrice: Double;
  116. FPDDesignQuantity1: Double;
  117. FPDDesignQuantity2: Double;
  118. FPDDesignPrice: Double;
  119. FCDDQuantity: Double;
  120. FCDDTotalPrice: Double;
  121. FCDDDesignQuantity1: Double;
  122. FCDDDesignQuantity2: Double;
  123. FCDDDesignPrice: Double;
  124. FABTotalPrice: Double;
  125. FABQuantity: Double;
  126. FABDesignQuantity1: Double;
  127. FABDesignQuantity2: Double;
  128. FABDesignPrice: Double;
  129. FProjectCount: Integer;
  130. FP_TotalPrice: TDoubleArray;
  131. FP_Quantity: TDoubleArray;
  132. FP_Price: TDoubleArray;
  133. FP_DgnQuantity1: TDoubleArray;
  134. FP_DgnQuantity2: TDoubleArray;
  135. procedure ResolveCode;
  136. function GetDoubleArrayTotal(ADoubleArray: TDoubleArray): Double;
  137. procedure SetCode(const Value: string);
  138. function GetGatherP_TotalPrice: Double;
  139. function GetAddGatherQuantity: Double;
  140. function GetAddGatherTotalPrice: Double;
  141. public
  142. constructor Create(ACacheTree: TCacheTree; AID, AProjectCount: Integer);
  143. property Code: string read FCode write SetCode;
  144. property B_Code: string read FB_Code write FB_Code;
  145. property Name: string read FName write FName;
  146. property Units: string read FUnits write FUnits;
  147. property MemoStr: string read FMemoStr write FMemoStr;
  148. property XiangCode: string read FXiangCode;
  149. property MuCode: string read FMuCode;
  150. property JieCode: string read FJieCode;
  151. property XiMuCode: string read FXiMuCode;
  152. // 用于汇总多个项目的合同、变更(数量、金额)
  153. property Price: Double read FPrice write FPrice;
  154. // 0号台账合同
  155. property Quantity: Double read FQuantity write FQuantity;
  156. property TotalPrice: Double read FTotalPrice write FTotalPrice;
  157. property RatioPercent: Double read FRatioPercent write FRatioPercent;
  158. property DesignQuantity1: Double read FDesignQuantity1 write FDesignQuantity1;
  159. property DesignQuantity2: Double read FDesignQuantity2 write FDesignQuantity2;
  160. // 累计各值
  161. property AddDealQuantity: Double read FAddDealQuantity write FAddDealQuantity;
  162. property AddDealTotalPrice: Double read FAddDealTotalPrice write FAddDealTotalPrice;
  163. property AddQcQuantity: Double read FAddQcQuantity write FAddQcQuantity;
  164. property AddQcTotalPrice: Double read FAddQcTotalPrice write FAddQcTotalPrice;
  165. property AddPcQuantity: Double read FAddPcQuantity write FAddPcQuantity;
  166. property AddPcTotalPrice: Double read FAddPcTotalPrice write FAddPcTotalPrice;
  167. property AddGatherQuantity: Double read GetAddGatherQuantity;
  168. property AddGatherTotalPrice: Double read GetAddGatherTotalPrice;
  169. property AddRatioPercent: Double read FAddRatioPercent write FAddRatioPercent;
  170. // 合同&变更 设计数量
  171. property DealDesignQuantity1: Double read FDealDesignQuantity1 write FDealDesignQuantity1;
  172. property DealDesignQuantity2: Double read FDealDesignQuantity2 write FDealDesignQuantity2;
  173. property CDesignQuantity1: Double read FCDesignQuantity1 write FCDesignQuantity1;
  174. property CDesignQuantity2: Double read FCDesignQuantity2 write FCDesignQuantity2;
  175. // ----仅用于汇总生成决算02表----
  176. // 初步设计 Preliminary Design
  177. property PDQuantity: Double read FPDQuantity write FPDQuantity;
  178. property PDTotalPrice: Double read FPDTotalPrice write FPDTotalPrice;
  179. property PDDesignQuantity1: Double read FPDDesignQuantity1 write FPDDesignQuantity1;
  180. property PDDesignQuantity2: Double read FPDDesignQuantity2 write FPDDesignQuantity2;
  181. property PDDesignPrice: Double read FPDDesignPrice write FPDDesignPrice;
  182. // 施工图设计 Construction Drawing Design
  183. property CDDQuantity: Double read FCDDQuantity write FCDDQuantity;
  184. property CDDTotalPrice: Double read FCDDTotalPrice write FCDDTotalPrice;
  185. property CDDDesignQuantity1: Double read FCDDDesignQuantity1 write FCDDDesignQuantity1;
  186. property CDDDesignQuantity2: Double read FCDDDesignQuantity2 write FCDDDesignQuantity2;
  187. property CDDDesignPrice: Double read FCDDDesignPrice write FCDDDesignPrice;
  188. // ------------------------------
  189. // ----仅用于汇总生成决算02表(部颁)----
  190. // 批准概(预算)算 Approved Budget
  191. property ABQuantity: Double read FABQuantity write FABQuantity;
  192. property ABTotalPrice: Double read FABTotalPrice write FABTotalPrice;
  193. property ABDesignQuantity1: Double read FABDesignQuantity1 write FABDesignQuantity1;
  194. property ABDesignQuantity2: Double read FABDesignQuantity2 write FABDesignQuantity2;
  195. property ABDesignPrice: Double read FABDesignPrice write FABDesignPrice;
  196. // ------------------------------------
  197. // 用于记录多个项目的数量、单价、金额、设计数量
  198. property P_Price: TDoubleArray read FP_Price write FP_Price;
  199. property P_Quantity: TDoubleArray read FP_Quantity write FP_Quantity;
  200. property P_TotalPrice: TDoubleArray read FP_TotalPrice write FP_TotalPrice;
  201. property P_DgnQuantity1: TDoubleArray read FP_DgnQuantity1 write FP_DgnQuantity1;
  202. property P_DgnQuantity2: TDoubleArray read FP_DgnQuantity2 write FP_DgnQuantity2;
  203. property GatherP_TotalPrice: Double read GetGatherP_TotalPrice;
  204. property ProjectCount: Integer read FProjectCount;
  205. end;
  206. TStringArray = array of string;
  207. TReportCacheTree = class(TCacheTree)
  208. private
  209. FProjectCount: Integer;
  210. FGatherCacheNode: TReportCacheNode;
  211. FProjectName: TStringArray;
  212. function GetNewNode(AProjectCount: Integer): TReportCacheNode; overload;
  213. public
  214. constructor Create(AProjectCount: Integer);
  215. destructor Destroy; override;
  216. function AddNode(AParent: TCacheNode; ANextSibling: TCacheNode = nil): TReportCacheNode;
  217. function FindNextSibling(AParent: TCacheNode; ACode, AB_Code: string): TReportCacheNode;
  218. function FindNode(AParent: TCacheNode; ACode, AB_Code: string): TReportCacheNode; overload;
  219. function FindNode(AParent: TCacheNode; AName: string): TReportCacheNode; overload;
  220. function FindNode(AParent: TCacheNode; ACode, AB_Code, AName: string): TReportCacheNode; overload;
  221. procedure ReCalcGatherData;
  222. // 调用此方法先须先调用ReCalcGatherData
  223. // RatioPercent = 金额/总金额,这里的总金额取GatherCacheNode的金额,故须先汇总计算GatherCacheNode。
  224. procedure ReCalcRatioPercent;
  225. // Only for Debugging lot of Data
  226. procedure SaveTreeToFile(const AFileName: string);
  227. property ProjectCount: Integer read FProjectCount;
  228. property GatherCacheNode: TReportCacheNode read FGatherCacheNode;
  229. property ProjectName: TStringArray read FProjectName write FProjectName;
  230. end;
  231. TapDoubleArray = array [1..50] of Double;
  232. TAllPhaseCacheNode = class(TCacheNode)
  233. private
  234. FCode: string;
  235. FB_Code: string;
  236. FName: string;
  237. FUnits: string;
  238. FPrice: Double;
  239. FQuantity: Double;
  240. FTotalPrice: Double;
  241. FMemoStr: string;
  242. public
  243. FP_Quantity: TapDoubleArray;
  244. FP_TotalPrice: TapDoubleArray;
  245. property Code: string read FCode write FCode;
  246. property B_Code: string read FB_Code write FB_Code;
  247. property Name: string read FName write FName;
  248. property Units: string read FUnits write FUnits;
  249. property Price: Double read FPrice write FPrice;
  250. property Quantity: Double read FQuantity write FQuantity;
  251. property TotalPrice: Double read FTotalPrice write FTotalPrice;
  252. property MemoStr: string read FMemoStr write FMemoStr;
  253. end;
  254. // 仅用于汇总同一项目的不同期数据
  255. TAllPhaseCacheTree = class(TCacheTree)
  256. private
  257. function GetNewNode(AID: Integer): TAllPhaseCacheNode;
  258. public
  259. function AddNode(AID: Integer; AParent: TCacheNode; ANextSibling: TCacheNode = nil): TAllPhaseCacheNode;
  260. function FindNode(AID: Integer): TAllPhaseCacheNode;
  261. // Only for Debugging lot of Data
  262. procedure SaveTreeToFile(const AFileName: string);
  263. end;
  264. implementation
  265. uses
  266. SysUtils, UtilMethods;
  267. { TBillsCacheTree }
  268. function TBillsCacheTree.AddNodeByCode(const ACode: string;
  269. AFixedID: Integer): TBillsCacheNode;
  270. var
  271. Parent, NextSibling: TBillsCacheNode;
  272. begin
  273. Result := FindNode(ACode);
  274. FLastNode := Result;
  275. if Assigned(Result) then Exit;
  276. Parent := FindParent(ACode);
  277. if AutoSort then
  278. NextSibling := FindNextSibling(ACode)
  279. else
  280. NextSibling := nil;
  281. Result := AddNode(Parent, NextSibling, AFixedID);
  282. Result.FLevelCode := ACode;
  283. FLastNode := Result;
  284. end;
  285. function TBillsCacheTree.FindNode(const ACode: string): TBillsCacheNode;
  286. begin
  287. Result := FindNode(TBillsCacheNode(Root), ACode);
  288. end;
  289. function TBillsCacheTree.FindNextSibling(
  290. const ACode: string): TBillsCacheNode;
  291. var
  292. Parent, Node: TBillsCacheNode;
  293. sCodeID, sCodeID2: string;
  294. begin
  295. Parent := FindParent(ACode);
  296. if Assigned(Parent) then
  297. Node := TBillsCacheNode(Parent.FirstChild)
  298. else
  299. Node := TBillsCacheNode(Root.FirstChild);
  300. Result := nil;
  301. sCodeID := ConvertDigitCode(ACode, 3, '-');
  302. while Assigned(Node) do
  303. begin
  304. sCodeID2 := ConvertDigitCode(Node.LevelCode, 3, SeparateChar);
  305. if sCodeID < sCodeID2 then
  306. begin
  307. Result := Node;
  308. Break;
  309. end;
  310. Node := TBillsCacheNode(Node.NextSibling);
  311. end;
  312. end;
  313. function TBillsCacheTree.FindNode(AParent: TBillsCacheNode;
  314. const ACode: string): TBillsCacheNode;
  315. begin
  316. Result := TBillsCacheNode(AParent.FirstChild);
  317. while Assigned(Result) do
  318. begin
  319. if Result.LevelCode = ACode then
  320. Break
  321. else if Pos(Result.LevelCode + SeparateChar, ACode) = 1 then
  322. begin
  323. Result := FindNode(Result, ACode);
  324. Break;
  325. end
  326. else
  327. Result := TBillsCacheNode(Result.NextSibling);
  328. end;
  329. end;
  330. function TBillsCacheTree.FindParent(const ACode: string): TBillsCacheNode;
  331. var
  332. sCode: string;
  333. begin
  334. Result := nil;
  335. sCode := GetPrefixOfCode(ACode, SeparateChar);
  336. while (Result = nil) and (sCode <> '') do
  337. begin
  338. Result := FindNode(sCode);
  339. sCode := GetPrefixOfCode(sCode, SeparateChar);
  340. end;
  341. end;
  342. function TBillsCacheTree.GetNewNode(AID: Integer): TBillsCacheNode;
  343. begin
  344. if AID = -1 then
  345. Result := TBillsCacheNode.Create(Self, GetNewNodeID)
  346. else
  347. Result := TBillsCacheNode.Create(Self, AID);
  348. CacheNodes.Add(Result);
  349. end;
  350. function TBillsCacheTree.AddNode(AParent, ANextSibling: TCacheNode;
  351. AFixedID: Integer): TBillsCacheNode;
  352. begin
  353. Result := GetNewNode(AFixedID);
  354. if Assigned(ANextSibling) then
  355. ANextSibling.InsertPreSibling(Result)
  356. else if Assigned(AParent) then
  357. AParent.InsertChild(Result)
  358. else
  359. Root.InsertChild(Result);
  360. end;
  361. function TBillsCacheTree.AddLeafBillsNode(const AB_Code: string): TBillsCacheNode;
  362. function GetLastXmjParent: TBillsCacheNode;
  363. begin
  364. Result := TBillsCacheNode(FLastNode);
  365. while Assigned(Result) and (Result.B_Code <> '') do
  366. Result := TBillsCacheNode(Result.Parent);
  367. end;
  368. var
  369. Parent: TBillsCacheNode;
  370. begin
  371. Parent := GetLastXmjParent;
  372. Result := AddNodeByCode(Parent.Code + '-' + AB_Code, -1);
  373. end;
  374. procedure TBillsCacheTree.SetSeparateChar(const Value: Char);
  375. var
  376. I: Integer;
  377. Node: TBillsCacheNode;
  378. begin
  379. for I := 0 to CacheNodes.Count - 1 do
  380. begin
  381. Node := TBillsCacheNode(CacheNodes.Items[I]);
  382. Node.FLevelCode := StringReplace(Node.FLevelCode, FSeparateChar, Value, [rfReplaceAll]);
  383. end;
  384. FSeparateChar := Value;
  385. end;
  386. procedure TBillsCacheTree.SaveTreeToFile(const AFileName: string);
  387. var
  388. sgs: TStringList;
  389. I: Integer;
  390. Node: TBillsCacheNode;
  391. begin
  392. sgs := TStringList.Create;
  393. try
  394. for I := 0 to CacheNodes.Count - 1 do
  395. begin
  396. Node := TBillsCacheNode(CacheNodes.Items[I]);
  397. sgs.Add(Format('ID: %3d; ParentID: %3d; NextID: %3d; Code: %s; B_Code: %s; Name: %s;',
  398. [Node.ID, Node.ParentID, Node.NextSiblingID, Node.Code, Node.B_Code, Node.Name]));
  399. end;
  400. sgs.SaveToFile(AFileName);
  401. finally
  402. sgs.Free;
  403. end;
  404. end;
  405. { TReportCacheNode }
  406. constructor TReportCacheNode.Create(ACacheTree: TCacheTree; AID,
  407. AProjectCount: Integer);
  408. begin
  409. inherited Create(ACacheTree, AID);
  410. FProjectCount := AProjectCount;
  411. SetLength(FP_Quantity, AProjectCount);
  412. SetLength(FP_Price, AProjectCount);
  413. SetLength(FP_TotalPrice, AProjectCount);
  414. SetLength(FP_DgnQuantity1, AProjectCount);
  415. SetLength(FP_DgnQuantity2, AProjectCount);
  416. end;
  417. function TReportCacheNode.GetAddGatherQuantity: Double;
  418. begin
  419. Result := AddDealQuantity + AddQcQuantity;
  420. end;
  421. function TReportCacheNode.GetAddGatherTotalPrice: Double;
  422. begin
  423. Result := AddDealTotalPrice + AddQcTotalPrice + AddPcTotalPrice;
  424. end;
  425. function TReportCacheNode.GetDoubleArrayTotal(
  426. ADoubleArray: TDoubleArray): Double;
  427. var
  428. i: Integer;
  429. begin
  430. Result := 0;
  431. for i := Low(ADoubleArray) to High(ADoubleArray) do
  432. Result := Result + ADoubleArray[i];
  433. end;
  434. function TReportCacheNode.GetGatherP_TotalPrice: Double;
  435. begin
  436. Result := GetDoubleArrayTotal(FP_TotalPrice);
  437. end;
  438. procedure TReportCacheNode.ResolveCode;
  439. var
  440. sgs: TStrings;
  441. i: Integer;
  442. begin
  443. sgs := TStringList.Create;
  444. try
  445. sgs.Delimiter := '-';
  446. sgs.DelimitedText := FCode;
  447. FXiangCode := '';
  448. FMuCode := '';
  449. FJieCode := '';
  450. FXiMuCode := '';
  451. case sgs.Count of
  452. 1: FXiangCode := '';
  453. 2: FXiangCode := ChinessNum(StrToIntDef(sgs[1], 0));
  454. 3: FMuCode := sgs[2];
  455. 4: FJieCode := sgs[3];
  456. else
  457. begin
  458. for i := 4 to sgs.Count - 1 do
  459. if FXiMuCode = '' then
  460. FXiMuCode := sgs[i]
  461. else
  462. FXiMuCode := FXiMuCode + '-' + sgs[i];
  463. end;
  464. end;
  465. finally
  466. sgs.Free;
  467. end;
  468. end;
  469. procedure TReportCacheNode.SetCode(const Value: string);
  470. begin
  471. FCode := Value;
  472. ResolveCode;
  473. end;
  474. { TReportCacheTree }
  475. function TReportCacheTree.AddNode(AParent,
  476. ANextSibling: TCacheNode): TReportCacheNode;
  477. begin
  478. Result := GetNewNode(FProjectCount);
  479. if Assigned(ANextSibling) then
  480. ANextSibling.InsertPreSibling(Result)
  481. else if Assigned(AParent) then
  482. AParent.InsertChild(Result)
  483. else
  484. Root.InsertChild(Result);
  485. end;
  486. constructor TReportCacheTree.Create(AProjectCount: Integer);
  487. begin
  488. inherited Create;
  489. FProjectCount := AProjectCount;
  490. FGatherCacheNode := TReportCacheNode.Create(nil, -2, AProjectCount);
  491. SetLength(FProjectName, AProjectCount);
  492. end;
  493. destructor TReportCacheTree.Destroy;
  494. begin
  495. FGatherCacheNode.Free;
  496. inherited;
  497. end;
  498. function TReportCacheTree.FindNextSibling(AParent: TCacheNode; ACode,
  499. AB_Code: string): TReportCacheNode;
  500. var
  501. Node: TReportCacheNode;
  502. sCodeID, sCodeID2, sB_CodeID, sB_CodeID2: string;
  503. begin
  504. if Assigned(AParent) then
  505. Node := TReportCacheNode(AParent.FirstChild)
  506. else
  507. Node := TReportCacheNode(Root.FirstChild);
  508. Result := nil;
  509. if (ACode = '') and (AB_Code = '') then Exit;
  510. sCodeID := ConvertDigitCode(ACode, 3, '-');
  511. sB_CodeID := ConvertDigitCode(AB_Code, 4, '-');
  512. while Assigned(Node) do
  513. begin
  514. sCodeID2 := ConvertDigitCode(Node.Code, 3, '-');
  515. sB_CodeID2 := ConvertDigitCode(AB_Code, 4, '-');
  516. if sCodeID < sCodeID2 then
  517. begin
  518. Result := Node;
  519. Break;
  520. end
  521. else if sB_CodeID < sB_CodeID2 then
  522. begin
  523. Result := Node;
  524. Break;
  525. end;
  526. Node := TReportCacheNode(Node.NextSibling);
  527. end;
  528. end;
  529. function TReportCacheTree.FindNode(AParent: TCacheNode; ACode,
  530. AB_Code: string): TReportCacheNode;
  531. var
  532. Node: TReportCacheNode;
  533. begin
  534. if Assigned(AParent) then
  535. Node := TReportCacheNode(AParent.FirstChild)
  536. else
  537. Node := TReportCacheNode(Root.FirstChild);
  538. Result := nil;
  539. while Assigned(Node) do
  540. begin
  541. if (Node.Code = ACode) and (Node.B_Code = AB_Code) then
  542. begin
  543. Result := Node;
  544. Break;
  545. end;
  546. Node := TReportCacheNode(Node.NextSibling);
  547. end;
  548. end;
  549. function TReportCacheTree.FindNode(AParent: TCacheNode;
  550. AName: string): TReportCacheNode;
  551. var
  552. Node: TReportCacheNode;
  553. begin
  554. if Assigned(AParent) then
  555. Node := TReportCacheNode(AParent.FirstChild)
  556. else
  557. Node := TReportCacheNode(Root.FirstChild);
  558. Result := nil;
  559. while Assigned(Node) do
  560. begin
  561. if SameText(Node.Name, AName) then
  562. begin
  563. Result := Node;
  564. Break;
  565. end;
  566. Node := TReportCacheNode(Node.NextSibling);
  567. end;
  568. end;
  569. function TReportCacheTree.FindNode(AParent: TCacheNode; ACode, AB_Code,
  570. AName: string): TReportCacheNode;
  571. var
  572. Node: TReportCacheNode;
  573. begin
  574. if Assigned(AParent) then
  575. Node := TReportCacheNode(AParent.FirstChild)
  576. else
  577. Node := TReportCacheNode(Root.FirstChild);
  578. Result := nil;
  579. while Assigned(Node) do
  580. begin
  581. if SameText(Node.Code, ACode) and SameText(Node.B_Code, AB_Code)
  582. and SameText(Node.Name, AName) then
  583. begin
  584. Result := Node;
  585. Break;
  586. end;
  587. Node := TReportCacheNode(Node.NextSibling);
  588. end;
  589. end;
  590. function TReportCacheTree.GetNewNode(
  591. AProjectCount: Integer): TReportCacheNode;
  592. begin
  593. Result := TReportCacheNode.Create(Self, GetNewNodeID, AProjectCount);
  594. CacheNodes.Add(Result);
  595. end;
  596. procedure TReportCacheTree.ReCalcGatherData;
  597. var
  598. i: Integer;
  599. CacheNode: TReportCacheNode;
  600. begin
  601. FGatherCacheNode.Free;
  602. FGatherCacheNode := TReportCacheNode.Create(nil, -2, FProjectCount);
  603. CacheNode := TReportCacheNode(FirstNode);
  604. while Assigned(CacheNode) do
  605. begin
  606. FGatherCacheNode.TotalPrice := FGatherCacheNode.TotalPrice + CacheNode.TotalPrice;
  607. FGatherCacheNode.AddDealTotalPrice := FGatherCacheNode.AddDealTotalPrice + CacheNode.AddDealTotalPrice;
  608. FGatherCacheNode.AddQcTotalPrice := FGatherCacheNode.AddQcTotalPrice + CacheNode.AddQcTotalPrice;
  609. FGatherCacheNode.AddPcTotalPrice := FGatherCacheNode.AddPcTotalPrice + CacheNode.AddPcTotalPrice;
  610. FGatherCacheNode.PDTotalPrice := FGatherCacheNode.PDTotalPrice + CacheNode.PDTotalPrice;
  611. FGatherCacheNode.CDDTotalPrice := FGatherCacheNode.CDDTotalPrice + CacheNode.CDDTotalPrice;
  612. FGatherCacheNode.ABTotalPrice := FGatherCacheNode.ABTotalPrice + CacheNode.ABTotalPrice;
  613. for i := 0 to FProjectCount - 1 do
  614. FGatherCacheNode.P_TotalPrice[i] := FGatherCacheNode.P_TotalPrice[i] + CacheNode.P_TotalPrice[i];
  615. CacheNode := TReportCacheNode(CacheNode.NextSibling);
  616. end;
  617. end;
  618. procedure TReportCacheTree.ReCalcRatioPercent;
  619. var
  620. i: Integer;
  621. CacheNode: TReportCacheNode;
  622. begin
  623. for i := 0 to CacheNodes.Count - 1 do
  624. begin
  625. CacheNode := TReportCacheNode(CacheNodes.Items[i]);
  626. if GatherCacheNode.TotalPrice <> 0 then
  627. CacheNode.RatioPercent := AdvRoundTo(CacheNode.TotalPrice/GatherCacheNode.TotalPrice*100);
  628. if GatherCacheNode.AddGatherTotalPrice <> 0 then
  629. CacheNode.AddRatioPercent := AdvRoundTo(CacheNode.AddGatherTotalPrice/GatherCacheNode.AddGatherTotalPrice*100);
  630. end;
  631. end;
  632. procedure TReportCacheTree.SaveTreeToFile(const AFileName: string);
  633. var
  634. sgs: TStringList;
  635. I: Integer;
  636. Node: TReportCacheNode;
  637. begin
  638. sgs := TStringList.Create;
  639. try
  640. for I := 0 to CacheNodes.Count - 1 do
  641. begin
  642. Node := TReportCacheNode(CacheNodes.Items[I]);
  643. sgs.Add(Format('ID: %3d; ParentID: %3d; NextID: %3d; Code: %s; B_Code: %s; Name: %s;',
  644. [Node.ID, Node.ParentID, Node.NextSiblingID, Node.Code, Node.B_Code, Node.Name]));
  645. end;
  646. sgs.SaveToFile(AFileName);
  647. finally
  648. sgs.Free;
  649. end;
  650. end;
  651. { TAllPhaseCacheTree }
  652. function TAllPhaseCacheTree.AddNode(AID: Integer; AParent,
  653. ANextSibling: TCacheNode): TAllPhaseCacheNode;
  654. begin
  655. Result := GetNewNode(AID);
  656. if Assigned(ANextSibling) then
  657. ANextSibling.InsertPreSibling(Result)
  658. else if Assigned(AParent) then
  659. AParent.InsertChild(Result)
  660. else
  661. Root.InsertChild(Result);
  662. end;
  663. function TAllPhaseCacheTree.FindNode(AID: Integer): TAllPhaseCacheNode;
  664. var
  665. i: Integer;
  666. Node: TAllPhaseCacheNode;
  667. begin
  668. Result := nil;
  669. for i := 0 to CacheNodes.Count - 1 do
  670. begin
  671. Node := TAllPhaseCacheNode(CacheNodes.Items[i]);
  672. if Node.ID = AID then
  673. begin
  674. Result := Node;
  675. Break;
  676. end;
  677. end;
  678. end;
  679. function TAllPhaseCacheTree.GetNewNode(
  680. AID: Integer): TAllPhaseCacheNode;
  681. begin
  682. Result := TAllPhaseCacheNode.Create(Self, AID);
  683. CacheNodes.Add(Result);
  684. end;
  685. procedure TAllPhaseCacheTree.SaveTreeToFile(const AFileName: string);
  686. var
  687. sgs: TStringList;
  688. I: Integer;
  689. Node: TAllPhaseCacheNode;
  690. begin
  691. sgs := TStringList.Create;
  692. try
  693. for I := 0 to CacheNodes.Count - 1 do
  694. begin
  695. Node := TAllPhaseCacheNode(CacheNodes.Items[I]);
  696. sgs.Add(Format('ID: %3d; ParentID: %3d; NextID: %3d; Code: %s; B_Code: %s; Name: %s;',
  697. [Node.ID, Node.ParentID, Node.NextSiblingID, Node.Code, Node.B_Code, Node.Name]));
  698. end;
  699. sgs.SaveToFile(AFileName);
  700. finally
  701. sgs.Free;
  702. end;
  703. end;
  704. { TGclCacheTree }
  705. function TGclCacheTree.AddNodeByB_Code(
  706. const AB_Code: string): TGclCacheNode;
  707. function FindParent: TGclCacheNode;
  708. begin
  709. Result := FLastNode;
  710. while Assigned(Result) and (Result.B_Code <> '') and (Pos(Result.B_Code + '-', AB_Code) <> 1) do
  711. Result := TGclCacheNode(Result.Parent);
  712. end;
  713. var
  714. vParent: TGclCacheNode;
  715. begin
  716. vParent := FindParent;
  717. Result := TGclCacheNode(AddNode(vParent));
  718. FLastNode := Result;
  719. end;
  720. function TGclCacheTree.AddNodeByData(const AB_Code,
  721. AName: string): TGclCacheNode;
  722. begin
  723. if AB_Code = '' then
  724. Result := AddNodeByName(AName)
  725. else
  726. Result := AddNodeByB_Code(AB_Code);
  727. end;
  728. function TGclCacheTree.AddNodeByName(const AName: string): TGclCacheNode;
  729. begin
  730. if Pos('第100章至', AName) <> 0 then
  731. begin
  732. Result := TGclCacheNode(AddNode(nil));
  733. FLastBlank1 := Result;
  734. end
  735. else
  736. Result := TGclCacheNode(AddNode(FLastBlank1));
  737. FLastNode := Result;
  738. end;
  739. function TGclCacheTree.GetNewNode: TCacheNode;
  740. begin
  741. Result := TGclCacheNode.Create(Self, GetNewNodeID);
  742. CacheNodes.Add(Result);
  743. end;
  744. procedure TGclCacheTree.SaveTreeToFile(const AFileName: string);
  745. var
  746. sgs: TStringList;
  747. I: Integer;
  748. Node: TGclCacheNode;
  749. begin
  750. sgs := TStringList.Create;
  751. try
  752. for I := 0 to CacheNodes.Count - 1 do
  753. begin
  754. Node := TGclCacheNode(CacheNodes.Items[I]);
  755. sgs.Add(Format('ID: %3d; ParentID: %3d; NextID: %3d; B_Code: %s; Name: %s;',
  756. [Node.ID, Node.ParentID, Node.NextSiblingID, Node.B_Code, Node.Name]));
  757. end;
  758. sgs.SaveToFile(AFileName);
  759. finally
  760. sgs.Free;
  761. end;
  762. end;
  763. end.