path_tree.js 82 KB

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