common_facade.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // 开这个模块的最初原因:重构ESModule为CommonJS以升级node版本后,遇到module循环引用问题,将造成循环引用的方法抽离放到这个模块
  2. module.exports = {
  3. getNewProjectGLJFromMissMixratio,
  4. getProjectGLJNewData,
  5. setIDfromCounter,
  6. getUnitPriceFileId,
  7. };
  8. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  9. const mongoose = require('mongoose');
  10. const std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  11. const counterModel = mongoose.model('counter');
  12. const projectsModel = mongoose.model('projects');
  13. //根据缺少项目工料机的组成物信息,反向生成对应的项目工料机
  14. async function getNewProjectGLJFromMissMixratio(projectID, lessMix, projectGLJMap, newProjectGLJList, ext) {
  15. let lessIDList = [];
  16. let uniqMap = {};//去重
  17. let lessStdMix = [];//防止组成物中改了名称等,但是通过glj_id取出来的是还没改前的原始数据
  18. if (lessMix.length > 0) {
  19. for (let lm of lessMix) {
  20. let parentglj = projectGLJMap[lm.connect_key];
  21. if (!parentglj) throw `含有组成物工料机${lm.connect_key},没有找到,添加定额失败`;
  22. if ((parentglj.from == "std" || lm.from == "std") && lm.code != "80CCS") {//车船税特殊处理
  23. if (!uniqMap[lm.glj_id]) {
  24. lessIDList.push(lm.glj_id);
  25. uniqMap[lm.glj_id] = lm;
  26. }
  27. lessStdMix.push(lm);
  28. } else {//来自组成物的直接设置
  29. lm.from = 'cpt';
  30. lm.gljType = lm.type;
  31. let t_mg = getProjectGLJNewData(lm, projectID);
  32. newProjectGLJList.push(t_mg);
  33. projectGLJMap[getIndex(lm)] = t_mg;
  34. }
  35. }
  36. }
  37. if (lessIDList.length > 0) {
  38. let less_stds = await std_glj_lib_gljList_model.find({ 'ID': { '$in': lessIDList } }).lean();
  39. let less_stds_map = {};
  40. for (let les of less_stds) {
  41. less_stds_map[les.ID] = les;
  42. }
  43. for (let t_l_m of lessStdMix) {
  44. let t_nglj = less_stds_map[t_l_m.glj_id];
  45. t_nglj.from = 'std';
  46. //防止组成物中改了名称等,但是通过glj_id取出来的是还没改前的原始数据
  47. t_nglj.name = t_l_m.name;
  48. t_nglj.code = t_l_m.code;
  49. t_nglj.gljType = t_l_m.type;
  50. t_nglj.specs = t_l_m.specs;
  51. t_nglj.unit = t_l_m.unit;
  52. let t_np = getProjectGLJNewData(t_nglj, projectID, ext);
  53. newProjectGLJList.push(t_np);
  54. projectGLJMap[getIndex(t_l_m)] = t_np;
  55. }
  56. }
  57. return newProjectGLJList;
  58. }
  59. function getProjectGLJNewData(tmp, projectId, ext) {
  60. let gljData = {
  61. glj_id: tmp.ID,
  62. repositoryId: tmp.repositoryId,
  63. project_id: projectId,
  64. code: tmp.code,
  65. name: tmp.name,
  66. specs: tmp.specs ? tmp.specs : '',
  67. unit: tmp.unit === undefined ? '' : tmp.unit,
  68. type: tmp.gljType,
  69. adjCoe: tmp.adjCoe,
  70. original_code: tmp.code,
  71. materialType: tmp.materialType, //三材类别
  72. materialCoe: tmp.materialCoe,
  73. base_price: tmp.basePrice,
  74. market_price: tmp.basePrice,
  75. is_main_material: tmp.is_main_material ? tmp.is_main_material : 1,
  76. from: tmp.from ? tmp.from : "std"
  77. };
  78. if (gljData.from == 'std' && ext && ext.priceField && (tmp.priceProperty && tmp.priceProperty[ext.priceField] != undefined && tmp.priceProperty[ext.priceField] != null)) {
  79. const basePrice = scMathUtil.roundTo(tmp.priceProperty[ext.priceField], -6);
  80. gljData.base_price = basePrice;
  81. gljData.market_price = basePrice;
  82. }
  83. return gljData;
  84. }
  85. async function setIDfromCounter(name, list, map, keyfield) {//map,keyfield
  86. let update = { $inc: { sequence_value: list.length } };
  87. let condition = { _id: name };
  88. let options = { new: true };
  89. // 先查找更新
  90. let counter = await counterModel.findOneAndUpdate(condition, update, options);
  91. let firstID = counter.sequence_value - (list.length - 1);
  92. for (let a of list) {
  93. a.id = firstID;
  94. firstID += 1
  95. if (map && keyfield) {
  96. let key = a[keyfield];
  97. map[key] ? map[key].push(a) : map[key] = [a]
  98. }
  99. }
  100. }
  101. /**
  102. * 根据项目id获取单价文件列表id
  103. *
  104. * @param {Number} projectId
  105. * @return {Promise}
  106. */
  107. async function getUnitPriceFileId(projectId) {
  108. let result = 0;
  109. let startTime = +new Date();
  110. let projectData = await projectsModel.find({ ID: projectId }, ['property.unitPriceFile']);
  111. if (projectData === null) {
  112. return result;
  113. }
  114. let endTime = +new Date();
  115. console.log("取单价文件列表id时间-----" + (endTime - startTime));
  116. projectData = projectData[0];
  117. result = projectData.property.unitPriceFile !== undefined ? projectData.property.unitPriceFile.id : 0;
  118. return result;
  119. };