/**
 * Created by Tony on 2017/5/2.
 */

var rationItem = require('../models/ration_item').Dao;
var callback = function(req, res, err, message, data){
    res.json({error: err, message: message, data: data});
};

module.exports = {
    getRationItemsBySection: function(req, res){
        var sectionId = req.body.sectionID;
        var rationRepId = req.body.rationRepId;
        rationItem.getRationItemsBySection(rationRepId, sectionId, function(err, message, rst){
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, rst);
            }
        });
    },
    getRationGljItemsBySection: async function(req, res){
        var sectionId = req.body.sectionID;
        rationItem.getRationGljItemsBySection(sectionId, function(err, message, rst){
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, rst);
            }
        });
    },
    mixUpdateRationItems: function(req, res){
        var sectionId = req.body.sectionID,
            rationLibId = req.body.rationLibId,
            updateItems = JSON.parse(req.body.updateItems),
            addItems = JSON.parse(req.body.addItems),
            removeIds = JSON.parse(req.body.removeIds);
        rationItem.mixUpdateRationItems(rationLibId, sectionId, updateItems, addItems, removeIds, function(err, message, rst){
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, rst);
            }
        });
    },
    removeRationItems: function(req, res){
        var rIds = JSON.parse(req.body.updateItems);
        if (rIds && rIds.length > 0) {
            rationItem.removeRationItems(rIds, function(err, message, rst){
                if (err) {
                    callback(req, res, err, message, null);
                } else {
                    callback(req, res, err, message, rst);
                }
            });
        }
    }
}