/**
 * Created by chen on 2017/6/29.
 */
let mongoose = require("mongoose")
let ration_glj_facade = require('../facade/ration_glj_facade')
import EngineeringLibModel from "../../users/models/engineering_lib_model";
let logger = require("../../../logs/log_helper").logger;
const { COMPLEMENTARY_LIB, COMPILATION } = require ('../../../public/common_constants');

module.exports={
    createRationGLJ:createRationGLJ,
    testGetQuantify:testGetQuantify,
    getGLJData:getGLJData,
    getGLJDataByCodes:getGLJDataByCodes,
    addGLJ:addGLJ,
    replaceGLJ:replaceGLJ,
    mReplaceGLJ:mReplaceGLJ,
    updateRationGLJByEdit:updateRationGLJByEdit,
    getGLJClass:getGLJClass,
    deleteRationGLJ:deleteRationGLJ,
    updateProportion:updateProportion
}
function createRationGLJ() {
    let gls = mongoose.model('ration_glj');
    gls.create({'GLJID':1,'basePrice':23.23,'name':"testgls"},function(err, gls){
        console.log(gls)
    })
}

//测试获取消耗量：
async function testGetQuantify() {
    var condition = {
        projectID:99,
        projectGLJIDList:[60,61,62]
    }

    try{
        let result = await ration_glj_facade.getQuantityByProjectGLJ(condition);
        console.log(result)
    }catch (err){
        console.log(err);
    }
}

async function getGLJDataByCodes(req,res){
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let datas= await ration_glj_facade.getGLJDataByCodes(data,req.session.sessionCompilation);
        result.data=datas;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}


async function getGLJData(req, res) {
    let result={
        error:0
    }
    try {
        let libData = null;
        let { engineerID, gljLibId, isInitial } = req.params;
        if (gljLibId !== COMPLEMENTARY_LIB) {
            gljLibId = +gljLibId;
        }
        isInitial = JSON.parse(isInitial);
        if (!gljLibId || isInitial) { // 替换人材机的话，可能存在gljLibID，但是是初始化的操作
            libData = engineerID === COMPILATION 
            ? await ration_glj_facade.getLibOptionsForCompilation(req.session.sessionCompilation._id)
            : await ration_glj_facade.getLibOptions(engineerID);
            libData.push({ name: '补充工料机', gljLibId: COMPLEMENTARY_LIB });
            if (gljLibId) { // 替换人材机初始化会触发此条件
                const orgDefalutLib = libData.find(lib => lib.isDefault);
                const newDefaultLib = libData.find(lib => lib.gljLibId === gljLibId);
                if (orgDefalutLib && newDefaultLib) {
                    orgDefalutLib.isDefault = false;
                    newDefaultLib.isDefault = true;
                }
            }
        }
        if (!gljLibId && libData) {
            gljLibId = (libData.find(lib => lib.isDefault) || {}).gljLibId;
        }
        const info = gljLibId === COMPLEMENTARY_LIB 
            ? { gljLibId: null, userID: req.session.sessionUser.id, compilationId: req.session.sessionCompilation._id }
            : { gljLibId, userID: null, compilationId: null };
        ration_glj_facade.getGLJData(info,function (err,datas) {
            if(err){
                result.error=1;
                result.message = err.message;
            }else {
                //多单价字段
                if(req.session.sessionCompilation.priceProperties) datas.priceProperties = req.session.sessionCompilation.priceProperties;
                result.datas = datas;
                result.datas.libData = libData;
            }
            res.json(result);
        });
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
        res.json(result);
    }
}
/* async function getGLJData(req, res) {
    let result={
        error:0
    }
    try {
        let info = await ration_glj_facade.getLibInfo(req);
        ration_glj_facade.getGLJData(info,function (err,datas) {
            if(err){
                result.error=1;
                result.message = err.message;
            }else {
                //多单价字段
                if(req.session.sessionCompilation.priceProperties) datas.priceProperties = req.session.sessionCompilation.priceProperties;
                result.datas = datas;
            }
            res.json(result);
        });
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
        res.json(result);
    }
} */

async function addGLJ(req, res){
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let datas= await ration_glj_facade.addGLJ(data,req.session.sessionCompilation);
        result.data=datas;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function deleteRationGLJ (req,res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let datas= await ration_glj_facade.deleteRationGLJ(data);
        result.data=datas;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function updateProportion(req, res) {
    try {
        const data = JSON.parse(req.body.data);
        const rst = await ration_glj_facade.updateProportion(data.proportionList, data.projectID, data.rationID);
        res.json({ error: 0, data: rst, message: 'success' });
    } catch (err) {
        res.json({ error: 1, data: null, message: err.message });
    }
}

async function replaceGLJ(req, res){
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let rdata= await ration_glj_facade.replaceGLJ(data,req.session.sessionCompilation);
        result.data=rdata;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function mReplaceGLJ(req, res){
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let mresult= await ration_glj_facade.mReplaceGLJ(data,req.session.sessionCompilation);
        result.data=mresult;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async  function updateRationGLJByEdit(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let uresult= await ration_glj_facade.updateRationGLJByEdit(data);
        result.data=uresult;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async  function getGLJClass(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let info = await ration_glj_facade.getLibInfo(req);
        let tresult= await ration_glj_facade.getGLJClass(info,data);
        result.data=tresult;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}