path_tree.js 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363
  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. }
  1235. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  1236. return result;
  1237. }
  1238. }
  1239. class ReviseTree extends LedgerTree {
  1240. constructor (setting) {
  1241. super(setting);
  1242. this.price = [];
  1243. }
  1244. checkNodeUsed(node, pos) {
  1245. if (node.children && node.children.length > 0) {
  1246. for (const child of node.children) {
  1247. const used = this.checkNodeUsed(child, pos);
  1248. if (used) return used;
  1249. }
  1250. } else {
  1251. if (node.used) return node.used;
  1252. const posRange = pos.getLedgerPos(node.id);
  1253. if (posRange && posRange.length > 0) {
  1254. for (const p of posRange) {
  1255. if (p.used) return p.used;
  1256. }
  1257. }
  1258. }
  1259. return false;
  1260. }
  1261. loadRevisePrice(price, decimal) {
  1262. this.decimal = decimal;
  1263. this.price = price || [];
  1264. this.rela_price = [];
  1265. this.common_price = [];
  1266. this.price.forEach(x => {
  1267. if (x.rela_lid) {
  1268. x.rela_lid = x.rela_lid.split(',');
  1269. this.rela_price.push(x);
  1270. } else {
  1271. this.common_price.push(x);
  1272. }
  1273. });
  1274. }
  1275. checkRevisePrice(d) {
  1276. if (d.settle_status) return false;
  1277. if (!this.price || this.price.length === 0) return false;
  1278. const setting = this.setting;
  1279. const pid = this.getAllParents(d).map(x => { return x[setting.id] + ''; });
  1280. const checkRela = function(rela_lid) {
  1281. if (!rela_lid || rela_lid.length === 0) return false;
  1282. for (const lid of rela_lid) {
  1283. if (pid.indexOf(lid) >= 0) return true;
  1284. }
  1285. return false;
  1286. };
  1287. let p = this.rela_price.find(x => {
  1288. return x.b_code === d.b_code &&
  1289. ((!x.name && !d.name) || x.name === d.name) &&
  1290. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1291. checkZero(x.org_price - d.unit_price) &&
  1292. checkRela(x.rela_lid);
  1293. });
  1294. if (!p) p = this.common_price.find(x => {
  1295. return x.b_code === d.b_code &&
  1296. ((!x.name && !d.name) || x.name === d.name) &&
  1297. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1298. checkZero(x.org_price - d.unit_price);
  1299. });
  1300. if (!p) return false;
  1301. d.org_price = p.org_price;
  1302. d.unit_price = p.new_price;
  1303. d.deal_tp = ZhCalc.mul(d.deal_qty, d.unit_price, this.decimal.tp);
  1304. d.sgfh_tp = ZhCalc.mul(d.sgfh_qty, d.unit_price, this.decimal.tp);
  1305. d.sjcl_tp = ZhCalc.mul(d.sjcl_qty, d.unit_price, this.decimal.tp);
  1306. d.qtcl_tp = ZhCalc.mul(d.qtcl_qty, d.unit_price, this.decimal.tp);
  1307. d.total_price = ZhCalc.mul(d.quantity, d.unit_price, this.decimal.tp);
  1308. d.ex_tp1 = ZhCalc.mul(d.ex_qty1, d.unit_price, this.decimal.tp);
  1309. return true;
  1310. }
  1311. loadDatas(datas) {
  1312. super.loadDatas(datas);
  1313. if (this.price.length > 0) {
  1314. for (const d of this.datas) {
  1315. if (d.children && d.children.length > 0) continue;
  1316. if (!d.b_code) continue;
  1317. this.checkRevisePrice(d);
  1318. }
  1319. }
  1320. }
  1321. loadPostReivsePrice(data) {
  1322. let result = false;
  1323. if (!this.price) return result;
  1324. for (const d of data) {
  1325. if (!d.is_leaf || !d.b_code) continue;
  1326. if (this.checkRevisePrice(d)) result = true;
  1327. }
  1328. return result;
  1329. }
  1330. loadPostData(data) {
  1331. const setting = this.setting;
  1332. const result = {}, reCalcNodes = [];
  1333. if (!data) return result;
  1334. if (data.delete) {
  1335. result.delete = this._freeData(data.delete);
  1336. this._getReCalcNodes(reCalcNodes, result.delete);
  1337. }
  1338. if (data.create) {
  1339. result.create = this._loadData(data.create);
  1340. this.loadPostReivsePrice(result.create);
  1341. this._getReCalcNodes(reCalcNodes, result.create);
  1342. }
  1343. if (data.update) {
  1344. result.update = this._updateData(data.update);
  1345. this.loadPostReivsePrice(result.update);
  1346. this._getReCalcNodes(reCalcNodes, result.update);
  1347. }
  1348. this.sortByLevelConverse(reCalcNodes);
  1349. for (const node of reCalcNodes) {
  1350. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  1351. }
  1352. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  1353. return result;
  1354. }
  1355. }
  1356. class StageTree extends FxTree {
  1357. /**
  1358. * 构造函数
  1359. */
  1360. constructor(setting) {
  1361. super(setting);
  1362. // stage关联索引
  1363. this.stageItems = {};
  1364. }
  1365. _loadLockedNodes(ids){
  1366. this.Locked = ids || [];
  1367. for (const f of this.Locked) {
  1368. f.locked_ledger_id = f.ass_ledger_id ? f.ass_ledger_id.split(',') : (f.audit_ledger_id ? f.audit_ledger_id.split(',') : []);
  1369. for (const id of f.locked_ledger_id) {
  1370. const node = this.getItems(id);
  1371. if (!node) continue;
  1372. node.locked = true;
  1373. const posterity = this.getPosterity(node);
  1374. for (const pn of posterity) {
  1375. pn.locked = true;
  1376. pn.lock = true;
  1377. }
  1378. }
  1379. }
  1380. }
  1381. /**
  1382. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1383. * @param datas
  1384. */
  1385. loadDatas(datas, locked) {
  1386. super.loadDatas(datas);
  1387. // 清空旧数据
  1388. this.stageItems = {};
  1389. // 加载全部数据
  1390. for (const data of this.datas) {
  1391. const keyName = itemsPre + data[this.setting.stageId];
  1392. this.stageItems[keyName] = data;
  1393. }
  1394. this._loadLockedNodes(locked)
  1395. }
  1396. getStageItems(id) {
  1397. return this.stageItems[itemsPre + id];
  1398. }
  1399. loadStageData(datas, fieldPre, fields) {
  1400. datas = datas instanceof Array ? datas : [datas];
  1401. const loadedData = [];
  1402. for (const data of datas) {
  1403. let node = this.getStageItems(data.lid);
  1404. if (node) {
  1405. for (const prop of fields) {
  1406. if (data[prop] !== undefined) {
  1407. node[fieldPre + prop] = data[prop];
  1408. }
  1409. }
  1410. loadedData.push(node);
  1411. }
  1412. }
  1413. }
  1414. loadPreStageData(datas) {
  1415. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1416. }
  1417. loadCurStageData(datas) {
  1418. this.loadStageData(datas, '', this.setting.updateFields);
  1419. }
  1420. /**
  1421. * 加载数据(动态),只加载不同部分
  1422. * @param {Array} datas
  1423. * @return {Array} 加载到树的数据
  1424. * @privateA
  1425. */
  1426. _updateData(datas) {
  1427. datas = datas instanceof Array ? datas : [datas];
  1428. let loadedData = [];
  1429. for (const data of datas) {
  1430. let node = this.getItems(data[this.setting.id]);
  1431. if (node) {
  1432. for (const prop in data) {
  1433. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1434. }
  1435. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1436. if (prop === this.setting.pid) {
  1437. loadedData.push(this.getItems(node[this.setting.pid]));
  1438. loadedData.push(this.getItems(data[this.setting.pid]));
  1439. }
  1440. node[prop] = data[prop];
  1441. }
  1442. }
  1443. loadedData.push(node);
  1444. }
  1445. }
  1446. loadedData = _.uniq(loadedData);
  1447. for (const node of loadedData) {
  1448. node.children = this.getChildren(node);
  1449. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1450. }
  1451. this.sortTreeNode(true);
  1452. return loadedData;
  1453. };
  1454. /**
  1455. * 加载数据(动态),只加载不同部分
  1456. * @param {Array} datas
  1457. * @return {Array} 加载到树的数据
  1458. * @privateA
  1459. */
  1460. _updateStageData(datas) {
  1461. datas = datas instanceof Array ? datas : [datas];
  1462. const loadedData = [];
  1463. for (const data of datas) {
  1464. let node = this.getStageItems(data.lid);
  1465. if (node) {
  1466. for (const prop of this.setting.updateFields) {
  1467. if (data[prop] !== undefined) {
  1468. node[prop] = data[prop];
  1469. }
  1470. }
  1471. loadedData.push(node);
  1472. }
  1473. }
  1474. return loadedData;
  1475. };
  1476. /**
  1477. *
  1478. * @param parent
  1479. * @param node
  1480. * @private
  1481. */
  1482. _getNodesParents(parents, nodes) {
  1483. for (const node of nodes) {
  1484. const parent = this.getParent(node);
  1485. if (parent) {
  1486. const paths = this.getFullPathNodes(parent[this.setting.fullPath]);
  1487. for (const p of paths) {
  1488. if (parents.indexOf(p) === -1) {
  1489. parents.push(p);
  1490. }
  1491. }
  1492. }
  1493. if (node.children && node.children.length > 0) {
  1494. parents.push(node);
  1495. }
  1496. }
  1497. }
  1498. _updateDgnData(datas) {
  1499. datas = datas instanceof Array ? datas : [datas];
  1500. let loadedData = [];
  1501. for (const data of datas) {
  1502. let node = this.getStageItems(data.id);
  1503. if (node) {
  1504. for (const prop in data) {
  1505. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1506. node[prop] = data[prop];
  1507. }
  1508. }
  1509. loadedData.push(node);
  1510. }
  1511. }
  1512. return loadedData;
  1513. }
  1514. /**
  1515. * 提交数据至后端,返回的前端树结构应刷新的部分
  1516. * StageTree仅有更新CurStage部分,不需要增删
  1517. *
  1518. * @param data - 需要更新的数据
  1519. * @returns {Array} - 界面需要刷新的数据
  1520. */
  1521. loadPostStageData(data) {
  1522. let result, parents = [];
  1523. if (data.bills) {
  1524. result = this._updateData(data.bills);
  1525. this._getNodesParents(parents, result);
  1526. }
  1527. if (data.curStageData) {
  1528. result = this._updateStageData(data.curStageData);
  1529. this._getNodesParents(parents, result);
  1530. }
  1531. if (data.dgn) {
  1532. const dgnResult = this._updateDgnData(data.dgn);
  1533. result = result ? result.concat(dgnResult) : dgnResult;
  1534. }
  1535. result = result ? result.concat(parents) : parents;
  1536. this.sortByLevelConverse(result);
  1537. for (const node of result) {
  1538. treeCalc.calculateNode(this, node);
  1539. }
  1540. return result;
  1541. }
  1542. getLockedInfo(node) {
  1543. const ownerNodes = this.getAllParents(node);
  1544. const lockedInfo = [];
  1545. for (const on of ownerNodes) {
  1546. const filter = this.Locked.filter(x => { return x.ass_ledger_id.indexOf(on.ledger_id + '') >= 0; });
  1547. lockedInfo.push(...filter);
  1548. }
  1549. return lockedInfo;
  1550. }
  1551. }
  1552. class MasterTree extends FxTree {
  1553. /**
  1554. * 构造函数
  1555. */
  1556. constructor(setting) {
  1557. super(setting);
  1558. // 关联索引
  1559. this.masterItems = {};
  1560. }
  1561. /**
  1562. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1563. * @param datas
  1564. */
  1565. loadDatas(datas) {
  1566. super.loadDatas(datas);
  1567. // 清空旧数据
  1568. this.masterItems = {};
  1569. // minor数据缓存
  1570. this.minorData = {};
  1571. // 加载全部数据
  1572. for (const data of this.datas) {
  1573. const keyName = itemsPre + data[this.setting.masterId];
  1574. this.masterItems[keyName] = data;
  1575. }
  1576. }
  1577. /**
  1578. * 根据关联id,查找节点
  1579. * @param id
  1580. * @returns {*}
  1581. */
  1582. getMasterItems(id) {
  1583. return this.masterItems[itemsPre + id];
  1584. }
  1585. /**
  1586. * 加载关联数据
  1587. *
  1588. * @param {Array|Object}datas - 需要关联的数据
  1589. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1590. * @param {Array} fields - 关联字段
  1591. * @returns {Array}
  1592. */
  1593. loadMinorData(datas, fieldSuf, fields, calcFields) {
  1594. for (const cf of calcFields) {
  1595. this.setting.calcFields.push(cf + fieldSuf);
  1596. }
  1597. if (!datas) return;
  1598. datas = datas instanceof Array ? datas : [datas];
  1599. this.minorData[fieldSuf] = datas;
  1600. const loadedData = [];
  1601. for (const data of datas) {
  1602. let node = this.getMasterItems(data[this.setting.minorId]);
  1603. if (node) {
  1604. for (const prop of fields) {
  1605. if (data[prop] !== undefined) {
  1606. node[prop + fieldSuf] = data[prop];
  1607. }
  1608. }
  1609. loadedData.push(node);
  1610. }
  1611. }
  1612. return loadedData;
  1613. }
  1614. reCalcSumData(fields, fieldSufs) {
  1615. this.datas.forEach(d => {
  1616. for (const prop of fields) {
  1617. delete d['sum_' + prop];
  1618. }
  1619. for (const prop of fields) {
  1620. for (const s of fieldSufs) {
  1621. if (d[prop + s] !== undefined) {
  1622. d['sum_'+ prop] = ZhCalc.add(d['sum_' + prop], d[prop + s]);
  1623. }
  1624. }
  1625. }
  1626. });
  1627. }
  1628. }
  1629. class FilterTree extends BaseTree {
  1630. addData(data, fields) {
  1631. const item = {};
  1632. for (const prop in data) {
  1633. if (fields.indexOf(prop) >= 0) {
  1634. item[prop] = data[prop];
  1635. }
  1636. }
  1637. const keyName = itemsPre + item[this.setting.id];
  1638. if (!this.items[keyName]) {
  1639. item.children = [];
  1640. item.is_leaf = true;
  1641. item.expanded = true;
  1642. item.visible = true;
  1643. this.items[keyName] = item;
  1644. this.datas.push(item);
  1645. if (item[setting.pid] === setting.rootId) {
  1646. this.children.push(item);
  1647. } else {
  1648. const parent = this.getParent(item);
  1649. if (parent) {
  1650. parent.is_leaf = false;
  1651. parent.children.push(item);
  1652. }
  1653. }
  1654. } else {
  1655. return this.items[keyName];
  1656. }
  1657. return item;
  1658. }
  1659. }
  1660. class GatherTree extends BaseTree {
  1661. get newId() {
  1662. if (!this._maxId) {
  1663. this._maxId = 0;
  1664. }
  1665. this._maxId++;
  1666. return this._maxId;
  1667. }
  1668. addNode(data, parent) {
  1669. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1670. let item = _.find(this.items, data);
  1671. if (item) return item;
  1672. item = data;
  1673. item[this.setting.id] = this.newId;
  1674. const keyName = itemsPre + item[this.setting.id];
  1675. item.children = [];
  1676. item.is_leaf = true;
  1677. item.expanded = true;
  1678. item.visible = true;
  1679. this.items[keyName] = item;
  1680. this.datas.push(item);
  1681. if (parent) {
  1682. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  1683. item[this.setting.level] = parent[this.setting.level] + 1;
  1684. item[this.setting.order] = parent.children.length + 1;
  1685. parent.is_leaf = false;
  1686. parent.children.push(item);
  1687. } else {
  1688. item[this.setting.fullPath] = '' + item[this.setting.id];
  1689. item[this.setting.level] = 1;
  1690. item[this.setting.order] = this.children.length + 1;
  1691. this.children.push(item);
  1692. }
  1693. return item;
  1694. }
  1695. sortTreeNodeCustom(field, fun, isResort) {
  1696. const self = this;
  1697. const sortNodes = function (nodes) {
  1698. nodes.sort(function (a, b) {
  1699. return fun(a[field], b[field]);
  1700. });
  1701. for (const [i, node] of nodes.entries()) {
  1702. node[self.setting.order] = i + 1;
  1703. }
  1704. };
  1705. const addSortNodes = function (nodes) {
  1706. if (!nodes) { return }
  1707. for (let i = 0; i < nodes.length; i++) {
  1708. self.nodes.push(nodes[i]);
  1709. nodes[i].index = self.nodes.length - 1;
  1710. if (!isResort) {
  1711. nodes[i].children = self.getChildren(nodes[i]);
  1712. }
  1713. sortNodes(nodes[i].children);
  1714. addSortNodes(nodes[i].children);
  1715. }
  1716. };
  1717. this.nodes = [];
  1718. if (!isResort) {
  1719. this.children = this.getChildren();
  1720. }
  1721. sortNodes(this.children);
  1722. addSortNodes(this.children);
  1723. }
  1724. }
  1725. class CompareTree extends FxTree {
  1726. constructor(setting) {
  1727. super(setting);
  1728. this._newId = 1;
  1729. }
  1730. get newId() {
  1731. return this._newId++;
  1732. }
  1733. findCompareNode(node, parent, source) {
  1734. if (this.setting.findNode) {
  1735. return this.setting.findNode(this, node, parent, source);
  1736. } else {
  1737. const siblings = parent ? parent.children : this.children;
  1738. const checkData = { code: node.code || '', b_code: node.b_code || '', name: node.name || '', unit: node.unit || '', unit_price: node.unit_price || 0 };
  1739. return siblings.find(function (x) {
  1740. return checkData.b_code
  1741. ? x.b_code === checkData.b_code && x.name === checkData.name && x.unit === checkData.unit && x.unit_price === checkData.unit_price
  1742. : x.code === checkData.code && x.name === checkData.name;
  1743. });
  1744. }
  1745. }
  1746. loadCompareNode(source, node, parent, loadFun) {
  1747. let cur = this.findCompareNode(node, parent, source);
  1748. if (!cur) {
  1749. const siblings = parent ? parent.children : this.children;
  1750. const id = this.newId;
  1751. cur = {
  1752. children: [], pos: [],
  1753. code: node.code || '', b_code: node.b_code || '', name: node.name || '',
  1754. unit: node.unit || '', unit_price: node.unit_price || 0,
  1755. };
  1756. cur[this.setting.id] = id;
  1757. cur[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1758. cur[this.setting.fullPath] = parent ? parent[this.setting.fullPath] + '-' + id : '' + id;
  1759. cur[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  1760. cur[this.setting.order] = siblings.length + 1;
  1761. siblings.push(cur);
  1762. this.datas.push(cur);
  1763. }
  1764. loadFun(cur, node, source);
  1765. for (const c of node.children) {
  1766. this.loadCompareNode(source, c, cur, loadFun);
  1767. }
  1768. }
  1769. generateSortNodes() {
  1770. const self = this;
  1771. const addSortNode = function (node) {
  1772. self.nodes.push(node);
  1773. if (self.setting.sortFun) {
  1774. self.setting.sortFun(node.children);
  1775. }
  1776. for (const c of node.children) {
  1777. addSortNode(c);
  1778. }
  1779. };
  1780. this.nodes = [];
  1781. if (this.setting.sortFun) {
  1782. this.setting.sortFun(this.children);
  1783. }
  1784. for (const n of this.children) {
  1785. addSortNode(n);
  1786. }
  1787. }
  1788. loadCompareTree(data, loadFun) {
  1789. for (const c of data.billsTree.children) {
  1790. this.loadCompareNode(data, c, null, loadFun);
  1791. }
  1792. }
  1793. calculateDiffer() {
  1794. if (this.setting.calcDiffer) {
  1795. for (const d of this.datas) {
  1796. this.setting.calcDiffer(d);
  1797. }
  1798. }
  1799. }
  1800. loadCompareData(data1, data2) {
  1801. this.clearDatas();
  1802. this.loadCompareTree(data1, this.setting.loadInfo1);
  1803. this.loadCompareTree(data2, this.setting.loadInfo2);
  1804. for (const d of this.datas) {
  1805. d.is_leaf = d.children.length === 0;
  1806. d.expanded = true;
  1807. d.visible = true;
  1808. this.items[itemsPre + d[this.setting.id]] = d;
  1809. }
  1810. this.generateSortNodes();
  1811. this.calculateDiffer();
  1812. if (this.setting.afterLoad) this.setting.afterLoad(this);
  1813. }
  1814. }
  1815. class TreeGatherTree extends FxTree {
  1816. constructor(setting) {
  1817. super(setting);
  1818. this._newId = 1;
  1819. }
  1820. get newId() {
  1821. return this._newId++;
  1822. }
  1823. findGatherNode(node, parent, source) {
  1824. if (this.setting.findNode) {
  1825. return this.setting.findNode(this, node, parent, source);
  1826. } else {
  1827. const siblings = parent ? parent.children : this.children;
  1828. const checkData = { code: node.code || '', b_code: node.b_code || '', name: node.name || '', unit: node.unit || '', unit_price: node.unit_price || 0 };
  1829. return siblings.find(function (x) {
  1830. return checkData.b_code
  1831. ? x.b_code === checkData.b_code && x.name === checkData.name && x.unit === checkData.unit && x.unit_price === checkData.unit_price
  1832. : x.code === checkData.code && x.name === checkData.name;
  1833. });
  1834. }
  1835. }
  1836. loadGatherNode(source, node, parent, index, loadFun) {
  1837. let cur = this.findGatherNode(node, parent, source);
  1838. if (!cur) {
  1839. const siblings = parent ? parent.children : this.children;
  1840. const id = this.newId;
  1841. cur = {
  1842. children: [], pos: [],
  1843. code: node.code || '', b_code: node.b_code || '', name: node.name || '',
  1844. unit: node.unit || '', unit_price: node.unit_price || 0,
  1845. };
  1846. cur[this.setting.id] = id;
  1847. cur[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1848. cur[this.setting.fullPath] = parent ? parent[this.setting.fullPath] + '-' + id : '' + id;
  1849. cur[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  1850. cur[this.setting.order] = siblings.length + 1;
  1851. siblings.push(cur);
  1852. this.datas.push(cur);
  1853. }
  1854. loadFun(cur, node, index, source);
  1855. for (const c of node.children) {
  1856. this.loadGatherNode(source, c, cur, index, loadFun);
  1857. }
  1858. }
  1859. loadGatherTree(data, index, loadFun) {
  1860. for (const c of data.billsTree.children) {
  1861. this.loadGatherNode(data, c, null, index, loadFun);
  1862. }
  1863. }
  1864. generateSortNodes() {
  1865. const self = this;
  1866. const addSortNode = function (node) {
  1867. self.nodes.push(node);
  1868. for (const c of node.children) {
  1869. addSortNode(c);
  1870. }
  1871. };
  1872. this.nodes = [];
  1873. for (const n of this.children) {
  1874. addSortNode(n);
  1875. }
  1876. }
  1877. calculateSum() {
  1878. if (this.setting.calcSum) {
  1879. for (const d of this.datas) {
  1880. this.setting.calcSum(d, this.count);
  1881. }
  1882. }
  1883. }
  1884. loadGatherData(datas) {
  1885. this.count = datas.length;
  1886. for (const [i, data] of datas.entries()) {
  1887. this.loadGatherTree(data, i+1, this.setting.loadInfo);
  1888. }
  1889. for (const d of this.datas) {
  1890. d.is_leaf = d.children.length === 0;
  1891. d.expanded = true;
  1892. d.visible = true;
  1893. this.items[itemsPre + d[this.setting.id]] = d;
  1894. }
  1895. this.generateSortNodes();
  1896. this.calculateSum();
  1897. }
  1898. }
  1899. class FinalTree extends BaseTree {
  1900. constructor(setting) {
  1901. super(setting);
  1902. this._newId = 1;
  1903. }
  1904. get newId() {
  1905. return this._newId++;
  1906. }
  1907. loadNode(node, parent, loadFun) {
  1908. if (node.b_code) return;
  1909. const siblings = parent ? parent.children : this.children;
  1910. let cur = siblings.find(function (x) {
  1911. return x.code === node.code && x.name === node.name;
  1912. });
  1913. if (!cur) {
  1914. const id = this.newId;
  1915. cur = {
  1916. id: id,
  1917. pid: parent ? parent.id : this.setting.rootId,
  1918. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1919. level: parent ? parent.level + 1 : 1,
  1920. order: siblings.length + 1,
  1921. children: [],
  1922. code: node.code, name: node.name, unit: node.unit,
  1923. };
  1924. siblings.push(cur);
  1925. this.datas.push(cur);
  1926. }
  1927. loadFun(cur, node);
  1928. for (const c of node.children) {
  1929. this.loadNode(c, cur, loadFun);
  1930. }
  1931. }
  1932. loadTree(tree, loadFun) {
  1933. for (const node of tree.children) {
  1934. this.loadNode(node, null, loadFun);
  1935. }
  1936. }
  1937. generateSortNodes() {
  1938. const self = this;
  1939. const addSortNode = function (node) {
  1940. self.nodes.push(node);
  1941. for (const c of node.children) {
  1942. addSortNode(c);
  1943. }
  1944. };
  1945. this.nodes = [];
  1946. for (const n of this.children) {
  1947. addSortNode(n);
  1948. }
  1949. }
  1950. afterLoad(fun) {
  1951. for (const d of this.datas) {
  1952. fun && fun(d);
  1953. d.is_leaf = d.children.length === 0;
  1954. d.expanded = true;
  1955. d.visible = true;
  1956. this.items[itemsPre + d[this.setting.id]] = d;
  1957. }
  1958. this.generateSortNodes();
  1959. }
  1960. clearFinal() {
  1961. this.datas = this.datas.filter(x => {
  1962. return !!x.base;
  1963. });
  1964. this.datas.forEach(x => {
  1965. delete x.total_price;
  1966. delete x.dgn_qty1;
  1967. delete x.dgn_qty2;
  1968. delete x.dgn_qty;
  1969. delete x.dgn_price;
  1970. delete x.final_dgn_qty1;
  1971. delete x.final_dgn_qty2;
  1972. delete x.final_dgn_qty;
  1973. delete x.final_dgn_price;
  1974. delete x.final_tp;
  1975. delete x.grow_dgn_qty1;
  1976. delete x.grow_dgn_qty2;
  1977. delete x.grow_dgn_qty;
  1978. delete x.grow_tp;
  1979. });
  1980. this.loadDatas(this.datas);
  1981. }
  1982. resortChildrenByCustom(fun) {
  1983. for (const n of this.nodes) {
  1984. if (n.children && n.children.length > 1) {
  1985. n.children.sort(fun);
  1986. n.children.forEach((x, y) => { x.order = y + 1; });
  1987. }
  1988. }
  1989. this.generateSortNodes();
  1990. }
  1991. }
  1992. if (type === 'base') {
  1993. return new BaseTree(setting);
  1994. } else if (type === 'fx') {
  1995. return new FxTree(setting);
  1996. } else if (type === 'stage') {
  1997. return new StageTree(setting);
  1998. } else if (type === 'ledger') {
  1999. return new LedgerTree(setting);
  2000. } else if (type === 'revise') {
  2001. return new ReviseTree(setting);
  2002. } else if (type === 'measure') {
  2003. return new MeasureTree(setting);
  2004. } else if (type === 'master') {
  2005. return new MasterTree(setting);
  2006. } else if (type === 'filter') {
  2007. return new FilterTree(setting);
  2008. } else if (type === 'gather') {
  2009. return new GatherTree(setting);
  2010. } else if (type === 'compare') {
  2011. return new CompareTree(setting);
  2012. } else if (type === 'tree-gather') {
  2013. return new TreeGatherTree(setting);
  2014. } else if (type === 'final') {
  2015. return new FinalTree(setting);
  2016. }
  2017. };
  2018. const createAncillaryGcl = function (setting) {
  2019. class AncillaryGcl {
  2020. /**
  2021. * 构造函数
  2022. * @param {id|Number, masterId|Number} setting
  2023. */
  2024. constructor(setting) {
  2025. this.itemPre = 'id_';
  2026. // 无索引
  2027. this.datas = [];
  2028. // 以key为索引
  2029. this.items = {};
  2030. // 以分类id为索引的有序
  2031. this.masterIndex = {};
  2032. // 设置
  2033. this.setting = setting;
  2034. }
  2035. resortPart(partData) {
  2036. const sortRule = this.setting.sort || [['g_order', 'asc']];
  2037. if (partData instanceof Array) {
  2038. partData.sort(function (a, b) {
  2039. for (const sr of sortRule) {
  2040. const iSort = sr[1] === 'asc' ? a[sr[0]] - b[sr[0]] : b[sr[0]] - a[sr[0]];
  2041. if (iSort) return iSort;
  2042. }
  2043. })
  2044. }
  2045. }
  2046. filterPart(partData) {
  2047. const check = this._filter;
  2048. partData.forEach(x => {
  2049. x.visible = true;
  2050. if (check) {
  2051. for (const prop in check) {
  2052. if (x[prop] !== check[prop]) x.visible = false;
  2053. }
  2054. }
  2055. });
  2056. }
  2057. /**
  2058. * 加载数据
  2059. * @param datas
  2060. */
  2061. loadDatas(datas) {
  2062. this.datas = datas;
  2063. this.items = {};
  2064. this.masterIndex = {};
  2065. for (const data of this.datas) {
  2066. const key = this.itemPre + data[this.setting.id];
  2067. this.items[key] = data;
  2068. const masterKey = this.itemPre + data[this.setting.masterId];
  2069. if (!this.masterIndex[masterKey]) {
  2070. this.masterIndex[masterKey] = [];
  2071. }
  2072. this.masterIndex[masterKey].push(data);
  2073. }
  2074. for (const prop in this.masterIndex) {
  2075. this.resortPart(this.masterIndex[prop]);
  2076. }
  2077. }
  2078. _addDatas(data, resort) {
  2079. const datas = data instanceof Array ? data : [data];
  2080. for (const d of datas) {
  2081. const key = this.itemPre + d[this.setting.id];
  2082. this.items[key] = d;
  2083. const masterKey = this.itemPre + d[this.setting.masterId];
  2084. if (!this.masterIndex[masterKey]) this.masterIndex[masterKey] = [];
  2085. this.masterIndex[masterKey].push(d);
  2086. if (resort.indexOf(masterKey) < 0) resort.push(masterKey);
  2087. }
  2088. }
  2089. /**
  2090. * 更新数据
  2091. * @param datas
  2092. */
  2093. _updateDatas(data, resort) {
  2094. const datas = data instanceof Array ? data : [data];
  2095. for (const d of datas) {
  2096. const item = this.getItem(d[this.setting.id]);
  2097. if (!item) continue;
  2098. for (const prop in d) {
  2099. item[prop] = d[prop];
  2100. }
  2101. const masterKey = this.itemPre + item[this.setting.masterId];
  2102. if (resort.indexOf(masterKey) < 0) resort.push(masterKey);
  2103. }
  2104. }
  2105. /**
  2106. * 移除数据
  2107. * @param datas
  2108. */
  2109. _removeDatas(data, resort) {
  2110. if (!data) { return; }
  2111. const datas = data instanceof Array ? data : [data];
  2112. for (let i = datas.length - 1; i >= 0; i--) {
  2113. const id = datas[i];
  2114. const d = this.getItem(id);
  2115. this.datas.splice(this.datas.indexOf(d), 1);
  2116. const key = this.itemPre + d[this.setting.id];
  2117. delete this.items[key];
  2118. const range = this.getPartData(d[this.setting.masterId]);
  2119. range.splice(range.indexOf(d), 1);
  2120. }
  2121. }
  2122. updateDatas(data) {
  2123. const resort = [];
  2124. if (data.add) this._addDatas(data.add, resort);
  2125. if (data.del) this._removeDatas(data.del, resort);
  2126. if (data.update) this._updateDatas(data.update, resort);
  2127. for (const s of resort) {
  2128. this.resortPart(this.masterIndex[s]);
  2129. this.filterPart(this.masterIndex[s]);
  2130. }
  2131. }
  2132. /**
  2133. * 移除数据 - 根据分类id
  2134. * @param mid
  2135. */
  2136. removeDatasByMasterId(mid) {
  2137. const masterKey = this.itemPre + mid;
  2138. const range = this.masterIndex[masterKey];
  2139. if (range) {
  2140. delete this.masterIndex[masterKey];
  2141. for (const r of range) {
  2142. this.datas.splice(this.datas.indexOf(r), 1);
  2143. const key = this.itemPre + r[this.setting.id];
  2144. delete this.items[key];
  2145. }
  2146. }
  2147. }
  2148. getItem(id) {
  2149. return this.items[this.itemPre + id];
  2150. }
  2151. getPartData(mid) {
  2152. return this.masterIndex[this.itemPre + mid];
  2153. }
  2154. set sort(sort) {
  2155. this.setting.sort = sort;
  2156. for (const key in this.masterIndex) {
  2157. this.resortPart(this.masterIndex[key]);
  2158. }
  2159. }
  2160. set filter(condition) {
  2161. this._filter = condition;
  2162. for (const key in this.masterIndex) {
  2163. this.filterPart(this.masterIndex[key]);
  2164. }
  2165. }
  2166. }
  2167. return new AncillaryGcl(setting);
  2168. };
  2169. const treeCalc = {
  2170. mapTreeNode: function (tree) {
  2171. const setting = tree.setting;
  2172. let map = {}, maxLevel = 0;
  2173. for (const node of tree.nodes) {
  2174. let levelArr = map[node[setting.level]];
  2175. if (!levelArr) {
  2176. levelArr = [];
  2177. map[node[setting.level]] = levelArr;
  2178. }
  2179. if (node[setting.level] > maxLevel) {
  2180. maxLevel = node[setting.level];
  2181. }
  2182. levelArr.push(node);
  2183. }
  2184. return [maxLevel, map];
  2185. },
  2186. getMaxLevel: function (tree) {
  2187. const setting = tree.setting;
  2188. return Math.max.apply(Math, tree.datas.map(function (o) { return o[setting.level] }));
  2189. },
  2190. calculateNode: function (tree, node) {
  2191. if (node.children && node.children.length > 0) {
  2192. const gather = {};
  2193. node.children.forEach(c => {
  2194. for (const cf of tree.setting.calcFields) {
  2195. gather[cf] = ZhCalc.add(gather[cf], c[cf]);
  2196. }
  2197. });
  2198. // const gather = node.children.reduce(function (rst, x) {
  2199. // const result = {};
  2200. // for (const cf of tree.setting.calcFields) {
  2201. // result[cf] = ZhCalc.add(rst[cf], x[cf]);
  2202. // }
  2203. // return result;
  2204. // });
  2205. // 汇总子项
  2206. for (const cf of tree.setting.calcFields) {
  2207. if (gather[cf]) {
  2208. node[cf] = gather[cf];
  2209. } else {
  2210. node[cf] = null;
  2211. }
  2212. }
  2213. }
  2214. // 自身运算
  2215. if (tree.setting.calcFun) {
  2216. tree.setting.calcFun(node);
  2217. }
  2218. },
  2219. calculateLevelNode: function (tree, level) {
  2220. const setting = tree.setting;
  2221. const nodes = tree.datas.filter((n) => { return n[setting.level] === level });
  2222. for (const node of nodes) {
  2223. this.calculateNode(tree, node);
  2224. }
  2225. },
  2226. calculateAll: function (tree) {
  2227. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  2228. for (let i = maxLevel; i >= 0; i--) {
  2229. const levelNodes = levelMap[i];
  2230. if (levelNodes && levelNodes.length > 0) {
  2231. for (const node of levelNodes) {
  2232. this.calculateNode(tree, node);
  2233. }
  2234. }
  2235. }
  2236. },
  2237. calculateParent: function (tree, node, converse = -1) {
  2238. const setting = tree.setting;
  2239. const nodes = tree.getFullPathNodes(node[setting.fullPath]);
  2240. converse === -1 ? tree.sortByLevel(nodes) : tree.sortByLevelConverse(nodes);
  2241. for (const n of nodes) {
  2242. this.calculateNode(tree, n);
  2243. }
  2244. return nodes;
  2245. }
  2246. };