import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';

/**
  * 数据变量记录
*/
@index({ activityId: 1 })

export default class ServerTemp extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服Id

    @prop({ required: true })
    getuiToken: string;				// 个推token
    @prop({ required: true })
    getuiTokenExpireTime: number;				// 个推token过期时间

    //查询当前服务器的数据
    public static async findData(serverId: number) {
        let result: ServerTempModelType = await ServerTempModel.findOne({ serverId }).lean(true);
        return result;
    }

    //更新个推token数据
    public static async updateGetuiData(getuiToken: string, getuiTokenExpireTime: number) {
        let result: ServerTempModelType = await ServerTempModel.findOneAndUpdate({ serverId: 0 },
            { $set: { getuiToken, getuiTokenExpireTime } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //查询个推token数据
    public static async findGetuiData() {
        let result: ServerTempModelType = await ServerTempModel.findOne({ serverId: 0 }).lean(true);
        return result;
    }
}

export const ServerTempModel = getModelForClass(ServerTemp);

export interface ServerTempModelType extends Pick<DocumentType<ServerTemp>, keyof ServerTemp> { }
export type ServerTempModelTypeParam = Partial<ServerTempModelType>; // 将所有字段变成可选项