path_tree.js 69 KB

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