/**
 * 组成物相关数据
 *
 * @author CaiAoLin
 * @date 2017/10/27
 * @version
 */

function Composition() {
    this.datas = null;
    this.isLoading = false;
}
/**
 * 加载数据
 *
 * @param {function} callback
 * @return {boolean}
 */
Composition.prototype.loadData = function (callback = null) {
    let self = this;
    if (self.isLoading) {
        return false;
    }
   /* // 加载组成物数据
    $.ajax({
        url: '/glj/get-composition',
        type: 'post',
        dataType: 'json',
        data: {project_id: scUrlUtil.GetQueryString('project')},
        error: function() {
            // alert('数据传输错误');
        },
        beforeSend: function() {
            self.isLoading = true;
        },
        success: function(response) {
            self.isLoading = false;
            if (response.err === 1) {
                let msg = response.msg !== undefined && response.msg !== '' ? response.msg : '读取组成物数据失败!';
                alert(msg);
                return false;
            }
            self.datas = response.data;
            // 回调函数
            if (callback !== null) {
                callback(response.data);
            }
            // 存入缓存
            projectObj.project.composition = self;
        }
    });*/
};

/**
 * 获取对应code的组成物数据
 *
 * @param {String} code
 * @param {Number} gljType
 * @return {Array}
 */
Composition.prototype.getCompositionByCode = function(code, gljType = 0) {
    let result = [];
    if (code === '') {
        return result;
    }
    if (gljType === 0) {
        return this.datas[code] !== undefined ? this.datas[code] : result;
    }

    for(let composition of this.datas[code]) {
        if (composition.glj_type === gljType) {
            result.push(composition);
        }
    }

    return result;
};

Composition.prototype.getCompositionByGLJ = function (glj) {
    let mixRatioMap = projectObj.project.projectGLJ.datas.mixRatioMap;
    let projectGljs = projectObj.project.projectGLJ.datas.gljList;
    let connect_index = gljOprObj.getIndex(glj,gljKeyArray);
    let gljList=[];
    if(mixRatioMap[connect_index]){ //说明是有组成物的类型
        gljList =  gljOprObj.getMixRationShowDatas(mixRatioMap[connect_index], projectGljs);
    }
    return gljList;
};

Composition.prototype.deleteComposition = function (updateData,recode,pid,callback) {
    let me = this;
    $.bootstrapLoading.start();
    CommonAjax.specialPost( '/glj/delete-ratio',{id: updateData.id},function (result){
        me.updateConsumptionInCache(pid,recode,updateData,"delete");
        if(callback){
            callback();
        }
        $.bootstrapLoading.end();
    })
};

Composition.prototype.updateConsumptionInCache = function (pid,recode,updateData,operation='modify') {
    let decimal = getDecimal("glj.quantity");
    let parentGlj = projectObj.project.projectGLJ.getByID(pid);//得到父工料机的数据
    let ratioData = recode;
    let con_key = gljOprObj.getIndex(ratioData,gljKeyArray);
    let mglj = projectObj.project.projectGLJ.getByConKey(con_key);//取组成物对应的工料机;
    if(mglj && parentGlj){
        let newValue = scMathUtil.roundForObj(updateData.value,decimal);
        let oldValue = scMathUtil.roundForObj(ratioData.consumption,decimal);
        let changValue = scMathUtil.roundForObj(newValue-oldValue,decimal);
        //乘以父工料机的消耗量得到该组成物总消耗量的改变量
        changValue = operationWithRound(parentGlj.quantity,changValue,"glj.quantity","*");
        let oldQuantity =  scMathUtil.roundForObj(mglj.quantity,decimal);
        mglj.quantity = scMathUtil.roundForObj(oldQuantity+changValue,decimal);
        recode.consumption = newValue;
        projectObj.project.projectGLJ.quantityChangeMap[mglj.id] = mglj.quantity;
        let p_key = gljOprObj.getIndex(parentGlj,gljKeyArray);
        let m_list = projectObj.project.projectGLJ.datas.mixRatioMap[p_key];
        if(operation == 'delete'){//如果是删除
            _.remove(parentGlj.ratio_data,{"id":updateData.id});
            _.remove(m_list,{"id":updateData.id});
        }else {
            let subData = _.find(parentGlj.ratio_data,{"id":updateData.id});
            if(subData){
                subData.consumption = updateData.value;
            }
            //更新组成物map里的数据
            let m_ratioData = _.find(m_list,{"id":updateData.id});
            if(m_ratioData){
                m_ratioData.consumption = updateData.value;
            }
        }
        // 设置父级3个价格
        parentGlj.unit_price.market_price =  updateData.market_price;
        parentGlj.unit_price.base_price =  updateData.base_price;
        return mglj.id;
    }
}


Composition.prototype.updateConsumption = function (updateData,recode,pid,callback){
    let me = this;
    $.bootstrapLoading.start();
    CommonAjax.specialPost( '/glj/update',updateData,function (result) {
        //更新缓存
       let sid = me.updateConsumptionInCache(pid,recode,updateData);
        if(callback){
            callback(sid);
        }
        $.bootstrapLoading.end();
    })
};
