path_tree.js 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763
  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. }
  2113. }
  2114. /**
  2115. * 更新数据
  2116. * @param datas
  2117. */
  2118. _updateDatas(data, resort) {
  2119. const datas = data instanceof Array ? data : [data];
  2120. for (const d of datas) {
  2121. const item = this.getItem(d[this.setting.id]);
  2122. if (!item) continue;
  2123. for (const prop in d) {
  2124. item[prop] = d[prop];
  2125. }
  2126. const masterKey = this.itemPre + item[this.setting.masterId];
  2127. if (resort.indexOf(masterKey) < 0) resort.push(masterKey);
  2128. }
  2129. }
  2130. /**
  2131. * 移除数据
  2132. * @param datas
  2133. */
  2134. _removeDatas(data, resort) {
  2135. if (!data) { return; }
  2136. const datas = data instanceof Array ? data : [data];
  2137. for (let i = datas.length - 1; i >= 0; i--) {
  2138. const id = datas[i];
  2139. const d = this.getItem(id);
  2140. this.datas.splice(this.datas.indexOf(d), 1);
  2141. const key = this.itemPre + d[this.setting.id];
  2142. delete this.items[key];
  2143. const range = this.getPartData(d[this.setting.masterId]);
  2144. range.splice(range.indexOf(d), 1);
  2145. }
  2146. }
  2147. _moveDatas(data, resort) {
  2148. const datas = data instanceof Array ? data : [data];
  2149. for (const d of datas) {
  2150. const item = this.getItem(d[this.setting.id]);
  2151. if (!item) continue;
  2152. const oMasterKey = this.itemPre + item[this.setting.masterId];
  2153. if (resort.indexOf(oMasterKey) < 0) resort.push(oMasterKey);
  2154. this.masterIndex[oMasterKey].splice(this.masterIndex[oMasterKey].indexOf(item), 1);
  2155. item[this.setting.masterId] = d[this.setting.masterId];
  2156. const nMasterKey = this.itemPre + item[this.setting.masterId];
  2157. if (!this.masterIndex[nMasterKey]) this.masterIndex[nMasterKey] = [];
  2158. this.masterIndex[nMasterKey].push(item);
  2159. if (resort.indexOf(nMasterKey) < 0) resort.push(nMasterKey);
  2160. }
  2161. }
  2162. updateDatas(data) {
  2163. const resort = [];
  2164. if (data.add) this._addDatas(data.add, resort);
  2165. if (data.del) this._removeDatas(data.del, resort);
  2166. if (data.update) this._updateDatas(data.update, resort);
  2167. if (data.move) this._moveDatas(data.move, resort);
  2168. for (const s of resort) {
  2169. this.resortPart(this.masterIndex[s]);
  2170. this.filterPart(this.masterIndex[s]);
  2171. }
  2172. }
  2173. /**
  2174. * 移除数据 - 根据分类id
  2175. * @param mid
  2176. */
  2177. removeDatasByMasterId(mid) {
  2178. const masterKey = this.itemPre + mid;
  2179. const range = this.masterIndex[masterKey];
  2180. if (range) {
  2181. delete this.masterIndex[masterKey];
  2182. for (const r of range) {
  2183. this.datas.splice(this.datas.indexOf(r), 1);
  2184. const key = this.itemPre + r[this.setting.id];
  2185. delete this.items[key];
  2186. }
  2187. }
  2188. }
  2189. getItem(id) {
  2190. return this.items[this.itemPre + id];
  2191. }
  2192. getPartData(mid) {
  2193. return this.masterIndex[this.itemPre + mid];
  2194. }
  2195. set sort(sort) {
  2196. this.setting.sort = sort;
  2197. for (const key in this.masterIndex) {
  2198. this.resortPart(this.masterIndex[key]);
  2199. }
  2200. }
  2201. set filter(condition) {
  2202. this._filter = condition;
  2203. for (const key in this.masterIndex) {
  2204. this.filterPart(this.masterIndex[key]);
  2205. }
  2206. }
  2207. eachPart(fun) {
  2208. for (const prop in this.masterIndex) {
  2209. fun(prop.substring(3, prop.length), this.masterIndex[prop]);
  2210. }
  2211. }
  2212. }
  2213. return new AncillaryGcl(setting);
  2214. };
  2215. const createPosCalcDetail = function (setting) {
  2216. class PosCalcDetail {
  2217. constructor(setting) {
  2218. this.itemPre = 'id_';
  2219. // 无索引
  2220. this.datas = [];
  2221. // 以key为索引
  2222. this.items = {};
  2223. // 以分类id为索引的有序
  2224. this.masterIndex = {}; // posCalcDetail ---pid---> pos
  2225. this.topIndex = {}; // pid ---lid---> bills
  2226. // 设置
  2227. this.setting = setting;
  2228. }
  2229. resortPart(partData) {
  2230. const sortRule = this.setting.sort || [['pcd_order', 'asc']];
  2231. if (partData instanceof Array) {
  2232. partData.sort(function (a, b) {
  2233. for (const sr of sortRule) {
  2234. const iSort = sr[1] === 'asc' ? a[sr[0]] - b[sr[0]] : b[sr[0]] - a[sr[0]];
  2235. if (iSort) return iSort;
  2236. }
  2237. })
  2238. }
  2239. }
  2240. /**
  2241. * 加载数据
  2242. * @param datas
  2243. */
  2244. loadDatas(datas) {
  2245. this.datas = datas;
  2246. this.items = {};
  2247. this.masterIndex = {};
  2248. this.topIndex = {};
  2249. for (const data of this.datas) {
  2250. const key = this.itemPre + data[this.setting.id];
  2251. this.items[key] = data;
  2252. const masterKey = this.itemPre + data[this.setting.masterId];
  2253. if (!this.masterIndex[masterKey]) {
  2254. this.masterIndex[masterKey] = [];
  2255. }
  2256. this.masterIndex[masterKey].push(data);
  2257. const topKey = this.itemPre + data[this.setting.topId];
  2258. if (!this.topIndex[topKey]) this.topIndex[topKey] = [];
  2259. if (this.topIndex[topKey].indexOf(data[this.setting.masterId]) < 0) this.topIndex[topKey].push(data[this.setting.masterId]);
  2260. }
  2261. for (const prop in this.masterIndex) {
  2262. this.resortPart(this.masterIndex[prop]);
  2263. }
  2264. }
  2265. _addDatas(data, resort) {
  2266. const datas = data instanceof Array ? data : [data];
  2267. for (const d of datas) {
  2268. const key = this.itemPre + d[this.setting.id];
  2269. this.items[key] = d;
  2270. const masterKey = this.itemPre + d[this.setting.masterId];
  2271. if (!this.masterIndex[masterKey]) this.masterIndex[masterKey] = [];
  2272. this.masterIndex[masterKey].push(d);
  2273. if (resort.indexOf(masterKey) < 0) resort.push(masterKey);
  2274. const topKey = this.itemPre + data[this.setting.topId];
  2275. if (!this.topIndex[topKey]) this.topIndex[topKey] = [];
  2276. if (this.topIndex[topKey].indexOf(data[this.setting.masterId]) < 0) this.topIndex[topKey].push(data[this.setting.masterId]);
  2277. }
  2278. }
  2279. /**
  2280. * 更新数据
  2281. * @param datas
  2282. */
  2283. _updateDatas(data, resort) {
  2284. const datas = data instanceof Array ? data : [data];
  2285. for (const d of datas) {
  2286. const item = this.getItem(d[this.setting.id]);
  2287. if (!item) continue;
  2288. for (const prop in d) {
  2289. item[prop] = d[prop];
  2290. }
  2291. const masterKey = this.itemPre + item[this.setting.masterId];
  2292. if (resort.indexOf(masterKey) < 0) resort.push(masterKey);
  2293. }
  2294. }
  2295. /**
  2296. * 移除数据
  2297. * @param datas
  2298. */
  2299. _removeDatas(data, resort) {
  2300. if (!data) { return; }
  2301. const datas = data instanceof Array ? data : [data];
  2302. for (let i = datas.length - 1; i >= 0; i--) {
  2303. const id = datas[i];
  2304. const d = this.getItem(id);
  2305. this.datas.splice(this.datas.indexOf(d), 1);
  2306. const key = this.itemPre + d[this.setting.id];
  2307. delete this.items[key];
  2308. const range = this.getPartData(d[this.setting.masterId]);
  2309. range.splice(range.indexOf(d), 1);
  2310. }
  2311. }
  2312. updateDatas(data) {
  2313. const resort = [];
  2314. if (data.add) this._addDatas(data.add, resort);
  2315. if (data.del) this._removeDatas(data.del, resort);
  2316. if (data.update) this._updateDatas(data.update, resort);
  2317. for (const s of resort) {
  2318. this.resortPart(this.masterIndex[s]);
  2319. }
  2320. }
  2321. /**
  2322. * 移除数据 - 根据分类id
  2323. * @param mid
  2324. */
  2325. removeDatasByMasterId(mid) {
  2326. const masterKey = this.itemPre + mid;
  2327. const range = this.masterIndex[masterKey];
  2328. if (range) {
  2329. delete this.masterIndex[masterKey];
  2330. for (const r of range) {
  2331. this.datas.splice(this.datas.indexOf(r), 1);
  2332. const key = this.itemPre + r[this.setting.id];
  2333. delete this.items[key];
  2334. }
  2335. }
  2336. }
  2337. removeDatasByTopId(topId) {
  2338. const topKey = this.itemPre + topId;
  2339. const topRange = this.topIndex[topKey];
  2340. if (topRange) {
  2341. delete this.topIndex[topKey];
  2342. for (const tr of topRange) {
  2343. this.removeDatasByMasterId(tr);
  2344. }
  2345. }
  2346. }
  2347. getItem(id) {
  2348. return this.items[this.itemPre + id];
  2349. }
  2350. getPartData(mid) {
  2351. return this.masterIndex[this.itemPre + mid];
  2352. }
  2353. set sort(sort) {
  2354. this.setting.sort = sort;
  2355. for (const key in this.masterIndex) {
  2356. this.resortPart(this.masterIndex[key]);
  2357. }
  2358. }
  2359. }
  2360. return new PosCalcDetail(setting);
  2361. };
  2362. const createAncGclDetail = function (setting) {
  2363. class PosCalcDetail {
  2364. constructor(setting) {
  2365. this.itemPre = 'id_';
  2366. // 无索引
  2367. this.datas = [];
  2368. // 以key为索引
  2369. this.items = {};
  2370. // 以分类id为索引的有序
  2371. this.masterIndex = {}; // posCalcDetail ---ag_id---> ancGcl
  2372. this.secondIndex = {}; // ag_id --- pid ---> pos
  2373. this.firstIndex = {}; // ag_id --- pid --- lid ---> bills
  2374. // 设置
  2375. this.setting = setting;
  2376. }
  2377. resortPart(partData) {
  2378. const sortRule = this.setting.sort || [['agd_order', 'asc']];
  2379. if (partData instanceof Array) {
  2380. partData.sort(function (a, b) {
  2381. for (const sr of sortRule) {
  2382. const iSort = sr[1] === 'asc' ? a[sr[0]] - b[sr[0]] : b[sr[0]] - a[sr[0]];
  2383. if (iSort) return iSort;
  2384. }
  2385. })
  2386. }
  2387. }
  2388. /**
  2389. * 加载数据
  2390. * @param datas
  2391. */
  2392. loadDatas(datas) {
  2393. this.datas = datas;
  2394. this.items = {};
  2395. this.masterIndex = {};
  2396. this.secondIndex = {};
  2397. this.firstIndex = {};
  2398. for (const data of this.datas) {
  2399. const key = this.itemPre + data[this.setting.id];
  2400. this.items[key] = data;
  2401. const masterKey = this.itemPre + data[this.setting.masterId];
  2402. if (!this.masterIndex[masterKey]) {
  2403. this.masterIndex[masterKey] = [];
  2404. }
  2405. this.masterIndex[masterKey].push(data);
  2406. const secondKey = this.itemPre + data[this.setting.secondId];
  2407. if (!this.secondIndex[secondKey]) this.secondIndex[secondKey] = [];
  2408. if (this.secondIndex[secondKey].indexOf(data[this.setting.masterId]) < 0) this.secondIndex[secondKey].push(data[this.setting.masterId]);
  2409. const firstKey = this.itemPre + data[this.setting.firstId];
  2410. if (!this.firstIndex[firstKey]) this.firstIndex[firstKey] = [];
  2411. if (this.firstIndex[firstKey].indexOf(data[this.setting.secondId]) < 0) this.firstIndex[firstKey].push(data[this.setting.secondId]);
  2412. }
  2413. for (const prop in this.masterIndex) {
  2414. this.resortPart(this.masterIndex[prop]);
  2415. }
  2416. }
  2417. _addDatas(data, resort) {
  2418. const datas = data instanceof Array ? data : [data];
  2419. for (const d of datas) {
  2420. const key = this.itemPre + d[this.setting.id];
  2421. this.items[key] = d;
  2422. const masterKey = this.itemPre + d[this.setting.masterId];
  2423. if (!this.masterIndex[masterKey]) this.masterIndex[masterKey] = [];
  2424. this.masterIndex[masterKey].push(d);
  2425. if (resort.indexOf(masterKey) < 0) resort.push(masterKey);
  2426. const secondKey = this.itemPre + data[this.setting.secondId];
  2427. if (!this.secondIndex[secondKey]) this.secondIndex[secondKey] = [];
  2428. if (this.secondIndex[secondKey].indexOf(data[this.setting.masterId]) < 0) this.secondIndex[secondKey].push(data[this.setting.masterId]);
  2429. const firstKey = this.itemPre + data[this.setting.firstId];
  2430. if (!this.firstIndex[firstKey]) this.firstIndex[firstKey] = [];
  2431. if (this.firstIndex[firstKey].indexOf(data[this.setting.secondId]) < 0) this.firstIndex[firstKey].push(data[this.setting.secondId]);
  2432. }
  2433. }
  2434. /**
  2435. * 更新数据
  2436. * @param datas
  2437. */
  2438. _updateDatas(data, resort) {
  2439. const datas = data instanceof Array ? data : [data];
  2440. for (const d of datas) {
  2441. const item = this.getItem(d[this.setting.id]);
  2442. if (!item) continue;
  2443. for (const prop in d) {
  2444. item[prop] = d[prop];
  2445. }
  2446. const masterKey = this.itemPre + item[this.setting.masterId];
  2447. if (resort.indexOf(masterKey) < 0) resort.push(masterKey);
  2448. }
  2449. }
  2450. /**
  2451. * 移除数据
  2452. * @param datas
  2453. */
  2454. _removeDatas(data, resort) {
  2455. if (!data) { return; }
  2456. const datas = data instanceof Array ? data : [data];
  2457. for (let i = datas.length - 1; i >= 0; i--) {
  2458. const id = datas[i];
  2459. const d = this.getItem(id);
  2460. this.datas.splice(this.datas.indexOf(d), 1);
  2461. const key = this.itemPre + d[this.setting.id];
  2462. delete this.items[key];
  2463. const range = this.getPartData(d[this.setting.masterId]);
  2464. range.splice(range.indexOf(d), 1);
  2465. }
  2466. }
  2467. updateDatas(data) {
  2468. const resort = [];
  2469. if (data.add) this._addDatas(data.add, resort);
  2470. if (data.del) this._removeDatas(data.del, resort);
  2471. if (data.update) this._updateDatas(data.update, resort);
  2472. for (const s of resort) {
  2473. this.resortPart(this.masterIndex[s]);
  2474. }
  2475. }
  2476. /**
  2477. * 移除数据 - 根据分类id
  2478. * @param mid
  2479. */
  2480. removeDatasByMasterId(mid) {
  2481. const masterKey = this.itemPre + mid;
  2482. const range = this.masterIndex[masterKey];
  2483. if (range) {
  2484. delete this.masterIndex[masterKey];
  2485. for (const r of range) {
  2486. this.datas.splice(this.datas.indexOf(r), 1);
  2487. const key = this.itemPre + r[this.setting.id];
  2488. delete this.items[key];
  2489. }
  2490. }
  2491. }
  2492. removeDatasBySecondId(secondId) {
  2493. const secondKey = this.itemPre + secondId;
  2494. const secondRange = this.secondIndex[secondKey];
  2495. if (secondRange) {
  2496. delete this.secondIndex[secondKey];
  2497. for (const tr of secondRange) {
  2498. this.removeDatasByMasterId(tr);
  2499. }
  2500. }
  2501. }
  2502. removeDatasByFirstId(firstId) {
  2503. const firstKey = this.itemPre + firstId;
  2504. const firstRange = this.secondIndex[firstKey];
  2505. if (firstRange) {
  2506. delete this.secondIndex[firstKey];
  2507. for (const tr of firstRange) {
  2508. this.removeDatasBySecondId(tr);
  2509. }
  2510. }
  2511. }
  2512. getItem(id) {
  2513. return this.items[this.itemPre + id];
  2514. }
  2515. getPartData(mid) {
  2516. return this.masterIndex[this.itemPre + mid];
  2517. }
  2518. set sort(sort) {
  2519. this.setting.sort = sort;
  2520. for (const key in this.masterIndex) {
  2521. this.resortPart(this.masterIndex[key]);
  2522. }
  2523. }
  2524. }
  2525. return new PosCalcDetail(setting);
  2526. };
  2527. const treeCalc = {
  2528. mapTreeNode: function (tree) {
  2529. const setting = tree.setting;
  2530. let map = {}, maxLevel = 0;
  2531. for (const node of tree.nodes) {
  2532. let levelArr = map[node[setting.level]];
  2533. if (!levelArr) {
  2534. levelArr = [];
  2535. map[node[setting.level]] = levelArr;
  2536. }
  2537. if (node[setting.level] > maxLevel) {
  2538. maxLevel = node[setting.level];
  2539. }
  2540. levelArr.push(node);
  2541. }
  2542. return [maxLevel, map];
  2543. },
  2544. getMaxLevel: function (tree) {
  2545. const setting = tree.setting;
  2546. return Math.max.apply(Math, tree.datas.map(function (o) { return o[setting.level] }));
  2547. },
  2548. calculateNode: function (tree, node, customFun) {
  2549. if (node.children && node.children.length > 0 && tree.setting.calcFields) {
  2550. const gather = {};
  2551. node.children.forEach(c => {
  2552. for (const cf of tree.setting.calcFields) {
  2553. gather[cf] = ZhCalc.add(gather[cf], c[cf]);
  2554. }
  2555. });
  2556. // const gather = node.children.reduce(function (rst, x) {
  2557. // const result = {};
  2558. // for (const cf of tree.setting.calcFields) {
  2559. // result[cf] = ZhCalc.add(rst[cf], x[cf]);
  2560. // }
  2561. // return result;
  2562. // });
  2563. // 汇总子项
  2564. for (const cf of tree.setting.calcFields) {
  2565. if (gather[cf]) {
  2566. node[cf] = gather[cf];
  2567. } else {
  2568. node[cf] = null;
  2569. }
  2570. }
  2571. }
  2572. // 自身运算
  2573. if (tree.setting.calcFun) {
  2574. tree.setting.calcFun(node);
  2575. }
  2576. if (customFun) customFun(node);
  2577. },
  2578. calculateLevelNode: function (tree, level) {
  2579. const setting = tree.setting;
  2580. const nodes = tree.datas.filter((n) => { return n[setting.level] === level });
  2581. for (const node of nodes) {
  2582. this.calculateNode(tree, node);
  2583. }
  2584. },
  2585. calculateAll: function (tree, customFun) {
  2586. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  2587. for (let i = maxLevel; i >= 0; i--) {
  2588. const levelNodes = levelMap[i];
  2589. if (levelNodes && levelNodes.length > 0) {
  2590. for (const node of levelNodes) {
  2591. this.calculateNode(tree, node, customFun);
  2592. }
  2593. }
  2594. }
  2595. },
  2596. calculateParent: function (tree, node, converse = -1) {
  2597. const setting = tree.setting;
  2598. const nodes = tree.getFullPathNodes(node[setting.fullPath]);
  2599. converse === -1 ? tree.sortByLevel(nodes) : tree.sortByLevelConverse(nodes);
  2600. for (const n of nodes) {
  2601. this.calculateNode(tree, n);
  2602. }
  2603. return nodes;
  2604. }
  2605. };