| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 | 
							- /**
 
-  * Created by Tony on 2021/10/6.
 
-  */
 
- let fs = require('fs');
 
- // let data = fs.readFileSync('D:/GitHome/ConstructionOperation/tmp/testDataResultSectionData_1633407022828.js');
 
- // let data = fs.readFileSync('D:/GitHome/YangHuOperation/tmp/testDataResult_SectionTree_广西公路日常养护预算指标(2021).js');
 
- let data = fs.readFileSync('D:/GitHome/YangHuOperation/tmp/testDataResult_SectionTree_广西公路养护预算定额(2021).js');
 
- //----------------------------------------
 
- const NODE_ID = "ID", P_ID = "ParentID", NEXT_ID = "NextSiblingID", ADHOC_PRE_ID="Previous_ID", CHILDREN_NODE = "items", SUB_ID = "sub_ids",
 
-     EMPTY_ID_VAL = -1, TREE_LEVEL = 'treeLevel', TOP_BILL_ID = "topBillID";
 
- let tree_Data_Helper = {
 
-     buildTreeNodeDirectly: function(data, addLevel) {
 
-         let topArr = [], rst = [], tmpNodes = {}, prefix = "id_";
 
-         let private_getStartNode = function (idArr) {
 
-             let tmpNodeRst = null;
 
-             for (let i = 0; i < idArr.length; i++) {
 
-                 if (parseInt(tmpNodes[prefix + idArr[i]][ADHOC_PRE_ID]) === EMPTY_ID_VAL) {
 
-                     tmpNodeRst = tmpNodes[prefix + idArr[i]];
 
-                     break;
 
-                 }
 
-             }
 
-             return tmpNodeRst;
 
-         };
 
-         let private_buildNodeData = function(parentItem, idArr, treeLevel, tbID) {
 
-             let iter = [], nextNode = private_getStartNode(idArr), pushedIds = [];
 
-             while (nextNode !== null && nextNode !== undefined ) {
 
-                 if (parentItem) {
 
-                     parentItem[CHILDREN_NODE].push(nextNode);
 
-                 } else {
 
-                     rst.push(nextNode);
 
-                 }
 
-                 iter.push(nextNode);
 
-                 pushedIds.push(nextNode[NODE_ID]);
 
-                 nextNode[TOP_BILL_ID] = tbID;
 
-                 if (parentItem === null) {
 
-                     nextNode[TOP_BILL_ID] = nextNode[NODE_ID];
 
-                     if (nextNode.flags && nextNode.flags.length > 0) {
 
-                         for (let flag of nextNode.flags) {
 
-                             if (flag.fieldName === "fixed") {
 
-                                 nextNode[TOP_BILL_ID] = flag.flag;
 
-                                 break;
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-                 if (addLevel) nextNode[TREE_LEVEL] = treeLevel;
 
-                 nextNode = tmpNodes[prefix + nextNode[NEXT_ID]];
 
-                 if (nextNode === null || nextNode === undefined) {
 
-                     //备注: 考虑到实际数据的健壮性,有些节点会掉链子,需要用 parentItem[SUB_ID] 比对已经加上的节点,如发现加上的节点数量不够,那就得在这里补充上去
 
-                     if (parentItem) {
 
-                         if (parentItem[SUB_ID].length > iter.length) {
 
-                             for (let subId of parentItem[SUB_ID]) {
 
-                                 if (pushedIds.indexOf(subId) < 0) {
 
-                                     let restNode = tmpNodes[prefix + subId];
 
-                                     if (addLevel) restNode[TREE_LEVEL] = treeLevel;
 
-                                     restNode[TOP_BILL_ID] = tbID;
 
-                                     parentItem[CHILDREN_NODE].push(restNode);
 
-                                     iter.push(restNode);
 
-                                 }
 
-                             }
 
-                         }
 
-                     } else {
 
-                         if (idArr.length > iter.length) {
 
-                             for (let topId of idArr) {
 
-                                 if (pushedIds.indexOf(topId) < 0) {
 
-                                     let restNode = tmpNodes[prefix + topId];
 
-                                     if (addLevel) restNode[TREE_LEVEL] = treeLevel;
 
-                                     restNode[TOP_BILL_ID] = restNode[NODE_ID];
 
-                                     if (restNode.flags && restNode.flags.length > 0) {
 
-                                         for (let flag of restNode.flags) {
 
-                                             if (flag.fieldName === "fixed") {
 
-                                                 restNode[TOP_BILL_ID] = flag.flag;
 
-                                                 break;
 
-                                             }
 
-                                         }
 
-                                     }
 
-                                     rst.push(restNode);
 
-                                     iter.push(restNode);
 
-                                 }
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             pushedIds = [];
 
-             for (let i = 0; i < iter.length; i++) {
 
-                 let rtbID = tbID;
 
-                 if (parentItem === null) {
 
-                     rtbID = iter[i][TOP_BILL_ID];
 
-                 }
 
-                 private_buildNodeData(iter[i], iter[i][SUB_ID], (treeLevel + 1), rtbID);
 
-             }
 
-         };
 
-         //1. 给每个节点设置key, 顺便找Top Node
 
-         for (let i = 0; i < data.length; i++) {
 
-             tmpNodes[prefix + data[i][NODE_ID]] = data[i];
 
-             data[i][ADHOC_PRE_ID] = EMPTY_ID_VAL;
 
-             data[i][SUB_ID] = [];
 
-             data[i][CHILDREN_NODE] = [];
 
-             if (parseInt(data[i][P_ID]) === EMPTY_ID_VAL) {
 
-                 topArr.push(data[i][NODE_ID]);
 
-             }
 
-         }
 
-         //2. 通过key,设置好兄弟/父子关系
 
-         for (let i = 0; i < data.length; i++) {
 
-             if (parseInt(data[i][NEXT_ID]) !== EMPTY_ID_VAL) {
 
-                 if (tmpNodes[prefix + data[i][NEXT_ID]] !== undefined){
 
-                     if (tmpNodes[prefix + data[i][NEXT_ID]][P_ID] === data[i][P_ID]) {
 
-                         tmpNodes[prefix + data[i][NEXT_ID]][ADHOC_PRE_ID] = data[i][NODE_ID];
 
-                     } else {
 
-                         tmpNodes[prefix + data[i][NEXT_ID]][ADHOC_PRE_ID] = EMPTY_ID_VAL;
 
-                         data[i][NEXT_ID] = EMPTY_ID_VAL;
 
-                     }
 
-                 }
 
-             }
 
-             if (parseInt(data[i][P_ID]) !== EMPTY_ID_VAL) {
 
-                 tmpNodes[prefix + data[i][P_ID]][SUB_ID].push(data[i][NODE_ID]);
 
-             }
 
-         }
 
-         //3. 开build
 
-         private_buildNodeData(null, topArr, 0, -1);
 
-         //try to release and return
 
-         tmpNodes = null;
 
-         topArr.length = 0;
 
-         return rst;
 
-     },
 
-     getFlatArray: function (srcArr, destArr) {
 
-         let private_put = function (parentItem) {
 
-             destArr.push(parentItem);
 
-             if (parentItem.items) {
 
-                 for (let subItem of parentItem.items) {
 
-                     private_put(subItem);
 
-                 }
 
-             }
 
-         }
 
-         for (let node of srcArr) {
 
-             private_put(node);
 
-         }
 
-         for (let item of destArr) {
 
-             delete item[CHILDREN_NODE];
 
-             delete item[SUB_ID];
 
-             delete item[ADHOC_PRE_ID];
 
-         }
 
-     }
 
- };
 
- let tmpTreeArr = tree_Data_Helper.buildTreeNodeDirectly(JSON.parse(data), true);
 
- function _setChapterLevel(chapterNode, cLv) {
 
-     chapterNode.chapterLv = cLv;
 
-     if (chapterNode.items && chapterNode.items.length > 0) {
 
-         for (let subNode of chapterNode.items) {
 
-             _setChapterLevel(subNode, cLv);
 
-         }
 
-     }
 
- }
 
- for (let idx = 0; idx < tmpTreeArr[0].items.length; idx++) {
 
-     _setChapterLevel(tmpTreeArr[0].items[idx], idx + 1);
 
- }
 
- // console.log(tmpTreeArr);
 
- let sectionTreeArr = [];
 
- tree_Data_Helper.getFlatArray(tmpTreeArr, sectionTreeArr);
 
- sectionTreeArr.splice(0,1);
 
- // console.log(sectionTreeArr);
 
- let newSectionData = [];
 
- let firstStr = 'ID,ParentID,NextSiblingID,Name,Code,FullCode,Name2';
 
- newSectionData.push(firstStr);
 
- for (let sectionNode of sectionTreeArr) {
 
-     // let node = [];
 
-     // node.push(sectionNode.ID);
 
-     // node.push(sectionNode.ParentID);
 
-     // node.push(sectionNode.NextSiblingID);
 
-     // node.push(sectionNode.name);
 
-     // node.push(sectionNode.chapterLv);
 
-     // node.push(''); //full code
 
-     // node.push(''); //name2
 
-     // let str = `INSERT INTO RationTree Values(${sectionNode.ID}, ${sectionNode.ParentID}, ${sectionNode.NextSiblingID}, '${sectionNode.name}', ${sectionNode.chapterLv === undefined?-1:sectionNode.chapterLv}, NULL, NULL );`
 
-     // let str = `${sectionNode.ID},${sectionNode.ParentID},${sectionNode.NextSiblingID},'${sectionNode.name}',${sectionNode.chapterLv === undefined?-1:sectionNode.chapterLv},'',''`
 
-     let str = `${sectionNode.ID},${sectionNode.ParentID},${sectionNode.NextSiblingID},${sectionNode.name},${sectionNode.chapterLv === undefined?-1:sectionNode.chapterLv},,,`
 
-     newSectionData.push(str);
 
- }
 
- // let ttlSQL = JSON.stringify(newSectionData);
 
- // let ttlSQL = newSectionData.join('');
 
- let ttlSQL = newSectionData.join('\n');
 
- let regExp = new RegExp('"', "gm");
 
- ttlSQL = ttlSQL.replace(regExp, '');
 
- // ttlSQL.replace()
 
- //fs.writeFile(`D:/GitHome/ConstructionOperation/tmp/章节树临时Data文件_${(new Date()).getTime()}.js`, ttlSQL, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
 
- // fs.writeFile(`D:/GitHome/ConstructionOperation/tmp/章节树临时Data文件_${(new Date()).getTime()}.txt`, ttlSQL, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
 
- fs.writeFile(`D:/GitHome/YangHuOperation/tmp/章节树临时Data文件_${(new Date()).getTime()}.txt`, ttlSQL, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
 
-     if(err) throw err;
 
- });
 
- // */
 
 
  |