/**
 * Created by Tony on 2017/5/5.
 */
var gljRepository = require("../models/glj_repository");

var callback = function(req,res,err,message, data){
    res.json({error: err, message: message, data: data});
}
module.exports ={
    getGljTree: function(req,res){
        var rationLibId = req.body.rationLibId;
        gljRepository.getGljTypes(rationLibId,function(err,data){
            callback(req,res,err, 'Get Tree', data)
        });
    },
    createNewGljTypeNode: function(req, res) {
        var repId = req.body.repositoryId;
        var lastNodeId = req.body.lastNodeId;
        var nodeData = JSON.parse(req.body.rawNodeData);
        gljRepository.createNewNode(repId, lastNodeId, nodeData, function(err, msg, data){
            callback(req,res,err,msg, data)
        });
    },
    updateGljNodes: function(req, res) {
        var nodes = JSON.parse(req.body.nodes);
        gljRepository.updateNodes(nodes, function(err,results){
            callback(req,res, err, results)
        });
    },
    deleteGljNodes: function(req, res) {
        var nodes = JSON.parse(req.body.nodes);
        var preNodeId = req.body.preNodeId;
        var preNodeNextId = req.body.preNodeNextId;
        gljRepository.removeNodes(nodes, preNodeId, preNodeNextId, function(err,results){
            callback(req,res, err, results)
        });
    },
    getGljItems: function(req, res) {
        var repId = req.body.repositoryId,
            gljType = req.body.type,
            gljCode = req.body.code;
        if (gljCode) {
            gljRepository.getGljItem(repId, gljCode, function(err, data){
                callback(req,res,err,'Get Items', data)
            });
        } else if (gljType) {
            gljRepository.getGljItemByType(repId, gljType, function(err, data){
                callback(req,res,err,'Get Types', data)
            });
        } else {
            gljRepository.getGljItemsByRep(repId, function(err, data){
                callback(req,res,err,'Get Items',data)
            });
        }
    },
    getGljItemsByIds: function(req, res) {
        var gljIds = JSON.parse(req.body.gljIds);
        gljRepository.getGljItems(gljIds, function(err, data){
            callback(req,res,err,'Get Items',data)
        });
    },
    getGljItemsByCodes: function(req, res) {
        var gljCodes = JSON.parse(req.body.gljCodes),
            repId = req.body.repId;
        gljRepository.getGljItemsByCode(repId, gljCodes, function(err, data){
            callback(req,res,err,'Get Items',data)
        });
    },
    mixUpdateGljItems: function(req, res){
        var repId = req.body.repositoryId,
            updateItems = JSON.parse(req.body.updateItems),
            addItems = JSON.parse(req.body.addItems),
            removeIds = JSON.parse(req.body.removeIds);
        gljRepository.mixUpdateGljItems(repId, updateItems, addItems, removeIds, function(err, message, rst){
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, rst);
            }
        });
    }

}