path_tree.js 70 KB

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