Files
ZYZ/shared/db/ActivityPopUpShop.ts
2022-10-24 18:46:24 +08:00

128 lines
4.8 KiB
TypeScript

import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { genCode } from '../pubUtils/util';
import { PopShopItem } from '../domain/activityField/popUpShopField';
import { Reward } from '../domain/battleField/pvp';
export class PopUpShopItem {
@prop({ required: true })
id: number; // 分档礼包id
@prop({ required: true })
productID: string; // 商品id
@prop({ required: true })
hasBoughtCnt: number = 0; // 已购买次数
@prop({ required: true, _id: false, type: Reward })
rewards: Reward[] = []; // 已购买次数
constructor(item: PopShopItem) {
this.id = item.id;
this.productID = item.productID;
this.rewards = item.rewardInter;
}
}
/**
* 活动系统 - 弹出商店
*/
@index({ roleId: 1, activityId: 1 })
@index({ code: 1 })
export default class Activity_Pop_Up_Shop 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 })
effectBeginTime: Date; // 刷新等生效开始时间
@prop({ required: true })
effectEndTime: Date; // 刷新等生效结束时间
@prop({ required: true })
id: number; // 礼包id
@prop({ required: true })
code: string; // 本礼包本次推送的唯一code
@prop({ required: true, type: PopUpShopItem, _id: false })
items: PopUpShopItem[]; // 礼包id
@prop({ required: true })
hasBought: boolean; // 是否购买了
@prop({ required: true })
isLast: boolean; // 是否是最后一个
@prop({ required: true, default: false})
hasShown: boolean; // 客户端是否已经展示
/**
* 获取自然日内的次数
* @param roleId
* @param beginTime
* @param endTime
*/
public static async findAllEffectData(serverId: number, activityId: number, roleId: string) {
let now = new Date();
let rec: ActivityPopUpShopModelType[] = await ActivityPopUpShopModel.find({ roleId, serverId, activityId, effectBeginTime: { $lte: now }, effectEndTime: { $gte: now } }).lean();
return rec;
}
public static async findAllLastData(serverId: number, activityId: number, roleId: string) {
let rec: ActivityPopUpShopModelType[] = await ActivityPopUpShopModel.find({ roleId, serverId, activityId, isLast: true }).lean();
return rec;
}
public static async createRecord(param: ActivityPopUpShopModelTypeParam) {
let { serverId, activityId, roleId, id,} = param;
await ActivityPopUpShopModel.updateMany({ serverId, activityId, roleId, id, isLast: true }, { $set: { isLast: false } });
let code = genCode(8);
let rec: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate({ code }, { $set: {...param, hasBought: false, isLast: true } }, { new: true, upsert: true }).lean();
return rec;
}
public static async findByCode(serverId: number, activityId: number, roleId: string, code: string) {
let rec: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOne({ serverId, activityId, roleId, code }).lean();
return rec;
}
public static async addRecord(serverId: number, activityId: number, roleId: string, code: string, productID: string) {
let rec: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate(
{ serverId, activityId, roleId, code, 'items.productID': productID },
{ $set: { hasBought: true }, $inc: { 'items.$.hasBoughtCnt': 1 } },
{ new: true} ).lean();
return rec;
}
public static async addRecordById(serverId: number, activityId: number, roleId: string, code: string, itemid: number) {
let rec: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate(
{ serverId, activityId, roleId, code, 'items.id': itemid },
{ $set: { hasBought: true }, $inc: { 'items.$.hasBoughtCnt': 1 } },
{ new: true} ).lean();
return rec;
}
public static async show(code: string) {
let rec: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate({ code }, { hasShown: true }).lean();
return rec;
}
}
export const ActivityPopUpShopModel = getModelForClass(Activity_Pop_Up_Shop);
export interface ActivityPopUpShopModelType extends Pick<DocumentType<Activity_Pop_Up_Shop>, keyof Activity_Pop_Up_Shop> { }
export type ActivityPopUpShopModelTypeParam = Partial<ActivityPopUpShopModelType>; // 将所有字段变成可选项