Files
ZYZ/shared/db/FriendPoint.ts

42 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import BaseModel from './BaseModel';
import { index, getModelForClass, prop } from '@typegoose/typegoose';
/**
* 情谊点记录
*/
@index({ roleId: 1, createdAt: -1 })
export default class FriendPoint extends BaseModel {
@prop({ required: true, default: '' })
roleId: string; // 角色 id
@prop({ required: true, default: '' })
roleName: string; // 角色名称
@prop({ required: true, default: 0 })
cnt: number; // 当天获取的点数
/**
* @description 更新每天的情谊点获取情况cnt 有可能超过当日最大值,可根据返回值判断
* @static
* @param {string} roleId
* @param {string} roleName
* @param {number} cntInc
* @param {number} maxPerDay
* @param {boolean} [lean=true]
* @memberof FriendPoint
*/
public static async updatePointToday(roleId: string, roleName: string, cntInc: number, maxPerDay: number, lean = true) {
const curTime = new Date((new Date()).setHours(0, 0, 0, 0));
// 当 oldCnt + cntInc > maxPerDay 时需计算实际可以获得的点数cntInc - (newCnt - maxPerDay)
const rec = await FriendPointModel.findOneAndUpdate({roleId, roleName, createdAt: {$gte: curTime}, cnt: {$lt: maxPerDay}}, {$inc: {cnt: cntInc}}, {upsert: true, new: true}).lean(lean);
return rec;
}
public static async getFrdPointRecToday(roleId: string, lean = true) {
const curTime = new Date((new Date()).setHours(0, 0, 0, 0));
const rec = await FriendPointModel.findOne({roleId, createdAt: {$gte: curTime}}).lean(lean);
return rec;
}
}
export const FriendPointModel = getModelForClass(FriendPoint);