|  | @@ -0,0 +1,182 @@
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * Created by zhang on 2018/2/9.
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +let mongoose = require('mongoose');
 | 
	
		
			
				|  |  | +import SearchDao from '../../complementary_ration_lib/models/searchModel';
 | 
	
		
			
				|  |  | +const scMathUtil = require('../../../public/scMathUtil').getUtil();
 | 
	
		
			
				|  |  | +let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade")
 | 
	
		
			
				|  |  | +let quantity_detail = require("../facade/quantity_detail_facade");
 | 
	
		
			
				|  |  | +let ration_glj = mongoose.model('ration_glj');
 | 
	
		
			
				|  |  | +let ration_coe = mongoose.model('ration_coe');
 | 
	
		
			
				|  |  | +let ration_model = require('../models/ration');
 | 
	
		
			
				|  |  | +var bill_model = require('../models/bills');
 | 
	
		
			
				|  |  | +let decimal_facade = require('./decimal_facade');
 | 
	
		
			
				|  |  | +const uuidV1 = require('uuid/v1');
 | 
	
		
			
				|  |  | +let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +module.exports = {
 | 
	
		
			
				|  |  | +    replaceRations: replaceRations
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +async function replaceRations(uerID,data) {
 | 
	
		
			
				|  |  | +    let searchDao = new SearchDao();
 | 
	
		
			
				|  |  | +    let recodes = [];
 | 
	
		
			
				|  |  | +    for(let recode of data.nodeInfo){
 | 
	
		
			
				|  |  | +        let stdRation = await searchDao.getRationItem(uerID,data.libID,recode.newCode);
 | 
	
		
			
				|  |  | +        let newRecode = await replaceRation(recode,stdRation,data.projectID,data.calQuantity);
 | 
	
		
			
				|  |  | +        if(newRecode){
 | 
	
		
			
				|  |  | +            recodes.push(newRecode);
 | 
	
		
			
				|  |  | +        }else {
 | 
	
		
			
				|  |  | +            break;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    return recodes;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +async function replaceRation(nodeInfo,stdRation,projectID,calQuantity) {
 | 
	
		
			
				|  |  | +    if(stdRation){
 | 
	
		
			
				|  |  | +        await deleRationSubRecode(projectID,nodeInfo.ID);
 | 
	
		
			
				|  |  | +        let newRation = await updateRation(stdRation,nodeInfo.ID,nodeInfo.billsItemID,projectID,calQuantity);//生成并插入新的定额
 | 
	
		
			
				|  |  | +        let ration_gljs = await addRationGLJ(stdRation,newRation);
 | 
	
		
			
				|  |  | +        //console.log(newRation);
 | 
	
		
			
				|  |  | +        //ration_model.model.bulkWrite(rationTask);//删除定额
 | 
	
		
			
				|  |  | +        console.log(newRation);
 | 
	
		
			
				|  |  | +        return newRation;
 | 
	
		
			
				|  |  | +    }else {
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +async function addRationCoe(std,newRation) {
 | 
	
		
			
				|  |  | +    let ration_coe_list = [];
 | 
	
		
			
				|  |  | +    if(std.hasOwnProperty('rationCoeList')&&data.rationCoeList.length>0){
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    var criteria= {};
 | 
	
		
			
				|  |  | +    criteria.ration_coe_list = [];
 | 
	
		
			
				|  |  | +    var dataLength = 0;
 | 
	
		
			
				|  |  | +    if(data.hasOwnProperty('rationCoeList')&&data.rationCoeList.length>0){
 | 
	
		
			
				|  |  | +        dataLength = data.rationCoeList.length
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    for(var i=0;i<=dataLength;i++){;
 | 
	
		
			
				|  |  | +        var newCoe = {
 | 
	
		
			
				|  |  | +            libID :data.rationRepId,
 | 
	
		
			
				|  |  | +            rationID:newRation.ID,
 | 
	
		
			
				|  |  | +            projectID :  newRation.projectID
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if(i==dataLength){
 | 
	
		
			
				|  |  | +            newCoe.coeID=-1;
 | 
	
		
			
				|  |  | +            newCoe.name = '自定义系数';
 | 
	
		
			
				|  |  | +            newCoe.content='人工×1,材料×1,机械×1,主材×1,设备×1';
 | 
	
		
			
				|  |  | +            newCoe.isAdjust=0;
 | 
	
		
			
				|  |  | +            newCoe.coes = this.getCustomerCoeData();
 | 
	
		
			
				|  |  | +        }else {
 | 
	
		
			
				|  |  | +            newCoe.coeID= data.rationCoeList[i].ID;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        criteria.ration_coe_list.push(newCoe);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +async function addRationGLJ(std,newRation) {
 | 
	
		
			
				|  |  | +    let newRationGLJList = [];
 | 
	
		
			
				|  |  | +    let rationGLJShowList = [];
 | 
	
		
			
				|  |  | +    if(std.hasOwnProperty('rationGljList') && std.rationGljList.length > 0){
 | 
	
		
			
				|  |  | +        for(let sub of std.rationGljList){
 | 
	
		
			
				|  |  | +            let newGLJ = {};
 | 
	
		
			
				|  |  | +            newGLJ.ID = uuidV1();
 | 
	
		
			
				|  |  | +            newGLJ.projectID = newRation.projectID;
 | 
	
		
			
				|  |  | +            newGLJ.GLJID = sub.gljId;
 | 
	
		
			
				|  |  | +            newGLJ.rationID = newRation.ID;
 | 
	
		
			
				|  |  | +            newGLJ.billsItemID = newRation.billsItemID,
 | 
	
		
			
				|  |  | +            newGLJ.rationItemQuantity = sub.consumeAmt;
 | 
	
		
			
				|  |  | +            newGLJ.quantity = sub.consumeAmt;
 | 
	
		
			
				|  |  | +            newGLJ.glj_repository_id = std.rationRepId;
 | 
	
		
			
				|  |  | +            let std_glj = await std_glj_lib_gljList_model.findOne({'ID': ration_glj.GLJID});
 | 
	
		
			
				|  |  | +            if(std_glj){
 | 
	
		
			
				|  |  | +                newGLJ.name = std_glj.name;
 | 
	
		
			
				|  |  | +                newGLJ.code = std_glj.code;
 | 
	
		
			
				|  |  | +                newGLJ.original_code = std_glj.code;
 | 
	
		
			
				|  |  | +                newGLJ.unit = std_glj.unit;
 | 
	
		
			
				|  |  | +                newGLJ.specs = std_glj.specs;
 | 
	
		
			
				|  |  | +                newGLJ.basePrice = std_glj.basePrice;
 | 
	
		
			
				|  |  | +                newGLJ.shortName = std_glj.shortName;
 | 
	
		
			
				|  |  | +                newGLJ.type = std_glj.gljType;
 | 
	
		
			
				|  |  | +                newGLJ.repositoryId = std_glj.repositoryId;
 | 
	
		
			
				|  |  | +                newGLJ.adjCoe = std_glj.adjCoe;
 | 
	
		
			
				|  |  | +               let info = await  ration_glj_facade.getInfoFromProjectGLJ(newGLJ);
 | 
	
		
			
				|  |  | +                newGLJ = ration_glj_facade.createNewRecord(info);
 | 
	
		
			
				|  |  | +                newRationGLJList.push(newGLJ);
 | 
	
		
			
				|  |  | +                rationGLJShowList.push(info);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    if(newRationGLJList.length>0){
 | 
	
		
			
				|  |  | +        await ration_glj.insertMany(newRationGLJList);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    return rationGLJShowList;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +async function deleRationSubRecode(projectID,rationID) {//删除挂在定额下的数据,如工程量明细,定额工料机等
 | 
	
		
			
				|  |  | +    let delete_query={projectID: projectID, rationID: rationID};
 | 
	
		
			
				|  |  | +    //删除工程量明细
 | 
	
		
			
				|  |  | +    await quantity_detail.deleteByQuery(delete_query) ;
 | 
	
		
			
				|  |  | +    await ration_coe.deleteMany(delete_query);//删除附注条件
 | 
	
		
			
				|  |  | +    await ration_glj.deleteMany(delete_query);//删除定额工料机
 | 
	
		
			
				|  |  | +    //todo 删除安装
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +async function  updateRation(std,rationID,billsItemID,projectID,calQuantity) {
 | 
	
		
			
				|  |  | +    // insertNewRation
 | 
	
		
			
				|  |  | +    let ration ={};
 | 
	
		
			
				|  |  | +    ration.code = std.code;
 | 
	
		
			
				|  |  | +    ration.name = std.name;
 | 
	
		
			
				|  |  | +    ration.caption = std.caption;
 | 
	
		
			
				|  |  | +    ration.unit = std.unit;
 | 
	
		
			
				|  |  | +    ration.libID = std.rationRepId;
 | 
	
		
			
				|  |  | +    ration.content = std.jobContent;
 | 
	
		
			
				|  |  | +    ration.adjustState = '';
 | 
	
		
			
				|  |  | +    if (std.chapter) {
 | 
	
		
			
				|  |  | +        ration.comments = std.chapter.explanation;
 | 
	
		
			
				|  |  | +        ration.ruleText = std.chapter.ruleText;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    ration.from = std.type === 'complementary' ? 'cpt' : 'std';
 | 
	
		
			
				|  |  | +    ration.programID = std.feeType;
 | 
	
		
			
				|  |  | +    ration.rationAssList = [];//生成辅助定额
 | 
	
		
			
				|  |  | +    if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
 | 
	
		
			
				|  |  | +        for(var i=0;i<std.rationAssList.length;i++){
 | 
	
		
			
				|  |  | +            var ass = std.rationAssList[i];
 | 
	
		
			
				|  |  | +            ass.actualValue = ass.stdValue;
 | 
	
		
			
				|  |  | +            ration.rationAssList.push(ass);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    if(calQuantity){
 | 
	
		
			
				|  |  | +       await CalculateQuantity(ration,billsItemID,projectID);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},ration,{new: true});
 | 
	
		
			
				|  |  | +    return newRation;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +async function CalculateQuantity (ration,billsItemID,projectID) {
 | 
	
		
			
				|  |  | +    // calculate ration Quantity
 | 
	
		
			
				|  |  | +    let decimalObject =await decimal_facade.getProjectDecimal(projectID);
 | 
	
		
			
				|  |  | +    let quantity_decimal = (decimalObject&&decimalObject.ration&&decimalObject.ration.quantity)?decimalObject.ration.quantity:3;
 | 
	
		
			
				|  |  | +    let pbill = await bill_model.model.findOne({projectID:projectID,ID:billsItemID});
 | 
	
		
			
				|  |  | +    let billsQuantity = pbill.quantity ? pbill.quantity : 0;
 | 
	
		
			
				|  |  | +    let bill_decimal = await decimal_facade.getBillsQuantityDecimal(projectID,pbill.unit);
 | 
	
		
			
				|  |  | +    billsQuantity=scMathUtil.roundForObj(billsQuantity,bill_decimal);
 | 
	
		
			
				|  |  | +    ration.quantityEXP="QDL";
 | 
	
		
			
				|  |  | +    ration.quantity = scMathUtil.roundForObj(billsQuantity / FilterNumberFromUnit(ration.unit),quantity_decimal);//不管是否打勾都做转换
 | 
	
		
			
				|  |  | +    ration.contain =  scMathUtil.roundForObj(ration.quantity/billsQuantity,6);
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +function FilterNumberFromUnit (unit) {
 | 
	
		
			
				|  |  | +    let reg = new RegExp('^[0-9]+');
 | 
	
		
			
				|  |  | +    if (reg.test(unit)) {
 | 
	
		
			
				|  |  | +        return parseInt(unit.match(reg)[0]);
 | 
	
		
			
				|  |  | +    } else {
 | 
	
		
			
				|  |  | +        return 1;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +};
 |