path_tree.js 97 KB

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