path_tree.js 79 KB

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