common_facade.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. is_adjust_price:0,
  78. is_evaluate:0,
  79. is_eval_material:0
  80. };
  81. if (gljData.from == 'std' && ext && ext.priceField && (tmp.priceProperty && tmp.priceProperty[ext.priceField] != undefined && tmp.priceProperty[ext.priceField] != null)) {
  82. const basePrice = scMathUtil.roundTo(tmp.priceProperty[ext.priceField], -6);
  83. gljData.base_price = basePrice;
  84. gljData.market_price = basePrice;
  85. }
  86. return gljData;
  87. }
  88. async function setIDfromCounter(name, list, map, keyfield) {//map,keyfield
  89. let update = { $inc: { sequence_value: list.length } };
  90. let condition = { _id: name };
  91. let options = { new: true };
  92. // 先查找更新
  93. let counter = await counterModel.findOneAndUpdate(condition, update, options);
  94. let firstID = counter.sequence_value - (list.length - 1);
  95. for (let a of list) {
  96. a.id = firstID;
  97. firstID += 1
  98. if (map && keyfield) {
  99. let key = a[keyfield];
  100. map[key] ? map[key].push(a) : map[key] = [a]
  101. }
  102. }
  103. }
  104. /**
  105. * 根据项目id获取单价文件列表id
  106. *
  107. * @param {Number} projectId
  108. * @return {Promise}
  109. */
  110. async function getUnitPriceFileId(projectId) {
  111. let result = 0;
  112. let startTime = +new Date();
  113. let projectData = await projectsModel.find({ ID: projectId }, ['property.unitPriceFile']);
  114. if (projectData === null) {
  115. return result;
  116. }
  117. let endTime = +new Date();
  118. console.log("取单价文件列表id时间-----" + (endTime - startTime));
  119. projectData = projectData[0];
  120. result = projectData.property.unitPriceFile !== undefined ? projectData.property.unitPriceFile.id : 0;
  121. return result;
  122. };