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

@index({ guildCode: 1, refTime: 1   })
export default class GuildActive extends BaseModel {

    @prop({ required: true })
    guildCode: string;       // 军团

    @prop({ required: true })
    refTime: Date;       // 刷新时间

    @prop({ required: true, default: 0 })
    active: number;        // 活跃

    // 更新
    public static async addActive(guildCode: string, refTime: Date, active: number) {
        let rec: GuildActiveType = await GuildActiveModel.findOneAndUpdate({ guildCode, refTime }, { $inc: { active } }, { new: true, upsert: true }).lean();
        return rec;
    }

    public static async getActive(guildCode: string, refTime: Date) {
        let rec: GuildActiveType = await GuildActiveModel.findOne({ guildCode, refTime }).lean();
        return rec;
    }
}

export const GuildActiveModel = getModelForClass(GuildActive);

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