/**
 * 消息通知相关控制器
 *
 * @author CaiAoLin
 * @date 2017/9/25
 * @version
 */
const UserMessageModel = require("../models/user_message_model");

class MessageController {

    /**
     * 初始化函数
     * @param {object} request
     * @param {object} response
     * @param {Object} next
     * @return {void|Object}
     */
    init(request, response, next) {
        let responseData = {
            err: 0,
            msg: ''
        };
        try {
            // 所有接口都需要保证访问者是已登录状态
            let sessionUserData = request.session.sessionUser;
            if (sessionUserData === undefined || sessionUserData.id === undefined || sessionUserData.id === '') {
                throw '用户未登录';
            }
            next();
        } catch (error) {
            responseData.err = 1;
            responseData.msg = error;
            return response.json();
        }

    }

    /**
     * 获取消息列表数据
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async list(request, response) {
        let responseData = {
            err: 0,
            msg: ''
        };
        try {
            let sessionUserData = request.session.sessionUser;
            let userMessageModel = new UserMessageModel();
            let messageList = await userMessageModel.getList(sessionUserData.id, 15, {is_read: 1});
            if (messageList === null) {
                throw '获取数据失败!';
            }
            responseData.data = messageList;
        } catch (error) {
            console.log(error);
            responseData.err = 1;
            responseData.msg = error;
        }

        response.json(responseData);
    }

    /**
     * 获取详细的消息数据
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async detail(request, response) {
        let responseData = {
            err: 0,
            msg: ''
        };
        try {
            let sessionUserData = request.session.sessionUser;
            let id = request.body.id;
            let userMessageModel = new UserMessageModel();
            let messageData = await userMessageModel.getDetail(sessionUserData.id, id);
            if (messageData === null) {
                throw '获取数据失败!';
            }
            responseData.data = messageData;
        } catch (error) {
            console.log(error);
            responseData.err = 1;
            responseData.msg = error;
        }

        response.json(responseData);
    }

    /**
     *
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async allRead(request, response) {
        let responseData = {
            err: 0,
            msg: ''
        };
        try {
            let sessionUserData = request.session.sessionUser;
            let userMessageModel = new UserMessageModel();
            let result = await userMessageModel.setAllRead(sessionUserData.id);
            if (result === null) {
                throw '设置失败!';
            }
        } catch (error) {
            responseData.err = 1;
            responseData.msg = error;
        }

        response.json(responseData);
    }
}

module.exports = MessageController;
