Ver código fonte

益寿缘app-优化App看课奖励累计天数逻辑

cgp 1 semana atrás
pai
commit
d18b375238

+ 5 - 0
fs-service/src/main/java/com/fs/his/service/IAppUserRewardService.java

@@ -48,4 +48,9 @@ public interface IAppUserRewardService {
      * App连续看课奖励弹窗
      * */
     public FsAppRole showWatchCourseRewardWindows(Long fsUserId);
+
+    /**
+     * 记录用户累计有效看课天数
+     * */
+    public void recordUserWatchCourseDays(Long fsUserId);
 }

+ 63 - 8
fs-service/src/main/java/com/fs/his/service/impl/AppUserRewardServiceImpl.java

@@ -2,10 +2,9 @@ package com.fs.his.service.impl;
 
 import com.fs.app.domain.FsAppRole;
 import com.fs.app.mapper.FsAppRoleMapper;
+import com.fs.common.core.redis.RedisCache;
 import com.fs.common.exception.CustomException;
 import com.fs.common.utils.DateUtils;
-import com.fs.course.domain.FsCourseWatchLog;
-import com.fs.course.mapper.FsCourseWatchLogMapper;
 import com.fs.his.domain.FsUser;
 import com.fs.his.domain.FsUserRewards;
 import com.fs.his.dto.ClaimRewardsAddDTO;
@@ -27,7 +26,10 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.time.Duration;
+import java.time.LocalDateTime;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 @Slf4j
 @Service
@@ -42,15 +44,16 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
     @Autowired
     private FsAppRoleMapper appRoleMapper;
 
-    @Autowired
-    private FsCourseWatchLogMapper watchLogMapper;
-
     @Autowired
     private ObjectMapper objectMapper;
 
     @Autowired
     private RewardStrategyFactory strategyFactory;
 
+    @Autowired
+    private RedisCache redisCache;
+
+    private static final String APP_WATCH_COURSE_DAY_KEY ="app:watch:course:day:";
 
     @Override
     public void claimRewards(ClaimRewardsAddDTO claimRewardsAddDTO) {
@@ -75,7 +78,7 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
         RewardStrategy strategy = strategyFactory.getStrategy(reward.getActivityType(), reward.getRewardType());
         RewardResult result = strategy.process(reward);
         //RewardResult result = new RewardResult();
-        result.setSuccess(true);
+        //result.setSuccess(true);
         if (result.isSuccess()) {
             // 更新奖品状态
             rewardsMapper.updateStatus(rewardsId, 1, result.getOrderCode(), DateUtils.getNowDate());
@@ -323,6 +326,7 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
 
     @Override
     public FsAppRole showWatchCourseRewardWindows(Long fsUserId) {
+        String watchCourseKey = APP_WATCH_COURSE_DAY_KEY + fsUserId;
         FsAppRole noShowWindow = new FsAppRole();
 
         // 获取用户和配置
@@ -351,9 +355,9 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
         }
 
         // 积分/红包逻辑
-        List<FsCourseWatchLog> finishedLogs = watchLogMapper.selectTodayFinishedLogsByUserId(fsUserId);
         // 今日没有完课 → 不展示
-        if (CollectionUtils.isEmpty(finishedLogs)) {
+        Object cacheObject = redisCache.getCacheObject(watchCourseKey);
+        if (cacheObject == null) {
             return noShowWindow;
         }
         // 今天已领过 → 不展示
@@ -364,6 +368,44 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
         return defaultConfig;
     }
 
+    @Override
+    public void recordUserWatchCourseDays(Long fsUserId) {
+        String watchCourseKey = APP_WATCH_COURSE_DAY_KEY  + fsUserId;
+
+        // 1. 检查Redis中是否有今日已记录的标记
+        Object cacheObject = redisCache.getCacheObject(watchCourseKey);
+        if (cacheObject != null) {
+            log.info("用户今日有效看课天数已记录, userId: {}", fsUserId);
+            return;
+        }
+
+        // 2. 计算到今晚23:59:59的剩余秒数
+        long secondsUntilEndOfDay = getSecondsUntilEndOfDay();
+
+        // 3. Redis记录今日已看课标记(过期时间为今晚23:59:59)
+        redisCache.setCacheObject(watchCourseKey, fsUserId, (int) secondsUntilEndOfDay, TimeUnit.SECONDS);
+
+        // 4. 更新数据库中的累计看课天数
+        FsUser fsUser = fsUserMapper.selectFsUserById(fsUserId);
+        if (fsUser != null) {
+            // 获取当前累计天数,如果为null则默认为0
+            Integer currentDays = fsUser.getAppRewardsViewedDays();
+            if (currentDays == null) {
+                currentDays = 0;
+            }
+
+            // 更新累计天数
+            FsUser updateUser = new FsUser();
+            updateUser.setUserId(fsUserId);
+            updateUser.setAppRewardsViewedDays(currentDays + 1);
+            fsUserMapper.updateFsUser(updateUser);
+
+            log.info("用户累计看课天数更新成功, userId: {}, 当前天数: {}", fsUserId, currentDays + 1);
+        } else {
+            log.error("用户不存在, userId: {}", fsUserId);
+        }
+    }
+
     //根据用户id、角色id获取配置的奖品信息(实物商品包含商品名称和图片)
     private FsAppRole getAppRoleConfig(Long fsUserId){
         //获取当前用户最高权重角色, "false"表示查询等级数字最小的权重角色
@@ -376,7 +418,20 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
         return fsAppRole;
     }
 
+    /**
+     * 获取到今晚23:59:59的剩余秒数
+     */
+    private long getSecondsUntilEndOfDay() {
+        LocalDateTime now = LocalDateTime.now();
+        LocalDateTime endOfDay = now.toLocalDate().atTime(23, 59, 59);
+
+        // 如果当前时间已经超过23:59:59,用明天
+        if (now.isAfter(endOfDay)) {
+            endOfDay = endOfDay.plusDays(1);
+        }
 
+        return Duration.between(now, endOfDay).getSeconds();
+    }
     /**
      * 积分、商品是否过期的判断方法(红包需要另外方法计算)
      * @param reward: 奖品

+ 17 - 7
fs-user-app/src/main/java/com/fs/app/controller/AppUserRewardController.java

@@ -6,6 +6,7 @@ import com.fs.common.core.domain.R;
 import com.fs.common.core.page.TableDataInfo;
 import com.fs.common.utils.StringUtils;
 import com.fs.his.domain.FsUserRewards;
+import com.fs.his.dto.ClaimRewardsAddDTO;
 import com.fs.his.enums.ActivityTypeEnum;
 import com.fs.his.enums.RewardTypeEnum;
 import com.fs.his.service.IAppUserRewardService;
@@ -31,7 +32,7 @@ public class AppUserRewardController  extends AppBaseController{
     /**
      * 跳转下发首次登录注册App奖励
      * */
-    @Login
+    //@Login
     @ApiOperation("跳转下发注册登录奖励")
     @GetMapping("/issueFirstLoginReward")
     public R issueFirstLoginReward() {
@@ -40,7 +41,6 @@ public class AppUserRewardController  extends AppBaseController{
             return R.error("请登录");
         }
         Long fsUserId = Long.valueOf(loginUserId);
-//        Long fsUserId = testUserId;
         boolean rewardAdded = appUserRewardService.checkFirstLoginRewardStatus(fsUserId);
         if (rewardAdded) {
             //添加首次登录奖励
@@ -79,12 +79,10 @@ public class AppUserRewardController  extends AppBaseController{
     @PostMapping("/getMyRewardList")
     public TableDataInfo getMyRewardList(@RequestBody FsUserRewards queryRewards) {
         String loginUserId = getUserId();
-//        String loginUserId = String.valueOf(testUserId);
         if (StringUtils.isEmpty(loginUserId)){
             return getDataTable(Collections.emptyList());
         }
         queryRewards.setFsUserId(Long.valueOf(loginUserId));
-        queryRewards.setFsUserId(Long.valueOf(loginUserId));
         startPage();
         List<FsUserRewards> list = appUserRewardService.getMyRewardList(queryRewards);
         if (CollectionUtils.isEmpty(list)){
@@ -98,13 +96,13 @@ public class AppUserRewardController  extends AppBaseController{
     @Login
     @ApiOperation("奖品列表用户领取奖励")
     @PostMapping("/claim")
-    public R claimRewards(@RequestParam("rewardsId") Long rewardsId,
-                          @RequestParam(value = "addressId", required = false) Long addressId) {//领取商品时必传收货地址id
+    public R claimRewards(@RequestBody ClaimRewardsAddDTO claimRewardsAddDTO) {//领取商品时必传收货地址id
         String loginUserId = getUserId();
         if (StringUtils.isEmpty(loginUserId)){
             return R.error("请登录");
         }
-        appUserRewardService.claimRewards(Long.valueOf(loginUserId),rewardsId,addressId);
+        claimRewardsAddDTO.setFsUserId(Long.valueOf(loginUserId));
+        appUserRewardService.claimRewards(claimRewardsAddDTO);
         return R.ok();
     }
 
@@ -161,4 +159,16 @@ public class AppUserRewardController  extends AppBaseController{
         response.put("data", values);
         return response;
     }
+
+    /**
+     * 查询活动类型
+     */
+    @Login
+    @ApiOperation("查询活动类型")
+    @GetMapping("/activityType")
+    public R recordUserWatchCourseDays() {
+        String loginUserId = getUserId();
+        appUserRewardService.recordUserWatchCourseDays(Long.valueOf(loginUserId));
+        return R.ok();
+    }
 }