path_tree.js 80 KB

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