path_tree.js 90 KB

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