material_replace_facade.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Created by zhang on 2018/9/12.
  3. */
  4. let logger = require("../../../logs/log_helper").logger;
  5. let mongoose = require('mongoose');
  6. let material_lib = mongoose.model('std_material_replace_lib');
  7. let replace_bills = mongoose.model('std_replace_bills');
  8. let replace_material = mongoose.model('std_replace_material');
  9. let _=require("lodash");
  10. let ration_glj_facade = require('../../ration_glj/facade/ration_glj_facade');
  11. let glj_calculate_facade = require('../../ration_glj/facade/glj_calculate_facade');
  12. module.exports = {
  13. findMaterial: async function(dataMap,compilationId){
  14. let libMap = {},resultMap={};
  15. for(let key in dataMap) {
  16. let billsLibId = dataMap[key].billsLibId;
  17. let libID = libMap[compilationId + billsLibId] ? libMap[compilationId + billsLibId] : await getLibID(billsLibId, compilationId);
  18. libMap[compilationId + billsLibId] = libID;
  19. if (libID == null) continue;
  20. let bills = await getBills(key, libID);
  21. if (bills == null) continue;
  22. let materialList = await getMaterialByBillsID(bills.ID);
  23. resultMap[key] = {bills:bills,materialMap:_.indexBy(materialList,'code')};
  24. }
  25. return resultMap;
  26. },
  27. replace:async function(datas){
  28. let resultList = [];
  29. let rationMap = _.groupBy(datas,function(item){//先按定额进行分组
  30. return item.glj.rationID;
  31. });
  32. for(let rationID in rationMap){
  33. let result = await eachRationGroup(rationID,rationMap[rationID]);
  34. resultList.push(result);
  35. }
  36. return resultList
  37. }
  38. };
  39. async function eachRationGroup(rationID,datas) {
  40. let query={rationID:rationID},gljs=[];
  41. for(let d of datas){
  42. query["projectID"] = d.glj.projectID;
  43. gljs.push(await updateRationGLJ(d));
  44. }
  45. if(query.projectID){
  46. let stateResult = await glj_calculate_facade.calculateQuantity(query,null,true);
  47. return {rationID:rationID,name:stateResult.rationName,adjustState:stateResult.adjustState,ration_gljs:gljs}
  48. }else {
  49. throw new Error("人材机的项目ID有问题");
  50. }
  51. }
  52. async function updateRationGLJ(data) {
  53. let glj = data.glj;
  54. let priceInfo = {
  55. base_price: glj.basePrice,
  56. market_price: glj.marketPrice
  57. };
  58. let [projcetGLJ_n,doc] = await ration_glj_facade.updateRationGLJFromDoc(glj,data.doc,priceInfo);
  59. return {ID:glj.ID,doc:doc}
  60. }
  61. /*async function doRationGLJUpdate(data) {
  62. let resutl = {};
  63. let doc = data.doc;
  64. let priceInfo = data.priceInfo;
  65. let rg = await ration_glj.findOne(data.query);
  66. let gljListModel = new GLJListModel();
  67. let projectGLJ = getGLJSearchInfo(rg);
  68. for (let key in doc) {
  69. projectGLJ[key] = doc[key]
  70. }
  71. projectGLJ.base_price = priceInfo.base_price;
  72. projectGLJ.market_price = priceInfo.market_price;
  73. let projcetGLJ_n = await gljListModel.modifyGLJ(projectGLJ, rg);
  74. doc.code = projcetGLJ_n.code;
  75. doc.projectGLJID = projcetGLJ_n.id;
  76. if (projcetGLJ_n.unit_price.is_add == 1) {
  77. doc.createType = 'replace';
  78. doc.rcode = projcetGLJ_n.original_code;
  79. } else {
  80. doc.createType = 'normal';
  81. doc.rcode = '';
  82. }
  83. await ration_glj.findOneAndUpdate(data.query, doc);
  84. //取价格
  85. gljListModel.getGLJPrice(projcetGLJ_n);
  86. doc.basePrice = projcetGLJ_n.unit_price.base_price;
  87. doc.marketPrice = projcetGLJ_n.unit_price.market_price;
  88. doc.adjustPrice = projcetGLJ_n.adjust_price;
  89. doc.isAdd = projcetGLJ_n.unit_price.is_add;
  90. resutl.doc = doc;
  91. let stateResult = await glj_calculate_facade.calculateQuantity({
  92. projectID: data.query.projectID,
  93. rationID: data.query.rationID
  94. },null,true);
  95. resutl.adjustState = stateResult.adjustState;
  96. resutl.name = stateResult.rationName;
  97. return resutl;
  98. }*/
  99. async function getLibID(billsLibId,compilationId) {
  100. let lib = await material_lib.findOne({"compilationId":compilationId,billsLibId:billsLibId});
  101. if(lib) return lib.ID;
  102. return null;
  103. }
  104. async function getBills(code,libID) {
  105. let bills = await replace_bills.findOne({code:code,libID:libID});
  106. if(bills) return bills;
  107. return null;
  108. }
  109. async function getMaterialByBillsID(billsItemID) {
  110. return await replace_material.find({billsItemID:billsItemID},['code','name','specs','type','unit']);
  111. }