/**
 * Created by Mai on 2017/6/5.
 */
var rationItem = require('../models/ration_item');
import BaseController from "../../common/base/base_controller";
var callback = function(req, res, err, message, data){
    res.json({error: err, message: message, data: data});
};

class SearchController extends BaseController{
    async getRationItem (req, res) {
        try {
            let data = JSON.parse(req.body.data);
            let rId = data.rationLibId, code = data.code;
            let ration = await rationItem.getRationItem(rId, code);
            callback(req, res, 0, '', ration);
        } catch (err) {
            callback(req, res, 1, err, null);
        }
    }
    findRation (req, res) {
        var rId = req.body.rationLibId, keyword = req.body.keyword;
        rationItem.findRation(rId, keyword, function (err, message, rst) {
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, 0, '', rst);
            }
        });
    }
}

export default SearchController;

/*
module.exports = {
    getRationItem: function (req, res) {
        var rId = req.body.rationLibId, code = req.body.code;
        var rationData = {}
        rationItem.getRationItem(rId, code).then(function (result) {
            rationData = result._doc;
            callback(req, res, null, '', rationData);
        }).catch(function (err, message) {
            callback(req, res, err, message, null);
        })
    },
    findRation: function (req, res) {
        var rId = req.body.rationLibId, keyword = req.body.keyword;
        rationItem.findRation(rId, keyword, function (err, message, rst) {
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, 0, '', rst);
            }
        });
    }
};*/
