/**
 * 用户数据结构
 *
 * @author CaiAoLin
 * @date 2017/6/28
 * @version
 */
let mongoose = require("mongoose");
let Schema = mongoose.Schema;

// 表名
let collectionName = 'user';

const invalidSchema = new Schema({
    invalid: Boolean,
    invalidDate: String,
    invalidBy: String   //管理者名
});

const versionSchema = new Schema({
    compilationId: String,
    activateCode: String,
    activatedDate: String,
    activatedBy: String,
    invalidInfo: [invalidSchema]
});

let upgrade = mongoose.Schema({
    compilationID:String,//编办ID
    upgrade_time:Number,
    isUpgrade:Boolean,
    remark:String//描述：广东办刘飞 2018-06-17 启用/关闭
}, { _id: false })

const userdList = mongoose.Schema({
    compilationId: String
}, {_id: false});

// 表结构
let schema = {
    ssoId: {
        type: Number,
        unique: true
    },
    username: String,
    email: String,
    mobile: {
        type: String,
        // unique: true
    },
    qq: {
        type: String,
        default: ''
    },
    real_name: {
        type: String,
        default: ''
    },
    company: {
        type: String,
        default: ''
    },
    province: {
        type: Number,
        default: -1
    },
    version: {
        type: [versionSchema],
        default: []
    },
    company_type: {
        type: Number,
        default: -1,
    },
    company_scale: {
        type: Number,
        default: -1
    },
    // 最后登录时间
    latest_login: {
        type: Number,
        default: 0
    },
    //最近使用编办
    latest_used:String,
    create_time: Number,
    upgrade_list:[upgrade],
    user_type:{
        type:String,
        default:'normal'//  normal : 普通用户，test:测试用户
    },
    //使用过的费用定额，主要目的拷贝用户在该费用定额下的一些数据模板 eg:用户第一次进入该费用定额的补充定额库时，拷贝补充定额的章节树
    used_list: {
        type: [userdList],
        default: []
    },
    // 联系人
    contacts: {
        type: Array,
        default: []
    },
    // 是否邮箱已通过验证
    isUserActive: Number,
    // 是否只允许短信登录
    isSmsLogin: {
        type: Number,
        default: 0
    },
    // 登录异常短信通知
    isLoginValid: {
        type: Number,
        default: 0
    },
    welcomeShowTime:String,
    token: String
};
mongoose.model(collectionName, new Schema(schema, {versionKey: false}));
