path_tree.js 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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. if (ledgerPos instanceof Array) {
  137. ledgerPos.sort(function (a, b) {
  138. return a.porder - b.porder;
  139. })
  140. }
  141. }
  142. /**
  143. * 计算全部
  144. */
  145. calculateAll() {
  146. if (!this.setting.calcFun) { return; }
  147. for (const pos of this.datas) {
  148. this.setting.calcFun(pos);
  149. }
  150. }
  151. }
  152. class StagePosData extends PosData {
  153. loadStageData(datas, fieldPre, fields) {
  154. if (!datas) { return; }
  155. datas = datas instanceof Array ? datas : [datas];
  156. const loadedData = [];
  157. for (const data of datas) {
  158. let node = this.getPos(data.pid);
  159. if (node) {
  160. for (const prop of fields) {
  161. if (data[prop] !== undefined) {
  162. node[fieldPre + prop] = data[prop];
  163. }
  164. }
  165. if (this.setting.calcFun) {
  166. this.setting.calcFun(node);
  167. }
  168. loadedData.push(node);
  169. }
  170. }
  171. }
  172. loadPreStageData(datas) {
  173. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  174. }
  175. loadCurStageData(datas) {
  176. this.loadStageData(datas, '', this.setting.updateFields);
  177. }
  178. }
  179. class MasterPosData extends PosData {
  180. /**
  181. * 构造函数
  182. * @param {id|Number, masterId|Number} setting
  183. */
  184. constructor(setting) {
  185. super(setting);
  186. // 关联索引
  187. this.masterItems = {};
  188. }
  189. /**
  190. * 加载主数据
  191. * @param datas
  192. */
  193. loadDatas(datas) {
  194. super.loadDatas(datas);
  195. // 清空旧数据
  196. this.masterItems = {};
  197. // minor数据缓存
  198. this.minorData = {};
  199. // 加载全部数据
  200. for (const data of this.datas) {
  201. const keyName = itemsPre + data[this.setting.masterId];
  202. this.masterItems[keyName] = data;
  203. }
  204. }
  205. /**
  206. * 根据关联id,查找节点
  207. * @param id
  208. * @returns {*}
  209. */
  210. getMasterItems(id) {
  211. return this.masterItems[itemsPre + id];
  212. }
  213. /**
  214. * 加载关联数据
  215. *
  216. * @param {Array|Object}datas - 需要关联的数据
  217. * @param {String} fieldPre - 关联字段前缀(关联结果)
  218. * @param {Array} fields - 关联字段
  219. * @returns {Array}
  220. */
  221. loadMinorData(datas, fieldSuf, fields) {
  222. if (!datas) return;
  223. datas = datas instanceof Array ? datas : [datas];
  224. this.minorData[fieldSuf] = datas;
  225. const loadedData = [];
  226. for (const data of datas) {
  227. let node = this.getMasterItems(data[this.setting.minorId]);
  228. if (node) {
  229. for (const prop of fields) {
  230. if (data[prop] !== undefined) {
  231. node[prop + fieldSuf] = data[prop];
  232. }
  233. }
  234. loadedData.push(node);
  235. }
  236. }
  237. return loadedData;
  238. }
  239. }
  240. const createNewPathTree = function (type, setting) {
  241. class BaseTree {
  242. /**
  243. * 构造函数
  244. */
  245. constructor(setting) {
  246. const self = this;
  247. // 无索引
  248. this.datas = [];
  249. // 以key为索引indexedDB
  250. this.items = {};
  251. // 以排序为索引
  252. this.nodes = [];
  253. // 根节点
  254. this.children = [];
  255. // 树设置
  256. this.setting = setting;
  257. this.hasMark = false;
  258. if (this.setting.markExpandKey) {
  259. const markStr = getLocalCache(this.setting.markExpandKey);
  260. const markData = markStr ? markStr.split('|') : ['', ''];
  261. this.hasMark = markData[0] === this.setting.markExpandSubKey;
  262. this.markExpand = this.hasMark && markData[1] ? _.map(markData[1].split(','), _.toInteger) : [];
  263. } else if (this.setting.markFoldKey) {
  264. const markStr = getLocalCache(this.setting.markFoldKey);
  265. const markData = markStr ? markStr.split('|') : ['', ''];
  266. this.hasMark = markData[0] === this.setting.markFoldSubKey;
  267. this.markFold = this.hasMark && markData[1] ? _.map(markData[1].split(','), _.toInteger) : [];
  268. }
  269. // if (this.setting.treeCacheKey) {
  270. // localforage.getItem(this.setting.treeCacheKey).then(function (v) {
  271. // self.markFold = v && v.markFold ? v.markFold : [];
  272. // });
  273. // }
  274. }
  275. /**
  276. * 树结构根据显示排序
  277. */
  278. sortTreeNode(isResort) {
  279. const self = this;
  280. const addSortNodes = function (nodes) {
  281. if (!nodes) { return }
  282. for (let i = 0; i < nodes.length; i++) {
  283. self.nodes.push(nodes[i]);
  284. nodes[i].index = self.nodes.length - 1;
  285. if (!isResort) {
  286. nodes[i].children = self.getChildren(nodes[i]);
  287. } else {
  288. nodes[i].children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; })
  289. }
  290. addSortNodes(nodes[i].children);
  291. }
  292. };
  293. this.nodes = [];
  294. if (!isResort) {
  295. this.children = this.getChildren();
  296. } else {
  297. this.children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; });
  298. }
  299. addSortNodes(this.children);
  300. }
  301. /**
  302. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  303. * @param datas
  304. */
  305. loadDatas(datas) {
  306. const self = this;
  307. // 清空旧数据
  308. this.items = {};
  309. this.nodes = [];
  310. this.datas = [];
  311. this.children = [];
  312. // 加载全部数据
  313. datas.sort(function (a, b) {
  314. return a.level - b.level;
  315. });
  316. for (const data of datas) {
  317. const keyName = itemsPre + data[this.setting.id];
  318. if (this.items[keyName]) continue;
  319. const item = JSON.parse(JSON.stringify(data));
  320. item.children = [];
  321. item.expanded = true;
  322. item.visible = true;
  323. if (item[setting.pid] === setting.rootId) {
  324. this.children.push(item);
  325. } else {
  326. const parent = this.getParent(item);
  327. if (!parent) continue;
  328. parent.children.push(item);
  329. }
  330. this.items[keyName] = item;
  331. this.datas.push(item);
  332. }
  333. this.children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; });
  334. this.sortTreeNode(true);
  335. if (this.hasMark) {
  336. if (this.setting.markExpandKey) {
  337. this.expandByCustom(node => {
  338. return self.markExpand.indexOf(node[self.setting.id]) >= 0;
  339. });
  340. } else if (this.setting.markFoldKey) {
  341. this.expandByCustom(function (node) {
  342. return self.markFold.indexOf(node[self.setting.id]) === -1;
  343. });
  344. }
  345. } else {
  346. if (this.setting.autoExpand >= 0) {
  347. this.expandByLevel(this.setting.autoExpand);
  348. for (const node of this.nodes) {
  349. this._markExpandFold(node);
  350. }
  351. this._saveMarkExpandFold();
  352. }
  353. }
  354. }
  355. getItemsByIndex(index) {
  356. return this.nodes[index];
  357. }
  358. /**
  359. * 根据id获取树结构节点数据
  360. * @param {Number} id
  361. * @returns {Object}
  362. */
  363. getItems(id) {
  364. return this.items[itemsPre + id];
  365. };
  366. getNodeIndex(node) {
  367. return this.nodes.indexOf(node);
  368. }
  369. /**
  370. * 查找node的parent
  371. * @param {Object} node
  372. * @returns {Object}
  373. */
  374. getParent(node) {
  375. return this.getItems(node[this.setting.pid]);
  376. };
  377. getAllParents(node) {
  378. const parents = [];
  379. if (node.full_path && node.full_path !== '') {
  380. const parentIds = node.full_path.split('-');
  381. for (const id of parentIds) {
  382. if (id !== node[this.setting.id]) {
  383. parents.push(this.getItems(id));
  384. }
  385. }
  386. } else {
  387. let vP = this.getParent(node);
  388. while (vP) {
  389. parents.push(vP);
  390. vP = this.getParent(vP);
  391. }
  392. }
  393. return parents;
  394. }
  395. /**
  396. * 查找node的前兄弟节点
  397. * @param node
  398. * @returns {*}
  399. */
  400. getPreSiblingNode(node) {
  401. if (!node) return null;
  402. const parent = this.getParent(node);
  403. const siblings = parent ? parent.children : this.children;
  404. const index = siblings.indexOf(node);
  405. return (index > 0) ? siblings[index - 1] : null;
  406. }
  407. /**
  408. * 查找node的后兄弟节点
  409. * @param node
  410. * @returns {*}
  411. */
  412. getNextSiblingNode(node) {
  413. const parent = this.getParent(node);
  414. const siblings = parent ? parent.children : this.children;
  415. const index = siblings.indexOf(node);
  416. if (index >= 0 && index < siblings.length - 1) {
  417. return siblings[index + 1];
  418. } else {
  419. return null;
  420. }
  421. }
  422. /**
  423. * 根据path查找完整节点
  424. * @param {Number} path
  425. */
  426. getFullPathNodes(path) {
  427. const self = this, ids = path.split('-');
  428. if (ids.length > 0) {
  429. return this.nodes.filter((x) => {
  430. return ids.indexOf('' + x[self.setting.id]) >= 0;
  431. });
  432. } else {
  433. return [];
  434. }
  435. };
  436. /**
  437. * 查询node的已下载子节点
  438. * @param {Object} node
  439. * @returns {Array}
  440. */
  441. getChildren(node) {
  442. const setting = this.setting;
  443. const pid = node ? node[setting.id] : setting.rootId;
  444. const children = this.datas.filter(function (x) {
  445. return x[setting.pid] === pid;
  446. });
  447. children.sort((a, b) => { return a[setting.order] - b[setting.order]; });
  448. return children;
  449. };
  450. /**
  451. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  452. * @param node
  453. * @returns {*}
  454. * @private
  455. */
  456. _recursiveGetPosterity(node) {
  457. let posterity = node.children;
  458. for (const c of node.children) {
  459. posterity = posterity.concat(this._recursiveGetPosterity(c));
  460. }
  461. return posterity;
  462. };
  463. /**
  464. * 查询node的已下载的全部后代
  465. * @param {Object} node
  466. * @returns {Array}
  467. */
  468. getPosterity(node) {
  469. const self = this;
  470. let posterity;
  471. if (node.full_path !== '') {
  472. const reg = new RegExp('^' + node.full_path + '-');
  473. posterity = this.datas.filter(function (x) {
  474. return reg.test(x.full_path);
  475. });
  476. } else {
  477. posterity = this._recursiveGetPosterity(node);
  478. }
  479. posterity.sort(function (x, y) {
  480. return self.getNodeIndex(x) - self.getNodeIndex(y);
  481. });
  482. return posterity;
  483. };
  484. /**
  485. * 查询node是否是父节点的最后一个子节点
  486. * @param {Object} node
  487. * @returns {boolean}
  488. */
  489. isLastSibling(node) {
  490. const siblings = this.getChildren(this.getParent(node));
  491. return (siblings && siblings.length > 0) ? node[this.setting.order] === siblings[siblings.length - 1][this.setting.order] : false;
  492. };
  493. /**
  494. * 提取节点key和索引数据
  495. * @param {Object} node - 节点
  496. * @returns {key}
  497. */
  498. getNodeKeyData(node) {
  499. const data = {};
  500. for (const key of this.setting.keys) {
  501. data[key] = node[key];
  502. }
  503. return data;
  504. };
  505. /**
  506. * 得到树结构构成id
  507. * @param node
  508. * @returns {*}
  509. */
  510. getNodeKey(node) {
  511. return node[this.setting.id];
  512. };
  513. _markExpandFold(node) {
  514. if (this.setting.markExpandKey && this.setting.markExpandSubKey) {
  515. if (node.expanded) {
  516. if (this.markExpand.indexOf(node[this.setting.id]) === -1)
  517. this.markExpand.push(node[this.setting.id]);
  518. } else {
  519. if (this.markExpand.indexOf(node[this.setting.id]) >= 0)
  520. this.markExpand.splice(this.markExpand.indexOf(node[this.setting.id]), 1);
  521. }
  522. } else if (this.setting.markFoldKey && this.setting.markFoldSubKey) {
  523. if (!node.expanded) {
  524. if (this.markFold.indexOf(node[this.setting.id]) === -1)
  525. this.markFold.push(node[this.setting.id]);
  526. } else {
  527. if (this.markFold.indexOf(node[this.setting.id]) >= 0)
  528. this.markFold.splice(this.markFold.indexOf(node[this.setting.id]), 1);
  529. }
  530. }
  531. }
  532. _saveMarkExpandFold() {
  533. if (this.setting.markExpandKey && this.setting.markExpandSubKey) {
  534. setLocalCache(this.setting.markExpandKey, this.setting.markExpandSubKey + '|' + this.markExpand.join(','));
  535. } else if (this.setting.markFoldKey && this.setting.markFoldSubKey) {
  536. setLocalCache(this.setting.markFoldKey, this.setting.markFoldSubKey + '|' + this.markFold.join(','));
  537. }
  538. // if (this.setting.treeCacheKey) {
  539. // localforage.setItem(this.setting.treeCacheKey, {
  540. // markFold: this.markFold,
  541. // time: new Date(),
  542. // });
  543. // }
  544. }
  545. /**
  546. * 刷新子节点是否可见
  547. * @param {Object} node
  548. * @private
  549. */
  550. _refreshChildrenVisible(node) {
  551. if (!node.children) {
  552. node.children = this.getChildren(node);
  553. }
  554. if (node.children && node.children.length > 0) {
  555. for (const child of node.children) {
  556. child.visible = node.expanded && node.visible;
  557. this._refreshChildrenVisible(child);
  558. }
  559. }
  560. };
  561. /**
  562. * 设置节点是否展开, 并控制子节点可见
  563. * @param {Object} node
  564. * @param {Boolean} expanded
  565. */
  566. setExpanded(node, expanded) {
  567. node.expanded = expanded;
  568. this._markExpandFold(node);
  569. this._refreshChildrenVisible(node);
  570. this._saveMarkExpandFold();
  571. };
  572. /**
  573. * 递归 设置节点展开状态
  574. * @param {Array} nodes - 需要设置状态的节点
  575. * @param {Object} parent - nodes的父节点
  576. * @param {Function} checkFun - 判断节点展开状态的方法
  577. * @private
  578. */
  579. _recursiveExpand(nodes, parent, checkFun) {
  580. for (const node of nodes) {
  581. const expanded = checkFun(node);
  582. if (node.expanded !== expanded) {
  583. node.expanded = expanded;
  584. this._markExpandFold(node);
  585. }
  586. node.visible = parent ? (parent.expanded && parent.visible) : true;
  587. this._recursiveExpand(node.children, node, checkFun);
  588. }
  589. }
  590. /**
  591. * 自定义展开规则
  592. * @param checkFun
  593. */
  594. expandByCustom(checkFun) {
  595. this._recursiveExpand(this.children, null, checkFun);
  596. this._saveMarkExpandFold();
  597. }
  598. /**
  599. * 展开到第几层
  600. * @param {Number} level - 展开层数
  601. */
  602. expandByLevel(level) {
  603. this.expandByCustom(function (n) {
  604. return n.level < level;
  605. });
  606. }
  607. /**
  608. * 自动展开节点node
  609. * @param node
  610. * @returns {*}
  611. */
  612. autoExpandNode(node) {
  613. const parents = this.getAllParents(node);
  614. const reload = [];
  615. for (const p of parents) {
  616. if (!p.expanded) {
  617. reload.push(p);
  618. this.setExpanded(p, true);
  619. }
  620. }
  621. return reload;
  622. }
  623. /**
  624. * 加载数据(动态),只加载不同部分
  625. * @param {Array} datas
  626. * @return {Array} 加载到树的数据
  627. * @privateA
  628. */
  629. _updateData(datas) {
  630. datas = datas instanceof Array ? datas : [datas];
  631. let loadedData = [];
  632. for (const data of datas) {
  633. let node = this.getItems(data[this.setting.id]);
  634. if (node) {
  635. for (const prop in data) {
  636. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  637. if (prop === this.setting.pid) {
  638. loadedData.push(this.getItems(node[this.setting.pid]));
  639. loadedData.push(this.getItems(data[this.setting.pid]));
  640. }
  641. if (prop === this.setting.order) {
  642. loadedData = loadedData.concat(this.getPosterity(node));
  643. }
  644. node[prop] = data[prop];
  645. }
  646. }
  647. loadedData.push(node);
  648. }
  649. }
  650. loadedData = _.uniq(loadedData);
  651. for (const node of loadedData) {
  652. if (node) {
  653. node.children = this.getChildren(node);
  654. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  655. } else {
  656. this.children = this.getChildren(null);
  657. }
  658. }
  659. this.sortTreeNode(true);
  660. return loadedData;
  661. };
  662. /**
  663. * 加载数据(动态),只加载不同部分
  664. * @param {Array} datas
  665. * @return {Array} 加载到树的数据
  666. * @privateA
  667. */
  668. _loadData(datas) {
  669. datas = datas instanceof Array ? datas : [datas];
  670. const loadedData = [], resortData = [];
  671. for (const data of datas) {
  672. let node = this.getItems(data[this.setting.id]);
  673. if (node) {
  674. const parent = this.getItems(node[this.setting.pid]);
  675. for (const prop in data) {
  676. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  677. node[prop] = data[prop];
  678. if (parent && resortData.indexOf(parent) === -1) {
  679. resortData.push(parent);
  680. }
  681. }
  682. }
  683. loadedData.push(node);
  684. } else {
  685. const keyName = itemsPre + data[this.setting.id];
  686. const node = JSON.parse(JSON.stringify(data));
  687. this.items[keyName] = node;
  688. this.datas.push(node);
  689. node.expanded = true;
  690. node.visible = true;
  691. loadedData.push(node);
  692. if (resortData.indexOf(node) === -1) {
  693. resortData.push(node);
  694. }
  695. const parent = this.getItems(node[this.setting.pid]);
  696. if (parent && resortData.indexOf(parent) === -1) {
  697. resortData.push(parent);
  698. } else {
  699. resortData.push(this.setting.rootId);
  700. }
  701. }
  702. }
  703. for (const node of resortData) {
  704. if (node && node !== this.setting.rootId) {
  705. node.children = this.getChildren(node);
  706. } else {
  707. this.children = this.getChildren(null);
  708. }
  709. }
  710. this.sortTreeNode(true);
  711. for (const node of loadedData) {
  712. if (!node.expanded) {
  713. this.setExpanded(node, true);
  714. }
  715. }
  716. return loadedData;
  717. };
  718. /**
  719. * 清理数据(动态)
  720. * @param datas
  721. * @private
  722. */
  723. _freeData(datas) {
  724. datas = datas instanceof Array ? datas : [datas];
  725. const freeDatas = [];
  726. const removeArrayData = function (array, data) {
  727. const index = array.indexOf(data);
  728. array.splice(index, 1);
  729. };
  730. for (const data of datas) {
  731. const node = this.getItems(data[this.setting.id]);
  732. if (node) {
  733. freeDatas.push(node);
  734. node.deleteIndex = this.nodes.indexOf(node);
  735. delete this.items[itemsPre + node[this.setting.id]];
  736. if (node[this.setting.pid] !== this.setting.rootId) {
  737. const parent = this.getItems(node[this.setting.pid]);
  738. if (parent) {
  739. removeArrayData(parent.children, node);
  740. }
  741. } else {
  742. removeArrayData(this.children, node);
  743. }
  744. removeArrayData(this.datas, node);
  745. }
  746. }
  747. for (const node of freeDatas) {
  748. removeArrayData(this.nodes, node);
  749. }
  750. return freeDatas;
  751. };
  752. /**
  753. * 加载需展开的数据
  754. * @param {Array} datas
  755. * @returns {Array}
  756. * @private
  757. */
  758. _loadExpandData(datas) {
  759. datas = datas instanceof Array ? datas : [datas];
  760. const loadedData = [], existData = [], expandData = [], resortData = [];
  761. for (const data of datas) {
  762. let node = this.getItems(data[this.setting.id]);
  763. if (node) {
  764. existData.push(node);
  765. } else {
  766. const keyName = itemsPre + data[this.setting.id];
  767. const node = JSON.parse(JSON.stringify(data));
  768. this.items[keyName] = node;
  769. this.datas.push(node);
  770. node.expanded = false;
  771. node.visible = true;
  772. loadedData.push(node);
  773. if (resortData.indexOf(node) === -1) {
  774. resortData.push(node);
  775. }
  776. const parent = this.getItems(node[this.setting.pid]);
  777. if (parent && resortData.indexOf(parent) === -1) {
  778. resortData.push(parent);
  779. }
  780. }
  781. }
  782. for (const node of resortData) {
  783. node.children = this.getChildren(node);
  784. }
  785. this.sortTreeNode(true);
  786. for (const node of loadedData) {
  787. if (!node.expanded) {
  788. this.setExpanded(node, true);
  789. }
  790. }
  791. for (const node of existData) {
  792. const parent = this.getItems(node[this.setting.pid]);
  793. if (expandData.indexOf(parent) === -1) {
  794. expandData.push(parent);
  795. if (!parent.expanded) {
  796. this.setExpanded(parent, true);
  797. }
  798. }
  799. if (!node.expanded) {
  800. this.setExpanded(node, true);
  801. }
  802. }
  803. return [loadedData, expandData];
  804. };
  805. /**
  806. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  807. *
  808. * @param data - 更新的数据 {update, create, delete}
  809. * @returns {{}}
  810. */
  811. loadPostData(data) {
  812. const result = {};
  813. if (data.delete) {
  814. result.delete = this._freeData(data.delete);
  815. }
  816. if (data.create) {
  817. result.create = this._loadData(data.create);
  818. }
  819. if (data.update) {
  820. result.update = this._updateData(data.update);
  821. }
  822. return result;
  823. }
  824. /**
  825. * 加载子节点
  826. * @param {Object} node
  827. * @param {function} callback
  828. */
  829. loadChildren(node, callback) {
  830. if (this.setting.url !== '') {
  831. const self = this;
  832. postData(this.setting.url, { postType: 'load-child', id: this.getNodeKeyData(node) }, function (data) {
  833. self._loadData(data);
  834. callback();
  835. });
  836. }
  837. };
  838. _getDefaultNodeData(node) {
  839. const result = {};
  840. for (const prop in node) {
  841. if (['children', 'visible', 'expanded'].indexOf(prop) >= 0) continue;
  842. result[prop] = node[prop];
  843. }
  844. return result;
  845. }
  846. getDefaultData(node) {
  847. if (node instanceof Array) {
  848. const arr = [];
  849. for (const n of node) {
  850. arr.push(this._getDefaultNodeData(n));
  851. }
  852. return arr;
  853. } else {
  854. this._getDefaultNodeData(node);
  855. }
  856. }
  857. }
  858. class MeasureTree extends BaseTree {
  859. addData(datas) {
  860. const loadedData = [];
  861. for (const data of datas) {
  862. let node = this.getItems(data[this.setting.id]);
  863. if (node) {
  864. for (const prop in data) {
  865. if (data[prop] !== undefined) {
  866. node[prop] = data[prop];
  867. }
  868. }
  869. loadedData.push(node);
  870. } else {
  871. const keyName = itemsPre + data[this.setting.id];
  872. const node = JSON.parse(JSON.stringify(data));
  873. this.items[keyName] = node;
  874. this.datas.push(node);
  875. node.expanded = false;
  876. node.visible = true;
  877. loadedData.push(node);
  878. }
  879. }
  880. this.sortTreeNode();
  881. for (const node of loadedData) {
  882. const children = node.children;
  883. if (!node.expanded && children.length > 0) {
  884. node.expanded = true;
  885. this._refreshChildrenVisible(node);
  886. }
  887. }
  888. return loadedData;
  889. }
  890. removeData(datas) {
  891. datas.sort(function (a, b) {
  892. return b.level - a.level;
  893. });
  894. const removeArrayData = function (array, data) {
  895. const index = array.indexOf(data);
  896. array.splice(index, 1);
  897. };
  898. for (const data of datas) {
  899. const node = this.getItems(data[this.setting.id]);
  900. if (node && this.getChildren(node).length === 0) {
  901. delete this.items[itemsPre + node[this.setting.id]];
  902. if (node[this.setting.pid] !== this.setting.rootId) {
  903. const parent = this.items[itemsPre + node[this.setting.pid]];
  904. removeArrayData(parent.children, node);
  905. }
  906. removeArrayData(this.datas, node);
  907. removeArrayData(this.nodes, node);
  908. }
  909. }
  910. };
  911. loadLeafData(data) {
  912. const datas = data instanceof Array ? data : [data];
  913. for (const d of datas) {
  914. let node = this.getItems(d[this.setting.id]);
  915. if (node && node.is_leaf) {
  916. for (const prop in d) {
  917. if (data[prop] !== undefined) {
  918. node[prop] = d[prop];
  919. }
  920. }
  921. }
  922. }
  923. };
  924. }
  925. class FxTree extends BaseTree {
  926. /**
  927. * 检查节点是否是最底层项目节
  928. * @param node
  929. * @returns {boolean}
  930. */
  931. isLeafXmj(node) {
  932. if (node.b_code && node.b_code !== '') {
  933. return false;
  934. }
  935. for (const child of node.children) {
  936. if (!child.b_code || child.b_code === '') {
  937. return false;
  938. }
  939. }
  940. return true;
  941. }
  942. /**
  943. * 查询最底层项目节(本身或父项)
  944. * @param {Object} node - 查询节点
  945. * @returns {Object}
  946. */
  947. getLeafXmjParent(node) {
  948. let parent = node;
  949. while (parent) {
  950. if (this.isLeafXmj(parent)) {
  951. return parent;
  952. } else {
  953. parent = this.getParent(parent);
  954. }
  955. }
  956. return null;
  957. }
  958. /**
  959. * 展开至最底层项目节
  960. */
  961. expandToLeafXmj() {
  962. const self = this;
  963. this.expandByCustom(function (node) {
  964. if (node.b_code && node.b_code !== '') {
  965. return false;
  966. } else {
  967. return !self.isLeafXmj(node);
  968. }
  969. })
  970. }
  971. /**
  972. * 展开至计算项
  973. */
  974. expandByCalcFields() {
  975. const self = this;
  976. this.expandByCustom(function (node) {
  977. for (const field of self.setting.calcFields) {
  978. if (node[field]) {
  979. return true;
  980. }
  981. }
  982. return false;
  983. })
  984. }
  985. checkCodeType() {
  986. for (const node of this.nodes) {
  987. if (!node.code) continue;
  988. if (node.code.indexOf('-') >= 0) {
  989. return '07';
  990. } else if (node.code.length > 5) {
  991. const num = _.toNumber(node.code);
  992. if (num && num > 10000) return '18';
  993. }
  994. }
  995. return '18';
  996. }
  997. getCodeSplit() {
  998. return this.checkCodeType() === '07' ? '-' : '0';
  999. }
  1000. }
  1001. class LedgerTree extends FxTree {
  1002. /**
  1003. *
  1004. * @param parent
  1005. * @param node
  1006. * @private
  1007. */
  1008. _getNodesParents(parents, nodes) {
  1009. for (const node of nodes) {
  1010. const parent = this.getParent(node);
  1011. if (parent) {
  1012. const paths = this.getFullPathNodes(parent.full_path);
  1013. for (const p of paths) {
  1014. if (parents.indexOf(p) === -1) {
  1015. parents.push(p);
  1016. }
  1017. }
  1018. }
  1019. if (this.getItems(node.ledger_id) && node.children.length > 0) {
  1020. parents.push(node);
  1021. }
  1022. }
  1023. }
  1024. _getReCalcNodes(reCalcNodes, nodes) {
  1025. for (const node of nodes) {
  1026. const parent = this.getParent(node);
  1027. if (parent) {
  1028. const paths = this.getFullPathNodes(parent.full_path);
  1029. for (const p of paths) {
  1030. if (reCalcNodes.indexOf(p) === -1) {
  1031. reCalcNodes.push(p);
  1032. }
  1033. }
  1034. }
  1035. // 最底层项目节,也需要计算
  1036. //if (this.getItems(node.ledger_id) && node.children.length > 0) {
  1037. reCalcNodes.push(node);
  1038. //}
  1039. }
  1040. }
  1041. /**
  1042. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  1043. *
  1044. * @param data - 更新的数据 {update, create, delete}
  1045. * @returns {{}}
  1046. */
  1047. loadPostData(data) {
  1048. const result = {}, reCalcNodes = [];
  1049. if (!data) return result;
  1050. if (data.delete) {
  1051. result.delete = this._freeData(data.delete);
  1052. this._getReCalcNodes(reCalcNodes, result.delete);
  1053. }
  1054. if (data.create) {
  1055. result.create = this._loadData(data.create);
  1056. this._getReCalcNodes(reCalcNodes, result.create);
  1057. }
  1058. if (data.update) {
  1059. result.update = this._updateData(data.update);
  1060. this._getReCalcNodes(reCalcNodes, result.update);
  1061. }
  1062. reCalcNodes.sort((a, b) => {
  1063. return b.level - a.level;
  1064. });
  1065. for (const node of reCalcNodes) {
  1066. treeCalc.calculateNode(this, node, this.setting.calcFields, this.setting.calcFun);
  1067. }
  1068. result.update = result.update ? result.update.concat(reCalcNodes) : reCalcNodes;
  1069. return result;
  1070. }
  1071. }
  1072. class ReviseTree extends LedgerTree {
  1073. checkNodeUsed(node, pos) {
  1074. if (node.children && node.children.length > 0) {
  1075. for (const child of node.children) {
  1076. const used = this.checkNodeUsed(child, pos);
  1077. if (used) return used;
  1078. }
  1079. } else {
  1080. if (node.used) return node.used;
  1081. const posRange = pos.getLedgerPos(node.id);
  1082. if (posRange && posRange.length > 0) {
  1083. for (const p of posRange) {
  1084. if (p.used) return p.used;
  1085. }
  1086. }
  1087. }
  1088. return false;
  1089. }
  1090. }
  1091. class StageTree extends FxTree {
  1092. /**
  1093. * 构造函数
  1094. */
  1095. constructor(setting) {
  1096. super(setting);
  1097. // stage关联索引
  1098. this.stageItems = {};
  1099. }
  1100. /**
  1101. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1102. * @param datas
  1103. */
  1104. loadDatas(datas) {
  1105. super.loadDatas(datas);
  1106. // 清空旧数据
  1107. this.stageItems = {};
  1108. // 加载全部数据
  1109. for (const data of this.datas) {
  1110. const keyName = itemsPre + data[this.setting.stageId];
  1111. this.stageItems[keyName] = data;
  1112. }
  1113. }
  1114. // 解锁相关
  1115. _loadPwdCache() {
  1116. const cache = getLocalCache(this.pwdCacheKey);
  1117. if (!cache) return;
  1118. const cacheArr = cache.split('|');
  1119. for (const ca of cacheArr) {
  1120. if (!ca) continue;
  1121. const [ledger_id, pwd] = ca.split(':');
  1122. if (!ledger_id || !pwd) continue;
  1123. const p = this.pwd.find(x => {return x.ledger_id == ledger_id});
  1124. if (p) p.check = p.pwd === pwd;
  1125. }
  1126. }
  1127. loadPwd(data, cacheKey, confirmList) {
  1128. this.loadingPwd = true;
  1129. try {
  1130. this.pwdCacheKey = cacheKey;
  1131. this.confirmList = confirmList;
  1132. this.pwd = data;
  1133. this._loadPwdCache();
  1134. this._loadOnlineConfirm();
  1135. for (const p of this.pwd) {
  1136. p.node = this.getItems(p.ledger_id);
  1137. this.lockNode(p, !p.check);
  1138. }
  1139. } catch(err) {}
  1140. this.loadingPwd = false;
  1141. }
  1142. // 确认相关
  1143. _loadOnlineConfirm() {
  1144. const cacheArr = this.confirmList;
  1145. for (const ca of cacheArr) {
  1146. if (!ca) continue;
  1147. if (!ca.ledger_id) continue;
  1148. const p = this.pwd.find(x => {return x.ledger_id == ca.ledger_id});
  1149. if (p) {
  1150. p.confirm = true;
  1151. p.confirm_time = ca.create_time;
  1152. }
  1153. }
  1154. }
  1155. getStageItems(id) {
  1156. return this.stageItems[itemsPre + id];
  1157. }
  1158. loadStageData(datas, fieldPre, fields) {
  1159. datas = datas instanceof Array ? datas : [datas];
  1160. const loadedData = [];
  1161. for (const data of datas) {
  1162. let node = this.getStageItems(data.lid);
  1163. if (node) {
  1164. for (const prop of fields) {
  1165. if (data[prop] !== undefined) {
  1166. node[fieldPre + prop] = data[prop];
  1167. }
  1168. }
  1169. loadedData.push(node);
  1170. }
  1171. }
  1172. }
  1173. loadPreStageData(datas) {
  1174. this.loadStageData(datas, 'pre_', this.setting.updateFields);
  1175. }
  1176. loadCurStageData(datas) {
  1177. this.loadStageData(datas, '', this.setting.updateFields);
  1178. }
  1179. /**
  1180. * 加载数据(动态),只加载不同部分
  1181. * @param {Array} datas
  1182. * @return {Array} 加载到树的数据
  1183. * @privateA
  1184. */
  1185. _updateData(datas) {
  1186. datas = datas instanceof Array ? datas : [datas];
  1187. let loadedData = [];
  1188. for (const data of datas) {
  1189. let node = this.getItems(data[this.setting.id]);
  1190. if (node) {
  1191. for (const prop in data) {
  1192. if (prop === this.setting.pid && data[prop] !== node[prop]) {
  1193. }
  1194. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1195. if (prop === this.setting.pid) {
  1196. loadedData.push(this.getItems(node[this.setting.pid]));
  1197. loadedData.push(this.getItems(data[this.setting.pid]));
  1198. }
  1199. node[prop] = data[prop];
  1200. }
  1201. }
  1202. loadedData.push(node);
  1203. }
  1204. }
  1205. loadedData = _.uniq(loadedData);
  1206. for (const node of loadedData) {
  1207. node.children = this.getChildren(node);
  1208. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  1209. }
  1210. this.sortTreeNode(true);
  1211. return loadedData;
  1212. };
  1213. /**
  1214. * 加载数据(动态),只加载不同部分
  1215. * @param {Array} datas
  1216. * @return {Array} 加载到树的数据
  1217. * @privateA
  1218. */
  1219. _updateStageData(datas) {
  1220. datas = datas instanceof Array ? datas : [datas];
  1221. const loadedData = [];
  1222. for (const data of datas) {
  1223. let node = this.getStageItems(data.lid);
  1224. if (node) {
  1225. for (const prop of this.setting.updateFields) {
  1226. if (data[prop] !== undefined) {
  1227. node[prop] = data[prop];
  1228. }
  1229. }
  1230. loadedData.push(node);
  1231. }
  1232. }
  1233. return loadedData;
  1234. };
  1235. /**
  1236. *
  1237. * @param parent
  1238. * @param node
  1239. * @private
  1240. */
  1241. _getNodesParents(parents, nodes) {
  1242. for (const node of nodes) {
  1243. const parent = this.getParent(node);
  1244. if (parent) {
  1245. const paths = this.getFullPathNodes(parent.full_path);
  1246. for (const p of paths) {
  1247. if (parents.indexOf(p) === -1) {
  1248. parents.push(p);
  1249. }
  1250. }
  1251. }
  1252. if (node.children && node.children.length > 0) {
  1253. parents.push(node);
  1254. }
  1255. }
  1256. }
  1257. _updateDgnData(datas) {
  1258. datas = datas instanceof Array ? datas : [datas];
  1259. let loadedData = [];
  1260. for (const data of datas) {
  1261. let node = this.getStageItems(data.id);
  1262. if (node) {
  1263. for (const prop in data) {
  1264. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  1265. node[prop] = data[prop];
  1266. }
  1267. }
  1268. loadedData.push(node);
  1269. }
  1270. }
  1271. return loadedData;
  1272. }
  1273. /**
  1274. * 提交数据至后端,返回的前端树结构应刷新的部分
  1275. * StageTree仅有更新CurStage部分,不需要增删
  1276. *
  1277. * @param data - 需要更新的数据
  1278. * @returns {Array} - 界面需要刷新的数据
  1279. */
  1280. loadPostStageData(data) {
  1281. let result, parents = [];
  1282. if (data.bills) {
  1283. result = this._updateData(data.bills);
  1284. this._getNodesParents(parents, result);
  1285. }
  1286. if (data.curStageData) {
  1287. result = this._updateStageData(data.curStageData);
  1288. this._getNodesParents(parents, result);
  1289. }
  1290. if (data.dgn) {
  1291. const dgnResult = this._updateDgnData(data.dgn);
  1292. result = result ? result.concat(dgnResult) : dgnResult;
  1293. }
  1294. result = result ? result.concat(parents) : parents;
  1295. result.sort((a, b) => {
  1296. return b.level - a.level;
  1297. });
  1298. for (const node of result) {
  1299. treeCalc.calculateNode(this, node);
  1300. }
  1301. return result;
  1302. }
  1303. _savePwdCache() {
  1304. const cache = [];
  1305. for (const p of this.pwd) {
  1306. if (p.check && p.pwd) cache.push(p.ledger_id + ':' + p.pwd);
  1307. }
  1308. setLocalCache(this.pwdCacheKey, cache.join('|'));
  1309. }
  1310. _getPwdPosterity(node) {
  1311. const children = [];
  1312. for (const c of node.children) {
  1313. const cPwd = this.pwd.find(x => {return x.node && x.node.id === c.id});
  1314. if (!cPwd) children.push(c);
  1315. }
  1316. let posterity = [...children];
  1317. for (const c of children) {
  1318. posterity = posterity.concat(this._getPwdPosterity(c));
  1319. }
  1320. return posterity;
  1321. }
  1322. lockNode(p, isLock) {
  1323. const refresh = [];
  1324. p.check = !isLock;
  1325. p.node.lock = isLock;
  1326. refresh.push(this.getNodeIndex(p.node));
  1327. const posterity = this._getPwdPosterity(p.node);
  1328. for (const pn of posterity) {
  1329. pn.lock = isLock;
  1330. refresh.push(this.getNodeIndex(pn));
  1331. }
  1332. if (!this.loadingPwd) this._savePwdCache();
  1333. return refresh;
  1334. }
  1335. }
  1336. class MasterTree extends FxTree {
  1337. /**
  1338. * 构造函数
  1339. */
  1340. constructor(setting) {
  1341. super(setting);
  1342. // 关联索引
  1343. this.masterItems = {};
  1344. }
  1345. /**
  1346. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  1347. * @param datas
  1348. */
  1349. loadDatas(datas) {
  1350. super.loadDatas(datas);
  1351. // 清空旧数据
  1352. this.masterItems = {};
  1353. // minor数据缓存
  1354. this.minorData = {};
  1355. // 加载全部数据
  1356. for (const data of this.datas) {
  1357. const keyName = itemsPre + data[this.setting.masterId];
  1358. this.masterItems[keyName] = data;
  1359. }
  1360. }
  1361. /**
  1362. * 根据关联id,查找节点
  1363. * @param id
  1364. * @returns {*}
  1365. */
  1366. getMasterItems(id) {
  1367. return this.masterItems[itemsPre + id];
  1368. }
  1369. /**
  1370. * 加载关联数据
  1371. *
  1372. * @param {Array|Object}datas - 需要关联的数据
  1373. * @param {String} fieldPre - 关联字段前缀(关联结果)
  1374. * @param {Array} fields - 关联字段
  1375. * @returns {Array}
  1376. */
  1377. loadMinorData(datas, fieldSuf, fields, calcFields) {
  1378. for (const cf of calcFields) {
  1379. this.setting.calcFields.push(cf + fieldSuf);
  1380. }
  1381. if (!datas) return;
  1382. datas = datas instanceof Array ? datas : [datas];
  1383. this.minorData[fieldSuf] = datas;
  1384. const loadedData = [];
  1385. for (const data of datas) {
  1386. let node = this.getMasterItems(data[this.setting.minorId]);
  1387. if (node) {
  1388. for (const prop of fields) {
  1389. if (data[prop] !== undefined) {
  1390. node[prop + fieldSuf] = data[prop];
  1391. }
  1392. }
  1393. loadedData.push(node);
  1394. }
  1395. }
  1396. return loadedData;
  1397. }
  1398. }
  1399. class FilterTree extends BaseTree {
  1400. clearDatas() {
  1401. this.items = {};
  1402. this.nodes = [];
  1403. this.datas = [];
  1404. this.children = [];
  1405. }
  1406. addData(data, fields) {
  1407. const item = {};
  1408. for (const prop in data) {
  1409. if (fields.indexOf(prop) >= 0) {
  1410. item[prop] = data[prop];
  1411. }
  1412. }
  1413. const keyName = itemsPre + item[this.setting.id];
  1414. if (!this.items[keyName]) {
  1415. item.children = [];
  1416. item.is_leaf = true;
  1417. item.expanded = true;
  1418. item.visible = true;
  1419. this.items[keyName] = item;
  1420. this.datas.push(item);
  1421. if (item[setting.pid] === setting.rootId) {
  1422. this.children.push(item);
  1423. } else {
  1424. const parent = this.getParent(item);
  1425. if (parent) {
  1426. parent.is_leaf = false;
  1427. parent.children.push(item);
  1428. }
  1429. }
  1430. } else {
  1431. return this.items[keyName];
  1432. }
  1433. return item;
  1434. }
  1435. }
  1436. class GatherTree extends BaseTree {
  1437. clearDatas() {
  1438. this.items = {};
  1439. this.nodes = [];
  1440. this.datas = [];
  1441. this.children = [];
  1442. }
  1443. get newId() {
  1444. if (!this._maxId) {
  1445. this._maxId = 0;
  1446. }
  1447. this._maxId++;
  1448. return this._maxId;
  1449. }
  1450. addNode(data, parent) {
  1451. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1452. let item = _.find(this.items, data);
  1453. if (item) return item;
  1454. item = data;
  1455. item[this.setting.id] = this.newId;
  1456. const keyName = itemsPre + item[this.setting.id];
  1457. item.children = [];
  1458. item.is_leaf = true;
  1459. item.expanded = true;
  1460. item.visible = true;
  1461. this.items[keyName] = item;
  1462. this.datas.push(item);
  1463. if (parent) {
  1464. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  1465. item[this.setting.level] = parent[this.setting.level] + 1;
  1466. item[this.setting.order] = parent.children.length + 1;
  1467. parent.is_leaf = false;
  1468. parent.children.push(item);
  1469. } else {
  1470. item[this.setting.fullPath] = '' + item[this.setting.id];
  1471. item[this.setting.level] = 1;
  1472. item[this.setting.order] = this.children.length + 1;
  1473. this.children.push(item);
  1474. }
  1475. return item;
  1476. }
  1477. sortTreeNodeCustom(field, fun, isResort) {
  1478. const self = this;
  1479. const sortNodes = function (nodes) {
  1480. nodes.sort(function (a, b) {
  1481. return fun(a[field], b[field]);
  1482. });
  1483. for (const [i, node] of nodes.entries()) {
  1484. node[self.setting.order] = i + 1;
  1485. }
  1486. };
  1487. const addSortNodes = function (nodes) {
  1488. if (!nodes) { return }
  1489. for (let i = 0; i < nodes.length; i++) {
  1490. self.nodes.push(nodes[i]);
  1491. nodes[i].index = self.nodes.length - 1;
  1492. if (!isResort) {
  1493. nodes[i].children = self.getChildren(nodes[i]);
  1494. }
  1495. sortNodes(nodes[i].children);
  1496. addSortNodes(nodes[i].children);
  1497. }
  1498. };
  1499. this.nodes = [];
  1500. if (!isResort) {
  1501. this.children = this.getChildren();
  1502. }
  1503. sortNodes(this.children);
  1504. addSortNodes(this.children);
  1505. }
  1506. }
  1507. class CompareTree extends FxTree {
  1508. constructor(setting) {
  1509. super(setting);
  1510. this._newId = 1;
  1511. }
  1512. get newId() {
  1513. return this._newId++;
  1514. }
  1515. findCompareNode(node, parent) {
  1516. if (this.setting.findNode) {
  1517. return this.setting.findNode(this, node, parent);
  1518. } else {
  1519. const siblings = parent ? parent.children : this.children;
  1520. return siblings.find(function (x) {
  1521. return node.b_code
  1522. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1523. : x.code === node.code && x.name === node.name;
  1524. });
  1525. }
  1526. }
  1527. loadCompareNode(source, node, parent, loadFun) {
  1528. let cur = this.findCompareNode(node, parent);
  1529. if (!cur) {
  1530. const siblings = parent ? parent.children : this.children;
  1531. const id = this.newId;
  1532. cur = {
  1533. children: [], pos: [],
  1534. code: node.code, b_code: node.b_code, name: node.name,
  1535. unit: node.unit, unit_price: node.unit_price,
  1536. };
  1537. cur[this.setting.id] = id;
  1538. cur[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  1539. cur[this.setting.full_path] = parent ? parent[this.setting.full_path] + '-' + id : '' + id;
  1540. cur[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  1541. cur[this.setting.order] = siblings.length + 1;
  1542. siblings.push(cur);
  1543. this.datas.push(cur);
  1544. }
  1545. loadFun(cur, node, source);
  1546. for (const c of node.children) {
  1547. this.loadCompareNode(source, c, cur, loadFun);
  1548. }
  1549. }
  1550. generateSortNodes() {
  1551. const self = this;
  1552. const addSortNode = function (node) {
  1553. self.nodes.push(node);
  1554. for (const c of node.children) {
  1555. addSortNode(c);
  1556. }
  1557. }
  1558. this.nodes = [];
  1559. for (const n of this.children) {
  1560. addSortNode(n);
  1561. }
  1562. }
  1563. loadCompareTree(data, loadFun) {
  1564. for (const c of data.billsTree.children) {
  1565. this.loadCompareNode(data, c, null, loadFun);
  1566. }
  1567. }
  1568. calculateDiffer() {
  1569. if (this.setting.calcDiffer) {
  1570. for (const d of this.datas) {
  1571. this.setting.calcDiffer(d);
  1572. }
  1573. }
  1574. }
  1575. loadCompareData(data1, data2) {
  1576. this.loadCompareTree(data1, this.setting.loadInfo1);
  1577. this.loadCompareTree(data2, this.setting.loadInfo2);
  1578. for (const d of this.datas) {
  1579. d.is_leaf = d.children.length === 0;
  1580. d.expanded = true;
  1581. d.visible = true;
  1582. this.items[itemsPre + d[this.setting.id]] = d;
  1583. }
  1584. this.generateSortNodes();
  1585. this.calculateDiffer();
  1586. if (this.setting.afterLoad) this.setting.afterLoad(this);
  1587. }
  1588. }
  1589. class TreeGatherTree extends FxTree {
  1590. constructor(setting) {
  1591. super(setting);
  1592. this._newId = 1;
  1593. }
  1594. get newId() {
  1595. return this._newId++;
  1596. }
  1597. loadGatherNode(node, parent, index, loadFun) {
  1598. const siblings = parent ? parent.children : this.children;
  1599. let cur = siblings.find(function (x) {
  1600. return node.b_code
  1601. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  1602. : x.code === node.code && x.name === node.name;
  1603. });
  1604. if (!cur) {
  1605. const id = this.newId;
  1606. cur = {
  1607. id: id,
  1608. pid: parent ? parent.id : this.setting.rootId,
  1609. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1610. level: parent ? parent.level + 1 : 1,
  1611. order: siblings.length + 1,
  1612. children: [],
  1613. code: node.code, b_code: node.b_code, name: node.name,
  1614. unit: node.unit, unit_price: node.unit_price,
  1615. };
  1616. siblings.push(cur);
  1617. this.datas.push(cur);
  1618. }
  1619. loadFun(cur, node, index);
  1620. for (const c of node.children) {
  1621. this.loadGatherNode(c, cur, index, loadFun);
  1622. }
  1623. }
  1624. generateSortNodes() {
  1625. const self = this;
  1626. const addSortNode = function (node) {
  1627. self.nodes.push(node);
  1628. for (const c of node.children) {
  1629. addSortNode(c);
  1630. }
  1631. };
  1632. this.nodes = [];
  1633. for (const n of this.children) {
  1634. addSortNode(n);
  1635. }
  1636. }
  1637. loadGatherTree(data, index, loadFun) {
  1638. for (const c of data.billsTree.children) {
  1639. this.loadGatherNode(c, null, index, loadFun);
  1640. }
  1641. // todo load Pos Data;
  1642. }
  1643. calculateSum() {
  1644. if (this.setting.calcSum) {
  1645. for (const d of this.datas) {
  1646. this.setting.calcSum(d, this.count);
  1647. }
  1648. }
  1649. }
  1650. loadGatherData(datas) {
  1651. this.count = datas.length;
  1652. for (const [i, data] of datas.entries()) {
  1653. this.loadGatherTree(data, i+1, this.setting.loadInfo);
  1654. }
  1655. for (const d of this.datas) {
  1656. d.is_leaf = d.children.length === 0;
  1657. d.expanded = true;
  1658. d.visible = true;
  1659. this.items[itemsPre + d[this.setting.id]] = d;
  1660. }
  1661. this.generateSortNodes();
  1662. this.calculateSum();
  1663. }
  1664. }
  1665. class FinalTree extends BaseTree {
  1666. constructor(setting) {
  1667. super(setting);
  1668. this._newId = 1;
  1669. }
  1670. get newId() {
  1671. return this._newId++;
  1672. }
  1673. loadNode(node, parent, loadFun) {
  1674. if (node.b_code) return;
  1675. const siblings = parent ? parent.children : this.children;
  1676. let cur = siblings.find(function (x) {
  1677. return x.code === node.code && x.name === node.name;
  1678. });
  1679. if (!cur) {
  1680. const id = this.newId;
  1681. cur = {
  1682. id: id,
  1683. pid: parent ? parent.id : this.setting.rootId,
  1684. full_path: parent ? parent.full_path + '-' + id : '' + id,
  1685. level: parent ? parent.level + 1 : 1,
  1686. order: siblings.length + 1,
  1687. children: [],
  1688. code: node.code, name: node.name, unit: node.unit,
  1689. };
  1690. siblings.push(cur);
  1691. this.datas.push(cur);
  1692. }
  1693. loadFun(cur, node);
  1694. for (const c of node.children) {
  1695. this.loadNode(c, cur, loadFun);
  1696. }
  1697. }
  1698. loadTree(tree, loadFun) {
  1699. for (const node of tree.children) {
  1700. this.loadNode(node, null, loadFun);
  1701. }
  1702. }
  1703. generateSortNodes() {
  1704. const self = this;
  1705. const addSortNode = function (node) {
  1706. self.nodes.push(node);
  1707. for (const c of node.children) {
  1708. addSortNode(c);
  1709. }
  1710. };
  1711. this.nodes = [];
  1712. for (const n of this.children) {
  1713. addSortNode(n);
  1714. }
  1715. }
  1716. afterLoad(fun) {
  1717. for (const d of this.datas) {
  1718. fun && fun(d);
  1719. d.is_leaf = d.children.length === 0;
  1720. d.expanded = true;
  1721. d.visible = true;
  1722. this.items[itemsPre + d[this.setting.id]] = d;
  1723. }
  1724. this.generateSortNodes();
  1725. }
  1726. clearFinal() {
  1727. this.datas = this.datas.filter(x => {
  1728. return !!x.base;
  1729. });
  1730. this.datas.forEach(x => {
  1731. delete x.total_price;
  1732. delete x.dgn_qty1;
  1733. delete x.dgn_qty2;
  1734. delete x.dgn_qty;
  1735. delete x.dgn_price;
  1736. delete x.final_dgn_qty1;
  1737. delete x.final_dgn_qty2;
  1738. delete x.final_dgn_qty;
  1739. delete x.final_dgn_price;
  1740. delete x.final_tp;
  1741. delete x.grow_dgn_qty1;
  1742. delete x.grow_dgn_qty2;
  1743. delete x.grow_dgn_qty;
  1744. delete x.grow_tp;
  1745. });
  1746. this.loadDatas(this.datas);
  1747. }
  1748. resortChildrenByCustom(fun) {
  1749. for (const n of this.nodes) {
  1750. if (n.children && n.children.length > 1) {
  1751. n.children.sort(fun);
  1752. n.children.forEach((x, y) => { x.order = y + 1; });
  1753. }
  1754. }
  1755. this.generateSortNodes();
  1756. }
  1757. }
  1758. if (type === 'base') {
  1759. return new BaseTree(setting);
  1760. } else if (type === 'fx') {
  1761. return new FxTree(setting);
  1762. } else if (type === 'stage') {
  1763. return new StageTree(setting);
  1764. } else if (type === 'ledger') {
  1765. return new LedgerTree(setting);
  1766. } else if (type === 'revise') {
  1767. return new ReviseTree(setting);
  1768. } else if (type === 'measure') {
  1769. return new MeasureTree(setting);
  1770. } else if (type === 'master') {
  1771. return new MasterTree(setting);
  1772. } else if (type === 'filter') {
  1773. return new FilterTree(setting);
  1774. } else if (type === 'gather') {
  1775. return new GatherTree(setting);
  1776. } else if (type === 'compare') {
  1777. return new CompareTree(setting);
  1778. } else if (type === 'tree-gather') {
  1779. return new TreeGatherTree(setting);
  1780. } else if (type === 'final') {
  1781. return new FinalTree(setting);
  1782. }
  1783. };
  1784. const treeCalc = {
  1785. mapTreeNode: function (tree) {
  1786. let map = {}, maxLevel = 0;
  1787. for (const node of tree.nodes) {
  1788. let levelArr = map[node.level];
  1789. if (!levelArr) {
  1790. levelArr = [];
  1791. map[node.level] = levelArr;
  1792. }
  1793. if (node.level > maxLevel) {
  1794. maxLevel = node.level;
  1795. }
  1796. levelArr.push(node);
  1797. }
  1798. return [maxLevel, map];
  1799. },
  1800. getMaxLevel: function (tree) {
  1801. return Math.max.apply(Math, tree.datas.map(function (o) { return o.level }));
  1802. },
  1803. calculateNode: function (tree, node) {
  1804. if (node.children && node.children.length > 0) {
  1805. const gather = node.children.reduce(function (rst, x) {
  1806. const result = {};
  1807. for (const cf of tree.setting.calcFields) {
  1808. result[cf] = ZhCalc.add(rst[cf], x[cf]);
  1809. }
  1810. return result;
  1811. });
  1812. // 汇总子项
  1813. for (const cf of tree.setting.calcFields) {
  1814. if (gather[cf]) {
  1815. node[cf] = gather[cf];
  1816. } else {
  1817. node[cf] = null;
  1818. }
  1819. }
  1820. }
  1821. // 自身运算
  1822. if (tree.setting.calcFun) {
  1823. tree.setting.calcFun(node);
  1824. }
  1825. },
  1826. calculateLevelNode: function (tree, level) {
  1827. const nodes = tree.datas.filter((n) => { return n.level === level });
  1828. for (const node of nodes) {
  1829. this.calculateNode(tree, node);
  1830. }
  1831. },
  1832. calculateAll: function (tree) {
  1833. const [maxLevel, levelMap] = this.mapTreeNode(tree);
  1834. for (let i = maxLevel; i >= 0; i--) {
  1835. const levelNodes = levelMap[i];
  1836. if (levelNodes && levelNodes.length > 0) {
  1837. for (const node of levelNodes) {
  1838. this.calculateNode(tree, node);
  1839. }
  1840. }
  1841. }
  1842. },
  1843. calculateParent: function (tree, node) {
  1844. const nodes = tree.getFullPathNodes(node.full_path);
  1845. nodes.sort((a, b) => {
  1846. return b.level - a.level;
  1847. });
  1848. for (const n of nodes) {
  1849. this.calculateNode(tree, n);
  1850. }
  1851. return nodes;
  1852. }
  1853. };