From da9819085fd230a817efbf55bb0b332e98728f18 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Tue, 21 Oct 2025 11:38:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=A5=96=20ticket=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../activity/LotteryTicketClientEndpoint.java | 93 ++------------ .../activity/LotteryTicketClientService.java | 39 ++++++ .../impl/LotteryTicketClientServiceImpl.java | 113 ++++++++++++++++++ 3 files changed, 160 insertions(+), 85 deletions(-) create mode 100644 rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/service/activity/LotteryTicketClientService.java create mode 100644 rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/service/activity/impl/LotteryTicketClientServiceImpl.java diff --git a/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/endpoint/activity/LotteryTicketClientEndpoint.java b/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/endpoint/activity/LotteryTicketClientEndpoint.java index 62927556..4b6235a7 100644 --- a/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/endpoint/activity/LotteryTicketClientEndpoint.java +++ b/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/endpoint/activity/LotteryTicketClientEndpoint.java @@ -1,16 +1,10 @@ package com.red.circle.other.app.inner.endpoint.activity; -import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.red.circle.framework.dto.ResultResponse; -import com.red.circle.other.infra.database.rds.entity.activity.LotteryTicket; -import com.red.circle.other.infra.database.rds.entity.activity.LotteryTicketRecord; -import com.red.circle.other.infra.database.rds.service.activity.LotteryTicketRecordService; -import com.red.circle.other.infra.database.rds.service.activity.LotteryTicketService; +import com.red.circle.other.app.inner.service.activity.LotteryTicketClientService; import com.red.circle.other.inner.endpoint.activity.api.LotteryTicketClientApi; -import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.http.MediaType; -import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -27,95 +21,24 @@ import org.springframework.web.bind.annotation.RestController; @RequiredArgsConstructor public class LotteryTicketClientEndpoint implements LotteryTicketClientApi { - private final LotteryTicketService lotteryTicketService; - private final LotteryTicketRecordService lotteryTicketRecordService; + private final LotteryTicketClientService lotteryTicketClientService; @Override public ResultResponse getUserTicketCount(Long userId) { - List tickets = lotteryTicketService.query() - .eq(LotteryTicket::getUserId, userId) - .eq(LotteryTicket::getStatus, 1) - .gt(LotteryTicket::getRemainingCount, 0) - .list(); - - int totalCount = tickets.stream() - .mapToInt(LotteryTicket::getRemainingCount) - .sum(); - - return ResultResponse.success(totalCount); + Integer count = lotteryTicketClientService.getUserTicketCount(userId); + return ResultResponse.success(count); } @Override - @Transactional(rollbackFor = Exception.class) public ResultResponse deductTicket(Long userId, Integer count) { - List tickets = lotteryTicketService.query() - .eq(LotteryTicket::getUserId, userId) - .eq(LotteryTicket::getStatus, 1) - .gt(LotteryTicket::getRemainingCount, 0) - .orderByAsc(LotteryTicket::getExpireTime) - .list(); - - int totalRemaining = tickets.stream() - .mapToInt(LotteryTicket::getRemainingCount) - .sum(); - - if (totalRemaining < count) { - return ResultResponse.success(false); - } - - int remaining = count; - for (LotteryTicket ticket : tickets) { - if (remaining <= 0) { - break; - } - - int deduct = Math.min(remaining, ticket.getRemainingCount()); - - LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper() - .eq(LotteryTicket::getId, ticket.getId()) - .setSql("used_count = used_count + " + deduct) - .setSql("remaining_count = remaining_count - " + deduct); - lotteryTicketService.update(updateWrapper); - - // 记录使用 - LotteryTicketRecord ticketRecord = new LotteryTicketRecord(); - ticketRecord.setUserId(userId); - ticketRecord.setTicketId(ticket.getId()); - ticketRecord.setChangeCount(-deduct); - ticketRecord.setChangeType(2); // 消耗 - ticketRecord.setRemark("抽奖消耗"); - lotteryTicketRecordService.save(ticketRecord); - - remaining -= deduct; - } - - return ResultResponse.success(true); + Boolean result = lotteryTicketClientService.deductTicket(userId, count); + return ResultResponse.success(result); } @Override - @Transactional(rollbackFor = Exception.class) public ResultResponse addTicket(Long userId, Integer count, String source, String sourceId) { - LotteryTicket ticket = new LotteryTicket(); - ticket.setUserId(userId); - ticket.setTicketSource(source != null ? source : "SYSTEM"); - ticket.setSourceId(sourceId); - ticket.setTotalCount(count); - ticket.setUsedCount(0); - ticket.setRemainingCount(count); - ticket.setStatus(1); - - lotteryTicketService.save(ticket); - - // 记录发放 - LotteryTicketRecord ticketRecord = new LotteryTicketRecord(); - ticketRecord.setUserId(userId); - ticketRecord.setTicketId(ticket.getId()); - ticketRecord.setChangeCount(count); - ticketRecord.setChangeType(1); // 发放 - ticketRecord.setRemark("系统发放"); - lotteryTicketRecordService.save(ticketRecord); - - return ResultResponse.success(true); + Boolean result = lotteryTicketClientService.addTicket(userId, count, source, sourceId); + return ResultResponse.success(result); } } diff --git a/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/service/activity/LotteryTicketClientService.java b/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/service/activity/LotteryTicketClientService.java new file mode 100644 index 00000000..9250ef93 --- /dev/null +++ b/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/service/activity/LotteryTicketClientService.java @@ -0,0 +1,39 @@ +package com.red.circle.other.app.inner.service.activity; + +/** + * 抽奖券Client服务. + * + * @author system + * @since 2025-10-21 + */ +public interface LotteryTicketClientService { + + /** + * 获取用户抽奖券数量. + * + * @param userId 用户ID + * @return 抽奖券数量 + */ + Integer getUserTicketCount(Long userId); + + /** + * 扣减抽奖券. + * + * @param userId 用户ID + * @param count 扣减数量 + * @return 是否成功 + */ + Boolean deductTicket(Long userId, Integer count); + + /** + * 增加抽奖券. + * + * @param userId 用户ID + * @param count 增加数量 + * @param source 来源 + * @param sourceId 来源ID + * @return 是否成功 + */ + Boolean addTicket(Long userId, Integer count, String source, String sourceId); + +} diff --git a/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/service/activity/impl/LotteryTicketClientServiceImpl.java b/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/service/activity/impl/LotteryTicketClientServiceImpl.java new file mode 100644 index 00000000..d5c0371a --- /dev/null +++ b/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/service/activity/impl/LotteryTicketClientServiceImpl.java @@ -0,0 +1,113 @@ +package com.red.circle.other.app.inner.service.activity.impl; + +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import com.red.circle.other.app.inner.service.activity.LotteryTicketClientService; +import com.red.circle.other.infra.database.rds.entity.activity.LotteryTicket; +import com.red.circle.other.infra.database.rds.entity.activity.LotteryTicketRecord; +import com.red.circle.other.infra.database.rds.service.activity.LotteryTicketRecordService; +import com.red.circle.other.infra.database.rds.service.activity.LotteryTicketService; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * 抽奖券Client服务实现. + * + * @author system + * @since 2025-10-21 + */ +@Service +@RequiredArgsConstructor +public class LotteryTicketClientServiceImpl implements LotteryTicketClientService { + + private final LotteryTicketService lotteryTicketService; + private final LotteryTicketRecordService lotteryTicketRecordService; + + @Override + public Integer getUserTicketCount(Long userId) { + List tickets = lotteryTicketService.query() + .eq(LotteryTicket::getUserId, userId) + .eq(LotteryTicket::getStatus, 1) + .gt(LotteryTicket::getRemainingCount, 0) + .list(); + + return tickets.stream() + .mapToInt(LotteryTicket::getRemainingCount) + .sum(); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public Boolean deductTicket(Long userId, Integer count) { + List tickets = lotteryTicketService.query() + .eq(LotteryTicket::getUserId, userId) + .eq(LotteryTicket::getStatus, 1) + .gt(LotteryTicket::getRemainingCount, 0) + .orderByAsc(LotteryTicket::getExpireTime) + .list(); + + int totalRemaining = tickets.stream() + .mapToInt(LotteryTicket::getRemainingCount) + .sum(); + + if (totalRemaining < count) { + return false; + } + + int remaining = count; + for (LotteryTicket ticket : tickets) { + if (remaining <= 0) { + break; + } + + int deduct = Math.min(remaining, ticket.getRemainingCount()); + + LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper() + .eq(LotteryTicket::getId, ticket.getId()) + .setSql("used_count = used_count + " + deduct) + .setSql("remaining_count = remaining_count - " + deduct); + lotteryTicketService.update(updateWrapper); + + // 记录使用 + LotteryTicketRecord ticketRecord = new LotteryTicketRecord(); + ticketRecord.setUserId(userId); + ticketRecord.setTicketId(ticket.getId()); + ticketRecord.setChangeCount(-deduct); + ticketRecord.setChangeType(2); // 消耗 + ticketRecord.setRemark("抽奖消耗"); + lotteryTicketRecordService.save(ticketRecord); + + remaining -= deduct; + } + + return true; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public Boolean addTicket(Long userId, Integer count, String source, String sourceId) { + LotteryTicket ticket = new LotteryTicket(); + ticket.setUserId(userId); + ticket.setTicketSource(source != null ? source : "SYSTEM"); + ticket.setSourceId(sourceId); + ticket.setTotalCount(count); + ticket.setUsedCount(0); + ticket.setRemainingCount(count); + ticket.setStatus(1); + + lotteryTicketService.save(ticket); + + // 记录发放 + LotteryTicketRecord ticketRecord = new LotteryTicketRecord(); + ticketRecord.setUserId(userId); + ticketRecord.setTicketId(ticket.getId()); + ticketRecord.setChangeCount(count); + ticketRecord.setChangeType(1); // 发放 + ticketRecord.setRemark("系统发放"); + lotteryTicketRecordService.save(ticketRecord); + + return true; + } + +}