Files
ZYZ/shared/db/ActivityPopUpShop.ts
2021-05-19 21:02:26 +08:00

81 lines
2.9 KiB
TypeScript

import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
/**
* 购买记录
*/
export class BuyRecord {
@prop({ required: true })
id: number; //物品id标识
@prop({ required: true })
time: Date; //购买时间
}
/**
* 活动系统 - 弹出商店
*/
@index({ roleId: 1 })
export default class ActivityPopUpShop extends BaseModel {
@prop({ required: true })
serverId: number; // 服id
@prop({ required: true })
activityId: number; // 活动Id
@prop({ required: true })
roleId: string; // 用户Id
@prop({ required: true })
beginTime: Date; // 开始时间
@prop({ required: true })
endTime: Date; // 结束时间
@prop({ required: true })
taskId: number; // 任务id
@prop({ required: true })
records: BuyRecord[]; // 购买记录
//购买奖励的记录
public static async addRecord(serverId: number, activityId: number, roleId: string, taskId: number, id: number) {
let nowDate = new Date();
let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate({
serverId, roleId, activityId, taskId,
beginTime: { $lt: nowDate }, endTime: { $gt: nowDate }
},
{ $push: { records: { id, time: new Date() } } }, { upsert: true, new: true }).lean(true);
return result;
}
//查询现在正在进行的活动数据
public static async findAllOpenData(serverId: number, activityId: number, roleId: string) {
let nowDate = new Date();
let result: ActivityPopUpShopModelType[] = await ActivityPopUpShopModel.find({
serverId, roleId, activityId,
beginTime: { $lt: nowDate }, endTime: { $gt: nowDate }
}).lean(true);
return result;
}
//根据活动taskId查询现在正在进行的活动数据
public static async findOpenDataByTaskId(serverId: number, activityId: number, roleId: string, taskId: number) {
let nowDate = new Date();
let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOne({
serverId, roleId, activityId, taskId,
beginTime: { $lt: nowDate }, endTime: { $gt: nowDate }
}).lean(true);
return result;
}
//删除活动领取记录
public static async deleteActivity(serverId: number, activityId: number, roleId: string, taskId: number) {
let nowDate = new Date();
await ActivityPopUpShopModel.deleteMany({
roleId, activityId, taskId,
beginTime: { $lt: nowDate }, endTime: { $gt: nowDate }
});
}
}
export const ActivityPopUpShopModel = getModelForClass(ActivityPopUpShop);
export interface ActivityPopUpShopModelType extends Pick<DocumentType<ActivityPopUpShop>, keyof ActivityPopUpShop> { }
export type ActivityPopUpShopModelTypeParam = Partial<ActivityPopUpShopModelType>; // 将所有字段变成可选项