/** * Created by chen on 2017/7/12. */ let mongoose = require('mongoose'); let _=require("lodash"); let ration_glj = mongoose.model('ration_glj'); let ration = mongoose.model('ration'); let ration_coe = mongoose.model('ration_coe'); let std_ration_lib_ration_items = mongoose.model('std_ration_lib_ration_items'); let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList'); let project_model = mongoose.model('projects'); let glj_type_util = require('../../../public/cache/std_glj_type_util'); const scMathUtil = require('../../../public/scMathUtil').getUtil(); let decimal_facade = require('../../main/facade/decimal_facade'); let gljUtil = require('../../../public/gljUtil'); module.exports={ calculateQuantity:calculateQuantity, getGLJTypeByID:getGLJTypeByID } //辅助定额调整、替换工料机、标准附注条件调整、添加工料机、自定义消耗量(包括删除工料机)、自定义乘系数、面积增加、市场单价调整 let stateSeq ={ ass:1, replace:2, coe:3, add:4, cusQuantity:5, cusCoe:6, area:7, adjMak:8 }; //自定义乘系数与定额工料机类型映射表 let coeTypeMap = { "人工":1, "材料":2, "机械":3, "施工机具":3, "主材":4, "设备":5 }; async function calculateQuantity(query,noNeedCal,refreshRationName = false,areaIncreaseFee,areaSetting){ try { let result ={ glj_result:[], rationID:query.rationID }; let impactRation = await ration.findOne({ID:query.rationID}); let gljList = await ration_glj.find(query);//{projectID:query.projectID,rationID:query.rationID} let coeList = await ration_coe.find({rationID:query.rationID}).sort('seq').exec(); let assList=[]; let assRation = null; let adjustState=[]; if(!impactRation){//如果定额不存在或者已删除,返回空 return null; } if(areaIncreaseFee !== null && areaIncreaseFee !== undefined) impactRation.areaIncreaseFee = areaIncreaseFee; if(impactRation._doc.hasOwnProperty("rationAssList")&&impactRation.rationAssList.length>0){ let temTimes = []; let thirdRationCodes=[]; for(let i=0;i 0){ await ration_glj.bulkWrite(generateUpdateTasks(result.glj_result)); } if(impactRation.areaIncreaseFee == true) await setAreaAdjustState(impactRation.projectID,adjustState,areaSetting); adjustState= _.sortByOrder(adjustState, ['index'], ['asc']); adjustState=_.map(adjustState, _.property('content')); let adjustStateString = adjustState.join(';'); let setData = {adjustState:adjustStateString}; if(refreshRationName == true){//需要更新定额名称 let newName = generateRationName(impactRation,gljList); setData.name = newName; result.rationName = newName; } //如果uareaIncreaseFee有值说明是从其它更新定额的地方进来的,后面会一起更新 if(areaIncreaseFee === null || areaIncreaseFee === undefined) await ration.update({ID:query.rationID},setData); result.adjustState=adjustStateString; return result; }catch (err){ console.log(err); throw err; } } async function setAreaAdjustState(projectID,adjustState,areaSetting){ if(!areaSetting){ let project = await project_model.findOne({ID:projectID},'property.areaSetting'); areaSetting = project.property.areaSetting; } if(areaSetting){ let stringArr = []; let labour = getStr(areaSetting.labour); let material = getStr(areaSetting.material); let machine = getStr(areaSetting.machine); if(labour !="") stringArr.push(`人工*${labour}`); if(material !="") stringArr.push(`材料*${material}`); if(machine !="") stringArr.push(`机械*${machine}`); if(stringArr.length > 0){ adjustState.push({index:stateSeq.area,content:`面积:${stringArr.join(',')}`});//面积:人工*1.1,材料*1.015”。 } } function getStr(num) { if(num != 0) return 1 + num/100; return ""; } } function quantityUpdateCheck(glj,r) {//检查,有改变的才更新 for(let key in r.doc){ if(glj._doc[key] != r.doc[key]) return true } return false } function generateRationName(ration,gljList) { let caption = ration.caption ? ration.caption:ration.name; let replaceList = []; if(ration.rationAssList && ration.rationAssList.length > 0){//这里要处理第三定额和多辅助定额的情况 let isThird = ration.rationAssList.length == 2 && ration.rationAssList[0].thirdRationCode&&ration.rationAssList[0].thirdRationCode!='';//说明有第三定额的情况 let adjustMatch = "",notAdjust=""; for(let ass of ration.rationAssList){ let tem = ""; if( ass.isAdjust == 1 && ass.actualValue != null && ass.actualValue != undefined ){ tem = ass.actualValue; adjustMatch = tem; }else { tem = ass.stdValue; notAdjust = tem; } if(isThird) replaceList.push(tem); } if(replaceList.length == 0){ adjustMatch!=""?replaceList.push(adjustMatch):replaceList.push(notAdjust); } for(let r of replaceList){ caption = caption.replace('%s',r); } } let reNameList = []; for(let g of gljList){ //glj._doc.createType=='replace'&&glj.rcode!=glj.code if(g.createType=='replace'&&g.rcode!=g.code){ //是替换工料机 let reName = g.name; if(!_.isEmpty(g.specs)) reName = reName + ' '+g.specs; reNameList.push(reName); } } if(reNameList.length > 0){ let reNameString = reNameList.join(" "); caption = caption + " 换为【"+ reNameString + "】"; } return caption; } function generateUpdateTasks(result) { let tasks = []; for(let i =0;i 1){//组里有多个定额的情况 let newList = _.sortByAll(a.groupList,['param']);//先按参数排序 for(let n of newList){ if(a.actualValue > n.stdValue && a.actualValue <= parseFloat(n.param)){//落在中间,则用组里的这条定额 a._doc.param = n.param; a._doc.paramName = n.paramName; a._doc.assistCode = n.assistCode; break; } } } } } function calculateTimes(ass){ if(ass.isAdjust == 0) return 0;//打勾辅助定额才计算 let times =(ass.actualValue-ass.stdValue)/ass.stepValue; let r = false; if(times<0){ r=true; times=times*-1; } if(ass.carryBit=='四舍五入'){ times = _.round(times,ass.decimal); }else if (ass.carryBit=='进一'){ times =_.ceil(times,ass.decimal); }else if(ass.carryBit == '舍一'){ times = _.floor(times,ass.decimal); } if(r){ times=times*-1; } return scMathUtil.roundTo(times,-6); } function calculateQuantityByCoes(quantity,coeList,glj,gljList,decimal){ let coeQuantity = quantity; if(coeList.length>1){ for(let i=0;i