path_tree.js 79 KB

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