path_tree.js 79 KB

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