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

import BaseController from "../../common/base/base_controller";
import stdgljutil  from "../../../public/cache/std_glj_type_util";
import GljDao from "../models/gljModel";
import rationItemDao from "../../ration_repository/models/ration_item";

let gljDao = new GljDao();
let callback = function(req, res, err, message, data){
    res.json({error: err, message: message, data: data});
};

class GljController 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){
        let data = JSON.parse(req.body.data);
        gljDao.getGljTypes(data.gljLibId, function(err,data){
            callback(req,res,err, 'Get Tree', data)
        });
    }
    updateRationBasePrc(req, res){
        let basePrcArr = JSON.parse(req.body.basePrcArr);
        rationItemDao.updateRationBasePrc(basePrcArr, function (err, message) {
            if(err){
                callback(req, res, err, message, null);
            }
            else{
                callback(req, res, err, message, null);
            }
        });
    }

    getRationGljIds(req, res){
        let rationLibs = req.body.rationLibs;
        gljDao.getRationGljIds(rationLibs, function (err, msg, data) {
            callback(req, res, err, msg, data);
        })
    }
    createNewGljTypeNode(req, res) {
        let repId = req.body.repositoryId;
        let lastNodeId = req.body.lastNodeId;
        let lastOpr = req.body.lastOpr;
        let nodeData = JSON.parse(req.body.rawNodeData);
        gljDao.createNewNode(repId, lastOpr, lastNodeId, nodeData, function(err, msg, data){
            callback(req,res,err,msg, data)
        });
    }
    updateGljNodes(req, res) {
        let nodes = JSON.parse(req.body.nodes);
        let repId = req.body.repId,
            lastOpr = req.body.lastOpr;
        gljDao.updateNodes(repId, lastOpr, nodes, function(err,results){
            callback(req,res, err, results)
        });
    }
    deleteGljNodes(req, res) {
        let nodes = JSON.parse(req.body.nodes);
        let preNodeId = req.body.preNodeId;
        let preNodeNextId = req.body.preNodeNextId;
        let repId = req.body.repId, lastOpr = req.body.lastOpr;
        gljDao.removeNodes(repId, lastOpr, nodes, preNodeId, preNodeNextId, function(err,results){
            callback(req,res, err, results)
        });
    }
    updateNodes(req, res){
        let data = JSON.parse(req.body.data);
        gljDao.updateNodes(data.updateData, data.lastOpr, function (err, data) {
            callback(req, res, err, '', data);
        });
    }
    getGljItems(req, res) {
        let repId = req.body.repositoryId,
            gljType = req.body.type,
            gljCode = req.body.code;
        if (gljCode) {
            gljDao.getGljItem(repId, gljCode, function(err, data){
                callback(req,res,err,'Get Items', data)
            });
        } else if (gljType) {
            gljDao.getGljItemByType(repId, gljType, function(err, data){
                callback(req,res,err,'Get Types', data)
            });
        } else {
            gljDao.getGljItemsByRep(repId, function(err, data){
                callback(req,res,err,'Get Items',data)
            });
        }
    }
    getGljItemsByIds(req, res) {
        let gljIds = JSON.parse(req.body.gljIds);
        gljDao.getGljItems(gljIds, function(err, data){
            callback(req,res,err,'Get Items',data)
        });
    }
    getGljItemsByCodes(req, res) {
        let gljCodes = JSON.parse(req.body.gljCodes),
            repId = req.body.repId;
        gljDao.getGljItemsByCode(repId, gljCodes, function(err, data){
            callback(req,res,err,'Get Items',data)
        });
    }
    updateComponent(req, res){
        let libId = req.body.libId,
            updateArr = req.body.updateArr,
            oprtor = req.body.oprtor;
        gljDao.updateComponent(libId, oprtor, updateArr, function (err, message, rst) {
            callback(req, res, err, message, rst);
        })
    }
    mixUpdateGljItems(req, res){
        let 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;
        gljDao.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);
            }
        });
    }
    getGljItemsOccupied(req, res){
        let repId = req.body.repId,
            occupation = req.body.occupation;
        gljDao.getGljItemsOccupied(repId, occupation, function (err, message, rst) {
            callback(req, res, err, message, rst);
        })
    }
}

export default GljController;
