From 9a74d8b5916dd8a7db392143a601814b3488093a Mon Sep 17 00:00:00 2001 From: huangyw <1207046171@qq.com> Date: Tue, 26 Nov 2024 19:42:18 +0800 Subject: [PATCH] =?UTF-8?q?release-=204.2.=20=E9=87=8D=E6=96=B0=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E7=A7=AF=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../luoo/user/controller/PointController.java | 7 +++++ .../com/luoo/user/dao/UserPointLogDao.java | 5 +++ .../com/luoo/user/service/LotteryService.java | 31 ++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/luoo_user/src/main/java/com/luoo/user/controller/PointController.java b/luoo_user/src/main/java/com/luoo/user/controller/PointController.java index f45e8cd..0619f82 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/PointController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/PointController.java @@ -435,5 +435,12 @@ public class PointController { return Result.success(); } + @ApiOperation(value = "4.2. 重新计算积分", notes = "") + @PostMapping("/recalculate") + public Result recalculate() { + lotteryService.recalculate(); + return Result.success(); + } + } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java index 3f17238..131279f 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java @@ -25,4 +25,9 @@ public interface UserPointLogDao extends JpaRepository, Jp public List findUserPointLogsByUserIdAndTaskPointId(String userId, String taskPointId); public List findUserPointLogByTaskPointIdInAndUserId(List taskPointIds, String userId); + + @Query(value = "select distinct user_id from tb_user_point_log", nativeQuery = true) + public List findAllUserId(); + + public List findUserPointLogByUserId(String userId); } diff --git a/luoo_user/src/main/java/com/luoo/user/service/LotteryService.java b/luoo_user/src/main/java/com/luoo/user/service/LotteryService.java index c6e2dab..074c6b7 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/LotteryService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/LotteryService.java @@ -9,6 +9,7 @@ import com.luoo.user.dao.LotteryDao; import com.luoo.user.dao.LotteryRegionDao; import com.luoo.user.dao.LotteryUserDao; import com.luoo.user.dao.UserInfoDao; +import com.luoo.user.dao.UserPointLogDao; import com.luoo.user.dto.point.DrawDTO; import com.luoo.user.dto.point.LotteryParticipatedSearchDto; import com.luoo.user.dto.point.LotterySearchDto; @@ -29,6 +30,7 @@ import enums.UserBetaEnum; import enums.UserTypeEnum; import enums.UserVipStatusEnum; import exception.BizException; +import java.util.concurrent.atomic.AtomicReference; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.amqp.rabbit.core.RabbitTemplate; @@ -93,7 +95,10 @@ public class LotteryService { private final UserPointLogService userPointLogService; - public LotteryService(LotteryDao lotteryDao, JwtUtil jwtUtil, IdWorker idWorker, RedisLockUtil redisLockUtil, RegionService regionService, LotteryRegionDao lotteryRegionDao, UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao, DrawLotteryScheduler drawLotteryScheduler, JPAQueryFactory jpaQueryFactory, EnvConfig envConfig, UserinfoShippingAddressService userinfoShippingAddressService, UserPointLogService userPointLogService) { + private final UserPointLogDao userPointLogDao; + + public LotteryService(LotteryDao lotteryDao, JwtUtil jwtUtil, IdWorker idWorker, RedisLockUtil redisLockUtil, RegionService regionService, LotteryRegionDao lotteryRegionDao, UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao, DrawLotteryScheduler drawLotteryScheduler, JPAQueryFactory jpaQueryFactory, EnvConfig envConfig, UserinfoShippingAddressService userinfoShippingAddressService, UserPointLogService userPointLogService, + UserPointLogDao userPointLogDao) { this.lotteryDao = lotteryDao; this.jwtUtil = jwtUtil; this.idWorker = idWorker; @@ -108,6 +113,7 @@ public class LotteryService { this.envConfig = envConfig; this.userinfoShippingAddressService = userinfoShippingAddressService; this.userPointLogService = userPointLogService; + this.userPointLogDao = userPointLogDao; } @@ -920,4 +926,27 @@ public class LotteryService { }); } + @Transactional(rollbackFor = Exception.class) + public void recalculate() { + List allUserId = userPointLogDao.findAllUserId(); + allUserId.forEach(userId -> { + log.info("用户:{},积分重算开始", userId); + // 查询userId对应所有的积分记录 + List userPointLogList = userPointLogDao.findUserPointLogByUserId(userId); + AtomicReference totalScore = new AtomicReference<>(0); + userPointLogList.forEach(userPointLog -> { + if (userPointLog.getType().equals(PointEnums.TASK_POINT_TYPE_ADD.getCode())) { + totalScore.updateAndGet(v -> v + userPointLog.getScore()); + } else { + totalScore.updateAndGet(v -> v - userPointLog.getScore()); + } + }); + UserInfo userInfo = userInfoDao.findById(userId).orElse(null); + assert userInfo != null; + userInfo.setPoint(totalScore.get()); + userInfoDao.save(userInfo); + }); + + } + }