Files
ZYZ/shared/db/ActivityTaskPoint.ts
2022-03-07 19:51:01 +08:00

90 lines
3.8 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, DocumentType } from '@typegoose/typegoose';
/**
* 游戏中的任务完成后,增加的积分点数关联到活动数据
*/
@index({ taskId: 1 })
export default class Activity_Task_Point extends BaseModel {
// @prop({ required: true })
// serverId: number; // 服Id
@prop({ required: true })
type: number; // 任务类型TASK_FUN_TYPE 1.主线2每日3.成就
@prop({ required: true })
taskType: number; // 任务type TASK_TYPE
@prop({ required: true })
taskId: number; // 任务id
@prop({ required: true })
activityId: number; // 关联的活动id
@prop({ required: true })
activityType: number; // 活动类型
@prop({ required: true })
point: number; // 增加点数
//更新活动数据
public static async updateData(type: number, taskId: number, activityId: number, point: number, uid = 1) {
let result: ActivityTaskPointModelType = await ActivityTaskPointModel.findOneAndUpdate(
{ type, taskId, activityId }, { $set: { point, updatedBy: uid }, $setOnInsert: { createdBy: uid } }, { upsert: true, new: true }
).lean(true);
return result;
}
//查询数据
public static async findData(type: number, taskId: number) {
let result: ActivityTaskPointModelType[] = await ActivityTaskPointModel.find(
{ type, taskId }).lean(true);
return result;
}
//删除数据
public static async deleteData(type: number, taskId: number, activityId: number) {
await ActivityTaskPointModel.deleteOne({ type, taskId, activityId });
}
public static async createDataIfNotExist(type: number, taskType: number, taskId: number, activityId: number, activityType: number, point: number, uid = 1) {
let result: ActivityTaskPointModelType = await ActivityTaskPointModel.findOneAndUpdate(
{ type, taskId, activityId }, { $setOnInsert: { point, createdBy: uid }, $set: { taskType, activityType, updatedBy: uid } }, { upsert: true, new: true }
).lean(true);
return result;
}
private static getSearchObj(form: { taskType?: number, taskId?: number, activityId?: number }) {
let searchObj = {};
if (form['taskType']) searchObj['taskType'] = form.taskType;
if (form['taskId']) searchObj['taskId'] = form.taskId;
if (form['activityId']) searchObj['activityId'] = form.activityId;
return searchObj
}
public static async findByCondition(page: number, pageSize: number, sortField: string = 'updatedAt', sortOrder: string = 'descend', form: { taskType?: number, taskId?: number, activityId?: number } = {}) {
let searchObj = this.getSearchObj(form);
let sort = {};
if (sortField && sortOrder) {
if (sortOrder == 'ascend') {
sort[sortField] = 1;
} else if (sortOrder == 'descend') {
sort[sortField] = -1;
}
}
const result: ActivityTaskPointModelType[] = await ActivityTaskPointModel.find(searchObj).limit(pageSize).skip((page - 1) * pageSize).sort(sort).lean({ getters: true, virtuals: true });
return result;
}
public static async countByCondition(form: { taskType?: number, taskId?: number, activityId?: number } = {}) {
let searchObj = this.getSearchObj(form);
const result = await ActivityTaskPointModel.count(searchObj);
return result;
}
}
export const ActivityTaskPointModel = getModelForClass(Activity_Task_Point);
export interface ActivityTaskPointModelType extends Pick<DocumentType<Activity_Task_Point>, keyof Activity_Task_Point> { }
export type ActivityTaskPointModelTypeParam = Partial<ActivityTaskPointModelType>; // 将所有字段变成可选项