/**
 * Created by Zhong on 2017/8/11.
 */

import BaseController from "../../common/base/base_controller";
import {GljMapDao} from "../models/gljMapModel";
import CompilationModel from "../../users/models/compilation_model";
let gljMapDao = new GljMapDao();

let callback = function(req, res, err, message, data){
    res.json({error: err, message: message, data: data});
};
class GljMapController extends BaseController{
    //该费用定额下补充人材机分类树模板数据条数
    async classTemplateCount(req, res) {
        try {
            let count = await gljMapDao.classTemplateCount(req.params.compilationId);
            callback(req, res, 0, 'success', {count});
        } catch (err) {
            callback(req, res, 1, err, {count: 0});
        }
    }
    //将该标准人材机库的分类树设置为该费用定额下补充人材机分类树的模板
    async initClassTemplate(req, res) {
        try {
            let data = JSON.parse(req.body.data);
            await gljMapDao.initClassTemplate(data.gljLibId, data.compilationId);
            callback(req, res, 0, 'success', null);
        } catch (err) {
            callback(req, res, 1, err, null);
        }
    }
    async getCompilationList(req, res){
        try{
            let compilationModel = new CompilationModel(), rst = [];
            let compilationList = await compilationModel.getCompilationList();
            if(compilationList.length <= 0){
                throw '没有数据';
            }
            else{

                compilationList.forEach(function (compilation) {
                    rst.push({_id: compilation._id, name: compilation.name});
                })
                callback(req, res, false, '', rst);
            }
        }
        catch(err) {
            callback(req, res, err, '没有数据', null);
        }
    }
    getGljLib(req, res){
        let libId = req.body.libId;
        gljMapDao.getGljLib(libId, function (err, message, data) {
            callback(req, res, err, message, data);
        })
    }
    getAllGljLib(req, res){
        gljMapDao.getAllGljLib(function (err, message, data) {
            callback(req, res, err, message, data);
        })
    }
    createGljLib(req, res){
        let gljLibObj = JSON.parse(req.body.gljLibObj);
        gljMapDao.createGljLib(gljLibObj, function (err, message, data) {
            callback(req, res, err, message, data);
        })
    }
    renameGljLib(req, res){
        let oprtor = req.body.oprtor,
            renameObj = JSON.parse(req.body.renameObj);
        gljMapDao.renameGljLib(oprtor, renameObj, function (err, message) {
            callback(req, res, err, message, null);
        })
    }
    removeGljLib(req, res){
        let oprtor = req.body.oprtor,
            libId = req.body.libId;
        gljMapDao.removeGljLib(oprtor, libId, function (err, message) {
            callback(req, res, err, message, null);
        });
    }
}

export default GljMapController;
