/**
 * Created by Tony on 2017/5/5.
 */
var gljRepository = require("../models/glj_repository");
let stdgljutil = require("../../../public/cache/std_glj_type_util");
import BaseController from "../../common/base/base_controller";

var callback = function(req,res,err,message, data){
    res.json({error: err, message: message, data: data});
}

class RepositoryGljController extends BaseController{
    getGljDistType (req, res) {
        let gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
        if(gljDistTypeCache.length >0 ){
            callback(req, res, null, '', gljDistTypeCache);
        }
        else {
            callback(req, res, 1, 'Error', null);
        }
    }
    getGljTree(req,res){
        var gljLibID = req.body.gljLibID;
        gljRepository.getGljTypes(gljLibID,function(err,data){
            callback(req,res,err, 'Get Tree', data)
        });
    }
    createNewGljTypeNode(req, res) {
        var repId = req.body.repositoryId;
        var lastNodeId = req.body.lastNodeId;
        let lastOpr = req.body.lastOpr;
        var nodeData = JSON.parse(req.body.rawNodeData);
        gljRepository.createNewNode(repId, lastOpr, lastNodeId, nodeData, function(err, msg, data){
            callback(req,res,err,msg, data)
        });
    }
    updateGljNodes(req, res) {
        var nodes = JSON.parse(req.body.nodes);
        let repId = req.body.repId,
            lastOpr = req.body.lastOpr;
        gljRepository.updateNodes(repId, lastOpr, nodes, function(err,results){
            callback(req,res, err, results)
        });
    }
    deleteGljNodes(req, res) {
        var nodes = JSON.parse(req.body.nodes);
        var preNodeId = req.body.preNodeId;
        var preNodeNextId = req.body.preNodeNextId;
        let repId = req.body.repId, lastOpr = req.body.lastOpr;
        gljRepository.removeNodes(repId, lastOpr, nodes, preNodeId, preNodeNextId, function(err,results){
            callback(req,res, err, results)
        });
    }
    getGljItems(req, res) {
        var repId = req.body.gljLibID,
            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(req, res) {
        var gljIds = JSON.parse(req.body.gljIds);
        gljRepository.getGljItems(gljIds, function(err, data){
            callback(req,res,err,'Get Items',data)
        });
    }
    getGljItemsByCodes(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(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),
            lastOpr = req.body.lastOpr;
        gljRepository.mixUpdateGljItems(repId, lastOpr, updateItems, addItems, removeIds, function(err, message, rst){
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, rst);
            }
        });
    }
}

export default RepositoryGljController;

/*
module.exports ={
    getGljDistType: function (req, res) {
        let gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
        if(gljDistTypeCache.length >0 ){
            callback(req, res, null, '', gljDistTypeCache);
        }
        else {
            callback(req, res, 1, 'Error', null);
        }
    },
    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);
            }
        });
    }
}*/
