path_tree.js 82 KB

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