path_tree.js 71 KB

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