path_tree.js 73 KB

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