path_tree.js 71 KB

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