/**
 * 个人设置业务逻辑
 *
 * @author CaiAoLin
 * @date 2017/7/27
 * @version
 */
import BaseModel from "../../common/base/base_model";
import SettingType from "../../common/const/setting_type_const";
import mongoose from "mongoose";

class SettingModel extends BaseModel {

    /**
     * 偏好设置默认
     *
     * @var {Object}
     */
    preferenceDefault = {
        login_ask: 1,
        select_version: -1
    };

    /**
     * 设置场景
     *
     * @param {string} scene
     * @return {void}
     */
    setScene(scene = '') {
        switch (scene) {
            // 新增数据的验证规则
            case 'add':
                this.model.schema.path('type').required(true);
                this.model.schema.path('data').required(true);
                this.model.schema.path('user_id').required(true);
                break;
            case 'update':
                this.model.schema.path('data').required(true);
                break;
        }
    }

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

    /**
     * 获取设置信息
     *
     * @param {String} userId
     * @param {Number} type
     * @return {Promise}
     */
    async getSetting(userId, type) {
        let condition = {
            user_id: userId,
            type: type
        };

        let settingData = await this.findDataByCondition(condition);
        return settingData !== null && Object.keys(settingData).length > 0 ?
            settingData.data : {};
    }

    /**
     * 保存设置
     *
     * @param {String} userId
     * @param {Number} type
     * @param {Object} data
     * @return {Promise}
     */
    async save(userId, type, data) {
        let result = false;
        if (typeof data === 'string' && data === '') {
            return result;
        }
        if (typeof data === 'object' && Object.keys(data).length <= 0) {
            return result;
        }

        // 查找是否有对应数据
        let condition = {
            user_id: userId,
            type: type
        };
        let counter = await this.db.count(condition);
        let saveData = {};

        if (counter > 0) {
            // 存在则更新
            saveData = {
                data: data
            };
            this.setScene('add');
            let updateResult = await this.db.update(condition, saveData);
            result = updateResult.ok === 1;
        } else {
            saveData = {
                user_id: userId,
                type: type,
                data: data,
                create_time: new Date().getTime()
            };
            this.setScene('update');
            let insertResult = await this.db.create(saveData);
            result = Object.keys(insertResult).length > 0;
        }

        return result;
    }

    /**
     * 获取个人偏好设置
     *
     * @param {String} userId
     * @return {Promise}
     */
    async getPreferenceSetting(userId) {
        let setting = await this.getSetting(userId, SettingType.PREFERENCE);
        setting = Object.keys(setting).length <= 0 ? this.preferenceDefault : setting;

        return setting;
    }

}

export default SettingModel;
