/**
 * Created by Zhong on 2017/12/21.
 */

import BaseController from '../../common/base/base_controller';
import CompleRationDao from '../models/compleRationModel';

let compleRationDao = new CompleRationDao();
let coeListDAO = require('../../ration_repository/models/coe');
const coeFacade = require('../facades/compleCoeFacade');
const installFacade = require('../facades/compleInstallFacade');
let gljUtil = require('../../../public/gljUtil');

let callback = function (req, res, err, msg, data) {
    res.json({error: err, message: msg, data: data})
};
const libType = {
    complementary: 0,
    std: 1
};


class CompleRationController extends BaseController{
    getRationItems(req, res){
        let data = JSON.parse(req.body.data);
        compleRationDao.getRationItems(data.sectionId, function (err, data) {
            callback(req, res, err, '', data);
        });
    }

    updateRations(req, res){
        let data = JSON.parse(req.body.data);
        compleRationDao.updateRation(req.session.sessionUser.id, req.session.sessionCompilation._id, data.updateData, function (err, data) {
            callback(req, res, err, '', data);
        });
    }

    //更新补充定额价格
    updateRationBasePrc(req, res){
        let data = JSON.parse(req.body.data);
        compleRationDao.updateRationBasePrc(req.session.sessionUser.id, data.basePrcArr, function (err, data) {
            callback(req, res, err, '', data);
        });
    }

    mixUpdateRationItems(req, res){
        let data = JSON.parse(req.body.data);
        compleRationDao.mixUpdateRationItems(req.session.sessionUser.id, req.session.sessionCompilation._id,
            data.rationRepId, data.sectionId, data.updateItems, data.addItems, data.removeIds, function (err, data) {
            callback(req, res, err, '', data);
        });
    }

    //获取所有定额的编号
    getRationsCodes(req, res){
        let data = JSON.parse(req.body.data);
        compleRationDao.getRationsCodes(req.session.sessionUser.id, data.rationRepId, function (err, data) {
            callback(req, res, err, '', data);
        });
    }

    getGljItems(req, res){
        let data = JSON.parse(req.body.data);
        compleRationDao.getGljItems(data.gljLibId, function (err, data) {
            callback(req, res, err, '', data);
        })
    }

    getGljItemsOccupied(req, res){
        let data = JSON.parse(req.body.data);
        compleRationDao.getGljItemsOccupied(data.gljLibId, data.occupation, function (err, data) {
            callback(req, res, err, '', data);
        })
    }

    getGljItemsByIds(req, res){
        let data = JSON.parse(req.body.data);
        compleRationDao.getGljItemsByIds(req.session.sessionUser.id, data.ids, function (err, data) {
            callback(req, res, err, '', data);
        });
    }

    getGljItemsByCodes(req, res){
        let data = JSON.parse(req.body.data);
        compleRationDao.getGljItemsByCodes(req.session.sessionUser.id, req.session.sessionCompilation._id, data.rationRepId, data.gljCodes, function (err, data) {
            callback(req, res, err, '', data);
        });
    }

    async getRationGljItemsBySection(req, res){
        let data = JSON.parse(req.body.data),
            rations = [];
        try {
            let compilation = req.session.sessionCompilation;
            let util = gljUtil;
            //查看编办中有没有重写路径
            if(compilation.overWriteUrl && compilation.overWriteUrl!=""){
                let overWrite = require("../../.."+compilation.overWriteUrl);
                if(overWrite.sortRationGLJ) util = overWrite;
            }
            if (data.type === libType.complementary) {
                rations = await compleRationDao.getCompleRationBySection(req.session.sessionUser.id, data.sectionId,util);
            }  else {
                rations = await compleRationDao.getRationGljItemsBySection(data.sectionId,util);
            }
            callback(req, res, 0, 'success', rations);
        } catch (err) {
            callback(req, res, 1, err, rations);
        }
    }

    async getCoeList(req, res){
        try{
            let coeList = await coeFacade.getComplementaryCoes(req.session.sessionUser.id, req.session.sessionCompilation._id);
            callback(req, res, 0, 'success', coeList);
        } catch (err) {
            callback(req, res, 1, err, null);
        }
    }

    async saveCoeList(req, res){
        try {
            let data = JSON.parse(req.body.data);
            let newDatas = await coeFacade.saveToCoeList(req.session.sessionUser.id, req.session.sessionCompilation._id, data);
            callback(req, res, 0, 'success', newDatas);
        } catch (err) {
            console.log(err);
            callback(req, res, 1, err, null);
        }
    }

    async getCoeItemsByIDs(req, res){
        try{
            let data = JSON.parse(req.body.data);
            let coeList = await coeFacade.getCoeItemsByIDs(data.coeIDs);
            callback(req, res, 0, 'success', coeList);
        } catch (err) {
            callback(req, res, 1, err, null);
        }
    }

    async getCoeItemsByNos(req, res){
        try{
            let data = JSON.parse(req.body.data);
            let coeList = await coeFacade.getCoeItemsByNos(req.session.sessionUser.id, req.session.sessionCompilation._id, data.coeNos);
            callback(req, res, 0, 'success', coeList);
        } catch (err) {
            console.log(err);
            callback(req, res, 1, err, null);
        }
    }

    async getInstallation(req, res){
        try {
            let installData = await installFacade.getInstallation(req.session.sessionUser.id, req.session.sessionCompilation._id);
            callback(req, res, 0, 'success', installData);
        } catch (err) {
            callback(req, res, 1, err, null);
        }
    }

    async updateFeeItem(req, res){
        try {
            let data = JSON.parse(req.body.data);
            await installFacade.updateFeeItem(req.session.sessionUser.id, req.session.sessionCompilation._id, data.updateData);
            callback(req, res, 0, 'success', null);
        } catch (err) {
            callback(req, res, 1, err, null);
        }
    }

    async updateInstallSection(req, res){
        try {
            let data = JSON.parse(req.body.data);
            await installFacade.updateSection(req.session.sessionUser.id, req.session.sessionCompilation._id, data.updateData);
            callback(req, res, 0, 'success', null);
        } catch (err) {
            callback(req, res, 1, err, null);
        }
    }

    async batchUpdateInst(req, res){
        try {
            let data = JSON.parse(req.body.data);
            await installFacade.batchUpdateInst(data.rationSection, data.inst);
            callback(req, res, 0, 'success', null);
        } catch (err) {
            callback(req, res, 1, err, null);
        }
    }
}

export default CompleRationController;