path_tree.js 97 KB

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