/**
 * Created by chen on 2017/7/10.
 */
let mongoose = require('mongoose');
let consts = require('../../main/models/project_consts');
let commonConsts = consts.commonConst;
let _=require("lodash");
let async_n = require("async");
let std_ration_lib_ration_items = mongoose.model('std_ration_lib_ration_items');
let rationModel = mongoose.model('ration');
let ration_glj = mongoose.model('ration_glj');
let glj_calculate_facade = require('./glj_calculate_facade');

module.exports={
    save:save,
    getData:getData,
    updateRationAss
}

let operationMap={
    'ut_create':create_ration_ass,
    'ut_update':update_ration_ass,
    'ut_delete':delete_ration_ass
};

function create_ration_ass(user_id,datas) {

};

async function updateRationAss(data){
    let result =await doRationAssAdjust(data.query.ration,data.doc);
    let ration_glj ={
        quantityRefresh:true,
        glj_result:result.cal_result.glj_result
    }
    let ration = {
        ID:result.cal_result.rationID,
        adjustState:result.cal_result.adjustState,
        name:result.cal_result.rationName,
        rationAssList:data.doc.rationAssList
    };
    return {ration_glj:ration_glj,ration:ration}
}


function update_ration_ass(user_id,datas) {
    return function (callback) {
        let editAss = datas.query.editAss;
        doRationAssAdjust(datas.query.ration,datas.doc).then(function (result) {
            if(result.err){
                callback(result.err,'')
            }else {
                let newObject = {
                    moduleName:consts.projectConst.RATION_ASS,
                    data:{
                        updateTpye:commonConsts.UT_UPDATE,
                        doc:datas.doc,
                        rationID:datas.query.ration.ID,
                        editIndex:datas.query.editIndex,
                        actualValue:editAss.actualValue
                    }
                };
                let ration_glj_data ={
                    moduleName:'ration_glj',
                    data:{
                        updateTpye:commonConsts.UT_UPDATE,
                        quantityRefresh:true,
                        glj_result:result.cal_result.glj_result
                    }
                };
                let ration_data ={
                    moduleName:'ration',
                    data:{
                        updateTpye:commonConsts.UT_UPDATE,
                        stateRefresh:true,
                        rationID:result.cal_result.rationID,
                        adjustState:result.cal_result.adjustState,
                        name:result.cal_result.rationName
                    }
                };
                callback(null,[newObject,ration_glj_data,ration_data]);
            }
        })

    }

};

async function doRationAssAdjust(ration,doc) {
    let result ={
        err:null,
        cal_result:[]
    };
    try{
        await rationModel.update({projectID:ration.projectID,ID:ration.ID,deleteInfo: null},doc);
        let cal_result = await glj_calculate_facade.calculateQuantity({projectID:ration.projectID,rationID:ration.ID},null,true);
        result.cal_result=cal_result;
        return result;
    }catch (err){
        result.err = err;
        console.log(err);
        return result;
    }
}




function delete_ration_ass(user_id,datas) {

};






function getData(projectID, callback) {
    callback(0, consts.projectConst.RATION_ASS, []);
}

function save (user_id, datas, callback) {
    let operations=[];
    if(_.isArray(datas)){
        for(let i=0;i<datas.length;i++){
            operations.push(operationMap[datas[i].updateType](user_id,datas[i]));
        }
    }else {
        operations.push(operationMap[datas.updateType](user_id,datas));
    }
    async_n.parallel(operations,function (err,results) {
        if(err){
            callback(err,'');
        }else {
            if(results.length==1){
                callback(null,results[0])
            }else {
                callback(null,results)
            }
        }
    })
}