/**
 * 前台用户相关数据模型
 *
 * @author CaiAoLin
 * @date 2017/6/12
 * @version
 */
import mongoose from "mongoose";
import BaseModel from "../../common/base/base_model";

class UserModel extends BaseModel {

    /**
     * 企业所在地区
     *
     * @var {object}
     */
    province = ['广东省', '北京市'];

    /**
     * 企业类型
     *
     * @var
     */
    companyType = ['设计', '施工'];

    /**
     * 企业规模
     *
     * @var
     */
    companyScale = ['1-20', '20-50', '50-100', '100+'];

    /**
     * 最近天数
     *
     * @var
     */
    dayMsg = ['所有', '最近24小时', '最近3天', '最近7天', '最近30天'];

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

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

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

        return userList;
    }

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

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

        let keyword = request.query.keyword;
        if (keyword !== '' && keyword !== undefined) {
            condition.$or = [{real_name : {$regex: keyword}},{email : {$regex: keyword}},{mobile : {$regex: keyword}},{company : {$regex: keyword}}];
        }

        return condition;
    }

    /**
     * 获取时间戳区间
     *
     * @return {Object}
     */
    getTimestamp(type) {
        let startTime = '';
        switch (type) {
            case 1 :
                startTime = Date.parse(new Date())-86400*1000;
                break;
            case 2 :
                startTime = Date.parse(new Date())-86400*1000*3;
                break;
            case 3 :
                startTime = Date.parse(new Date())-86400*1000*7;
                break;
            case 4 :
                startTime = Date.parse(new Date())-86400*1000*30;
                break;
            default :
                break;
        }
        let endTime =  Date.parse(new Date());
        return startTime === '' ? '' : {'$gte': startTime, '$lt': endTime};
    }

    /**
     * 获取daymsg
     *
     */
    getDayMsg(index){
        return this.dayMsg[index];
    }

}

export default UserModel;