path_tree.js 97 KB

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