/**
 * 管委会用户表
 * 没有权限 2023-02-06 确定此需求
 */

import {Schema} from 'mongoose';
import { baseDB } from '../../db/mongo/dbInit';

const guanWeiHuiSchema = new Schema({
    name:String,//名称
    loginId: {type:String, index:true},//登录账号
    pwd:String,//密码
    token:String,
    tokenMs:Number
});

var guanWeiHuiModel;
export function initModel(){
    guanWeiHuiModel = baseDB.model('guanWeiHui', guanWeiHuiSchema);
    guanWeiHuiModel.selectOnceData = async function (paramater:object) {
        let selectInfo = await guanWeiHuiModel.findOne(paramater).exec();
        if (selectInfo) {
            if (!selectInfo.runSave) {
                selectInfo.runSave = selectInfo.save;
                selectInfo.save = save.bind(selectInfo)
            }
        }
        return selectInfo;
    }
}
export async function save(throwError=false) {
    if (!this.isModified()) return;
    await this.runSave({validateBeforeSave:false}).catch(err=>{
        console.log(err);
    });
}


export async function findGuanWeiHuiUserInfoByLoginId(loginId:string) {
    return await guanWeiHuiModel.findOne({loginId}).exec();
}