unit_price_facade.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. module.exports = {
  2. getUnitPriceData:getUnitPriceData,
  3. setIDfromCounter:setIDfromCounter,
  4. getNewProjectGLJFromMissMixratio:getNewProjectGLJFromMissMixratio
  5. };
  6. let mongoose = require("mongoose");
  7. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  8. let unitPriceModel = mongoose.model('unit_price');
  9. let counterModel = mongoose.model('counter');
  10. async function setIDfromCounter(name,list,map,keyfield){//map,keyfield
  11. let update = {$inc: {sequence_value: list.length}};
  12. let condition = {_id: name};
  13. let options = {new: true};
  14. // 先查找更新
  15. let counter = await counterModel.findOneAndUpdate(condition, update, options);
  16. let firstID = counter.sequence_value - (list.length - 1);
  17. for(let a of list){
  18. a.id = firstID;
  19. firstID+=1
  20. if(map && keyfield){
  21. let key = a[keyfield];
  22. map[key]?map[key].push(a):map[key]=[a]
  23. }
  24. }
  25. }
  26. function getProjectGLJNewData(tmp,projectId,ext){
  27. let gljData = {
  28. glj_id: tmp.ID,
  29. repositoryId:tmp.repositoryId,
  30. project_id: projectId,
  31. code: tmp.code,
  32. name: tmp.name,
  33. specs: tmp.specs?tmp.specs:'',
  34. unit: tmp.unit === undefined ? '' : tmp.unit,
  35. type: tmp.gljType,
  36. adjCoe:tmp.adjCoe,
  37. original_code:tmp.code,
  38. materialType: tmp.materialType, //三材类别
  39. materialCoe: tmp.materialCoe,
  40. base_price: tmp.basePrice,
  41. market_price: tmp.basePrice,
  42. from:tmp.from?tmp.from:"std"
  43. };
  44. if(gljData.from == 'std' && ext && ext.priceField &&( tmp.priceProperty[ext.priceField]!= undefined && tmp.priceProperty[ext.priceField]!=null)){
  45. basePrice = scMathUtil.roundTo(tmp.priceProperty[ext.priceField],-6);
  46. gljData.base_price = basePrice;
  47. gljData.market_price = basePrice;
  48. }
  49. return gljData;
  50. }
  51. //根据缺少项目工料机的组成物信息,反向生成对应的项目工料机
  52. async function getNewProjectGLJFromMissMixratio(projectID,lessMix,projectGLJMap,newProjectGLJList,ext){
  53. let lessIDList=[];
  54. let uniqMap ={};//去重
  55. let lessStdMix = [];//防止组成物中改了名称等,但是通过glj_id取出来的是还没改前的原始数据
  56. if(lessMix.length > 0){
  57. for(let lm of lessMix){
  58. let parentglj = projectGLJMap[lm.connect_key];
  59. if(!parentglj) throw `含有组成物工料机${lm.connect_key},没有找到,添加定额失败`;
  60. if((parentglj.from == "std" || lm.from == "std") && lm.code!="80CCS"){//车船税特殊处理
  61. if(!uniqMap[lm.glj_id]){
  62. lessIDList.push(lm.glj_id);
  63. uniqMap[lm.glj_id] = lm;
  64. }
  65. lessStdMix.push(lm);
  66. }else {//来自组成物的直接设置
  67. lm.from = 'cpt';
  68. lm.gljType = lm.type;
  69. let t_mg = getProjectGLJNewData(lm,projectID);
  70. newProjectGLJList.push(t_mg);
  71. projectGLJMap[getIndex(lm)] = t_mg;
  72. }
  73. }
  74. }
  75. if(lessIDList.length > 0){
  76. let less_stds = await std_glj_lib_gljList_model.find({'ID':{'$in':lessIDList}}).lean();
  77. let less_stds_map = {};
  78. for(let les of less_stds){
  79. less_stds_map[les.ID] = les;
  80. }
  81. for(let t_l_m of lessStdMix){
  82. let t_nglj = less_stds_map[t_l_m.glj_id];
  83. t_nglj.from = 'std';
  84. //防止组成物中改了名称等,但是通过glj_id取出来的是还没改前的原始数据
  85. t_nglj.name = t_l_m.name;
  86. t_nglj.code = t_l_m.code;
  87. t_nglj.gljType = t_l_m.type;
  88. t_nglj.specs = t_l_m.specs;
  89. t_nglj.unit = t_l_m.unit;
  90. let t_np = getProjectGLJNewData(t_nglj,projectID,ext);
  91. newProjectGLJList.push(t_np);
  92. projectGLJMap[getIndex(t_l_m)] = t_np;
  93. }
  94. }
  95. return newProjectGLJList;
  96. }
  97. //找到并返回单价文件信息,如果没有自动插入
  98. async function getUnitPriceData(newProjectGLJList,gljCodes,unitPriceFileId){
  99. let unitPriceMap = {};
  100. let newUnitPriceList = [];
  101. let unitPriceList = await unitPriceModel.find({unit_price_file_id: unitPriceFileId,'code':{'$in':gljCodes}}).lean();
  102. for(let u of unitPriceList){
  103. unitPriceMap[getIndex(u)]=u;
  104. }
  105. for(let np of newProjectGLJList){
  106. let pkey = getIndex(np);
  107. if(unitPriceMap[pkey]) continue;
  108. let insertData = {
  109. code: np.code,
  110. base_price: np.base_price,
  111. market_price: np.market_price,
  112. unit_price_file_id: unitPriceFileId,
  113. name: np.name,
  114. specs:np.specs?np.specs:'',
  115. original_code:np.original_code,
  116. unit:np.unit?np.unit:'',
  117. type: np.type,
  118. short_name: np.shortName !== undefined ? np.shortName : '',
  119. glj_id: np.glj_id,
  120. is_add:0,
  121. grossWeightCoe:np.grossWeightCoe,
  122. purchaseStorageRate:np.purchaseStorageRate,
  123. offSiteTransportLossRate:np.offSiteTransportLossRate,
  124. handlingLossRate:np.handlingLossRate
  125. };
  126. if(np.from=='cpt') insertData.is_add=1;//如果是来自补充工料机,则都添加新增标记
  127. if(insertData.code != insertData.original_code) insertData.is_add=1;//添加的时候如果是复制整块来的,可能在源项目中是新增的工料机,这里也要添上(暂时可能还用不到)
  128. newUnitPriceList.push(insertData);
  129. unitPriceMap[pkey] = insertData;
  130. }
  131. if(newUnitPriceList.length > 0){
  132. await setIDfromCounter("unit_price",newUnitPriceList);
  133. await unitPriceModel.insertMany(newUnitPriceList);
  134. }
  135. return [unitPriceMap,newUnitPriceList];
  136. }
  137. function getIndex(obj, pops){
  138. let t_index = '';
  139. let k_arr = [];
  140. if(!pops) pops = ['code','name','specs','unit','type'];
  141. for (let p of pops) {
  142. let tmpK = (obj[p] == undefined || obj[p] == null || obj[p] == '') ? 'null' : obj[p];
  143. k_arr.push(tmpK);
  144. }
  145. t_index = k_arr.join("|-|");
  146. return t_index;
  147. }