import BaseModel from './BaseModel'; import { index, getModelForClass, prop, DocumentType, Ref, mongoose } from '@typegoose/typegoose'; import Guild from './Guild'; import Role, { } from './Role'; /** * 活动系统 - 限时排行榜 */ @index({ activityId: 1 }) export default class Activity_Time_Limit_Rank extends BaseModel { @prop({ required: true }) serverId: number; // 区Id @prop({ required: true }) activityId: number; // 活动Id @prop({ required: false }) guildCode: string; // 军团 @prop({ ref: 'Guild', type: mongoose.Schema.Types.ObjectId }) guild: Ref; @prop({ required: false }) roleId: string; // 军团 @prop({ ref: 'Role', type: mongoose.Schema.Types.ObjectId }) role: Ref; @prop({ required: true }) score: number; // 活动期间内的积分 @prop({ required: true }) time: number; // 更新时间 public static async addGuildScore(serverId: number, activityId: number, rankType: number, guildCode: string, fund: number, guild?: string) { let rec: ActivityTimeLimitRankModelType = await ActivityTimeLimitRankModel.findOneAndUpdate({ serverId, activityId, guildCode }, { $setOnInsert: { guild }, $set: { time: Date.now(), rankType }, $inc: { score: fund } }, { new: true, upsert: true }).lean(); return rec; } public static async findByCode(serverId: number, activityId: number, guildCode: string) { let rec: ActivityTimeLimitRankModelType = await ActivityTimeLimitRankModel.findOne({ serverId, activityId, guildCode }).lean(); return rec; } public static async insertRanks(insertInfos: ActivityTimeLimitRankModelTypeParam[]) { await ActivityTimeLimitRankModel.insertMany(insertInfos); return insertInfos; } public static async getRank(serverId: number, activityId: number) { let rec: ActivityTimeLimitRankModelType[] = await ActivityTimeLimitRankModel.find({ serverId, activityId }) .populate('role').populate('guild') .sort({ score: 1, time: -1 }).lean(); return rec; } } export const ActivityTimeLimitRankModel = getModelForClass(Activity_Time_Limit_Rank); export interface ActivityTimeLimitRankModelType extends Pick, keyof Activity_Time_Limit_Rank> { } export type ActivityTimeLimitRankModelTypeParam = Partial; // 将所有字段变成可选项