MCacheTree.pas 27 KB

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