/**
 * 用户消息业务逻辑
 *
 * @author CaiAoLin
 * @date 2017/9/22
 * @version
 */
const BaseModel = require('../../common/base/base_model');
const MessageModel = require('./message_model');
const mongoose = require('mongoose');

class UserMessageModel extends BaseModel {

    /**
     * 构造函数
     *
     * @return {void}
     */
    constructor() {
        let parent = super();
        parent.model = mongoose.model('user_message');
        parent.init();
    }

    /**
     * 初始化用户消息
     *
     * @param {String} userId
     * @return {boolean}
     */
    async initMessage(userId) {
        userId = userId.toString();
        let result = true;
        // 获取最后一条消息的信息
        let option = {pageSize: 1, offset: 0};
        option.sort = {create_time: -1};
        let lastMessageData = await this.db.find({user_id: userId}, null, option);
        lastMessageData = lastMessageData === null || lastMessageData.length <= 0 ? null : lastMessageData[0];
        let lastTime = lastMessageData === null ? 0 : lastMessageData.create_time;

        // 查找已发布的数据
        let messageModel = new MessageModel();
        let messageData = await messageModel.getListByTime(lastTime);
        if (messageData === null) {
            return result;
        }

        // 组合新增数据
        let insertData = [];
        let currentTime = new Date().getTime();
        for(let message of messageData) {
            let tmp = {
                message: message._id,
                user_id: userId,
                create_time: currentTime
            };
            insertData.push(tmp);
        }

        result = await this.db.create(insertData);
        return result;
    }

    /**
     * 获取列表
     *
     * @param {String} userId
     * @param {Number} pageSize
     * @param {Object} sort
     * @return {promise}
     */
    async getList(userId, pageSize = 15, sort = {}) {
        let option = {pageSize: pageSize, offset: 0};
        option.sort = sort;

        let messageList = await this.db.findWithPopulate({user_id: userId, is_delete: 0}, null, option, 'message');
        messageList = messageList.length > 0 ? messageList : [];

        return messageList;
    }

    /**
     * 获取详细数据
     *
     * @param {String} userId
     * @param {String} id
     * @return {Promise}
     */
    async getDetail(userId, id) {
        let result = {};
        let messageList = await this.db.findWithPopulate({user_id: userId, is_delete: 0, _id: id}, null, null, 'message');
        if (messageList === null) {
            return result;
        }
        result = messageList.length > 0 ? messageList[0] : result;
        // 如果是未读数据则更改状态
        if (result.is_read === 0) {
            let updateData = {is_read: 1};
            await this.db.update({_id: id}, updateData);
        }

        return result;
    }

    /**
     * 设置全部已读
     *
     * @param {String} userId
     * @return {Promise}
     */
    async setAllRead(userId) {
        let updateData = {is_read: 1};
        return await this.db.update({user_id: userId}, updateData)
    }

}

module.exports = UserMessageModel;