path_tree.js 68 KB

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