path_tree.js 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  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. _loadLockedNodes(ids){
  1181. this.Locked = ids || [];
  1182. for (const f of this.Locked) {
  1183. const node = this.getItems(f.ledger_id);
  1184. node.locked = true;
  1185. const posterity = this.getPosterity(node);
  1186. for (const pn of posterity) {
  1187. pn.locked = true;
  1188. pn.lock = true;
  1189. }
  1190. }
  1191. }
  1192. /**
  1193. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1194. * @param datas
  1195. */
  1196. loadDatas(datas, locked) {
  1197. super.loadDatas(datas);
  1198. // 清空旧数据
  1199. this.stageItems = {};
  1200. // 加载全部数据
  1201. for (const data of this.datas) {
  1202. const keyName = itemsPre + data[this.setting.stageId];
  1203. this.stageItems[keyName] = data;
  1204. }
  1205. this._loadLockedNodes(locked)
  1206. }
  1207. // 解锁相关
  1208. _loadPwdCache() {
  1209. const cache = getLocalCache(this.pwdCacheKey);
  1210. if (!cache) return;
  1211. const cacheArr = cache.split('|');
  1212. for (const ca of cacheArr) {
  1213. if (!ca) continue;
  1214. const [ledger_id, pwd] = ca.split(':');
  1215. if (!ledger_id || !pwd) continue;
  1216. const p = this.pwd.find(x => {return x.ledger_id == ledger_id});
  1217. if (p) p.check = p.pwd === pwd;
  1218. }
  1219. }
  1220. loadPwd(data, cacheKey, confirmList) {
  1221. this.loadingPwd = true;
  1222. try {
  1223. this.pwdCacheKey = cacheKey;
  1224. this.confirmList = confirmList;
  1225. this.pwd = data;
  1226. this._loadPwdCache();
  1227. this._loadOnlineConfirm();
  1228. for (const p of this.pwd) {
  1229. p.node = this.getItems(p.ledger_id);
  1230. this.lockNode(p, !p.check);
  1231. }
  1232. } catch(err) {}
  1233. this.loadingPwd = false;
  1234. }
  1235. // 确认相关
  1236. _loadOnlineConfirm() {
  1237. const cacheArr = this.confirmList;
  1238. for (const ca of cacheArr) {
  1239. if (!ca) continue;
  1240. if (!ca.ledger_id) continue;
  1241. const p = this.pwd.find(x => {return x.ledger_id == ca.ledger_id});
  1242. if (p) {
  1243. p.confirm = true;
  1244. p.confirm_time = ca.create_time;
  1245. }
  1246. }
  1247. }
  1248. getStageItems(id) {
  1249. return this.stageItems[itemsPre + id];
  1250. }
  1251. loadStageData(datas, fieldPre, fields) {
  1252. datas = datas instanceof Array ? datas : [datas];
  1253. const loadedData = [];
  1254. for (const data of datas) {
  1255. let node = this.getStageItems(data.lid);
  1256. if (node) {
  1257. for (const prop of fields) {
  1258. if (data[prop] !== undefined) {
  1259. node[fieldPre + prop] = data[prop];
  1260. }
  1261. }
  1262. loadedData.push(node);
  1263. }
  1264. }
  1265. }
  1266. loadPreStageData(datas) {
  1267. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1268. }
  1269. loadCurStageData(datas) {
  1270. this.loadStageData(datas, '', this.setting.updateFields);
  1271. }
  1272. /**
  1273. * 加载数据(动态),只加载不同部分
  1274. * @param {Array} datas
  1275. * @return {Array} 加载到树的数据
  1276. * @privateA
  1277. */
  1278. _updateData(datas) {
  1279. datas = datas instanceof Array ? datas : [datas];
  1280. let loadedData = [];
  1281. for (const data of datas) {
  1282. let node = this.getItems(data[this.setting.id]);
  1283. if (node) {
  1284. for (const prop in data) {
  1285. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1286. }
  1287. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1288. if (prop === this.setting.pid) {
  1289. loadedData.push(this.getItems(node[this.setting.pid]));
  1290. loadedData.push(this.getItems(data[this.setting.pid]));
  1291. }
  1292. node[prop] = data[prop];
  1293. }
  1294. }
  1295. loadedData.push(node);
  1296. }
  1297. }
  1298. loadedData = _.uniq(loadedData);
  1299. for (const node of loadedData) {
  1300. node.children = this.getChildren(node);
  1301. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1302. }
  1303. this.sortTreeNode(true);
  1304. return loadedData;
  1305. };
  1306. /**
  1307. * 加载数据(动态),只加载不同部分
  1308. * @param {Array} datas
  1309. * @return {Array} 加载到树的数据
  1310. * @privateA
  1311. */
  1312. _updateStageData(datas) {
  1313. datas = datas instanceof Array ? datas : [datas];
  1314. const loadedData = [];
  1315. for (const data of datas) {
  1316. let node = this.getStageItems(data.lid);
  1317. if (node) {
  1318. for (const prop of this.setting.updateFields) {
  1319. if (data[prop] !== undefined) {
  1320. node[prop] = data[prop];
  1321. }
  1322. }
  1323. loadedData.push(node);
  1324. }
  1325. }
  1326. return loadedData;
  1327. };
  1328. /**
  1329. *
  1330. * @param parent
  1331. * @param node
  1332. * @private
  1333. */
  1334. _getNodesParents(parents, nodes) {
  1335. for (const node of nodes) {
  1336. const parent = this.getParent(node);
  1337. if (parent) {
  1338. const paths = this.getFullPathNodes(parent.full_path);
  1339. for (const p of paths) {
  1340. if (parents.indexOf(p) === -1) {
  1341. parents.push(p);
  1342. }
  1343. }
  1344. }
  1345. if (node.children && node.children.length > 0) {
  1346. parents.push(node);
  1347. }
  1348. }
  1349. }
  1350. _updateDgnData(datas) {
  1351. datas = datas instanceof Array ? datas : [datas];
  1352. let loadedData = [];
  1353. for (const data of datas) {
  1354. let node = this.getStageItems(data.id);
  1355. if (node) {
  1356. for (const prop in data) {
  1357. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1358. node[prop] = data[prop];
  1359. }
  1360. }
  1361. loadedData.push(node);
  1362. }
  1363. }
  1364. return loadedData;
  1365. }
  1366. /**
  1367. * 提交数据至后端,返回的前端树结构应刷新的部分
  1368. * StageTree仅有更新CurStage部分,不需要增删
  1369. *
  1370. * @param data - 需要更新的数据
  1371. * @returns {Array} - 界面需要刷新的数据
  1372. */
  1373. loadPostStageData(data) {
  1374. let result, parents = [];
  1375. if (data.bills) {
  1376. result = this._updateData(data.bills);
  1377. this._getNodesParents(parents, result);
  1378. }
  1379. if (data.curStageData) {
  1380. result = this._updateStageData(data.curStageData);
  1381. this._getNodesParents(parents, result);
  1382. }
  1383. if (data.dgn) {
  1384. const dgnResult = this._updateDgnData(data.dgn);
  1385. result = result ? result.concat(dgnResult) : dgnResult;
  1386. }
  1387. result = result ? result.concat(parents) : parents;
  1388. result.sort((a, b) => {
  1389. return b.level - a.level;
  1390. });
  1391. for (const node of result) {
  1392. treeCalc.calculateNode(this, node);
  1393. }
  1394. return result;
  1395. }
  1396. _savePwdCache() {
  1397. const cache = [];
  1398. for (const p of this.pwd) {
  1399. if (p.check && p.pwd) cache.push(p.ledger_id + ':' + p.pwd);
  1400. }
  1401. setLocalCache(this.pwdCacheKey, cache.join('|'));
  1402. }
  1403. _getPwdPosterity(node) {
  1404. const children = [];
  1405. for (const c of node.children) {
  1406. const cPwd = this.pwd.find(x => {return x.node && x.node.id === c.id});
  1407. if (!cPwd) children.push(c);
  1408. }
  1409. let posterity = [...children];
  1410. for (const c of children) {
  1411. posterity = posterity.concat(this._getPwdPosterity(c));
  1412. }
  1413. return posterity;
  1414. }
  1415. lockNode(p, isLock) {
  1416. const refresh = [];
  1417. p.check = !isLock;
  1418. p.node.lock = isLock;
  1419. refresh.push(this.getNodeIndex(p.node));
  1420. const posterity = this._getPwdPosterity(p.node);
  1421. for (const pn of posterity) {
  1422. pn.lock = pn.locked || isLock;
  1423. refresh.push(this.getNodeIndex(pn));
  1424. }
  1425. if (!this.loadingPwd) this._savePwdCache();
  1426. return refresh;
  1427. }
  1428. getLockedInfo(node) {
  1429. const ownerNodes = this.getAllParents(node);
  1430. const lockedInfo = [];
  1431. for (const on of ownerNodes) {
  1432. const filter = this.Locked.filter(x => { return x.ledger_id === on.ledger_id; });
  1433. lockedInfo.push(...filter);
  1434. }
  1435. return lockedInfo;
  1436. }
  1437. }
  1438. class MasterTree extends FxTree {
  1439. /**
  1440. * 构造函数
  1441. */
  1442. constructor(setting) {
  1443. super(setting);
  1444. // 关联索引
  1445. this.masterItems = {};
  1446. }
  1447. /**
  1448. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1449. * @param datas
  1450. */
  1451. loadDatas(datas) {
  1452. super.loadDatas(datas);
  1453. // 清空旧数据
  1454. this.masterItems = {};
  1455. // minor数据缓存
  1456. this.minorData = {};
  1457. // 加载全部数据
  1458. for (const data of this.datas) {
  1459. const keyName = itemsPre + data[this.setting.masterId];
  1460. this.masterItems[keyName] = data;
  1461. }
  1462. }
  1463. /**
  1464. * 根据关联id,查找节点
  1465. * @param id
  1466. * @returns {*}
  1467. */
  1468. getMasterItems(id) {
  1469. return this.masterItems[itemsPre + id];
  1470. }
  1471. /**
  1472. * 加载关联数据
  1473. *
  1474. * @param {Array|Object}datas - 需要关联的数据
  1475. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1476. * @param {Array} fields - 关联字段
  1477. * @returns {Array}
  1478. */
  1479. loadMinorData(datas, fieldSuf, fields, calcFields) {
  1480. for (const cf of calcFields) {
  1481. this.setting.calcFields.push(cf + fieldSuf);
  1482. }
  1483. if (!datas) return;
  1484. datas = datas instanceof Array ? datas : [datas];
  1485. this.minorData[fieldSuf] = datas;
  1486. const loadedData = [];
  1487. for (const data of datas) {
  1488. let node = this.getMasterItems(data[this.setting.minorId]);
  1489. if (node) {
  1490. for (const prop of fields) {
  1491. if (data[prop] !== undefined) {
  1492. node[prop + fieldSuf] = data[prop];
  1493. }
  1494. }
  1495. loadedData.push(node);
  1496. }
  1497. }
  1498. return loadedData;
  1499. }
  1500. }
  1501. class FilterTree extends BaseTree {
  1502. clearDatas() {
  1503. this.items = {};
  1504. this.nodes = [];
  1505. this.datas = [];
  1506. this.children = [];
  1507. }
  1508. addData(data, fields) {
  1509. const item = {};
  1510. for (const prop in data) {
  1511. if (fields.indexOf(prop) >= 0) {
  1512. item[prop] = data[prop];
  1513. }
  1514. }
  1515. const keyName = itemsPre + item[this.setting.id];
  1516. if (!this.items[keyName]) {
  1517. item.children = [];
  1518. item.is_leaf = true;
  1519. item.expanded = true;
  1520. item.visible = true;
  1521. this.items[keyName] = item;
  1522. this.datas.push(item);
  1523. if (item[setting.pid] === setting.rootId) {
  1524. this.children.push(item);
  1525. } else {
  1526. const parent = this.getParent(item);
  1527. if (parent) {
  1528. parent.is_leaf = false;
  1529. parent.children.push(item);
  1530. }
  1531. }
  1532. } else {
  1533. return this.items[keyName];
  1534. }
  1535. return item;
  1536. }
  1537. }
  1538. class GatherTree extends BaseTree {
  1539. clearDatas() {
  1540. this.items = {};
  1541. this.nodes = [];
  1542. this.datas = [];
  1543. this.children = [];
  1544. }
  1545. get newId() {
  1546. if (!this._maxId) {
  1547. this._maxId = 0;
  1548. }
  1549. this._maxId++;
  1550. return this._maxId;
  1551. }
  1552. addNode(data, parent) {
  1553. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1554. let item = _.find(this.items, data);
  1555. if (item) return item;
  1556. item = data;
  1557. item[this.setting.id] = this.newId;
  1558. const keyName = itemsPre + item[this.setting.id];
  1559. item.children = [];
  1560. item.is_leaf = true;
  1561. item.expanded = true;
  1562. item.visible = true;
  1563. this.items[keyName] = item;
  1564. this.datas.push(item);
  1565. if (parent) {
  1566. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  1567. item[this.setting.level] = parent[this.setting.level] + 1;
  1568. item[this.setting.order] = parent.children.length + 1;
  1569. parent.is_leaf = false;
  1570. parent.children.push(item);
  1571. } else {
  1572. item[this.setting.fullPath] = '' + item[this.setting.id];
  1573. item[this.setting.level] = 1;
  1574. item[this.setting.order] = this.children.length + 1;
  1575. this.children.push(item);
  1576. }
  1577. return item;
  1578. }
  1579. sortTreeNodeCustom(field, fun, isResort) {
  1580. const self = this;
  1581. const sortNodes = function (nodes) {
  1582. nodes.sort(function (a, b) {
  1583. return fun(a[field], b[field]);
  1584. });
  1585. for (const [i, node] of nodes.entries()) {
  1586. node[self.setting.order] = i + 1;
  1587. }
  1588. };
  1589. const addSortNodes = function (nodes) {
  1590. if (!nodes) { return }
  1591. for (let i = 0; i < nodes.length; i++) {
  1592. self.nodes.push(nodes[i]);
  1593. nodes[i].index = self.nodes.length - 1;
  1594. if (!isResort) {
  1595. nodes[i].children = self.getChildren(nodes[i]);
  1596. }
  1597. sortNodes(nodes[i].children);
  1598. addSortNodes(nodes[i].children);
  1599. }
  1600. };
  1601. this.nodes = [];
  1602. if (!isResort) {
  1603. this.children = this.getChildren();
  1604. }
  1605. sortNodes(this.children);
  1606. addSortNodes(this.children);
  1607. }
  1608. }
  1609. class CompareTree extends FxTree {
  1610. constructor(setting) {
  1611. super(setting);
  1612. this._newId = 1;
  1613. }
  1614. get newId() {
  1615. return this._newId++;
  1616. }
  1617. findCompareNode(node, parent) {
  1618. if (this.setting.findNode) {
  1619. return this.setting.findNode(this, node, parent);
  1620. } else {
  1621. const siblings = parent ? parent.children : this.children;
  1622. return siblings.find(function (x) {
  1623. return node.b_code
  1624. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1625. : x.code === node.code && x.name === node.name;
  1626. });
  1627. }
  1628. }
  1629. loadCompareNode(source, node, parent, loadFun) {
  1630. let cur = this.findCompareNode(node, parent);
  1631. if (!cur) {
  1632. const siblings = parent ? parent.children : this.children;
  1633. const id = this.newId;
  1634. cur = {
  1635. children: [], pos: [],
  1636. code: node.code, b_code: node.b_code, name: node.name,
  1637. unit: node.unit, unit_price: node.unit_price,
  1638. };
  1639. cur[this.setting.id] = id;
  1640. cur[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1641. cur[this.setting.full_path] = parent ? parent[this.setting.full_path] + '-' + id : '' + id;
  1642. cur[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  1643. cur[this.setting.order] = siblings.length + 1;
  1644. siblings.push(cur);
  1645. this.datas.push(cur);
  1646. }
  1647. loadFun(cur, node, source);
  1648. for (const c of node.children) {
  1649. this.loadCompareNode(source, c, cur, loadFun);
  1650. }
  1651. }
  1652. generateSortNodes() {
  1653. const self = this;
  1654. const addSortNode = function (node) {
  1655. self.nodes.push(node);
  1656. for (const c of node.children) {
  1657. addSortNode(c);
  1658. }
  1659. }
  1660. this.nodes = [];
  1661. for (const n of this.children) {
  1662. addSortNode(n);
  1663. }
  1664. }
  1665. loadCompareTree(data, loadFun) {
  1666. for (const c of data.billsTree.children) {
  1667. this.loadCompareNode(data, c, null, loadFun);
  1668. }
  1669. }
  1670. calculateDiffer() {
  1671. if (this.setting.calcDiffer) {
  1672. for (const d of this.datas) {
  1673. this.setting.calcDiffer(d);
  1674. }
  1675. }
  1676. }
  1677. loadCompareData(data1, data2) {
  1678. this.loadCompareTree(data1, this.setting.loadInfo1);
  1679. this.loadCompareTree(data2, this.setting.loadInfo2);
  1680. for (const d of this.datas) {
  1681. d.is_leaf = d.children.length === 0;
  1682. d.expanded = true;
  1683. d.visible = true;
  1684. this.items[itemsPre + d[this.setting.id]] = d;
  1685. }
  1686. this.generateSortNodes();
  1687. this.calculateDiffer();
  1688. if (this.setting.afterLoad) this.setting.afterLoad(this);
  1689. }
  1690. }
  1691. class TreeGatherTree extends FxTree {
  1692. constructor(setting) {
  1693. super(setting);
  1694. this._newId = 1;
  1695. }
  1696. get newId() {
  1697. return this._newId++;
  1698. }
  1699. loadGatherNode(node, parent, index, loadFun) {
  1700. const siblings = parent ? parent.children : this.children;
  1701. let cur = siblings.find(function (x) {
  1702. return node.b_code
  1703. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1704. : x.code === node.code && x.name === node.name;
  1705. });
  1706. if (!cur) {
  1707. const id = this.newId;
  1708. cur = {
  1709. id: id,
  1710. pid: parent ? parent.id : this.setting.rootId,
  1711. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1712. level: parent ? parent.level + 1 : 1,
  1713. order: siblings.length + 1,
  1714. children: [],
  1715. code: node.code, b_code: node.b_code, name: node.name,
  1716. unit: node.unit, unit_price: node.unit_price,
  1717. };
  1718. siblings.push(cur);
  1719. this.datas.push(cur);
  1720. }
  1721. loadFun(cur, node, index);
  1722. for (const c of node.children) {
  1723. this.loadGatherNode(c, cur, index, loadFun);
  1724. }
  1725. }
  1726. generateSortNodes() {
  1727. const self = this;
  1728. const addSortNode = function (node) {
  1729. self.nodes.push(node);
  1730. for (const c of node.children) {
  1731. addSortNode(c);
  1732. }
  1733. };
  1734. this.nodes = [];
  1735. for (const n of this.children) {
  1736. addSortNode(n);
  1737. }
  1738. }
  1739. loadGatherTree(data, index, loadFun) {
  1740. for (const c of data.billsTree.children) {
  1741. this.loadGatherNode(c, null, index, loadFun);
  1742. }
  1743. // todo load Pos Data;
  1744. }
  1745. calculateSum() {
  1746. if (this.setting.calcSum) {
  1747. for (const d of this.datas) {
  1748. this.setting.calcSum(d, this.count);
  1749. }
  1750. }
  1751. }
  1752. loadGatherData(datas) {
  1753. this.count = datas.length;
  1754. for (const [i, data] of datas.entries()) {
  1755. this.loadGatherTree(data, i+1, this.setting.loadInfo);
  1756. }
  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.calculateSum();
  1765. }
  1766. }
  1767. class FinalTree extends BaseTree {
  1768. constructor(setting) {
  1769. super(setting);
  1770. this._newId = 1;
  1771. }
  1772. get newId() {
  1773. return this._newId++;
  1774. }
  1775. loadNode(node, parent, loadFun) {
  1776. if (node.b_code) return;
  1777. const siblings = parent ? parent.children : this.children;
  1778. let cur = siblings.find(function (x) {
  1779. return x.code === node.code && x.name === node.name;
  1780. });
  1781. if (!cur) {
  1782. const id = this.newId;
  1783. cur = {
  1784. id: id,
  1785. pid: parent ? parent.id : this.setting.rootId,
  1786. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1787. level: parent ? parent.level + 1 : 1,
  1788. order: siblings.length + 1,
  1789. children: [],
  1790. code: node.code, name: node.name, unit: node.unit,
  1791. };
  1792. siblings.push(cur);
  1793. this.datas.push(cur);
  1794. }
  1795. loadFun(cur, node);
  1796. for (const c of node.children) {
  1797. this.loadNode(c, cur, loadFun);
  1798. }
  1799. }
  1800. loadTree(tree, loadFun) {
  1801. for (const node of tree.children) {
  1802. this.loadNode(node, null, loadFun);
  1803. }
  1804. }
  1805. generateSortNodes() {
  1806. const self = this;
  1807. const addSortNode = function (node) {
  1808. self.nodes.push(node);
  1809. for (const c of node.children) {
  1810. addSortNode(c);
  1811. }
  1812. };
  1813. this.nodes = [];
  1814. for (const n of this.children) {
  1815. addSortNode(n);
  1816. }
  1817. }
  1818. afterLoad(fun) {
  1819. for (const d of this.datas) {
  1820. fun && fun(d);
  1821. d.is_leaf = d.children.length === 0;
  1822. d.expanded = true;
  1823. d.visible = true;
  1824. this.items[itemsPre + d[this.setting.id]] = d;
  1825. }
  1826. this.generateSortNodes();
  1827. }
  1828. clearFinal() {
  1829. this.datas = this.datas.filter(x => {
  1830. return !!x.base;
  1831. });
  1832. this.datas.forEach(x => {
  1833. delete x.total_price;
  1834. delete x.dgn_qty1;
  1835. delete x.dgn_qty2;
  1836. delete x.dgn_qty;
  1837. delete x.dgn_price;
  1838. delete x.final_dgn_qty1;
  1839. delete x.final_dgn_qty2;
  1840. delete x.final_dgn_qty;
  1841. delete x.final_dgn_price;
  1842. delete x.final_tp;
  1843. delete x.grow_dgn_qty1;
  1844. delete x.grow_dgn_qty2;
  1845. delete x.grow_dgn_qty;
  1846. delete x.grow_tp;
  1847. });
  1848. this.loadDatas(this.datas);
  1849. }
  1850. resortChildrenByCustom(fun) {
  1851. for (const n of this.nodes) {
  1852. if (n.children && n.children.length > 1) {
  1853. n.children.sort(fun);
  1854. n.children.forEach((x, y) => { x.order = y + 1; });
  1855. }
  1856. }
  1857. this.generateSortNodes();
  1858. }
  1859. }
  1860. if (type === 'base') {
  1861. return new BaseTree(setting);
  1862. } else if (type === 'fx') {
  1863. return new FxTree(setting);
  1864. } else if (type === 'stage') {
  1865. return new StageTree(setting);
  1866. } else if (type === 'ledger') {
  1867. return new LedgerTree(setting);
  1868. } else if (type === 'revise') {
  1869. return new ReviseTree(setting);
  1870. } else if (type === 'measure') {
  1871. return new MeasureTree(setting);
  1872. } else if (type === 'master') {
  1873. return new MasterTree(setting);
  1874. } else if (type === 'filter') {
  1875. return new FilterTree(setting);
  1876. } else if (type === 'gather') {
  1877. return new GatherTree(setting);
  1878. } else if (type === 'compare') {
  1879. return new CompareTree(setting);
  1880. } else if (type === 'tree-gather') {
  1881. return new TreeGatherTree(setting);
  1882. } else if (type === 'final') {
  1883. return new FinalTree(setting);
  1884. }
  1885. };
  1886. const treeCalc = {
  1887. mapTreeNode: function (tree) {
  1888. let map = {}, maxLevel = 0;
  1889. for (const node of tree.nodes) {
  1890. let levelArr = map[node.level];
  1891. if (!levelArr) {
  1892. levelArr = [];
  1893. map[node.level] = levelArr;
  1894. }
  1895. if (node.level > maxLevel) {
  1896. maxLevel = node.level;
  1897. }
  1898. levelArr.push(node);
  1899. }
  1900. return [maxLevel, map];
  1901. },
  1902. getMaxLevel: function (tree) {
  1903. return Math.max.apply(Math, tree.datas.map(function (o) { return o.level }));
  1904. },
  1905. calculateNode: function (tree, node) {
  1906. if (node.children && node.children.length > 0) {
  1907. const gather = node.children.reduce(function (rst, x) {
  1908. const result = {};
  1909. for (const cf of tree.setting.calcFields) {
  1910. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1911. }
  1912. return result;
  1913. });
  1914. // 汇总子项
  1915. for (const cf of tree.setting.calcFields) {
  1916. if (gather[cf]) {
  1917. node[cf] = gather[cf];
  1918. } else {
  1919. node[cf] = null;
  1920. }
  1921. }
  1922. }
  1923. // 自身运算
  1924. if (tree.setting.calcFun) {
  1925. tree.setting.calcFun(node);
  1926. }
  1927. },
  1928. calculateLevelNode: function (tree, level) {
  1929. const nodes = tree.datas.filter((n) => { return n.level === level });
  1930. for (const node of nodes) {
  1931. this.calculateNode(tree, node);
  1932. }
  1933. },
  1934. calculateAll: function (tree) {
  1935. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  1936. for (let i = maxLevel; i >= 0; i--) {
  1937. const levelNodes = levelMap[i];
  1938. if (levelNodes && levelNodes.length > 0) {
  1939. for (const node of levelNodes) {
  1940. this.calculateNode(tree, node);
  1941. }
  1942. }
  1943. }
  1944. },
  1945. calculateParent: function (tree, node) {
  1946. const nodes = tree.getFullPathNodes(node.full_path);
  1947. nodes.sort((a, b) => {
  1948. return b.level - a.level;
  1949. });
  1950. for (const n of nodes) {
  1951. this.calculateNode(tree, n);
  1952. }
  1953. return nodes;
  1954. }
  1955. };