/**
 * 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;

module.exports={
    createRationGLJ:createRationGLJ,
    testGetQuantify:testGetQuantify,
    getGLJData:getGLJData,
    getGLJDataByCodes:getGLJDataByCodes,
    addGLJ:addGLJ,
    replaceGLJ:replaceGLJ,
    mReplaceGLJ:mReplaceGLJ,
    updateRationGLJByEdit:updateRationGLJByEdit,
    getGLJClass:getGLJClass
}
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 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 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);
}