From cdd73bfc03c052c161694c6b17f9946e7c21d748 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Wed, 26 Nov 2025 17:15:28 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BA=A2=E5=8C=85=E5=8F=AF=E8=83=BD=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E7=9A=84=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98=E5=A4=84?= =?UTF-8?q?=E7=90=86=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redpacket/GrabRoomRedPacketCmdExe.java | 6 ++--- .../redpacket/SendRoomRedPacketCmdExe.java | 23 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/GrabRoomRedPacketCmdExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/GrabRoomRedPacketCmdExe.java index 544cbf4b..e67d3d8a 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/GrabRoomRedPacketCmdExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/GrabRoomRedPacketCmdExe.java @@ -57,7 +57,7 @@ public class GrabRoomRedPacketCmdExe { if (packetData == null || packetData.isEmpty()) { // Redis未命中,从数据库加载 RoomRedPacket redPacket = roomRedPacketGateway.findByPacketId(packetId); - ResponseAssert.notNull(OtherErrorCode.RED_PACKET_NOT_FOUND, grabbed); + ResponseAssert.notNull(OtherErrorCode.RED_PACKET_NOT_FOUND, redPacket); // 检查状态 checkRedPacketStatus(redPacket); @@ -81,7 +81,7 @@ public class GrabRoomRedPacketCmdExe { ResponseAssert.isFalse(OtherErrorCode.RED_PACKET_FINISHED, remainCount == null || remainCount < 0); // 6. 设置防重标记(Redis) - long expireSeconds = Duration.between(LocalDateTime.now(), expireTime).getSeconds(); + long expireSeconds = Math.max(60, Duration.between(LocalDateTime.now(), expireTime).getSeconds()); boolean setSuccess = roomRedPacketCacheService.setUserGrabbedFlag(packetId, userId, expireSeconds); ResponseAssert.isTrue(OtherErrorCode.RED_PACKET_ALREADY_GRABBED, setSuccess); @@ -221,7 +221,7 @@ public class GrabRoomRedPacketCmdExe { "status", redPacket.getStatus() ); - long expireSeconds = Duration.between(LocalDateTime.now(), redPacket.getExpireTime()).getSeconds() + 600; + long expireSeconds = Duration.between(LocalDateTime.now(), redPacket.getExpireTime()).getSeconds(); roomRedPacketCacheService.setRedPacket(redPacket.getPacketId(), packetData, expireSeconds); } } diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/SendRoomRedPacketCmdExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/SendRoomRedPacketCmdExe.java index c29a423a..b1349028 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/SendRoomRedPacketCmdExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/SendRoomRedPacketCmdExe.java @@ -61,33 +61,32 @@ public class SendRoomRedPacketCmdExe { Long userId = cmd.getReqUserId(); cmd.setPacketType(2); - // 1. 参数校验 + // 参数校验 validateParams(cmd); - // 2. 生成红包ID String packetId = IdWorkerUtils.getIdStr(); - // 3. 扣除用户金币(同步调用钱包服务) - deductUserGold(userId, cmd, packetId); - - // 4. 计算过期时间 + // 计算过期时间 LocalDateTime expireTime = LocalDateTime.now().plusMinutes(cmd.getExpireMinutes()); - // 5. 创建红包记录 + // 创建红包记录 RoomRedPacket redPacket = buildRedPacket(cmd, userId, packetId, expireTime); roomRedPacketGateway.save(redPacket); + + // 扣除用户金币(同步调用钱包服务) + deductUserGold(userId, cmd, packetId); - // 6. 拼手气红包:预生成随机金额列表 + // 拼手气红包:预生成随机金额列表 if (RoomRedPacketType.RANDOM.getCode().equals(cmd.getPacketType())) { List amounts = generateRandomAmounts(cmd.getTotalAmount(), cmd.getTotalCount()); long expireSeconds = cmd.getExpireMinutes() * 60; roomRedPacketCacheService.setRandomAmounts(packetId, amounts, expireSeconds); } - // 7. 写入Redis(红包主数据) + // 写入Redis(红包主数据) cacheRedPacketData(redPacket); - // 8. 添加到房间红包列表 + // 添加到房间红包列表 roomRedPacketCacheService.addToRoomList( cmd.getRoomId(), packetId, @@ -216,11 +215,11 @@ public class SendRoomRedPacketCmdExe { List amounts = new ArrayList<>(totalCount); long remainAmount = totalAmount; int remainCount = totalCount; - + for (int i = 0; i < totalCount - 1; i++) { // 二倍均值法:随机金额范围 [1, 剩余平均值 * 2] long avgAmount = remainAmount / remainCount; - long maxAmount = avgAmount * 2; + long maxAmount = Math.min(avgAmount * 2, remainAmount - (remainCount - 1)); // 保证剩余够分 // 确保至少1金币 long amount = ThreadLocalRandom.current().nextLong(1, Math.max(2, maxAmount + 1));