diff --git a/game-server/app/servers/role/handler/itemHandler.ts b/game-server/app/servers/role/handler/itemHandler.ts index d3da7b2ee..8a21a89a7 100644 --- a/game-server/app/servers/role/handler/itemHandler.ts +++ b/game-server/app/servers/role/handler/itemHandler.ts @@ -15,6 +15,7 @@ import { GiftCodeType, GiftCodeModel } from "../../../db/GiftCode"; import { nowSeconds } from "../../../pubUtils/timeUtil"; import { AP } from "../../../pubUtils/dicParam"; import { GiftPackageFloorModel } from "../../../db/GiftPackageFloor"; +import { UserGiftCodeDetailModel } from "../../../db/UserGiftCodeDetail"; export default function (app: Application) { new HandlerService(app, {}); @@ -187,17 +188,17 @@ export class ItemHandler { return resResult(STATUS.GIFT_CODE_CHANNEL_ERR); } - if (giftCodeDetail.roleIds.indexOf(roleId) != -1) { - return resResult(STATUS.YOU_HAVE_USED_THIS_CODE); - } - let checkHasUse = await GiftCodeDetailModel.checkHasUsed(roleId, giftCodeDetail.giftId) if(checkHasUse) { return resResult(STATUS.YOU_HAVE_USED_THIS_CODE); } - + let checkHasUse2 = await UserGiftCodeDetailModel.checkHasUsed(roleId, giftCodeDetail.giftId) + if(checkHasUse2) { + return resResult(STATUS.YOU_HAVE_USED_THIS_CODE); + } - await GiftCodeDetailModel.increaseUsedNum(code, roleId, roleName, serverId); + await UserGiftCodeDetailModel.record(roleId, roleName, serverId, giftCodeDetail.giftId, code); + await GiftCodeDetailModel.increaseUsedNum(code); await GiftCodeModel.increaseUsedNum(giftCode.id); let goods = await addItems(roleId, roleName, sid, giftCode.goods, ITEM_CHANGE_REASON.USE_GIFT_CODE); diff --git a/shared/db/GiftCodeDetail.ts b/shared/db/GiftCodeDetail.ts index bfd881683..f30820d69 100644 --- a/shared/db/GiftCodeDetail.ts +++ b/shared/db/GiftCodeDetail.ts @@ -2,7 +2,6 @@ import BaseModel from './BaseModel'; import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose'; import { GIFT_GENERATE_TYPE } from '../consts'; import { GiftCodeModel, GiftCodeType } from './GiftCode'; -import { nowSeconds } from '../pubUtils/timeUtil'; import { genCode } from '../pubUtils/util'; import { SearchGiftCodeDetailParam } from '../domain/backEndField/search'; @@ -55,7 +54,7 @@ export default class GiftCodeDetail extends BaseModel { // 根据code public static async findByCode(code: string) { - let result: GiftCodeDetailType = await GiftCodeDetailModel.findOne({ code }).lean(true); + let result: GiftCodeDetailType = await GiftCodeDetailModel.findOne({ code }).select('-record').lean(true); return result; } @@ -78,11 +77,10 @@ export default class GiftCodeDetail extends BaseModel { return result; } - public static async increaseUsedNum(code: string, roleId: string, roleName: string, serverId: number, orderId?: string) { - let result: GiftCodeDetailType = await GiftCodeDetailModel.findOneAndUpdate({ code }, { - $inc: { usedNum: 1 }, $push: { roleIds: roleId, record: { roleId, roleName, serverId, time: nowSeconds(), orderId } } + public static async increaseUsedNum(code: string) { + await GiftCodeDetailModel.updateOne({ code }, { + $inc: { usedNum: 1 } }, { new: true }).lean(); - return result; } public static async generateMany(giftCode: GiftCodeType, count = 1, uid = 1) { diff --git a/shared/db/UserGiftCodeDetail.ts b/shared/db/UserGiftCodeDetail.ts new file mode 100644 index 000000000..7f49eeba9 --- /dev/null +++ b/shared/db/UserGiftCodeDetail.ts @@ -0,0 +1,49 @@ +import BaseModel from './BaseModel'; +import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose'; + +/** + * 领取记录 +**/ +@modelOptions({ schemaOptions: { id: false } }) +@index({ roleId: 1, giftId: 1 }) +@index({ orderId: 1, giftId: 1 }) +export default class UserGiftCodeDetail extends BaseModel { + + @prop({ required: true, default: '' }) + roleId: string; + + @prop({ required: true, default: '' }) + roleName: string; + + @prop({ required: true, default: 0 }) + serverId: number; + + @prop({ required: false, default: '' }) + orderId?: string; + + @prop({ required: true, default: '' }) + giftId: number; // giftCode表的id + + @prop({ required: true, default: '' }) + code: string; // 兑换码 + + public static async record(roleId: string, roleName: string, serverId: number, giftId: number, code: string, orderId?: string) { + await UserGiftCodeDetailModel.insertMany([{ roleId, roleName, serverId, giftId, code, orderId }]); + } + + public static async checkHasUsed(roleId: string, id: number) { + let result = await UserGiftCodeDetailModel.exists({ giftId: id, roleId }); + return result; + } + + public static async checkOrderHasUsed(id: number, orderId: string) { + let result = await UserGiftCodeDetailModel.exists({ giftId: id, orderId }); + return result; + } + +} + +export const UserGiftCodeDetailModel = getModelForClass(UserGiftCodeDetail); + +export interface UserGiftCodeDetailType extends Pick, keyof UserGiftCodeDetail> { } +export type UserGiftCodeDetailParam = Partial; diff --git a/web-server/app/service/Sdk.ts b/web-server/app/service/Sdk.ts index d9043dedf..26de94430 100644 --- a/web-server/app/service/Sdk.ts +++ b/web-server/app/service/Sdk.ts @@ -18,6 +18,7 @@ import { RegionModel } from '@db/Region'; import { ActivityPublicAccountCodeModel } from '@db/ActivityPublicAccountCode'; import { GiftCodeDetailModel } from '@db/GiftCodeDetail'; import { GiftCodeModel } from '@db/GiftCode'; +import { UserGiftCodeDetailModel } from '@db/UserGiftCodeDetail'; /** * Test Service @@ -473,8 +474,13 @@ export default class Sdk extends Service { if(checkHasUse) { return resResult(SDK_37_ACTIVITY_CODE.ORDER_DUPLICATE); } + let checkHasUse2 = await UserGiftCodeDetailModel.checkOrderHasUsed(giftCodeDetail.giftId, params.order_id); + if(checkHasUse2) { + return resResult(SDK_37_ACTIVITY_CODE.ORDER_DUPLICATE); + } - await GiftCodeDetailModel.increaseUsedNum(giftCodeDetail.code, role.roleId, role.roleName, role.serverId, params.order_id); + await UserGiftCodeDetailModel.record(role.roleId, role.roleName, role.serverId, giftCodeDetail.giftId, giftCodeDetail.code, params.order_id); + await GiftCodeDetailModel.increaseUsedNum(giftCodeDetail.code); await GiftCodeModel.increaseUsedNum(giftCode.id); await ctx.service.utils.pushGiftCodeChannel(role.roleId, giftCode.id);