|
|
@@ -42,7 +42,7 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
|
|
|
@Autowired
|
|
|
private FsUserRewardsMapper rewardsMapper;
|
|
|
-
|
|
|
+
|
|
|
@Autowired
|
|
|
private FsUserMapper fsUserMapper;
|
|
|
|
|
|
@@ -64,21 +64,22 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
@Autowired
|
|
|
private FsCourseRedPacketLogMapper redPacketLogMapper;
|
|
|
|
|
|
- private static final String APP_WATCH_COURSE_DAY_KEY ="app:watch:course:day:";
|
|
|
+ private static final String APP_WATCH_COURSE_DAY_KEY = "app:watch:course:day:";
|
|
|
|
|
|
- private static final String LOCK_WATCH_REWARD_KEY ="lock:watch:reward:";
|
|
|
+ private static final String LOCK_WATCH_REWARD_KEY = "lock:watch:reward:";
|
|
|
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public R claimRewards(ClaimRewardsAddDTO claimRewardsAddDTO) {
|
|
|
- Long fsUserId=claimRewardsAddDTO.getFsUserId();
|
|
|
- Long rewardsId=claimRewardsAddDTO.getRewardsId();
|
|
|
- FsUserRewards reward = rewardsMapper.selectByUserIdAndRewardsId(claimRewardsAddDTO.getFsUserId(), rewardsId);
|
|
|
- if (reward==null){
|
|
|
+ Long fsUserId = claimRewardsAddDTO.getFsUserId();
|
|
|
+ Long rewardsId = claimRewardsAddDTO.getRewardsId();
|
|
|
+ FsUserRewards reward = rewardsMapper.selectByUserIdAndRewardsIdForUpdate(claimRewardsAddDTO.getFsUserId(), rewardsId);
|
|
|
+ if (reward == null) {
|
|
|
log.info("用户:{}没有可领取的奖品:{}", fsUserId, rewardsId);
|
|
|
return R.error("操作频繁,请稍后再试!");
|
|
|
}
|
|
|
- if (reward.getStatus()==1||reward.getStatus()==2){
|
|
|
- log.info("用户:{}奖品已领取: rewardsId={}",fsUserId, rewardsId);
|
|
|
+ if (reward.getStatus() == 1 || reward.getStatus() == 2) {
|
|
|
+ log.info("用户:{}奖品已领取: rewardsId={}", fsUserId, rewardsId);
|
|
|
return R.error("操作频繁,请稍后再试!");
|
|
|
}
|
|
|
// 校验是否过期(目前只有看课奖品需要校验过期)
|
|
|
@@ -98,11 +99,34 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
// 更新奖品状态
|
|
|
rewardsMapper.updateStatus(rewardsId, 1, result.getOrderCode(), DateUtils.getNowDate());
|
|
|
log.info("奖品领取成功: rewardsId={}, orderCode={}", rewardsId, result.getOrderCode());
|
|
|
- if (reward.getRewardType()==1) return (R)result.getExtData();
|
|
|
- return R.ok();
|
|
|
+ // 构建统一的返回对象
|
|
|
+ R response = R.ok();
|
|
|
+
|
|
|
+ // 如果是红包类型,从extData中提取需要的信息放入返回对象
|
|
|
+ //if (reward.getRewardType() == 1) return result.getExtData();
|
|
|
+ if (reward.getRewardType() == 1 && result.getExtData() != null) {
|
|
|
+ R extR = result.getExtData();
|
|
|
+ // 创建一个data对象存放特定字段
|
|
|
+ Map<String, Object> dataMap = new HashMap<>();
|
|
|
+
|
|
|
+ // 只提取需要的字段放入data
|
|
|
+ if (extR.containsKey("packageInfo")) {
|
|
|
+ dataMap.put("packageInfo", extR.get("packageInfo"));
|
|
|
+ }
|
|
|
+ if (extR.containsKey("mchId")) {
|
|
|
+ dataMap.put("mchId", extR.get("mchId"));
|
|
|
+ }
|
|
|
+ if (extR.containsKey("appId")) {
|
|
|
+ dataMap.put("appId", extR.get("appId"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将data放入response
|
|
|
+ response.put("data", dataMap);
|
|
|
+ }
|
|
|
+ return response;
|
|
|
} else {
|
|
|
log.error("奖品领取失败: rewardsId={}, reason={}", rewardsId, result.getMessage());
|
|
|
- throw new CustomException(result.getMessage(),result.getCode());
|
|
|
+ throw new CustomException(result.getMessage(), result.getCode());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -117,7 +141,7 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
return;
|
|
|
}
|
|
|
// 构建奖品记录
|
|
|
- FsUserRewards reward = buildAppUserReward(fsUserId, activityType, appRoleConfig,null,null);
|
|
|
+ FsUserRewards reward = buildAppUserReward(fsUserId, activityType, appRoleConfig, null, null);
|
|
|
try {
|
|
|
// 插入奖品表
|
|
|
int insertResult = rewardsMapper.insertFsUserRewards(reward);
|
|
|
@@ -146,8 +170,8 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public void addUserWatchCourseRewards(Long fsUserId,Long videoId) {
|
|
|
- String watchCourseKey = APP_WATCH_COURSE_DAY_KEY + fsUserId;
|
|
|
+ public void addUserWatchCourseRewards(Long fsUserId, Long videoId) {
|
|
|
+ String watchCourseKey = APP_WATCH_COURSE_DAY_KEY + fsUserId;
|
|
|
Object cacheObject = redisCache.getCacheObject(watchCourseKey);
|
|
|
if (cacheObject == null) {
|
|
|
log.info("用户:{}今日无有效看课记录", fsUserId);
|
|
|
@@ -170,7 +194,7 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
log.info("获取分布式锁成功: userId={}, videoId={}, secret={}",
|
|
|
fsUserId, videoId, lock.getLockSecretValue());
|
|
|
|
|
|
- String toDay=DateUtils.getDate();
|
|
|
+ String toDay = DateUtils.getDate();
|
|
|
// 4. 双重检查:再次确认是否已发放过奖励(防止锁内重复)
|
|
|
FsUserRewards existingReward = rewardsMapper.selectByUserIdAndVideoId(fsUserId, videoId, toDay);
|
|
|
if (existingReward != null) {
|
|
|
@@ -189,7 +213,7 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
}
|
|
|
|
|
|
// 构建奖品记录
|
|
|
- FsUserRewards reward = buildAppUserReward(fsUserId, activityType, appRoleConfig, videoId,toDay);
|
|
|
+ FsUserRewards reward = buildAppUserReward(fsUserId, activityType, appRoleConfig, videoId, toDay);
|
|
|
|
|
|
// 插入奖品表
|
|
|
try {
|
|
|
@@ -225,11 +249,12 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
|
|
|
/**
|
|
|
* 根据活动类型和角色配置,构建奖品记录
|
|
|
- * @param fsUserId 用户ID
|
|
|
- * @param activityType 活动类型
|
|
|
+ *
|
|
|
+ * @param fsUserId 用户ID
|
|
|
+ * @param activityType 活动类型
|
|
|
* @param appRoleConfig 角色配置
|
|
|
*/
|
|
|
- private FsUserRewards buildAppUserReward(Long fsUserId, String activityType, FsAppRole appRoleConfig,Long videoId,String toDay) {
|
|
|
+ private FsUserRewards buildAppUserReward(Long fsUserId, String activityType, FsAppRole appRoleConfig, Long videoId, String toDay) {
|
|
|
FsUserRewards reward = new FsUserRewards();
|
|
|
reward.setFsUserId(fsUserId);
|
|
|
reward.setActivityType(activityType);
|
|
|
@@ -290,9 +315,10 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
|
|
|
/**
|
|
|
* 计算奖励值(红包或积分)
|
|
|
+ *
|
|
|
* @param appRoleConfig 角色配置
|
|
|
- * @param targetDay 目标天数
|
|
|
- * @param isRedPackage true-红包,false-积分
|
|
|
+ * @param targetDay 目标天数
|
|
|
+ * @param isRedPackage true-红包,false-积分
|
|
|
*/
|
|
|
private <T extends Number> T calculateRewardValue(FsAppRole appRoleConfig, Integer targetDay, boolean isRedPackage) {
|
|
|
if (appRoleConfig.getCourseRewardRuleType() == 2) {
|
|
|
@@ -305,7 +331,8 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
try {
|
|
|
List<Map<String, Object>> rewardList = objectMapper.readValue(
|
|
|
courseRewardRule,
|
|
|
- new TypeReference<List<Map<String, Object>>>() {}
|
|
|
+ new TypeReference<List<Map<String, Object>>>() {
|
|
|
+ }
|
|
|
);
|
|
|
|
|
|
for (Map<String, Object> item : rewardList) {
|
|
|
@@ -379,7 +406,7 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
|
|
|
@Override
|
|
|
public FsAppRole showFirstLoginRewardWindows(Long fsUserId) {
|
|
|
- FsAppRole fsAppRole =new FsAppRole();
|
|
|
+ FsAppRole fsAppRole = new FsAppRole();
|
|
|
// 1. 查用户表判断是否首次登录注册App
|
|
|
FsUser fsUser = fsUserMapper.selectFsUserById(fsUserId);
|
|
|
if (fsUser != null && fsUser.getFirstLoginTime() != null) {
|
|
|
@@ -502,7 +529,7 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
|
|
|
@Override
|
|
|
public void recordUserWatchCourseDays(Long fsUserId) {
|
|
|
- String watchCourseKey = APP_WATCH_COURSE_DAY_KEY + fsUserId;
|
|
|
+ String watchCourseKey = APP_WATCH_COURSE_DAY_KEY + fsUserId;
|
|
|
|
|
|
// 1. 检查Redis中是否有今日已记录的标记
|
|
|
Object cacheObject = redisCache.getCacheObject(watchCourseKey);
|
|
|
@@ -539,10 +566,10 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
}
|
|
|
|
|
|
//根据用户id、角色id获取配置的奖品信息(实物商品包含商品名称和图片)
|
|
|
- private FsAppRole getAppRoleConfig(Long fsUserId){
|
|
|
+ private FsAppRole getAppRoleConfig(Long fsUserId) {
|
|
|
//获取当前用户最高权重角色, "false"表示查询等级数字最大的权重角色
|
|
|
- FsAppRole fsAppRole = appRoleMapper.selectHighestLevelAppRoleByUserId(fsUserId,"false");
|
|
|
- if (fsAppRole==null){
|
|
|
+ FsAppRole fsAppRole = appRoleMapper.selectHighestLevelAppRoleByUserId(fsUserId, "false");
|
|
|
+ if (fsAppRole == null) {
|
|
|
log.info("未找到用户:{}对应的角色配置", fsUserId);
|
|
|
return null;
|
|
|
}
|
|
|
@@ -564,11 +591,13 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
|
|
|
return Duration.between(now, endOfDay).getSeconds();
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 积分、商品是否过期的判断方法(红包需要另外方法计算)
|
|
|
+ *
|
|
|
* @param reward: 奖品
|
|
|
* @return true: 过期 false: 未过期
|
|
|
- * */
|
|
|
+ */
|
|
|
private boolean isExpired(FsUserRewards reward) {
|
|
|
// 奖品产生时间
|
|
|
Date rewardTime = reward.getCreateTime();
|
|
|
@@ -605,6 +634,7 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
|
|
|
/**
|
|
|
* 用于计算奖品列表里面的单个奖品的剩余过期天数
|
|
|
+ *
|
|
|
* @param reward 奖品实体
|
|
|
* @return 剩余天数。null: 表示无有效期或查询失败;0: 今天过期或已过期;正数: 剩余天数
|
|
|
*/
|