billsUtil.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2019/11/1
  7. * @version
  8. */
  9. ((factory) => {
  10. if (typeof module !== 'undefined') {
  11. module.exports = factory();
  12. } else {
  13. window.BILLS_UTIL = factory();
  14. }
  15. })(() => {
  16. // 清单模板各清单重设ID时,重新转换清单基数的ID引用
  17. function parseCalcBase(calcBase, uuidMapping) {
  18. const orgIDRefs = [...new Set(calcBase.match(/@\d+/g))];
  19. orgIDRefs.forEach(orgRef => {
  20. const orgID = orgRef.match(/\d+/)[0];
  21. const newID = uuidMapping[orgID] || null;
  22. // ID匹配不上则不转换这个引用
  23. if (!newID) {
  24. return;
  25. }
  26. const replaceStr = `@${newID}`;
  27. calcBase = calcBase.replace(new RegExp(`${orgRef}\\b`, 'g'), replaceStr);
  28. });
  29. return calcBase;
  30. }
  31. /*
  32. * @param {Array}billsList (完整的清单树结构数据)
  33. * @param {Function}idFactory 生成ID的方法
  34. * @return {void}
  35. * */
  36. function resetTreeData(billsList, idFactory) {
  37. const idMapping = {};
  38. idMapping['-1'] = -1;
  39. // 建立新ID-旧ID映射
  40. billsList.forEach(bills => idMapping[bills.ID] = idFactory());
  41. const reg = /@\d+/;
  42. billsList.forEach(function (bills) {
  43. bills.ID = idMapping[bills.ID] ? idMapping[bills.ID] : -1;
  44. bills.ParentID = idMapping[bills.ParentID] ? idMapping[bills.ParentID] : -1;
  45. bills.NextSiblingID = idMapping[bills.NextSiblingID] ? idMapping[bills.NextSiblingID] : -1;
  46. const needToParseCalcBase = bills.calcBase && reg.test(bills.calcBase);
  47. if (needToParseCalcBase) {
  48. bills.calcBase = parseCalcBase(bills.calcBase, idMapping);
  49. }
  50. });
  51. }
  52. return {
  53. parseCalcBase,
  54. resetTreeData
  55. };
  56. });