path_tree.js 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142
  1. /**
  2. * (需使用 decimal.min.js, zh_calc.js)
  3. *
  4. * 构建pathTree
  5. * 可动态加载子节点,要求子节点获取接口按/xxx/get-children定义
  6. *
  7. * 所台账结构数据均用到该文件,请勿随意修改。
  8. * @param {Object} setting - 设置
  9. * @returns {PathTree}
  10. */
  11. 'use strict';
  12. const itemsPre = 'id_';
  13. class PosData {
  14. /**
  15. * 构造函数
  16. * @param {id|Number, masterId|Number} setting
  17. */
  18. constructor(setting) {
  19. // 无索引
  20. this.datas = [];
  21. // 以key为索引
  22. this.items = {};
  23. // 以分类id为索引的有序
  24. this.ledgerPos = {};
  25. // pos设置
  26. this.setting = setting;
  27. }
  28. /**
  29. * 加载部位明细数据
  30. * @param datas
  31. */
  32. loadDatas(datas) {
  33. this.datas = datas;
  34. this.items = {};
  35. this.ledgerPos = {};
  36. for (const data of this.datas) {
  37. const key = itemsPre + data[this.setting.id];
  38. this.items[key] = data;
  39. const masterKey = itemsPre + data[this.setting.ledgerId];
  40. if (!this.ledgerPos[masterKey]) {
  41. this.ledgerPos[masterKey] = [];
  42. }
  43. this.ledgerPos[masterKey].push(data);
  44. }
  45. for (const prop in this.ledgerPos) {
  46. this.resortLedgerPos(this.ledgerPos[prop]);
  47. }
  48. }
  49. /**
  50. * 更新数据
  51. * @param datas
  52. */
  53. updateDatas(data) {
  54. const datas = data instanceof Array ? data : [data];
  55. const result = { create: [], update: [] }, resort = [];
  56. for (const d of datas) {
  57. const key = itemsPre + d[this.setting.id];
  58. if (!this.items[key]) {
  59. this.datas.push(d);
  60. this.items[key] = d;
  61. const masterKey = itemsPre + d[this.setting.ledgerId];
  62. if (!this.ledgerPos[masterKey]) {
  63. this.ledgerPos[masterKey] = [];
  64. }
  65. this.ledgerPos[masterKey].push(d);
  66. result.create.push(d);
  67. } else {
  68. const pos = this.items[key];
  69. for (const prop in d) {
  70. pos[prop] = d[prop];
  71. }
  72. result.update.push(pos);
  73. }
  74. const masterKey = itemsPre + d[this.setting.ledgerId];
  75. if (resort.indexOf(masterKey) === -1) {
  76. resort.push(masterKey);
  77. }
  78. }
  79. if (this.setting.calcFun) {
  80. for (const u of result.update) {
  81. this.setting.calcFun(u);
  82. }
  83. for (const c of result.create) {
  84. this.setting.calcFun(c);
  85. }
  86. }
  87. for (const s of resort) {
  88. this.resortLedgerPos(this.ledgerPos[s]);
  89. }
  90. return result;
  91. }
  92. /**
  93. * 移除数据
  94. * @param datas
  95. */
  96. removeDatas(data) {
  97. if (!data) { return; }
  98. const datas = data instanceof Array ? data : [data];
  99. for (let i = datas.length - 1; i >= 0; i--) {
  100. const id = datas[i];
  101. const d = this.getPos(id);
  102. this.datas.splice(this.datas.indexOf(d), 1);
  103. const key = itemsPre + d[this.setting.id];
  104. delete this.items[key];
  105. const masterKey = itemsPre + d[this.setting.ledgerId];
  106. const range = this.ledgerPos[masterKey];
  107. range.splice(range.indexOf(d), 1);
  108. if (range.length === 0) {
  109. delete this.ledgerPos[masterKey];
  110. }
  111. }
  112. }
  113. /**
  114. * 移除数据 - 根据分类id
  115. * @param mid
  116. */
  117. removeDatasByMasterId(mid) {
  118. const masterKey = itemsPre + mid;
  119. const range = this.ledgerPos[masterKey];
  120. if (range) {
  121. delete this.ledgerPos[masterKey];
  122. for (const r of range) {
  123. this.datas.splice(this.datas.indexOf(r), 1);
  124. const key = itemsPre + r[this.setting.id];
  125. delete this.items[key];
  126. }
  127. }
  128. }
  129. getPos(id) {
  130. return this.items[itemsPre + id];
  131. }
  132. getLedgerPos(mid) {
  133. return this.ledgerPos[itemsPre + mid];
  134. }
  135. resortLedgerPos(ledgerPos) {
  136. const sortRule = this.setting.sort || [['porder', 'asc']];
  137. if (ledgerPos instanceof Array) {
  138. ledgerPos.sort(function (a, b) {
  139. for (const sr of sortRule) {
  140. const iSort = sr[1] === 'asc' ? a[sr[0]] - b[sr[0]] : b[sr[0]] - a[sr[0]];
  141. if (iSort) return iSort;
  142. }
  143. })
  144. }
  145. }
  146. /**
  147. * 计算全部
  148. */
  149. calculateAll() {
  150. if (!this.setting.calcFun) { return; }
  151. for (const pos of this.datas) {
  152. this.setting.calcFun(pos);
  153. }
  154. }
  155. set sort(sort) {
  156. this.setting.sort = sort;
  157. for (const key in this.ledgerPos) {
  158. this.resortLedgerPos(this.ledgerPos[key]);
  159. }
  160. }
  161. }
  162. class StagePosData extends PosData {
  163. loadStageData(datas, fieldPre, fields) {
  164. if (!datas) { return; }
  165. datas = datas instanceof Array ? datas : [datas];
  166. const loadedData = [];
  167. for (const data of datas) {
  168. let node = this.getPos(data.pid);
  169. if (node) {
  170. for (const prop of fields) {
  171. if (data[prop] !== undefined) {
  172. node[fieldPre + prop] = data[prop];
  173. }
  174. }
  175. if (this.setting.calcFun) {
  176. this.setting.calcFun(node);
  177. }
  178. loadedData.push(node);
  179. }
  180. }
  181. }
  182. loadPreStageData(datas) {
  183. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  184. }
  185. loadCurStageData(datas) {
  186. this.loadStageData(datas, '', this.setting.updateFields);
  187. }
  188. }
  189. class MasterPosData extends PosData {
  190. /**
  191. * 构造函数
  192. * @param {id|Number, masterId|Number} setting
  193. */
  194. constructor(setting) {
  195. super(setting);
  196. // 关联索引
  197. this.masterItems = {};
  198. }
  199. /**
  200. * 加载主数据
  201. * @param datas
  202. */
  203. loadDatas(datas) {
  204. super.loadDatas(datas);
  205. // 清空旧数据
  206. this.masterItems = {};
  207. // minor数据缓存
  208. this.minorData = {};
  209. // 加载全部数据
  210. for (const data of this.datas) {
  211. const keyName = itemsPre + data[this.setting.masterId];
  212. this.masterItems[keyName] = data;
  213. }
  214. }
  215. /**
  216. * 根据关联id,查找节点
  217. * @param id
  218. * @returns {*}
  219. */
  220. getMasterItems(id) {
  221. return this.masterItems[itemsPre + id];
  222. }
  223. /**
  224. * 加载关联数据
  225. *
  226. * @param {Array|Object}datas - 需要关联的数据
  227. * @param {String} fieldPre - 关联字段前缀(关联结果)
  228. * @param {Array} fields - 关联字段
  229. * @returns {Array}
  230. */
  231. loadMinorData(datas, fieldSuf, fields) {
  232. if (!datas) return;
  233. datas = datas instanceof Array ? datas : [datas];
  234. this.minorData[fieldSuf] = datas;
  235. const loadedData = [];
  236. for (const data of datas) {
  237. let node = this.getMasterItems(data[this.setting.minorId]);
  238. if (node) {
  239. for (const prop of fields) {
  240. if (data[prop] !== undefined) {
  241. node[prop + fieldSuf] = data[prop];
  242. }
  243. }
  244. loadedData.push(node);
  245. }
  246. }
  247. return loadedData;
  248. }
  249. }
  250. const createNewPathTree = function (type, setting) {
  251. class BaseTree {
  252. /**
  253. * 构造函数
  254. */
  255. constructor(setting) {
  256. const self = this;
  257. // 无索引
  258. this.datas = [];
  259. // 以key为索引indexedDB
  260. this.items = {};
  261. // 以排序为索引
  262. this.nodes = [];
  263. // 根节点
  264. this.children = [];
  265. // 树设置
  266. this.setting = setting;
  267. this.hasMark = false;
  268. if (this.setting.markExpandKey) {
  269. const markStr = getLocalCache(this.setting.markExpandKey);
  270. const markData = markStr ? markStr.split('|') : ['', ''];
  271. this.hasMark = markData[0] === this.setting.markExpandSubKey;
  272. this.markExpand = this.hasMark && markData[1] ? _.map(markData[1].split(','), _.toInteger) : [];
  273. } else if (this.setting.markFoldKey) {
  274. const markStr = getLocalCache(this.setting.markFoldKey);
  275. const markData = markStr ? markStr.split('|') : ['', ''];
  276. this.hasMark = markData[0] === this.setting.markFoldSubKey;
  277. this.markFold = this.hasMark && markData[1] ? _.map(markData[1].split(','), _.toInteger) : [];
  278. }
  279. // if (this.setting.treeCacheKey) {
  280. // localforage.getItem(this.setting.treeCacheKey).then(function (v) {
  281. // self.markFold = v && v.markFold ? v.markFold : [];
  282. // });
  283. // }
  284. }
  285. /**
  286. * 树结构根据显示排序
  287. */
  288. sortTreeNode(isResort) {
  289. const self = this;
  290. const addSortNodes = function (nodes) {
  291. if (!nodes) { return }
  292. for (let i = 0; i < nodes.length; i++) {
  293. self.nodes.push(nodes[i]);
  294. nodes[i].index = self.nodes.length - 1;
  295. if (!isResort) {
  296. nodes[i].children = self.getChildren(nodes[i]);
  297. } else {
  298. nodes[i].children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; })
  299. }
  300. addSortNodes(nodes[i].children);
  301. }
  302. };
  303. this.nodes = [];
  304. if (!isResort) {
  305. this.children = this.getChildren();
  306. } else {
  307. this.children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; });
  308. }
  309. addSortNodes(this.children);
  310. }
  311. /**
  312. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  313. * @param datas
  314. */
  315. loadDatas(datas) {
  316. const self = this;
  317. // 清空旧数据
  318. this.items = {};
  319. this.nodes = [];
  320. this.datas = [];
  321. this.children = [];
  322. // 加载全部数据
  323. datas.sort(function (a, b) {
  324. return a.level - b.level;
  325. });
  326. for (const data of datas) {
  327. const keyName = itemsPre + data[this.setting.id];
  328. if (this.items[keyName]) continue;
  329. const item = JSON.parse(JSON.stringify(data));
  330. item.children = [];
  331. item.expanded = true;
  332. item.visible = true;
  333. if (item[setting.pid] === setting.rootId) {
  334. this.children.push(item);
  335. } else {
  336. const parent = this.getParent(item);
  337. if (!parent) continue;
  338. parent.children.push(item);
  339. }
  340. this.items[keyName] = item;
  341. this.datas.push(item);
  342. }
  343. this.children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; });
  344. this.sortTreeNode(true);
  345. if (this.hasMark) {
  346. if (this.setting.markExpandKey) {
  347. this.expandByCustom(node => {
  348. return self.markExpand.indexOf(node[self.setting.id]) >= 0;
  349. });
  350. } else if (this.setting.markFoldKey) {
  351. this.expandByCustom(function (node) {
  352. return self.markFold.indexOf(node[self.setting.id]) === -1;
  353. });
  354. }
  355. } else {
  356. if (this.setting.autoExpand >= 0) {
  357. this.expandByLevel(this.setting.autoExpand);
  358. for (const node of this.nodes) {
  359. this._markExpandFold(node);
  360. }
  361. this._saveMarkExpandFold();
  362. }
  363. }
  364. }
  365. loadFilter(select, filterType = 'access') {
  366. const defaultValue = filterType === 'access' ? true : false;
  367. this.select = select ? select.split(',') : [];
  368. for (const d of this.datas) {
  369. d.filter = defaultValue;
  370. }
  371. for (const s of this.select) {
  372. const node = this.getItems(s);
  373. if (!node) continue;
  374. node.filter = !defaultValue;
  375. const posterity = this.getPosterity(node);
  376. for (const p of posterity) {
  377. p.filter = !defaultValue;
  378. }
  379. const parents = this.getAllParents(node);
  380. for (const p of parents) {
  381. p.filter = !defaultValue;
  382. }
  383. }
  384. for (const node of this.datas) {
  385. const parent = this.getParent(node);
  386. node.visible = parent ? (parent.expanded && parent.visible && !node.filter) : !node.filter;
  387. }
  388. }
  389. getItemsByIndex(index) {
  390. return this.nodes[index];
  391. }
  392. /**
  393. * 根据id获取树结构节点数据
  394. * @param {Number} id
  395. * @returns {Object}
  396. */
  397. getItems(id) {
  398. return this.items[itemsPre + id];
  399. };
  400. getNodeIndex(node) {
  401. return this.nodes.indexOf(node);
  402. }
  403. /**
  404. * 查找node的parent
  405. * @param {Object} node
  406. * @returns {Object}
  407. */
  408. getParent(node) {
  409. return this.getItems(node[this.setting.pid]);
  410. };
  411. getTopParent(node) {
  412. const parents = this.getAllParents(node);
  413. parents.sort((a, b) => { return a.level - b.level; });
  414. return parents[0];
  415. };
  416. getAllParents(node) {
  417. const parents = [];
  418. if (!node) return parents;
  419. if (node.full_path && node.full_path !== '') {
  420. const parentIds = node.full_path.split('-');
  421. parentIds.length = parentIds.length - 1;
  422. for (const id of parentIds) {
  423. if (id !== node[this.setting.id]) {
  424. parents.push(this.getItems(id));
  425. }
  426. }
  427. } else {
  428. let vP = this.getParent(node);
  429. while (vP) {
  430. parents.push(vP);
  431. vP = this.getParent(vP);
  432. }
  433. }
  434. return parents;
  435. }
  436. /**
  437. * 查找node的前兄弟节点
  438. * @param node
  439. * @returns {*}
  440. */
  441. getPreSiblingNode(node) {
  442. if (!node) return null;
  443. const parent = this.getParent(node);
  444. const siblings = parent ? parent.children : this.children;
  445. const index = siblings.indexOf(node);
  446. return (index > 0) ? siblings[index - 1] : null;
  447. }
  448. /**
  449. * 查找node的后兄弟节点
  450. * @param node
  451. * @returns {*}
  452. */
  453. getNextSiblingNode(node) {
  454. const parent = this.getParent(node);
  455. const siblings = parent ? parent.children : this.children;
  456. const index = siblings.indexOf(node);
  457. if (index >= 0 && index < siblings.length - 1) {
  458. return siblings[index + 1];
  459. } else {
  460. return null;
  461. }
  462. }
  463. /**
  464. * 根据path查找完整节点
  465. * @param {Number} path
  466. */
  467. getFullPathNodes(path) {
  468. const self = this, ids = path.split('-');
  469. if (ids.length > 0) {
  470. return this.nodes.filter((x) => {
  471. return ids.indexOf('' + x[self.setting.id]) >= 0;
  472. });
  473. } else {
  474. return [];
  475. }
  476. };
  477. /**
  478. * 查询node的已下载子节点
  479. * @param {Object} node
  480. * @returns {Array}
  481. */
  482. getChildren(node) {
  483. const setting = this.setting;
  484. const pid = node ? node[setting.id] : setting.rootId;
  485. const children = this.datas.filter(function (x) {
  486. return x[setting.pid] === pid;
  487. });
  488. children.sort((a, b) => { return a[setting.order] - b[setting.order]; });
  489. return children;
  490. };
  491. /**
  492. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  493. * @param node
  494. * @returns {*}
  495. * @private
  496. */
  497. _recursiveGetPosterity(node) {
  498. let posterity = node.children;
  499. for (const c of node.children) {
  500. posterity = posterity.concat(this._recursiveGetPosterity(c));
  501. }
  502. return posterity;
  503. };
  504. /**
  505. * 查询node的已下载的全部后代
  506. * @param {Object} node
  507. * @returns {Array}
  508. */
  509. getPosterity(node) {
  510. const self = this;
  511. let posterity;
  512. if (node.full_path !== '') {
  513. const reg = new RegExp('^' + node.full_path + '-');
  514. posterity = this.datas.filter(function (x) {
  515. return reg.test(x.full_path);
  516. });
  517. } else {
  518. posterity = this._recursiveGetPosterity(node);
  519. }
  520. posterity.sort(function (x, y) {
  521. return self.getNodeIndex(x) - self.getNodeIndex(y);
  522. });
  523. return posterity;
  524. };
  525. /**
  526. * 查询node是否是父节点的最后一个子节点
  527. * @param {Object} node
  528. * @returns {boolean}
  529. */
  530. isLastSibling(node) {
  531. const siblings = this.getChildren(this.getParent(node));
  532. return (siblings && siblings.length > 0) ? node[this.setting.order] === siblings[siblings.length - 1][this.setting.order] : false;
  533. };
  534. /**
  535. * 查询node是否是父节点的最后一个可见子节点
  536. * @param {Object} node
  537. * @returns {boolean}
  538. */
  539. isLastViewSibling(node) {
  540. const siblings = (this.getChildren(this.getParent(node))).filter(x => { return !x.filter });
  541. return (siblings && siblings.length > 0) ? node.order === siblings[siblings.length - 1].order : false;
  542. };
  543. /**
  544. * 提取节点key和索引数据
  545. * @param {Object} node - 节点
  546. * @returns {key}
  547. */
  548. getNodeKeyData(node) {
  549. const data = {};
  550. for (const key of this.setting.keys) {
  551. data[key] = node[key];
  552. }
  553. return data;
  554. };
  555. /**
  556. * 得到树结构构成id
  557. * @param node
  558. * @returns {*}
  559. */
  560. getNodeKey(node) {
  561. return node[this.setting.id];
  562. };
  563. _markExpandFold(node) {
  564. if (this.setting.markExpandKey && this.setting.markExpandSubKey) {
  565. if (node.expanded) {
  566. if (this.markExpand.indexOf(node[this.setting.id]) === -1)
  567. this.markExpand.push(node[this.setting.id]);
  568. } else {
  569. if (this.markExpand.indexOf(node[this.setting.id]) >= 0)
  570. this.markExpand.splice(this.markExpand.indexOf(node[this.setting.id]), 1);
  571. }
  572. } else if (this.setting.markFoldKey && this.setting.markFoldSubKey) {
  573. if (!node.expanded) {
  574. if (this.markFold.indexOf(node[this.setting.id]) === -1)
  575. this.markFold.push(node[this.setting.id]);
  576. } else {
  577. if (this.markFold.indexOf(node[this.setting.id]) >= 0)
  578. this.markFold.splice(this.markFold.indexOf(node[this.setting.id]), 1);
  579. }
  580. }
  581. }
  582. _saveMarkExpandFold() {
  583. if (this.setting.markExpandKey && this.setting.markExpandSubKey) {
  584. setLocalCache(this.setting.markExpandKey, this.setting.markExpandSubKey + '|' + this.markExpand.join(','));
  585. } else if (this.setting.markFoldKey && this.setting.markFoldSubKey) {
  586. setLocalCache(this.setting.markFoldKey, this.setting.markFoldSubKey + '|' + this.markFold.join(','));
  587. }
  588. // if (this.setting.treeCacheKey) {
  589. // localforage.setItem(this.setting.treeCacheKey, {
  590. // markFold: this.markFold,
  591. // time: new Date(),
  592. // });
  593. // }
  594. }
  595. /**
  596. * 刷新子节点是否可见
  597. * @param {Object} node
  598. * @private
  599. */
  600. _refreshChildrenVisible(node) {
  601. if (!node.children) {
  602. node.children = this.getChildren(node);
  603. }
  604. if (node.children && node.children.length > 0) {
  605. for (const child of node.children) {
  606. child.visible = node.expanded && node.visible && !child.filter;
  607. this._refreshChildrenVisible(child);
  608. }
  609. }
  610. };
  611. /**
  612. * 设置节点是否展开, 并控制子节点可见
  613. * @param {Object} node
  614. * @param {Boolean} expanded
  615. */
  616. setExpanded(node, expanded) {
  617. node.expanded = expanded;
  618. this._markExpandFold(node);
  619. this._refreshChildrenVisible(node);
  620. this._saveMarkExpandFold();
  621. };
  622. /**
  623. * 递归 设置节点展开状态
  624. * @param {Array} nodes - 需要设置状态的节点
  625. * @param {Object} parent - nodes的父节点
  626. * @param {Function} checkFun - 判断节点展开状态的方法
  627. * @private
  628. */
  629. _recursiveExpand(nodes, parent, checkFun) {
  630. for (const node of nodes) {
  631. const expanded = checkFun(node);
  632. if (node.expanded !== expanded) {
  633. node.expanded = expanded;
  634. this._markExpandFold(node);
  635. }
  636. node.visible = parent ? (parent.expanded && parent.visible && !node.filter) : !node.filter;
  637. this._recursiveExpand(node.children, node, checkFun);
  638. }
  639. }
  640. /**
  641. * 自定义展开规则
  642. * @param checkFun
  643. */
  644. expandByCustom(checkFun) {
  645. this._recursiveExpand(this.children, null, checkFun);
  646. this._saveMarkExpandFold();
  647. }
  648. /**
  649. * 展开到第几层
  650. * @param {Number} level - 展开层数
  651. */
  652. expandByLevel(level) {
  653. this.expandByCustom(function (n) {
  654. return n.level < level;
  655. });
  656. }
  657. /**
  658. * 自动展开节点node
  659. * @param node
  660. * @returns {*}
  661. */
  662. autoExpandNode(node) {
  663. const parents = this.getAllParents(node);
  664. const reload = [];
  665. for (const p of parents) {
  666. if (!p.expanded) {
  667. reload.push(p);
  668. this.setExpanded(p, true);
  669. }
  670. }
  671. return reload;
  672. }
  673. /**
  674. * 加载数据(动态),只加载不同部分
  675. * @param {Array} datas
  676. * @return {Array} 加载到树的数据
  677. * @privateA
  678. */
  679. _updateData(datas) {
  680. datas = datas instanceof Array ? datas : [datas];
  681. let loadedData = [];
  682. for (const data of datas) {
  683. let node = this.getItems(data[this.setting.id]);
  684. if (node) {
  685. for (const prop in data) {
  686. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  687. if (prop === this.setting.pid) {
  688. loadedData.push(this.getItems(node[this.setting.pid]));
  689. loadedData.push(this.getItems(data[this.setting.pid]));
  690. }
  691. if (prop === this.setting.order) {
  692. loadedData = loadedData.concat(this.getPosterity(node));
  693. }
  694. node[prop] = data[prop];
  695. }
  696. }
  697. loadedData.push(node);
  698. }
  699. }
  700. loadedData = _.uniq(loadedData);
  701. for (const node of loadedData) {
  702. if (node) {
  703. node.children = this.getChildren(node);
  704. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  705. } else {
  706. this.children = this.getChildren(null);
  707. }
  708. }
  709. this.sortTreeNode(true);
  710. return loadedData;
  711. };
  712. /**
  713. * 加载数据(动态),只加载不同部分
  714. * @param {Array} datas
  715. * @return {Array} 加载到树的数据
  716. * @privateA
  717. */
  718. _loadData(datas) {
  719. datas = datas instanceof Array ? datas : [datas];
  720. const loadedData = [], resortData = [];
  721. for (const data of datas) {
  722. let node = this.getItems(data[this.setting.id]);
  723. if (node) {
  724. const parent = this.getItems(node[this.setting.pid]);
  725. for (const prop in data) {
  726. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  727. node[prop] = data[prop];
  728. if (parent && resortData.indexOf(parent) === -1) {
  729. resortData.push(parent);
  730. }
  731. }
  732. }
  733. loadedData.push(node);
  734. } else {
  735. const keyName = itemsPre + data[this.setting.id];
  736. const node = JSON.parse(JSON.stringify(data));
  737. this.items[keyName] = node;
  738. this.datas.push(node);
  739. node.expanded = true;
  740. node.visible = true;
  741. loadedData.push(node);
  742. if (resortData.indexOf(node) === -1) {
  743. resortData.push(node);
  744. }
  745. const parent = this.getItems(node[this.setting.pid]);
  746. if (parent && resortData.indexOf(parent) === -1) {
  747. resortData.push(parent);
  748. } else {
  749. resortData.push(this.setting.rootId);
  750. }
  751. }
  752. }
  753. for (const node of resortData) {
  754. if (node && node !== this.setting.rootId) {
  755. node.children = this.getChildren(node);
  756. } else {
  757. this.children = this.getChildren(null);
  758. }
  759. }
  760. this.sortTreeNode(true);
  761. for (const node of loadedData) {
  762. if (!node.expanded) {
  763. this.setExpanded(node, true);
  764. }
  765. }
  766. return loadedData;
  767. };
  768. /**
  769. * 清理数据(动态)
  770. * @param datas
  771. * @private
  772. */
  773. _freeData(datas) {
  774. datas = datas instanceof Array ? datas : [datas];
  775. const freeDatas = [];
  776. const removeArrayData = function (array, data) {
  777. const index = array.indexOf(data);
  778. array.splice(index, 1);
  779. };
  780. for (const data of datas) {
  781. const node = this.getItems(data[this.setting.id]);
  782. if (node) {
  783. freeDatas.push(node);
  784. node.deleteIndex = this.nodes.indexOf(node);
  785. delete this.items[itemsPre + node[this.setting.id]];
  786. if (node[this.setting.pid] !== this.setting.rootId) {
  787. const parent = this.getItems(node[this.setting.pid]);
  788. if (parent) {
  789. removeArrayData(parent.children, node);
  790. }
  791. } else {
  792. removeArrayData(this.children, node);
  793. }
  794. removeArrayData(this.datas, node);
  795. }
  796. }
  797. for (const node of freeDatas) {
  798. removeArrayData(this.nodes, node);
  799. }
  800. return freeDatas;
  801. };
  802. /**
  803. * 加载需展开的数据
  804. * @param {Array} datas
  805. * @returns {Array}
  806. * @private
  807. */
  808. _loadExpandData(datas) {
  809. datas = datas instanceof Array ? datas : [datas];
  810. const loadedData = [], existData = [], expandData = [], resortData = [];
  811. for (const data of datas) {
  812. let node = this.getItems(data[this.setting.id]);
  813. if (node) {
  814. existData.push(node);
  815. } else {
  816. const keyName = itemsPre + data[this.setting.id];
  817. const node = JSON.parse(JSON.stringify(data));
  818. this.items[keyName] = node;
  819. this.datas.push(node);
  820. node.expanded = false;
  821. node.visible = true;
  822. loadedData.push(node);
  823. if (resortData.indexOf(node) === -1) {
  824. resortData.push(node);
  825. }
  826. const parent = this.getItems(node[this.setting.pid]);
  827. if (parent && resortData.indexOf(parent) === -1) {
  828. resortData.push(parent);
  829. }
  830. }
  831. }
  832. for (const node of resortData) {
  833. node.children = this.getChildren(node);
  834. }
  835. this.sortTreeNode(true);
  836. for (const node of loadedData) {
  837. if (!node.expanded) {
  838. this.setExpanded(node, true);
  839. }
  840. }
  841. for (const node of existData) {
  842. const parent = this.getItems(node[this.setting.pid]);
  843. if (expandData.indexOf(parent) === -1) {
  844. expandData.push(parent);
  845. if (!parent.expanded) {
  846. this.setExpanded(parent, true);
  847. }
  848. }
  849. if (!node.expanded) {
  850. this.setExpanded(node, true);
  851. }
  852. }
  853. return [loadedData, expandData];
  854. };
  855. /**
  856. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  857. *
  858. * @param data - 更新的数据 {update, create, delete}
  859. * @returns {{}}
  860. */
  861. loadPostData(data) {
  862. const result = {};
  863. if (data.delete) {
  864. result.delete = this._freeData(data.delete);
  865. }
  866. if (data.create) {
  867. result.create = this._loadData(data.create);
  868. }
  869. if (data.update) {
  870. result.update = this._updateData(data.update);
  871. }
  872. return result;
  873. }
  874. /**
  875. * 加载子节点
  876. * @param {Object} node
  877. * @param {function} callback
  878. */
  879. loadChildren(node, callback) {
  880. if (this.setting.url !== '') {
  881. const self = this;
  882. postData(this.setting.url, { postType: 'load-child', id: this.getNodeKeyData(node) }, function (data) {
  883. self._loadData(data);
  884. callback();
  885. });
  886. }
  887. };
  888. _getDefaultNodeData(node) {
  889. const result = {};
  890. for (const prop in node) {
  891. if (['children', 'visible', 'expanded'].indexOf(prop) >= 0) continue;
  892. result[prop] = node[prop];
  893. }
  894. return result;
  895. }
  896. getDefaultData(node) {
  897. if (node instanceof Array) {
  898. const arr = [];
  899. for (const n of node) {
  900. arr.push(this._getDefaultNodeData(n));
  901. }
  902. return arr;
  903. } else {
  904. this._getDefaultNodeData(node);
  905. }
  906. }
  907. checkParent(node, field = 'check') {
  908. const parent = this.getParent(node);
  909. return parent
  910. ? (parent[field] ? parent[field] : this.checkParent(parent, field))
  911. : false;
  912. }
  913. checkChildren(node, field = 'check') {
  914. for (const child of node.children) {
  915. if (child[field]) {
  916. return true;
  917. } else if (this.checkChildren(child, field)) {
  918. return true;
  919. }
  920. }
  921. return false;
  922. }
  923. }
  924. class MeasureTree extends BaseTree {
  925. addData(datas) {
  926. const loadedData = [];
  927. for (const data of datas) {
  928. let node = this.getItems(data[this.setting.id]);
  929. if (node) {
  930. for (const prop in data) {
  931. if (data[prop] !== undefined) {
  932. node[prop] = data[prop];
  933. }
  934. }
  935. loadedData.push(node);
  936. } else {
  937. const keyName = itemsPre + data[this.setting.id];
  938. const node = JSON.parse(JSON.stringify(data));
  939. this.items[keyName] = node;
  940. this.datas.push(node);
  941. node.expanded = false;
  942. node.visible = true;
  943. loadedData.push(node);
  944. }
  945. }
  946. this.sortTreeNode();
  947. for (const node of loadedData) {
  948. const children = node.children;
  949. if (!node.expanded && children.length > 0) {
  950. node.expanded = true;
  951. this._refreshChildrenVisible(node);
  952. }
  953. }
  954. return loadedData;
  955. }
  956. removeData(datas) {
  957. datas.sort(function (a, b) {
  958. return b.level - a.level;
  959. });
  960. const removeArrayData = function (array, data) {
  961. const index = array.indexOf(data);
  962. array.splice(index, 1);
  963. };
  964. for (const data of datas) {
  965. const node = this.getItems(data[this.setting.id]);
  966. if (node && this.getChildren(node).length === 0) {
  967. delete this.items[itemsPre + node[this.setting.id]];
  968. if (node[this.setting.pid] !== this.setting.rootId) {
  969. const parent = this.items[itemsPre + node[this.setting.pid]];
  970. removeArrayData(parent.children, node);
  971. }
  972. removeArrayData(this.datas, node);
  973. removeArrayData(this.nodes, node);
  974. }
  975. }
  976. };
  977. loadLeafData(data) {
  978. const datas = data instanceof Array ? data : [data];
  979. for (const d of datas) {
  980. let node = this.getItems(d[this.setting.id]);
  981. if (node && node.is_leaf) {
  982. for (const prop in d) {
  983. if (data[prop] !== undefined) {
  984. node[prop] = d[prop];
  985. }
  986. }
  987. }
  988. }
  989. };
  990. }
  991. class FxTree extends BaseTree {
  992. /**
  993. * 检查节点是否是最底层项目节
  994. * @param node
  995. * @returns {boolean}
  996. */
  997. isLeafXmj(node) {
  998. if (node.b_code && node.b_code !== '') {
  999. return false;
  1000. }
  1001. for (const child of node.children) {
  1002. if (!child.b_code || child.b_code === '') {
  1003. return false;
  1004. }
  1005. }
  1006. return true;
  1007. }
  1008. /**
  1009. * 查询最底层项目节(本身或父项)
  1010. * @param {Object} node - 查询节点
  1011. * @returns {Object}
  1012. */
  1013. getLeafXmjParent(node) {
  1014. let parent = node;
  1015. while (parent) {
  1016. if (this.isLeafXmj(parent)) {
  1017. return parent;
  1018. } else {
  1019. parent = this.getParent(parent);
  1020. }
  1021. }
  1022. return null;
  1023. }
  1024. /**
  1025. * 展开至最底层项目节
  1026. */
  1027. expandToLeafXmj() {
  1028. const self = this;
  1029. this.expandByCustom(function (node) {
  1030. if (node.b_code && node.b_code !== '') {
  1031. return false;
  1032. } else {
  1033. return !self.isLeafXmj(node);
  1034. }
  1035. })
  1036. }
  1037. /**
  1038. * 展开至计算项
  1039. */
  1040. expandByCalcFields() {
  1041. const self = this;
  1042. this.expandByCustom(function (node) {
  1043. for (const field of self.setting.calcFields) {
  1044. if (node[field]) {
  1045. return true;
  1046. }
  1047. }
  1048. return false;
  1049. })
  1050. }
  1051. checkCodeType() {
  1052. for (const node of this.nodes) {
  1053. if (!node.code) continue;
  1054. if (node.code.indexOf('-') >= 0) {
  1055. return '07';
  1056. } else if (node.code.length > 5) {
  1057. const num = _.toNumber(node.code);
  1058. if (num && num > 10000) return '18';
  1059. }
  1060. }
  1061. return '18';
  1062. }
  1063. getCodeSplit() {
  1064. return this.checkCodeType() === '07' ? '-' : '0';
  1065. }
  1066. }
  1067. class LedgerTree extends FxTree {
  1068. /**
  1069. *
  1070. * @param parent
  1071. * @param node
  1072. * @private
  1073. */
  1074. _getNodesParents(parents, nodes) {
  1075. for (const node of nodes) {
  1076. const parent = this.getParent(node);
  1077. if (parent) {
  1078. const paths = this.getFullPathNodes(parent.full_path);
  1079. for (const p of paths) {
  1080. if (parents.indexOf(p) === -1) {
  1081. parents.push(p);
  1082. }
  1083. }
  1084. }
  1085. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  1086. parents.push(node);
  1087. }
  1088. }
  1089. }
  1090. _getReCalcNodes(reCalcNodes, nodes) {
  1091. for (const node of nodes) {
  1092. const parent = this.getParent(node);
  1093. if (parent) {
  1094. const paths = this.getFullPathNodes(parent.full_path);
  1095. for (const p of paths) {
  1096. if (reCalcNodes.indexOf(p) === -1) {
  1097. reCalcNodes.push(p);
  1098. }
  1099. }
  1100. }
  1101. // 最底层项目节,也需要计算
  1102. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  1103. reCalcNodes.push(node);
  1104. //}
  1105. }
  1106. }
  1107. /**
  1108. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  1109. *
  1110. * @param data - 更新的数据 {update, create, delete}
  1111. * @returns {{}}
  1112. */
  1113. loadPostData(data) {
  1114. const result = {}, reCalcNodes = [];
  1115. if (!data) return result;
  1116. if (data.delete) {
  1117. result.delete = this._freeData(data.delete);
  1118. this._getReCalcNodes(reCalcNodes, result.delete);
  1119. }
  1120. if (data.create) {
  1121. result.create = this._loadData(data.create);
  1122. this._getReCalcNodes(reCalcNodes, result.create);
  1123. }
  1124. if (data.update) {
  1125. result.update = this._updateData(data.update);
  1126. this._getReCalcNodes(reCalcNodes, result.update);
  1127. }
  1128. reCalcNodes.sort((a, b) => {
  1129. return b.level - a.level;
  1130. });
  1131. for (const node of reCalcNodes) {
  1132. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  1133. }
  1134. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  1135. return result;
  1136. }
  1137. }
  1138. class ReviseTree extends LedgerTree {
  1139. constructor (setting) {
  1140. super(setting);
  1141. this.price = [];
  1142. }
  1143. checkNodeUsed(node, pos) {
  1144. if (node.children && node.children.length > 0) {
  1145. for (const child of node.children) {
  1146. const used = this.checkNodeUsed(child, pos);
  1147. if (used) return used;
  1148. }
  1149. } else {
  1150. if (node.used) return node.used;
  1151. const posRange = pos.getLedgerPos(node.id);
  1152. if (posRange && posRange.length > 0) {
  1153. for (const p of posRange) {
  1154. if (p.used) return p.used;
  1155. }
  1156. }
  1157. }
  1158. return false;
  1159. }
  1160. loadRevisePrice(price, decimal) {
  1161. this.decimal = decimal;
  1162. this.price = price || [];
  1163. this.rela_price = [];
  1164. this.common_price = [];
  1165. this.price.forEach(x => {
  1166. if (x.rela_lid) {
  1167. x.rela_lid = x.rela_lid.split(',');
  1168. this.rela_price.push(x);
  1169. } else {
  1170. this.common_price.push(x);
  1171. }
  1172. });
  1173. }
  1174. checkRevisePrice(d) {
  1175. const setting = this.setting;
  1176. const pid = this.getAllParents(d).map(x => { return x[setting.id] + ''; });
  1177. const checkRela = function(rela_lid) {
  1178. if (!rela_lid || rela_lid.length === 0) return false;
  1179. for (const lid of rela_lid) {
  1180. if (pid.indexOf(lid) >= 0) return true;
  1181. }
  1182. return false;
  1183. };
  1184. let p = this.rela_price.find(x => {
  1185. return x.b_code === d.b_code &&
  1186. ((!x.name && !d.name) || x.name === d.name) &&
  1187. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1188. checkZero(x.org_price - d.unit_price) &&
  1189. checkRela(x.rela_lid);
  1190. });
  1191. if (!p) p = this.common_price.find(x => {
  1192. return x.b_code === d.b_code &&
  1193. ((!x.name && !d.name) || x.name === d.name) &&
  1194. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1195. checkZero(x.org_price - d.unit_price);
  1196. });
  1197. if (!p) return false;
  1198. d.org_price = p.org_price;
  1199. d.unit_price = p.new_price;
  1200. d.deal_tp = ZhCalc.mul(d.deal_qty, d.unit_price, this.decimal.tp);
  1201. d.sgfh_tp = ZhCalc.mul(d.sgfh_qty, d.unit_price, this.decimal.tp);
  1202. d.sjcl_tp = ZhCalc.mul(d.sjcl_qty, d.unit_price, this.decimal.tp);
  1203. d.qtcl_tp = ZhCalc.mul(d.qtcl_qty, d.unit_price, this.decimal.tp);
  1204. d.total_price = ZhCalc.mul(d.quantity, d.unit_price, this.decimal.tp);
  1205. return true;
  1206. }
  1207. loadDatas(datas) {
  1208. super.loadDatas(datas);
  1209. if (this.price.length > 0) {
  1210. for (const d of this.datas) {
  1211. if (d.children && d.children.length > 0) continue;
  1212. if (!d.b_code) continue;
  1213. this.checkRevisePrice(d);
  1214. }
  1215. }
  1216. }
  1217. loadPostReivsePrice(data) {
  1218. let result = false;
  1219. if (!this.price) return result;
  1220. for (const d of data) {
  1221. if (!d.is_leaf || !d.b_code) continue;
  1222. if (this.checkRevisePrice(d)) result = true;
  1223. }
  1224. return result;
  1225. }
  1226. loadPostData(data) {
  1227. const result = {}, reCalcNodes = [];
  1228. if (!data) return result;
  1229. if (data.delete) {
  1230. result.delete = this._freeData(data.delete);
  1231. this._getReCalcNodes(reCalcNodes, result.delete);
  1232. }
  1233. if (data.create) {
  1234. result.create = this._loadData(data.create);
  1235. this.loadPostReivsePrice(result.create);
  1236. this._getReCalcNodes(reCalcNodes, result.create);
  1237. }
  1238. if (data.update) {
  1239. result.update = this._updateData(data.update);
  1240. this.loadPostReivsePrice(result.update);
  1241. this._getReCalcNodes(reCalcNodes, result.update);
  1242. }
  1243. reCalcNodes.sort((a, b) => {
  1244. return b.level - a.level;
  1245. });
  1246. for (const node of reCalcNodes) {
  1247. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  1248. }
  1249. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  1250. return result;
  1251. }
  1252. }
  1253. class StageTree extends FxTree {
  1254. /**
  1255. * 构造函数
  1256. */
  1257. constructor(setting) {
  1258. super(setting);
  1259. // stage关联索引
  1260. this.stageItems = {};
  1261. }
  1262. _loadLockedNodes(ids){
  1263. this.Locked = ids || [];
  1264. for (const f of this.Locked) {
  1265. const node = this.getItems(f.ledger_id);
  1266. node.locked = true;
  1267. const posterity = this.getPosterity(node);
  1268. for (const pn of posterity) {
  1269. pn.locked = true;
  1270. pn.lock = true;
  1271. }
  1272. }
  1273. }
  1274. /**
  1275. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1276. * @param datas
  1277. */
  1278. loadDatas(datas, locked) {
  1279. super.loadDatas(datas);
  1280. // 清空旧数据
  1281. this.stageItems = {};
  1282. // 加载全部数据
  1283. for (const data of this.datas) {
  1284. const keyName = itemsPre + data[this.setting.stageId];
  1285. this.stageItems[keyName] = data;
  1286. }
  1287. this._loadLockedNodes(locked)
  1288. }
  1289. // 解锁相关
  1290. _loadPwdCache() {
  1291. const cache = getLocalCache(this.pwdCacheKey);
  1292. if (!cache) return;
  1293. const cacheArr = cache.split('|');
  1294. for (const ca of cacheArr) {
  1295. if (!ca) continue;
  1296. const [ledger_id, pwd] = ca.split(':');
  1297. if (!ledger_id || !pwd) continue;
  1298. const p = this.pwd.find(x => {return x.ledger_id == ledger_id});
  1299. if (p) p.check = p.pwd === pwd;
  1300. }
  1301. }
  1302. loadPwd(data, cacheKey, confirmList) {
  1303. this.loadingPwd = true;
  1304. try {
  1305. this.pwdCacheKey = cacheKey;
  1306. this.confirmList = confirmList;
  1307. this.pwd = data;
  1308. this._loadPwdCache();
  1309. this._loadOnlineConfirm();
  1310. for (const p of this.pwd) {
  1311. p.node = this.getItems(p.ledger_id);
  1312. this.lockNode(p, !p.check);
  1313. }
  1314. } catch(err) {}
  1315. this.loadingPwd = false;
  1316. }
  1317. // 确认相关
  1318. _loadOnlineConfirm() {
  1319. const cacheArr = this.confirmList;
  1320. for (const ca of cacheArr) {
  1321. if (!ca) continue;
  1322. if (!ca.ledger_id) continue;
  1323. const p = this.pwd.find(x => {return x.ledger_id == ca.ledger_id});
  1324. if (p) {
  1325. p.confirm = true;
  1326. p.confirm_time = ca.create_time;
  1327. }
  1328. }
  1329. }
  1330. getStageItems(id) {
  1331. return this.stageItems[itemsPre + id];
  1332. }
  1333. loadStageData(datas, fieldPre, fields) {
  1334. datas = datas instanceof Array ? datas : [datas];
  1335. const loadedData = [];
  1336. for (const data of datas) {
  1337. let node = this.getStageItems(data.lid);
  1338. if (node) {
  1339. for (const prop of fields) {
  1340. if (data[prop] !== undefined) {
  1341. node[fieldPre + prop] = data[prop];
  1342. }
  1343. }
  1344. loadedData.push(node);
  1345. }
  1346. }
  1347. }
  1348. loadPreStageData(datas) {
  1349. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1350. }
  1351. loadCurStageData(datas) {
  1352. this.loadStageData(datas, '', this.setting.updateFields);
  1353. }
  1354. /**
  1355. * 加载数据(动态),只加载不同部分
  1356. * @param {Array} datas
  1357. * @return {Array} 加载到树的数据
  1358. * @privateA
  1359. */
  1360. _updateData(datas) {
  1361. datas = datas instanceof Array ? datas : [datas];
  1362. let loadedData = [];
  1363. for (const data of datas) {
  1364. let node = this.getItems(data[this.setting.id]);
  1365. if (node) {
  1366. for (const prop in data) {
  1367. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1368. }
  1369. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1370. if (prop === this.setting.pid) {
  1371. loadedData.push(this.getItems(node[this.setting.pid]));
  1372. loadedData.push(this.getItems(data[this.setting.pid]));
  1373. }
  1374. node[prop] = data[prop];
  1375. }
  1376. }
  1377. loadedData.push(node);
  1378. }
  1379. }
  1380. loadedData = _.uniq(loadedData);
  1381. for (const node of loadedData) {
  1382. node.children = this.getChildren(node);
  1383. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1384. }
  1385. this.sortTreeNode(true);
  1386. return loadedData;
  1387. };
  1388. /**
  1389. * 加载数据(动态),只加载不同部分
  1390. * @param {Array} datas
  1391. * @return {Array} 加载到树的数据
  1392. * @privateA
  1393. */
  1394. _updateStageData(datas) {
  1395. datas = datas instanceof Array ? datas : [datas];
  1396. const loadedData = [];
  1397. for (const data of datas) {
  1398. let node = this.getStageItems(data.lid);
  1399. if (node) {
  1400. for (const prop of this.setting.updateFields) {
  1401. if (data[prop] !== undefined) {
  1402. node[prop] = data[prop];
  1403. }
  1404. }
  1405. loadedData.push(node);
  1406. }
  1407. }
  1408. return loadedData;
  1409. };
  1410. /**
  1411. *
  1412. * @param parent
  1413. * @param node
  1414. * @private
  1415. */
  1416. _getNodesParents(parents, nodes) {
  1417. for (const node of nodes) {
  1418. const parent = this.getParent(node);
  1419. if (parent) {
  1420. const paths = this.getFullPathNodes(parent.full_path);
  1421. for (const p of paths) {
  1422. if (parents.indexOf(p) === -1) {
  1423. parents.push(p);
  1424. }
  1425. }
  1426. }
  1427. if (node.children && node.children.length > 0) {
  1428. parents.push(node);
  1429. }
  1430. }
  1431. }
  1432. _updateDgnData(datas) {
  1433. datas = datas instanceof Array ? datas : [datas];
  1434. let loadedData = [];
  1435. for (const data of datas) {
  1436. let node = this.getStageItems(data.id);
  1437. if (node) {
  1438. for (const prop in data) {
  1439. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1440. node[prop] = data[prop];
  1441. }
  1442. }
  1443. loadedData.push(node);
  1444. }
  1445. }
  1446. return loadedData;
  1447. }
  1448. /**
  1449. * 提交数据至后端,返回的前端树结构应刷新的部分
  1450. * StageTree仅有更新CurStage部分,不需要增删
  1451. *
  1452. * @param data - 需要更新的数据
  1453. * @returns {Array} - 界面需要刷新的数据
  1454. */
  1455. loadPostStageData(data) {
  1456. let result, parents = [];
  1457. if (data.bills) {
  1458. result = this._updateData(data.bills);
  1459. this._getNodesParents(parents, result);
  1460. }
  1461. if (data.curStageData) {
  1462. result = this._updateStageData(data.curStageData);
  1463. this._getNodesParents(parents, result);
  1464. }
  1465. if (data.dgn) {
  1466. const dgnResult = this._updateDgnData(data.dgn);
  1467. result = result ? result.concat(dgnResult) : dgnResult;
  1468. }
  1469. result = result ? result.concat(parents) : parents;
  1470. result.sort((a, b) => {
  1471. return b.level - a.level;
  1472. });
  1473. for (const node of result) {
  1474. treeCalc.calculateNode(this, node);
  1475. }
  1476. return result;
  1477. }
  1478. _savePwdCache() {
  1479. const cache = [];
  1480. for (const p of this.pwd) {
  1481. if (p.check && p.pwd) cache.push(p.ledger_id + ':' + p.pwd);
  1482. }
  1483. setLocalCache(this.pwdCacheKey, cache.join('|'));
  1484. }
  1485. _getPwdPosterity(node) {
  1486. const children = [];
  1487. for (const c of node.children) {
  1488. const cPwd = this.pwd.find(x => {return x.node && x.node.id === c.id});
  1489. if (!cPwd) children.push(c);
  1490. }
  1491. let posterity = [...children];
  1492. for (const c of children) {
  1493. posterity = posterity.concat(this._getPwdPosterity(c));
  1494. }
  1495. return posterity;
  1496. }
  1497. lockNode(p, isLock) {
  1498. const refresh = [];
  1499. p.check = !isLock;
  1500. p.node.lock = isLock;
  1501. refresh.push(this.getNodeIndex(p.node));
  1502. const posterity = this._getPwdPosterity(p.node);
  1503. for (const pn of posterity) {
  1504. pn.lock = pn.locked || isLock;
  1505. refresh.push(this.getNodeIndex(pn));
  1506. }
  1507. if (!this.loadingPwd) this._savePwdCache();
  1508. return refresh;
  1509. }
  1510. getLockedInfo(node) {
  1511. const ownerNodes = this.getAllParents(node);
  1512. const lockedInfo = [];
  1513. for (const on of ownerNodes) {
  1514. const filter = this.Locked.filter(x => { return x.ledger_id === on.ledger_id; });
  1515. lockedInfo.push(...filter);
  1516. }
  1517. return lockedInfo;
  1518. }
  1519. }
  1520. class MasterTree extends FxTree {
  1521. /**
  1522. * 构造函数
  1523. */
  1524. constructor(setting) {
  1525. super(setting);
  1526. // 关联索引
  1527. this.masterItems = {};
  1528. }
  1529. /**
  1530. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1531. * @param datas
  1532. */
  1533. loadDatas(datas) {
  1534. super.loadDatas(datas);
  1535. // 清空旧数据
  1536. this.masterItems = {};
  1537. // minor数据缓存
  1538. this.minorData = {};
  1539. // 加载全部数据
  1540. for (const data of this.datas) {
  1541. const keyName = itemsPre + data[this.setting.masterId];
  1542. this.masterItems[keyName] = data;
  1543. }
  1544. }
  1545. /**
  1546. * 根据关联id,查找节点
  1547. * @param id
  1548. * @returns {*}
  1549. */
  1550. getMasterItems(id) {
  1551. return this.masterItems[itemsPre + id];
  1552. }
  1553. /**
  1554. * 加载关联数据
  1555. *
  1556. * @param {Array|Object}datas - 需要关联的数据
  1557. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1558. * @param {Array} fields - 关联字段
  1559. * @returns {Array}
  1560. */
  1561. loadMinorData(datas, fieldSuf, fields, calcFields) {
  1562. for (const cf of calcFields) {
  1563. this.setting.calcFields.push(cf + fieldSuf);
  1564. }
  1565. if (!datas) return;
  1566. datas = datas instanceof Array ? datas : [datas];
  1567. this.minorData[fieldSuf] = datas;
  1568. const loadedData = [];
  1569. for (const data of datas) {
  1570. let node = this.getMasterItems(data[this.setting.minorId]);
  1571. if (node) {
  1572. for (const prop of fields) {
  1573. if (data[prop] !== undefined) {
  1574. node[prop + fieldSuf] = data[prop];
  1575. }
  1576. }
  1577. loadedData.push(node);
  1578. }
  1579. }
  1580. return loadedData;
  1581. }
  1582. }
  1583. class FilterTree extends BaseTree {
  1584. clearDatas() {
  1585. this.items = {};
  1586. this.nodes = [];
  1587. this.datas = [];
  1588. this.children = [];
  1589. }
  1590. addData(data, fields) {
  1591. const item = {};
  1592. for (const prop in data) {
  1593. if (fields.indexOf(prop) >= 0) {
  1594. item[prop] = data[prop];
  1595. }
  1596. }
  1597. const keyName = itemsPre + item[this.setting.id];
  1598. if (!this.items[keyName]) {
  1599. item.children = [];
  1600. item.is_leaf = true;
  1601. item.expanded = true;
  1602. item.visible = true;
  1603. this.items[keyName] = item;
  1604. this.datas.push(item);
  1605. if (item[setting.pid] === setting.rootId) {
  1606. this.children.push(item);
  1607. } else {
  1608. const parent = this.getParent(item);
  1609. if (parent) {
  1610. parent.is_leaf = false;
  1611. parent.children.push(item);
  1612. }
  1613. }
  1614. } else {
  1615. return this.items[keyName];
  1616. }
  1617. return item;
  1618. }
  1619. }
  1620. class GatherTree extends BaseTree {
  1621. clearDatas() {
  1622. this.items = {};
  1623. this.nodes = [];
  1624. this.datas = [];
  1625. this.children = [];
  1626. }
  1627. get newId() {
  1628. if (!this._maxId) {
  1629. this._maxId = 0;
  1630. }
  1631. this._maxId++;
  1632. return this._maxId;
  1633. }
  1634. addNode(data, parent) {
  1635. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1636. let item = _.find(this.items, data);
  1637. if (item) return item;
  1638. item = data;
  1639. item[this.setting.id] = this.newId;
  1640. const keyName = itemsPre + item[this.setting.id];
  1641. item.children = [];
  1642. item.is_leaf = true;
  1643. item.expanded = true;
  1644. item.visible = true;
  1645. this.items[keyName] = item;
  1646. this.datas.push(item);
  1647. if (parent) {
  1648. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  1649. item[this.setting.level] = parent[this.setting.level] + 1;
  1650. item[this.setting.order] = parent.children.length + 1;
  1651. parent.is_leaf = false;
  1652. parent.children.push(item);
  1653. } else {
  1654. item[this.setting.fullPath] = '' + item[this.setting.id];
  1655. item[this.setting.level] = 1;
  1656. item[this.setting.order] = this.children.length + 1;
  1657. this.children.push(item);
  1658. }
  1659. return item;
  1660. }
  1661. sortTreeNodeCustom(field, fun, isResort) {
  1662. const self = this;
  1663. const sortNodes = function (nodes) {
  1664. nodes.sort(function (a, b) {
  1665. return fun(a[field], b[field]);
  1666. });
  1667. for (const [i, node] of nodes.entries()) {
  1668. node[self.setting.order] = i + 1;
  1669. }
  1670. };
  1671. const addSortNodes = function (nodes) {
  1672. if (!nodes) { return }
  1673. for (let i = 0; i < nodes.length; i++) {
  1674. self.nodes.push(nodes[i]);
  1675. nodes[i].index = self.nodes.length - 1;
  1676. if (!isResort) {
  1677. nodes[i].children = self.getChildren(nodes[i]);
  1678. }
  1679. sortNodes(nodes[i].children);
  1680. addSortNodes(nodes[i].children);
  1681. }
  1682. };
  1683. this.nodes = [];
  1684. if (!isResort) {
  1685. this.children = this.getChildren();
  1686. }
  1687. sortNodes(this.children);
  1688. addSortNodes(this.children);
  1689. }
  1690. }
  1691. class CompareTree extends FxTree {
  1692. constructor(setting) {
  1693. super(setting);
  1694. this._newId = 1;
  1695. }
  1696. get newId() {
  1697. return this._newId++;
  1698. }
  1699. findCompareNode(node, parent) {
  1700. if (this.setting.findNode) {
  1701. return this.setting.findNode(this, node, parent);
  1702. } else {
  1703. const siblings = parent ? parent.children : this.children;
  1704. return siblings.find(function (x) {
  1705. return node.b_code
  1706. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1707. : x.code === node.code && x.name === node.name;
  1708. });
  1709. }
  1710. }
  1711. loadCompareNode(source, node, parent, loadFun) {
  1712. let cur = this.findCompareNode(node, parent);
  1713. if (!cur) {
  1714. const siblings = parent ? parent.children : this.children;
  1715. const id = this.newId;
  1716. cur = {
  1717. children: [], pos: [],
  1718. code: node.code, b_code: node.b_code, name: node.name,
  1719. unit: node.unit, unit_price: node.unit_price,
  1720. };
  1721. cur[this.setting.id] = id;
  1722. cur[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1723. cur[this.setting.full_path] = parent ? parent[this.setting.full_path] + '-' + id : '' + id;
  1724. cur[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  1725. cur[this.setting.order] = siblings.length + 1;
  1726. siblings.push(cur);
  1727. this.datas.push(cur);
  1728. }
  1729. loadFun(cur, node, source);
  1730. for (const c of node.children) {
  1731. this.loadCompareNode(source, c, cur, loadFun);
  1732. }
  1733. }
  1734. generateSortNodes() {
  1735. const self = this;
  1736. const addSortNode = function (node) {
  1737. self.nodes.push(node);
  1738. for (const c of node.children) {
  1739. addSortNode(c);
  1740. }
  1741. }
  1742. this.nodes = [];
  1743. for (const n of this.children) {
  1744. addSortNode(n);
  1745. }
  1746. }
  1747. loadCompareTree(data, loadFun) {
  1748. for (const c of data.billsTree.children) {
  1749. this.loadCompareNode(data, c, null, loadFun);
  1750. }
  1751. }
  1752. calculateDiffer() {
  1753. if (this.setting.calcDiffer) {
  1754. for (const d of this.datas) {
  1755. this.setting.calcDiffer(d);
  1756. }
  1757. }
  1758. }
  1759. loadCompareData(data1, data2) {
  1760. this.loadCompareTree(data1, this.setting.loadInfo1);
  1761. this.loadCompareTree(data2, this.setting.loadInfo2);
  1762. for (const d of this.datas) {
  1763. d.is_leaf = d.children.length === 0;
  1764. d.expanded = true;
  1765. d.visible = true;
  1766. this.items[itemsPre + d[this.setting.id]] = d;
  1767. }
  1768. this.generateSortNodes();
  1769. this.calculateDiffer();
  1770. if (this.setting.afterLoad) this.setting.afterLoad(this);
  1771. }
  1772. }
  1773. class TreeGatherTree extends FxTree {
  1774. constructor(setting) {
  1775. super(setting);
  1776. this._newId = 1;
  1777. }
  1778. get newId() {
  1779. return this._newId++;
  1780. }
  1781. loadGatherNode(node, parent, index, loadFun) {
  1782. const siblings = parent ? parent.children : this.children;
  1783. let cur = siblings.find(function (x) {
  1784. return node.b_code
  1785. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1786. : x.code === node.code && x.name === node.name;
  1787. });
  1788. if (!cur) {
  1789. const id = this.newId;
  1790. cur = {
  1791. id: id,
  1792. pid: parent ? parent.id : this.setting.rootId,
  1793. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1794. level: parent ? parent.level + 1 : 1,
  1795. order: siblings.length + 1,
  1796. children: [],
  1797. code: node.code, b_code: node.b_code, name: node.name,
  1798. unit: node.unit, unit_price: node.unit_price,
  1799. };
  1800. siblings.push(cur);
  1801. this.datas.push(cur);
  1802. }
  1803. loadFun(cur, node, index);
  1804. for (const c of node.children) {
  1805. this.loadGatherNode(c, cur, index, loadFun);
  1806. }
  1807. }
  1808. generateSortNodes() {
  1809. const self = this;
  1810. const addSortNode = function (node) {
  1811. self.nodes.push(node);
  1812. for (const c of node.children) {
  1813. addSortNode(c);
  1814. }
  1815. };
  1816. this.nodes = [];
  1817. for (const n of this.children) {
  1818. addSortNode(n);
  1819. }
  1820. }
  1821. loadGatherTree(data, index, loadFun) {
  1822. for (const c of data.billsTree.children) {
  1823. this.loadGatherNode(c, null, index, loadFun);
  1824. }
  1825. // todo load Pos Data;
  1826. }
  1827. calculateSum() {
  1828. if (this.setting.calcSum) {
  1829. for (const d of this.datas) {
  1830. this.setting.calcSum(d, this.count);
  1831. }
  1832. }
  1833. }
  1834. loadGatherData(datas) {
  1835. this.count = datas.length;
  1836. for (const [i, data] of datas.entries()) {
  1837. this.loadGatherTree(data, i+1, this.setting.loadInfo);
  1838. }
  1839. for (const d of this.datas) {
  1840. d.is_leaf = d.children.length === 0;
  1841. d.expanded = true;
  1842. d.visible = true;
  1843. this.items[itemsPre + d[this.setting.id]] = d;
  1844. }
  1845. this.generateSortNodes();
  1846. this.calculateSum();
  1847. }
  1848. }
  1849. class FinalTree extends BaseTree {
  1850. constructor(setting) {
  1851. super(setting);
  1852. this._newId = 1;
  1853. }
  1854. get newId() {
  1855. return this._newId++;
  1856. }
  1857. loadNode(node, parent, loadFun) {
  1858. if (node.b_code) return;
  1859. const siblings = parent ? parent.children : this.children;
  1860. let cur = siblings.find(function (x) {
  1861. return x.code === node.code && x.name === node.name;
  1862. });
  1863. if (!cur) {
  1864. const id = this.newId;
  1865. cur = {
  1866. id: id,
  1867. pid: parent ? parent.id : this.setting.rootId,
  1868. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1869. level: parent ? parent.level + 1 : 1,
  1870. order: siblings.length + 1,
  1871. children: [],
  1872. code: node.code, name: node.name, unit: node.unit,
  1873. };
  1874. siblings.push(cur);
  1875. this.datas.push(cur);
  1876. }
  1877. loadFun(cur, node);
  1878. for (const c of node.children) {
  1879. this.loadNode(c, cur, loadFun);
  1880. }
  1881. }
  1882. loadTree(tree, loadFun) {
  1883. for (const node of tree.children) {
  1884. this.loadNode(node, null, loadFun);
  1885. }
  1886. }
  1887. generateSortNodes() {
  1888. const self = this;
  1889. const addSortNode = function (node) {
  1890. self.nodes.push(node);
  1891. for (const c of node.children) {
  1892. addSortNode(c);
  1893. }
  1894. };
  1895. this.nodes = [];
  1896. for (const n of this.children) {
  1897. addSortNode(n);
  1898. }
  1899. }
  1900. afterLoad(fun) {
  1901. for (const d of this.datas) {
  1902. fun && fun(d);
  1903. d.is_leaf = d.children.length === 0;
  1904. d.expanded = true;
  1905. d.visible = true;
  1906. this.items[itemsPre + d[this.setting.id]] = d;
  1907. }
  1908. this.generateSortNodes();
  1909. }
  1910. clearFinal() {
  1911. this.datas = this.datas.filter(x => {
  1912. return !!x.base;
  1913. });
  1914. this.datas.forEach(x => {
  1915. delete x.total_price;
  1916. delete x.dgn_qty1;
  1917. delete x.dgn_qty2;
  1918. delete x.dgn_qty;
  1919. delete x.dgn_price;
  1920. delete x.final_dgn_qty1;
  1921. delete x.final_dgn_qty2;
  1922. delete x.final_dgn_qty;
  1923. delete x.final_dgn_price;
  1924. delete x.final_tp;
  1925. delete x.grow_dgn_qty1;
  1926. delete x.grow_dgn_qty2;
  1927. delete x.grow_dgn_qty;
  1928. delete x.grow_tp;
  1929. });
  1930. this.loadDatas(this.datas);
  1931. }
  1932. resortChildrenByCustom(fun) {
  1933. for (const n of this.nodes) {
  1934. if (n.children && n.children.length > 1) {
  1935. n.children.sort(fun);
  1936. n.children.forEach((x, y) => { x.order = y + 1; });
  1937. }
  1938. }
  1939. this.generateSortNodes();
  1940. }
  1941. }
  1942. if (type === 'base') {
  1943. return new BaseTree(setting);
  1944. } else if (type === 'fx') {
  1945. return new FxTree(setting);
  1946. } else if (type === 'stage') {
  1947. return new StageTree(setting);
  1948. } else if (type === 'ledger') {
  1949. return new LedgerTree(setting);
  1950. } else if (type === 'revise') {
  1951. return new ReviseTree(setting);
  1952. } else if (type === 'measure') {
  1953. return new MeasureTree(setting);
  1954. } else if (type === 'master') {
  1955. return new MasterTree(setting);
  1956. } else if (type === 'filter') {
  1957. return new FilterTree(setting);
  1958. } else if (type === 'gather') {
  1959. return new GatherTree(setting);
  1960. } else if (type === 'compare') {
  1961. return new CompareTree(setting);
  1962. } else if (type === 'tree-gather') {
  1963. return new TreeGatherTree(setting);
  1964. } else if (type === 'final') {
  1965. return new FinalTree(setting);
  1966. }
  1967. };
  1968. const treeCalc = {
  1969. mapTreeNode: function (tree) {
  1970. let map = {}, maxLevel = 0;
  1971. for (const node of tree.nodes) {
  1972. let levelArr = map[node.level];
  1973. if (!levelArr) {
  1974. levelArr = [];
  1975. map[node.level] = levelArr;
  1976. }
  1977. if (node.level > maxLevel) {
  1978. maxLevel = node.level;
  1979. }
  1980. levelArr.push(node);
  1981. }
  1982. return [maxLevel, map];
  1983. },
  1984. getMaxLevel: function (tree) {
  1985. return Math.max.apply(Math, tree.datas.map(function (o) { return o.level }));
  1986. },
  1987. calculateNode: function (tree, node) {
  1988. if (node.children && node.children.length > 0) {
  1989. const gather = node.children.reduce(function (rst, x) {
  1990. const result = {};
  1991. for (const cf of tree.setting.calcFields) {
  1992. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1993. }
  1994. return result;
  1995. });
  1996. // 汇总子项
  1997. for (const cf of tree.setting.calcFields) {
  1998. if (gather[cf]) {
  1999. node[cf] = gather[cf];
  2000. } else {
  2001. node[cf] = null;
  2002. }
  2003. }
  2004. }
  2005. // 自身运算
  2006. if (tree.setting.calcFun) {
  2007. tree.setting.calcFun(node);
  2008. }
  2009. },
  2010. calculateLevelNode: function (tree, level) {
  2011. const nodes = tree.datas.filter((n) => { return n.level === level });
  2012. for (const node of nodes) {
  2013. this.calculateNode(tree, node);
  2014. }
  2015. },
  2016. calculateAll: function (tree) {
  2017. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  2018. for (let i = maxLevel; i >= 0; i--) {
  2019. const levelNodes = levelMap[i];
  2020. if (levelNodes && levelNodes.length > 0) {
  2021. for (const node of levelNodes) {
  2022. this.calculateNode(tree, node);
  2023. }
  2024. }
  2025. }
  2026. },
  2027. calculateParent: function (tree, node) {
  2028. const nodes = tree.getFullPathNodes(node.full_path);
  2029. nodes.sort((a, b) => {
  2030. return b.level - a.level;
  2031. });
  2032. for (const n of nodes) {
  2033. this.calculateNode(tree, n);
  2034. }
  2035. return nodes;
  2036. }
  2037. };