path_tree.js 70 KB

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