'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2018/5/29
 * @version
 */
import BaseController from '../../common/base/base_controller';
const _ = require('lodash');
const billsGuidanceFacade = require('../facade/facades');
let callback = function (req, res, err, msg, data) {
    res.json({error: err, message: msg, data: data});
};

class BillsGuideLibController extends BaseController{
    //获取清单指引库和该库引用的清单
    async getLibWithBills(req, res){
        try{
            const { libID, isGuidanceLib } = JSON.parse(req.body.data);
            const rst = await billsGuidanceFacade.getLibWithBills(libID, isGuidanceLib);
            callback(req, res, 0, '', rst);
        }
        catch(err){
            console.log(err);
            callback(req, res, 1, err, null);
        }
    }

    async getItemsByBills(req, res){
        try{
            let data = JSON.parse(req.body.data);
            let items = await billsGuidanceFacade.getItemsBybills(data.guidanceLibID, data.billsID);
            callback(req, res, 0, '', items);
        }
        catch(err){
            callback(req, res, 1, err, null);
        }
    }
    //根据清单编号获取项目指引
    async getItemsByCode(req, res){
        try{
            let data = JSON.parse(req.body.data);
            let items = await billsGuidanceFacade.getItemsByCode(data.guidanceLibID, data.code);
            callback(req, res, 0, '', items);
        }
        catch(err){
            callback(req, res, 1, err, null);
        }
    }

    async updateItems(req, res){
        try{
            let data = JSON.parse(req.body.data);
            let updateDatas = data.updateDatas;
            await billsGuidanceFacade.updateItems(updateDatas);
            callback(req, res, 0, '', null);
        }
        catch(err){
            callback(req, res, 1, err, null);
        }
    }
}

export default BillsGuideLibController;