'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2019/4/17
 * @version
 */

//允许使用的工料机类型：人工、普通材料、混凝土、砂浆、配合比、商品混凝土、商品砂浆、其他材料费、机械台班、机械组成物、机上人工、主材、设备、企业管理费、利润
if(typeof allowGljType !== 'undefined'){
    allowGljType = [1, 201, 202, 203, 204, 205, 206, 207, 301, 302, 303, 4, 5, 6, 7];
}
if(typeof allowComponent !== 'undefined'){
    //允许含有组成物的工料机类型：混凝土、砂浆、配合比、机械台班、主材
    allowComponent = [202, 203, 204, 301, 4];
}
if(typeof componentType !== 'undefined'){
    //可以作为组成物的工料机类型：普通材料、机械组成物、机上人工、主材
    componentType = [201, 302, 303, 4];
}
if(typeof machineAllowComponent !== 'undefined'){
    //允许含有组成物的机械工料机类型：机械台班
    machineAllowComponent = [301];
}
if(typeof machineComponent !== 'undefined'){
    //可以作为机械工料机组成物的工料机类型：机械组成物、机上人工
    machineComponent = [302, 303];
}
if(typeof materialAllowComponent !== 'undefined'){
    //允许含有组成物的材料工料机类型：混凝土、砂浆、配合比
    materialAllowComponent = [202, 203, 204];
}
if(typeof materialComponent !== 'undefined'){
    //可以作为材料工料机组成物的工料机类型：普通材料
    materialComponent = [201];
}

// 计算单个人材机定额价小数位数
let singleDecimal = -6,
    // 汇总小数位数
    summaryDecimal = -2,
    // 中间过程
    processDecimal = -6;

//覆盖前端定额基价计算
//基价=人工费+材料费+机械费+管理费利润
//管理费利润=Round（人工费*（管理费消耗量+利润消耗量）%，2） 注：书中管理费、利润的单位都是“%”
//材料费相关：应该是先算单位不是%的，算一个临时材料费，然后%的单个材料费算法是临时材料费*消耗量*0.01，然后最终的材料费是临时材料费+%的单个材料费
if (typeof rationGLJOprObj !== 'undefined' && typeof rationGLJOprObj.rationCal !== 'undefined') {
    rationGLJOprObj.rationCal = function () {
        let me = rationGLJOprObj;
        let price = {gljType1: [], gljType2: [], gljType3: []},
            // 临时材料费(非%单位的材料)
            tempPrice = {gljType1: 0, gljType2: 0, gljType3: 0},
            rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0},
            rationBasePrc = 0;
        let manageProfitConsume = 0;   //管理费、利润消耗量
        if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
            let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
            // 获取临时材料费
            cacheArr.forEach(function (gljData) {
                if(gljData.gljType && gljData.consumeAmt && gljData.unit !== '%'){
                    let parentGLJType = parseInt(String(gljData.gljType)[0]);
                    if (parentGLJType <= 3) { // 人工、材料、机械
                        let single = scMathUtil.roundTo(gljData.basePrice * gljData.consumeAmt, singleDecimal);
                        tempPrice['gljType' + parentGLJType] = scMathUtil.roundTo(tempPrice['gljType' + parentGLJType] + single, processDecimal);
                    }
                }
            });
            // 临时材料费放入待汇总价格数组中
            for (let attr in price) {
                price[attr].push(tempPrice[attr])
            }
            cacheArr.forEach(function (gljData) {
                if(gljData.gljType && gljData.consumeAmt){
                    let parentGLJType = parseInt(String(gljData.gljType)[0]);
                    if (parentGLJType <= 3 && gljData.unit === '%') { // 人工、材料、机械
                        // %的单个材料费算法是临时材料费*消耗量*0.01
                        price['gljType' + parentGLJType].push(scMathUtil.roundTo(tempPrice['gljType' + parentGLJType] * gljData.consumeAmt * 0.01, singleDecimal));
                    } else if([6, 7].includes(gljData.gljType)){
                        manageProfitConsume = scMathUtil.roundTo(manageProfitConsume + gljData.consumeAmt, processDecimal);
                    }
                }
            });
            if(price.gljType1.length > 0){
                let labourPrice = 0;
                price.gljType1.forEach(function (singlePrc) {
                    labourPrice = scMathUtil.roundTo(labourPrice + singlePrc, processDecimal);
                });
                let roundPrice = scMathUtil.roundTo(labourPrice, summaryDecimal);
                rst.labourPrice = roundPrice;
                rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, summaryDecimal);
                //管理费利润
                let manageProfitPrc = scMathUtil.roundTo(roundPrice * manageProfitConsume * 0.01, summaryDecimal);
                rationBasePrc = scMathUtil.roundTo(rationBasePrc + manageProfitPrc, summaryDecimal);
            }
            if(price.gljType2.length > 0){
                let materialPrice = 0;
                price.gljType2.forEach(function (singlePrc) {
                    materialPrice = scMathUtil.roundTo(materialPrice + singlePrc, processDecimal);
                });
                let roundPrice = scMathUtil.roundTo(materialPrice, summaryDecimal);
                rst.materialPrice = roundPrice;
                rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, summaryDecimal);
            }
            if(price.gljType3.length > 0){
                let machinePrice = 0;
                price.gljType3.forEach(function (singlePrc) {
                    machinePrice = scMathUtil.roundTo(machinePrice + singlePrc, processDecimal);
                });
                let roundPrice = scMathUtil.roundTo(machinePrice, summaryDecimal);
                rst.machinePrice = roundPrice;
                rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, summaryDecimal);
            }
            rst.rationBasePrc = rationBasePrc;
        }
        return rst;
    }
}

// 后端用 计算定额基价
function calcRation(gljArr, scMathUtil) {
    let labourPrc = [],
        materialPrc = [],
        machinePrc = [],
        manageProfitConsume = 0,
        updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, manageProfitPrice: 0, basePrice: 0};
    // 临时材料费
    let tempPrice = {gljType1: 0, gljType2: 0, gljType3: 0};
    gljArr.forEach(function (gljItem) {
        if (gljItem.gljParentType !== -1 &&
            gljItem.gljParentType <= 3 &&
            gljItem.unit !== '%') {
            let single = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, singleDecimal);
            tempPrice['gljType' + gljItem.gljParentType] = scMathUtil.roundTo(tempPrice['gljType' + gljItem.gljParentType] + single, processDecimal);
        }
    });
    labourPrc.push(tempPrice.gljType1);
    materialPrc.push(tempPrice.gljType2);
    machinePrc.push(tempPrice.gljType3);
    gljArr.forEach(function (gljItem) {
        let singlePrc = 0;
        if(gljItem.gljParentType !== -1){
            if (gljItem.gljParentType <= 3 && gljItem.unit === '%') {
                singlePrc = scMathUtil.roundTo(tempPrice['gljType' + gljItem.gljParentType] * gljItem.consumeAmt * 0.01, singleDecimal);
            } else if (gljItem.gljParentType > 3) {
                singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, singleDecimal);
            }
            if(gljItem.gljParentType === 1){
                labourPrc.push(singlePrc);
            } else if(gljItem.gljParentType ===2){
                materialPrc.push(singlePrc);
            } else if(gljItem.gljParentType === 3){
                machinePrc.push(singlePrc);
            } else if(gljItem.gljParentType === 6){
                manageProfitConsume = scMathUtil.roundTo(manageProfitConsume + gljItem.consumeAmt, processDecimal);
            } else if(gljItem.gljParentType === 7){
                manageProfitConsume = scMathUtil.roundTo(manageProfitConsume + gljItem.consumeAmt, processDecimal);
            }
        }
    });
    if(labourPrc.length > 0){
        let sumLaP = 0;
        for(let i=0; i<labourPrc.length; i++){
            sumLaP = scMathUtil.roundTo(sumLaP + labourPrc[i], processDecimal);
        }
        updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, summaryDecimal);
        updatePrc.manageProfitPrice = scMathUtil.roundTo(updatePrc.labourPrice * manageProfitConsume * 0.01, summaryDecimal);
        updatePrc.basePrice = scMathUtil.roundTo(updatePrc.labourPrice + updatePrc.manageProfitPrice, summaryDecimal);
    }
    if(materialPrc.length > 0){
        let sumMtP = 0;
        for(let i= 0; i<materialPrc.length; i++){
            sumMtP = scMathUtil.roundTo(sumMtP + materialPrc[i], processDecimal);
        }
        updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, summaryDecimal);
        updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.materialPrice, summaryDecimal);
    }
    if(machinePrc.length > 0){
        let sumMaP = 0;
        for(let i =0; i< machinePrc.length; i++){
            sumMaP = scMathUtil.roundTo(sumMaP + machinePrc[i], processDecimal);
        }
        updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, summaryDecimal);
        updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.machinePrice, summaryDecimal);
    }
    return updatePrc;
}

//计税方式
const taxModel = {'common': 1, 'simple': 2};
//价格属性：计税方式
const pricePropertiesTemplate = [
    {
        region: '全省',
        taxModel: taxModel.common,
        price: {
            dataCode: 'price1',
            dataName: '单价-一般'
        }
    },
    {
        region: '全省',
        taxModel: taxModel.simple,
        price: {
            dataCode: 'price2',
            dataName: '单价-简易'
        }
    }
];

//消耗量属性：计税方式
const consumeAmtPropertiesTemplate = [
    {
        region: '全省',
        taxModel: taxModel.common,
        consumeAmt: {
            dataCode: 'consumeAmt1',
            dataName: '消耗-一般',
            refPrice: 'price1' //关联的单价字段
        }
    },
    {
        region: '全省',
        taxModel: taxModel.simple,
        consumeAmt: {
            dataCode: 'consumeAmt2',
            dataName: '消耗-简易',
            refPrice: 'price2' //关联的单价字段
        }
    }
];

if(typeof module !== 'undefined'){
    module.exports = {
        pricePropertiesTemplate: pricePropertiesTemplate,
        consumeAmtPropertiesTemplate: consumeAmtPropertiesTemplate,
        calcRation: calcRation
    };
}