path_tree.js 80 KB

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