path_tree.js 79 KB

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