Files
ZYZ/shared/db/ActivityTreasureHunt.ts
2021-05-25 21:38:23 +08:00

91 lines
4.1 KiB
TypeScript

import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { ORDER_STATE } from '../consts';
/**
* 玩家寻宝猎人活动数据
*/
@index({ roleId: 1 })
export default class ActivityTreasureHunt extends BaseModel {
@prop({ required: true })
serverId: number; // 区号
@prop({ required: true })
roleId: string; // 用户id
@prop({ required: true })
activityId: number; // 活动Id
@prop({ required: true })
roundIndex: number; // 周期Id
//保存平台订单号
public static async saveOrderID(roleId: string, localOrderID: string, aliOrderID: string) {
let result: ActivityTreasureHuntType = await ActivityTreasureHuntModel.findOneAndUpdate({ roleId, localOrderID },
{ $set: { orderID: aliOrderID } },
{ new: true }).lean(true);
return result;
}
//校验订单
public static async check(roleId: string, localOrderID: string, message: string = '') {
let result: ActivityTreasureHuntType = await ActivityTreasureHuntModel.findOneAndUpdate({ roleId, localOrderID, state: { $ne: ORDER_STATE.RESULT_SUCCESS } },
{ $set: { state: ORDER_STATE.CHECK_ORDER, message } },
{ new: true }).lean(true);
return result;
}
//订单支付失败
public static async fail(roleId: string, localOrderID: string, message: string = '') {
let result: ActivityTreasureHuntType = await ActivityTreasureHuntModel.findOneAndUpdate({ roleId, localOrderID, state: { $ne: ORDER_STATE.RESULT_SUCCESS } },
{ $set: { state: ORDER_STATE.RESULT_FAIL, message } },
{ new: true }).lean(true);
return result;
}
//订单支付成功
public static async success(roleId: string, localOrderID: string, message: string = '') {
let result: ActivityTreasureHuntType = await ActivityTreasureHuntModel.findOneAndUpdate({ roleId, localOrderID, state: { $ne: ORDER_STATE.RESULT_SUCCESS } },
{ $set: { state: ORDER_STATE.RESULT_SUCCESS, message } },
{ new: true }).lean(true);
return result;
}
//查询订单详情
public static async findOrderByProductID(productID: string, roleId: string, activityId: number) {
let result: ActivityTreasureHuntType[] = await ActivityTreasureHuntModel.find({ productID, roleId, activityId }).lean(true);
return result;
}
//查询订单详情
public static async findPlayerOrder(productID: string, roleId: string, activityId: number, limit: number) {
let result: ActivityTreasureHuntType[] = await ActivityTreasureHuntModel.find({ productID, roleId, activityId }, {}).limit(limit).sort({ createdAt: -1 }).lean(true);
return result;
}
//查询订单详情
public static async findOrderByActivityID(activityId: number, roleId: string,) {
let result: ActivityTreasureHuntType[] = await ActivityTreasureHuntModel.find({ activityId, roleId }).lean(true);
return result;
}
//查询订单详情
public static async findOrder(localOrderID: string) {
let result: ActivityTreasureHuntType = await ActivityTreasureHuntModel.findOne({ localOrderID }).lean(true);
return result;
}
//新增订单
public static async applyOrder(serverId: number, roleId: string, productID: string, localOrderID: string, orderID: string, price: number, payType: number, activityId: number, message: string = '') {
let result: ActivityTreasureHuntType = await ActivityTreasureHuntModel.findOneAndUpdate({ serverId, roleId, productID, localOrderID, orderID, payType, activityId },
{ $set: { price, state: ORDER_STATE.APPLY, message } },
{ upsert: true, new: true }).lean(true);
return result;
}
}
export const ActivityTreasureHuntModel = getModelForClass(ActivityTreasureHunt);
export interface ActivityTreasureHuntType extends Pick<DocumentType<ActivityTreasureHunt>, keyof ActivityTreasureHunt> { }
export type ActivityTreasureHuntTypeParam = Partial<ActivityTreasureHuntType>; // 将所有字段变成可选项