/**
 * 消息业务逻辑
 *
 * @author CaiAoLin
 * @date 2017/9/21
 * @version
 */
import mongoose from "mongoose";
import BaseModel from "../../common/base/base_model";

class MessageModel extends BaseModel {

    /**
     * 用户通知类型
     *
     * @var {Number}
     */
    USER = 1;

    /**
     * 内部通知类型
     *
     * @var {Number}
     */
    SYSTEM = 2;

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

    /**
     * 设置场景
     *
     * @param {string} scene
     * @return {void}
     */
    setScene(scene = '') {
        switch (scene) {
            // 新增
            case 'add':
                this.model.schema.path('message_type').required(true);
                this.model.schema.path('title').required(true);
                this.model.schema.path('content').required(true);
                break;
            // 修改
            case 'modify':
                this.model.schema.path('title').required(true);
                this.model.schema.path('content').required(true);
                break;
        }
    }

    /**
     * 获取列表
     *
     * @param {object} condition
     * @param {number} page
     * @param {Number} pageSize
     * @param {Object} sort
     * @return {promise}
     */
    async getList(condition = null, page = 1, pageSize = 30, sort = {}) {
        page = parseInt(page);
        page = page <= 1 ? 1 : page;
        let option = {pageSize: pageSize, offset: parseInt((page - 1) * pageSize)};
        option.sort = sort;

        let messageList = await this.db.find(condition, null, option);
        messageList = messageList.length > 0 ? messageList : [];

        return messageList;
    }

    /**
     * 新增消息
     *
     * @param {Object} data
     * @param {String} creator
     * @return {Promise}
     */
    async addData(data, creator) {
        let result = false;

        if (Object.keys(data).length <= 0 || creator === '') {
            return result;
        }

        // 设置场景
        this.setScene('add');

        data.create_time = new Date().getTime();
        data.creator = creator;

        result = await this.db.create(data);

        return result;
    }

    /**
     * 修改消息
     *
     * @param {Number} id
     * @param {Object} data
     * @return {Promise}
     */
    async modifyData(id, data) {
        let messageData = await this.findDataByCondition({_id: id});
        if (messageData === null) {
            throw '不存在对应数据';
        }

        this.setScene('modify');
        data.update_time = new Date().getTime();
        return await this.updateById(id, data);
    }

    /**
     * 发布消息
     *
     * @param {String} id
     * @param {String} releaseUser
     * @return {Promise}
     */
    async release(id, releaseUser) {
        let data = {
            status: 1,
            release_time: new Date().getTime(),
            release_user: releaseUser
        };

        return await this.updateById(id, data);
    }

    /**
     * 获取过滤条件
     *
     * @return {Object}
     */
    getFilterCondition(request) {
        let condition = {};
        let status = request.query.status;
        status = status !== '' && status !== undefined ? parseInt(status) : '';
        if (status !== '') {
            condition.status = status;
        }

        let year = request.query.year;
        let month = request.query.month;
        let startMonth = 1;
        let endMonth = 12;
        let lastDate = 31;

        if (month !== undefined && month !== '') {
            year = year !== undefined && year !== '' ? year : new Date().getYear();
            let datetime = new Date(year, month, 0);
            lastDate = datetime.getDate();
            startMonth = month;
            endMonth = month;
        }

        if (year !== undefined || month !== undefined) {
            let startTime = Date.parse(new Date(year + '-'+ startMonth +'-1 00:00:00'));
            let endTime =  Date.parse(new Date(year + '-'+ endMonth +'-'+ lastDate +' 23:59:59'));
            condition.create_time = {'$gte': startTime, '$lt': endTime};
        }

        let keyword = request.query.keyword;
        if (keyword !== '' && keyword !== undefined) {
            condition.title = keyword;
        }

        return condition;
    }
}

export default MessageModel;