exportRationSectionTree.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * Created by Tony on 2021/10/6.
  3. */
  4. let fs = require('fs');
  5. // let data = fs.readFileSync('D:/GitHome/ConstructionOperation/tmp/testDataResultSectionData_1633407022828.js');
  6. // let data = fs.readFileSync('D:/GitHome/YangHuOperation/tmp/testDataResult_SectionTree_广西公路日常养护预算指标(2021).js');
  7. let data = fs.readFileSync('D:/GitHome/YangHuOperation/tmp/testDataResult_SectionTree_广西公路养护预算定额(2021).js');
  8. //----------------------------------------
  9. const NODE_ID = "ID", P_ID = "ParentID", NEXT_ID = "NextSiblingID", ADHOC_PRE_ID="Previous_ID", CHILDREN_NODE = "items", SUB_ID = "sub_ids",
  10. EMPTY_ID_VAL = -1, TREE_LEVEL = 'treeLevel', TOP_BILL_ID = "topBillID";
  11. let tree_Data_Helper = {
  12. buildTreeNodeDirectly: function(data, addLevel) {
  13. let topArr = [], rst = [], tmpNodes = {}, prefix = "id_";
  14. let private_getStartNode = function (idArr) {
  15. let tmpNodeRst = null;
  16. for (let i = 0; i < idArr.length; i++) {
  17. if (parseInt(tmpNodes[prefix + idArr[i]][ADHOC_PRE_ID]) === EMPTY_ID_VAL) {
  18. tmpNodeRst = tmpNodes[prefix + idArr[i]];
  19. break;
  20. }
  21. }
  22. return tmpNodeRst;
  23. };
  24. let private_buildNodeData = function(parentItem, idArr, treeLevel, tbID) {
  25. let iter = [], nextNode = private_getStartNode(idArr), pushedIds = [];
  26. while (nextNode !== null && nextNode !== undefined ) {
  27. if (parentItem) {
  28. parentItem[CHILDREN_NODE].push(nextNode);
  29. } else {
  30. rst.push(nextNode);
  31. }
  32. iter.push(nextNode);
  33. pushedIds.push(nextNode[NODE_ID]);
  34. nextNode[TOP_BILL_ID] = tbID;
  35. if (parentItem === null) {
  36. nextNode[TOP_BILL_ID] = nextNode[NODE_ID];
  37. if (nextNode.flags && nextNode.flags.length > 0) {
  38. for (let flag of nextNode.flags) {
  39. if (flag.fieldName === "fixed") {
  40. nextNode[TOP_BILL_ID] = flag.flag;
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. if (addLevel) nextNode[TREE_LEVEL] = treeLevel;
  47. nextNode = tmpNodes[prefix + nextNode[NEXT_ID]];
  48. if (nextNode === null || nextNode === undefined) {
  49. //备注: 考虑到实际数据的健壮性,有些节点会掉链子,需要用 parentItem[SUB_ID] 比对已经加上的节点,如发现加上的节点数量不够,那就得在这里补充上去
  50. if (parentItem) {
  51. if (parentItem[SUB_ID].length > iter.length) {
  52. for (let subId of parentItem[SUB_ID]) {
  53. if (pushedIds.indexOf(subId) < 0) {
  54. let restNode = tmpNodes[prefix + subId];
  55. if (addLevel) restNode[TREE_LEVEL] = treeLevel;
  56. restNode[TOP_BILL_ID] = tbID;
  57. parentItem[CHILDREN_NODE].push(restNode);
  58. iter.push(restNode);
  59. }
  60. }
  61. }
  62. } else {
  63. if (idArr.length > iter.length) {
  64. for (let topId of idArr) {
  65. if (pushedIds.indexOf(topId) < 0) {
  66. let restNode = tmpNodes[prefix + topId];
  67. if (addLevel) restNode[TREE_LEVEL] = treeLevel;
  68. restNode[TOP_BILL_ID] = restNode[NODE_ID];
  69. if (restNode.flags && restNode.flags.length > 0) {
  70. for (let flag of restNode.flags) {
  71. if (flag.fieldName === "fixed") {
  72. restNode[TOP_BILL_ID] = flag.flag;
  73. break;
  74. }
  75. }
  76. }
  77. rst.push(restNode);
  78. iter.push(restNode);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. pushedIds = [];
  86. for (let i = 0; i < iter.length; i++) {
  87. let rtbID = tbID;
  88. if (parentItem === null) {
  89. rtbID = iter[i][TOP_BILL_ID];
  90. }
  91. private_buildNodeData(iter[i], iter[i][SUB_ID], (treeLevel + 1), rtbID);
  92. }
  93. };
  94. //1. 给每个节点设置key, 顺便找Top Node
  95. for (let i = 0; i < data.length; i++) {
  96. tmpNodes[prefix + data[i][NODE_ID]] = data[i];
  97. data[i][ADHOC_PRE_ID] = EMPTY_ID_VAL;
  98. data[i][SUB_ID] = [];
  99. data[i][CHILDREN_NODE] = [];
  100. if (parseInt(data[i][P_ID]) === EMPTY_ID_VAL) {
  101. topArr.push(data[i][NODE_ID]);
  102. }
  103. }
  104. //2. 通过key,设置好兄弟/父子关系
  105. for (let i = 0; i < data.length; i++) {
  106. if (parseInt(data[i][NEXT_ID]) !== EMPTY_ID_VAL) {
  107. if (tmpNodes[prefix + data[i][NEXT_ID]] !== undefined){
  108. if (tmpNodes[prefix + data[i][NEXT_ID]][P_ID] === data[i][P_ID]) {
  109. tmpNodes[prefix + data[i][NEXT_ID]][ADHOC_PRE_ID] = data[i][NODE_ID];
  110. } else {
  111. tmpNodes[prefix + data[i][NEXT_ID]][ADHOC_PRE_ID] = EMPTY_ID_VAL;
  112. data[i][NEXT_ID] = EMPTY_ID_VAL;
  113. }
  114. }
  115. }
  116. if (parseInt(data[i][P_ID]) !== EMPTY_ID_VAL) {
  117. tmpNodes[prefix + data[i][P_ID]][SUB_ID].push(data[i][NODE_ID]);
  118. }
  119. }
  120. //3. 开build
  121. private_buildNodeData(null, topArr, 0, -1);
  122. //try to release and return
  123. tmpNodes = null;
  124. topArr.length = 0;
  125. return rst;
  126. },
  127. getFlatArray: function (srcArr, destArr) {
  128. let private_put = function (parentItem) {
  129. destArr.push(parentItem);
  130. if (parentItem.items) {
  131. for (let subItem of parentItem.items) {
  132. private_put(subItem);
  133. }
  134. }
  135. }
  136. for (let node of srcArr) {
  137. private_put(node);
  138. }
  139. for (let item of destArr) {
  140. delete item[CHILDREN_NODE];
  141. delete item[SUB_ID];
  142. delete item[ADHOC_PRE_ID];
  143. }
  144. }
  145. };
  146. let tmpTreeArr = tree_Data_Helper.buildTreeNodeDirectly(JSON.parse(data), true);
  147. function _setChapterLevel(chapterNode, cLv) {
  148. chapterNode.chapterLv = cLv;
  149. if (chapterNode.items && chapterNode.items.length > 0) {
  150. for (let subNode of chapterNode.items) {
  151. _setChapterLevel(subNode, cLv);
  152. }
  153. }
  154. }
  155. for (let idx = 0; idx < tmpTreeArr[0].items.length; idx++) {
  156. _setChapterLevel(tmpTreeArr[0].items[idx], idx + 1);
  157. }
  158. // console.log(tmpTreeArr);
  159. let sectionTreeArr = [];
  160. tree_Data_Helper.getFlatArray(tmpTreeArr, sectionTreeArr);
  161. sectionTreeArr.splice(0,1);
  162. // console.log(sectionTreeArr);
  163. let newSectionData = [];
  164. let firstStr = 'ID,ParentID,NextSiblingID,Name,Code,FullCode,Name2';
  165. newSectionData.push(firstStr);
  166. for (let sectionNode of sectionTreeArr) {
  167. // let node = [];
  168. // node.push(sectionNode.ID);
  169. // node.push(sectionNode.ParentID);
  170. // node.push(sectionNode.NextSiblingID);
  171. // node.push(sectionNode.name);
  172. // node.push(sectionNode.chapterLv);
  173. // node.push(''); //full code
  174. // node.push(''); //name2
  175. // let str = `INSERT INTO RationTree Values(${sectionNode.ID}, ${sectionNode.ParentID}, ${sectionNode.NextSiblingID}, '${sectionNode.name}', ${sectionNode.chapterLv === undefined?-1:sectionNode.chapterLv}, NULL, NULL );`
  176. // let str = `${sectionNode.ID},${sectionNode.ParentID},${sectionNode.NextSiblingID},'${sectionNode.name}',${sectionNode.chapterLv === undefined?-1:sectionNode.chapterLv},'',''`
  177. let str = `${sectionNode.ID},${sectionNode.ParentID},${sectionNode.NextSiblingID},${sectionNode.name},${sectionNode.chapterLv === undefined?-1:sectionNode.chapterLv},,,`
  178. newSectionData.push(str);
  179. }
  180. // let ttlSQL = JSON.stringify(newSectionData);
  181. // let ttlSQL = newSectionData.join('');
  182. let ttlSQL = newSectionData.join('\n');
  183. let regExp = new RegExp('"', "gm");
  184. ttlSQL = ttlSQL.replace(regExp, '');
  185. // ttlSQL.replace()
  186. //fs.writeFile(`D:/GitHome/ConstructionOperation/tmp/章节树临时Data文件_${(new Date()).getTime()}.js`, ttlSQL, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
  187. // fs.writeFile(`D:/GitHome/ConstructionOperation/tmp/章节树临时Data文件_${(new Date()).getTime()}.txt`, ttlSQL, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
  188. fs.writeFile(`D:/GitHome/YangHuOperation/tmp/章节树临时Data文件_${(new Date()).getTime()}.txt`, ttlSQL, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
  189. if(err) throw err;
  190. });
  191. // */