path_tree.js 82 KB

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