path_tree.js 70 KB

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