|
@@ -6,6 +6,7 @@ import com.fs.common.exception.CustomException;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
import com.fs.common.utils.DateUtils;
|
|
|
import com.fs.his.domain.FsUser;
|
|
import com.fs.his.domain.FsUser;
|
|
|
import com.fs.his.domain.FsUserRewards;
|
|
import com.fs.his.domain.FsUserRewards;
|
|
|
|
|
+import com.fs.his.dto.ClaimRewardsAddDTO;
|
|
|
import com.fs.his.enums.ActivityTypeEnum;
|
|
import com.fs.his.enums.ActivityTypeEnum;
|
|
|
import com.fs.his.mapper.FsUserMapper;
|
|
import com.fs.his.mapper.FsUserMapper;
|
|
|
import com.fs.his.mapper.FsUserRewardsMapper;
|
|
import com.fs.his.mapper.FsUserRewardsMapper;
|
|
@@ -13,6 +14,8 @@ import com.fs.his.service.IAppUserRewardService;
|
|
|
import com.fs.his.strategy.RewardResult;
|
|
import com.fs.his.strategy.RewardResult;
|
|
|
import com.fs.his.strategy.RewardStrategy;
|
|
import com.fs.his.strategy.RewardStrategy;
|
|
|
import com.fs.his.strategy.RewardStrategyFactory;
|
|
import com.fs.his.strategy.RewardStrategyFactory;
|
|
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -21,9 +24,8 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.Collections;
|
|
|
|
|
-import java.util.Date;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
+import java.math.RoundingMode;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@Service
|
|
@Service
|
|
@@ -38,30 +40,59 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private FsAppRoleMapper appRoleMapper;
|
|
private FsAppRoleMapper appRoleMapper;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
|
|
+
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private RewardStrategyFactory strategyFactory;
|
|
private RewardStrategyFactory strategyFactory;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void claimRewards(ClaimRewardsAddDTO claimRewardsAddDTO) {
|
|
|
|
|
+ Long fsUserId=claimRewardsAddDTO.getFsUserId();
|
|
|
|
|
+ Long rewardsId=claimRewardsAddDTO.getRewardsId();
|
|
|
|
|
+ FsUserRewards reward = rewardsMapper.selectByUserIdAndRewardsId(claimRewardsAddDTO.getFsUserId(), rewardsId);
|
|
|
|
|
+ if (reward==null){
|
|
|
|
|
+ log.info("用户:{}没有奖品:{}", fsUserId, rewardsId);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 校验是否过期(目前只有看课奖品需要校验过期)
|
|
|
|
|
+ if (ActivityTypeEnum.WATCH_COURSE.getCode().equals(reward.getActivityType())) {
|
|
|
|
|
+ if (isExpired(reward)) {
|
|
|
|
|
+ log.info("奖品已过期: rewardsId={}", rewardsId);
|
|
|
|
|
+ // 更新奖品状态为已过期
|
|
|
|
|
+ rewardsMapper.updateStatus(reward.getId(), 2, null, null);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ reward.setAddressId(claimRewardsAddDTO.getAddressId());
|
|
|
|
|
+ //执行奖品领取逻辑
|
|
|
|
|
+ RewardStrategy strategy = strategyFactory.getStrategy(reward.getActivityType(), reward.getRewardType());
|
|
|
|
|
+ RewardResult result = strategy.process(reward);
|
|
|
|
|
+ //RewardResult result = new RewardResult();
|
|
|
|
|
+ result.setSuccess(true);
|
|
|
|
|
+ if (result.isSuccess()) {
|
|
|
|
|
+ // 更新奖品状态
|
|
|
|
|
+ rewardsMapper.updateStatus(rewardsId, 1, result.getOrderCode(), DateUtils.getNowDate());
|
|
|
|
|
+ log.info("奖品领取成功: rewardsId={}, orderCode={}", rewardsId, result.getOrderCode());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("奖品领取失败: rewardsId={}, reason={}", rewardsId, result.getMessage());
|
|
|
|
|
+ throw new CustomException(result.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void addUserFirstLoginRewards(Long fsUserId) {
|
|
public void addUserFirstLoginRewards(Long fsUserId) {
|
|
|
String activityType = ActivityTypeEnum.FIRST_LOGIN.getCode();
|
|
String activityType = ActivityTypeEnum.FIRST_LOGIN.getCode();
|
|
|
|
|
|
|
|
- // TODO 根据配置获取奖品类型
|
|
|
|
|
FsAppRole appRoleConfig = getAppRoleConfig(fsUserId);
|
|
FsAppRole appRoleConfig = getAppRoleConfig(fsUserId);
|
|
|
- if (appRoleConfig==null){
|
|
|
|
|
|
|
+ if (appRoleConfig == null) {
|
|
|
log.info("当前用户未匹配到App奖励角色");
|
|
log.info("当前用户未匹配到App奖励角色");
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- Integer rewardType = appRoleConfig.getCourseRewardType();
|
|
|
|
|
- Integer productType=appRoleConfig.getCourseProductType();
|
|
|
|
|
- Long goodsId=appRoleConfig.getCourseProductId();
|
|
|
|
|
- BigDecimal rewardAmount=appRoleConfig.getCourseRedPacket();
|
|
|
|
|
- Long rewardPoints=appRoleConfig.getCourseIntegral().longValue();
|
|
|
|
|
- String goodsName = appRoleConfig.getGoodsName();
|
|
|
|
|
- String goodsUrl = appRoleConfig.getGoodsUrl();
|
|
|
|
|
// 构建奖品记录
|
|
// 构建奖品记录
|
|
|
- FsUserRewards reward = buildAppUserReward(fsUserId, activityType, rewardType, productType, goodsId, rewardAmount,rewardPoints,goodsName,goodsUrl);
|
|
|
|
|
|
|
+ FsUserRewards reward = buildAppUserReward(fsUserId, activityType, appRoleConfig);
|
|
|
try {
|
|
try {
|
|
|
// 插入奖品表
|
|
// 插入奖品表
|
|
|
int insertResult = rewardsMapper.insertFsUserRewards(reward);
|
|
int insertResult = rewardsMapper.insertFsUserRewards(reward);
|
|
@@ -75,7 +106,7 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
|
|
|
|
|
if (updateResult > 0) {
|
|
if (updateResult > 0) {
|
|
|
log.info("用户首次注册奖励发放成功: userId={}, rewardType={}, rewardsId={}",
|
|
log.info("用户首次注册奖励发放成功: userId={}, rewardType={}, rewardsId={}",
|
|
|
- fsUserId, rewardType, reward.getId());
|
|
|
|
|
|
|
+ fsUserId, reward.getRewardType(), reward.getId());
|
|
|
} else {
|
|
} else {
|
|
|
log.error("用户首次登录时间更新失败: userId={}, 但奖品已发放, rewardsId={}",
|
|
log.error("用户首次登录时间更新失败: userId={}, 但奖品已发放, rewardsId={}",
|
|
|
fsUserId, reward.getId());
|
|
fsUserId, reward.getId());
|
|
@@ -89,105 +120,163 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public List<FsUserRewards> getMyRewardList(FsUserRewards fsUserRewards) {
|
|
|
|
|
- List<FsUserRewards> fsUserRewardsList = rewardsMapper.selectFsUserRewardsList(fsUserRewards);
|
|
|
|
|
- if (CollectionUtils.isEmpty(fsUserRewardsList)){
|
|
|
|
|
- return Collections.emptyList();
|
|
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public void addUserWatchCourseRewards(Long fsUserId) {
|
|
|
|
|
+ String activityType = ActivityTypeEnum.WATCH_COURSE.getCode();
|
|
|
|
|
+
|
|
|
|
|
+ //根据配置获取奖品类型、金额等
|
|
|
|
|
+ FsAppRole appRoleConfig = getAppRoleConfig(fsUserId);
|
|
|
|
|
+ if (appRoleConfig == null) {
|
|
|
|
|
+ log.info("当前用户未匹配到App奖励角色");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 构建奖品记录 - 把appRoleConfig传进去,让build方法自己处理
|
|
|
|
|
+ FsUserRewards reward = buildAppUserReward(fsUserId, activityType, appRoleConfig);
|
|
|
|
|
+
|
|
|
|
|
+ // 插入奖品表
|
|
|
|
|
+ int insertResult = rewardsMapper.insertFsUserRewards(reward);
|
|
|
|
|
+ if (insertResult > 0) {
|
|
|
|
|
+ log.info("用户看课奖励发放成功: userId={}, rewardType={}, rewardsId={}",
|
|
|
|
|
+ fsUserId, reward.getRewardType(), reward.getId());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("用户看课奖励发放失败: userId={}", fsUserId);
|
|
|
}
|
|
}
|
|
|
- return fsUserRewardsList;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * 根据活动类型和角色配置,构建奖品记录
|
|
|
* @param fsUserId 用户ID
|
|
* @param fsUserId 用户ID
|
|
|
* @param activityType 活动类型
|
|
* @param activityType 活动类型
|
|
|
- * @param rewardType 奖品类型
|
|
|
|
|
- * @param productType 产品类型 1-药品 2-套餐包
|
|
|
|
|
- * @param goodsId 1-红包 2-积分 3-商品
|
|
|
|
|
- * @param rewardAmount 红包
|
|
|
|
|
- * @param rewardPoints 积分
|
|
|
|
|
- * */
|
|
|
|
|
- private FsUserRewards buildAppUserReward(Long fsUserId, String activityType, Integer rewardType, Integer productType, Long goodsId,BigDecimal rewardAmount,Long rewardPoints,String goodsName,String goodsUrl) {
|
|
|
|
|
|
|
+ * @param appRoleConfig 角色配置
|
|
|
|
|
+ */
|
|
|
|
|
+ private FsUserRewards buildAppUserReward(Long fsUserId, String activityType, FsAppRole appRoleConfig) {
|
|
|
FsUserRewards reward = new FsUserRewards();
|
|
FsUserRewards reward = new FsUserRewards();
|
|
|
reward.setFsUserId(fsUserId);
|
|
reward.setFsUserId(fsUserId);
|
|
|
reward.setActivityType(activityType);
|
|
reward.setActivityType(activityType);
|
|
|
- reward.setRewardType(rewardType);
|
|
|
|
|
reward.setStatus(0); // 待领取
|
|
reward.setStatus(0); // 待领取
|
|
|
reward.setCreateTime(DateUtils.getNowDate());
|
|
reward.setCreateTime(DateUtils.getNowDate());
|
|
|
- if (ActivityTypeEnum.FIRST_LOGIN.getCode().equals(activityType)){
|
|
|
|
|
- reward.setIsFirstLogin(1); // 标记为首次注册奖励(用于虚拟列索引),首次登录奖励 必传1!!!
|
|
|
|
|
- }
|
|
|
|
|
- if (rewardType == 1){//红包
|
|
|
|
|
- reward.setRewardAmount(rewardAmount);
|
|
|
|
|
- }
|
|
|
|
|
- else if (rewardType == 2){//积分
|
|
|
|
|
- reward.setRewardPoints(rewardPoints);
|
|
|
|
|
- }else if (rewardType == 3){//实物商品
|
|
|
|
|
- reward.setProductType(productType); // 1-药品 2-套餐包
|
|
|
|
|
- reward.setGoodsId(goodsId); // 药品/套餐包id
|
|
|
|
|
- reward.setGoodsName(goodsName);
|
|
|
|
|
- reward.setGoodsUrl(goodsUrl);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 根据活动类型设置不同字段
|
|
|
|
|
+ if (ActivityTypeEnum.FIRST_LOGIN.getCode().equals(activityType)) {
|
|
|
|
|
+ // 首次登录奖励
|
|
|
|
|
+ reward.setIsFirstLogin(1); // 标记为首次注册奖励
|
|
|
|
|
+
|
|
|
|
|
+ // 首次登录用 register 相关字段
|
|
|
|
|
+ reward.setRewardType(appRoleConfig.getRegisterRewardType());
|
|
|
|
|
+
|
|
|
|
|
+ if (appRoleConfig.getRegisterRewardType() == 1) { // 红包
|
|
|
|
|
+ reward.setRewardAmount(appRoleConfig.getRegisterRedPacket());
|
|
|
|
|
+ } else if (appRoleConfig.getRegisterRewardType() == 2) { // 积分
|
|
|
|
|
+ reward.setRewardPoints(appRoleConfig.getRegisterIntegral() != null ?
|
|
|
|
|
+ appRoleConfig.getRegisterIntegral().longValue() : 0L);
|
|
|
|
|
+ } else if (appRoleConfig.getRegisterRewardType() == 3) { // 实物商品
|
|
|
|
|
+ reward.setProductType(appRoleConfig.getRegisterProductType());
|
|
|
|
|
+ reward.setGoodsId(appRoleConfig.getRegisterProductId());
|
|
|
|
|
+ reward.setGoodsName(appRoleConfig.getGoodsName());
|
|
|
|
|
+ reward.setGoodsUrl(appRoleConfig.getGoodsUrl());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } else if (ActivityTypeEnum.WATCH_COURSE.getCode().equals(activityType)) {
|
|
|
|
|
+ // 看课奖励
|
|
|
|
|
+ reward.setRewardType(appRoleConfig.getCourseRewardType());
|
|
|
|
|
+
|
|
|
|
|
+ // 获取当前用户的有效看课天数
|
|
|
|
|
+ FsUser fsUser = fsUserMapper.selectFsUserById(fsUserId);
|
|
|
|
|
+ if (fsUser == null) {
|
|
|
|
|
+ log.info("用户不存在,用户id:{}", fsUserId);
|
|
|
|
|
+ return reward;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer targetDay = fsUser.getAppRewardsViewedDays() == null ? 0 : fsUser.getAppRewardsViewedDays();
|
|
|
|
|
+
|
|
|
|
|
+ // 根据奖品类型设置具体值
|
|
|
|
|
+ if (appRoleConfig.getCourseRewardType() == 1) { // 红包
|
|
|
|
|
+ BigDecimal amount = calculateRewardValue(appRoleConfig, targetDay, true);
|
|
|
|
|
+ reward.setRewardAmount(amount);
|
|
|
|
|
+ } else if (appRoleConfig.getCourseRewardType() == 2) { // 积分
|
|
|
|
|
+ Long points = calculateRewardValue(appRoleConfig, targetDay, false);
|
|
|
|
|
+ reward.setRewardPoints(points);
|
|
|
|
|
+ } else if (appRoleConfig.getCourseRewardType() == 3) { // 实物商品
|
|
|
|
|
+ reward.setProductType(appRoleConfig.getCourseProductType());
|
|
|
|
|
+ reward.setGoodsId(appRoleConfig.getCourseProductId());
|
|
|
|
|
+ reward.setGoodsName(appRoleConfig.getGoodsName());
|
|
|
|
|
+ reward.setGoodsUrl(appRoleConfig.getGoodsUrl());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return reward;
|
|
return reward;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //为当前用户新增看课类型的奖励
|
|
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
- public void addUserWatchCourseRewards(Long fsUserId) {
|
|
|
|
|
- String activityType = ActivityTypeEnum.WATCH_COURSE.getCode();
|
|
|
|
|
- // TODO 根据配置获取奖品类型、金额等
|
|
|
|
|
- FsAppRole appRoleConfig = getAppRoleConfig(fsUserId);
|
|
|
|
|
- if (appRoleConfig==null){
|
|
|
|
|
- log.info("当前用户未匹配到App奖励角色");
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- Integer rewardType = appRoleConfig.getRegisterRewardType();
|
|
|
|
|
- Integer productType=appRoleConfig.getRegisterProductType();
|
|
|
|
|
- Long goodsId=appRoleConfig.getRegisterProductId();
|
|
|
|
|
- BigDecimal rewardAmount=appRoleConfig.getRegisterRedPacket();
|
|
|
|
|
- Long rewardPoints=appRoleConfig.getRegisterIntegral().longValue();
|
|
|
|
|
- String goodsName = appRoleConfig.getGoodsName();
|
|
|
|
|
- String goodsUrl = appRoleConfig.getGoodsUrl();
|
|
|
|
|
- // 构建奖品记录
|
|
|
|
|
- FsUserRewards reward = buildAppUserReward(fsUserId, activityType, rewardType, productType, goodsId, rewardAmount,rewardPoints,goodsName,goodsUrl);
|
|
|
|
|
- // 插入奖品表
|
|
|
|
|
- int insertResult = rewardsMapper.insertFsUserRewards(reward);
|
|
|
|
|
- if (insertResult > 0) {
|
|
|
|
|
- log.info("用户看课奖励发放成功: userId={}, rewardType={}, rewardsId={}",
|
|
|
|
|
- fsUserId, rewardType, reward.getId());
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 计算奖励值(红包或积分)
|
|
|
|
|
+ * @param appRoleConfig 角色配置
|
|
|
|
|
+ * @param targetDay 目标天数
|
|
|
|
|
+ * @param isRedPackage true-红包,false-积分
|
|
|
|
|
+ */
|
|
|
|
|
+ private <T extends Number> T calculateRewardValue(FsAppRole appRoleConfig, Integer targetDay, boolean isRedPackage) {
|
|
|
|
|
+ if (appRoleConfig.getCourseRewardRuleType() == 2) {
|
|
|
|
|
+ // 定制规则
|
|
|
|
|
+ String courseRewardRule = appRoleConfig.getCourseRewardRule();
|
|
|
|
|
+ if (courseRewardRule == null || courseRewardRule.isEmpty()) {
|
|
|
|
|
+ return isRedPackage ? (T) BigDecimal.ZERO : (T) Long.valueOf(0L);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ List<Map<String, Object>> rewardList = objectMapper.readValue(
|
|
|
|
|
+ courseRewardRule,
|
|
|
|
|
+ new TypeReference<List<Map<String, Object>>>() {}
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ for (Map<String, Object> item : rewardList) {
|
|
|
|
|
+ Integer day = ((Number) item.get("day")).intValue();
|
|
|
|
|
+ if (day.equals(targetDay)) {
|
|
|
|
|
+ if (isRedPackage) {
|
|
|
|
|
+ return (T) new BigDecimal(item.get("amount").toString());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return (T) Long.valueOf(((Number) item.get("amount")).longValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return isRedPackage ? (T) BigDecimal.ZERO : (T) Long.valueOf(0L);
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("解析规则失败: {}", courseRewardRule, e);
|
|
|
|
|
+ return isRedPackage ? (T) BigDecimal.ZERO : (T) Long.valueOf(0L);
|
|
|
|
|
+ }
|
|
|
} else {
|
|
} else {
|
|
|
- log.error("用户看课奖励发放失败: userId={}, 但奖品已发放, rewardsId={}",
|
|
|
|
|
- fsUserId, reward.getId());
|
|
|
|
|
|
|
+ // 平均规则
|
|
|
|
|
+ if (isRedPackage) {
|
|
|
|
|
+ BigDecimal total = appRoleConfig.getCourseRedPacket();
|
|
|
|
|
+ Long days = appRoleConfig.getCourseNeedDay();
|
|
|
|
|
+ if (total == null || days == null || days == 0) {
|
|
|
|
|
+ return (T) BigDecimal.ZERO;
|
|
|
|
|
+ }
|
|
|
|
|
+ return (T) total.divide(BigDecimal.valueOf(days), 2, RoundingMode.HALF_UP);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Long total = appRoleConfig.getCourseIntegral() != null ?
|
|
|
|
|
+ appRoleConfig.getCourseIntegral().longValue() : 0L;
|
|
|
|
|
+ Long days = appRoleConfig.getCourseNeedDay();
|
|
|
|
|
+ if (days == null || days == 0) {
|
|
|
|
|
+ return (T) Long.valueOf(0L);
|
|
|
|
|
+ }
|
|
|
|
|
+ return (T) Long.valueOf(total / days);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
- public void claimRewards(Long fsUserId,Long rewardsId,Long addressId) {
|
|
|
|
|
- FsUserRewards reward = rewardsMapper.selectByUserIdAndRewardsId(fsUserId, rewardsId);
|
|
|
|
|
- if (reward==null){
|
|
|
|
|
- log.info("用户:{}没有奖品:{}", fsUserId, rewardsId);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- // 校验是否过期(目前只有看课奖品需要校验过期)
|
|
|
|
|
- if (ActivityTypeEnum.WATCH_COURSE.getCode().equals(reward.getActivityType())) {
|
|
|
|
|
- if (isExpired(reward.getCreateTime())) {
|
|
|
|
|
- log.info("奖品已过期: rewardsId={}", rewardsId);
|
|
|
|
|
- // 更新奖品状态为已过期
|
|
|
|
|
- rewardsMapper.updateStatus(reward.getId(), 2, null, null);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public List<FsUserRewards> getMyRewardList(FsUserRewards fsUserRewards) {
|
|
|
|
|
+ List<FsUserRewards> fsUserRewardsList = rewardsMapper.selectFsUserRewardsList(fsUserRewards);
|
|
|
|
|
+ if (CollectionUtils.isEmpty(fsUserRewardsList)) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
}
|
|
}
|
|
|
- reward.setAddressId(addressId);
|
|
|
|
|
- //执行奖品领取逻辑
|
|
|
|
|
- RewardStrategy strategy = strategyFactory.getStrategy(reward.getActivityType(), reward.getRewardType());
|
|
|
|
|
- RewardResult result = strategy.process(reward);
|
|
|
|
|
- if (result.isSuccess()) {
|
|
|
|
|
- // 更新奖品状态
|
|
|
|
|
- rewardsMapper.updateStatus(rewardsId, 1, result.getOrderCode(), DateUtils.getNowDate());
|
|
|
|
|
- log.info("奖品领取成功: rewardsId={}, orderCode={}", rewardsId, result.getOrderCode());
|
|
|
|
|
- } else {
|
|
|
|
|
- log.error("奖品领取失败: rewardsId={}, reason={}", rewardsId, result.getMessage());
|
|
|
|
|
- throw new CustomException(result.getMessage());
|
|
|
|
|
|
|
+ // 计算每个奖品的剩余过期天数
|
|
|
|
|
+ for (FsUserRewards reward : fsUserRewardsList) {
|
|
|
|
|
+ Long expireDays = calculateExpireDays(reward);
|
|
|
|
|
+ reward.setExpireDays(expireDays);
|
|
|
}
|
|
}
|
|
|
|
|
+ return fsUserRewardsList;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -223,9 +312,8 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
String activityType = ActivityTypeEnum.FIRST_LOGIN.getCode();
|
|
String activityType = ActivityTypeEnum.FIRST_LOGIN.getCode();
|
|
|
FsUserRewards queryReward = rewardsMapper.selectByUserIdAndActivityType(fsUserId, activityType);
|
|
FsUserRewards queryReward = rewardsMapper.selectByUserIdAndActivityType(fsUserId, activityType);
|
|
|
if (queryReward == null) {
|
|
if (queryReward == null) {
|
|
|
- //TODO 查询当前用户对应的角色配置返回红包、积分、商品弹窗信息
|
|
|
|
|
- fsAppRole = appRoleMapper.selectFsAppRoleAndRewardsById(1L);
|
|
|
|
|
- fsAppRole.setShow(true);//弹窗显示
|
|
|
|
|
|
|
+ //查询当前用户对应的角色配置返回红包、积分、商品弹窗信息
|
|
|
|
|
+ fsAppRole = getAppRoleConfig(fsUserId);
|
|
|
return fsAppRole;
|
|
return fsAppRole;
|
|
|
}
|
|
}
|
|
|
return fsAppRole;
|
|
return fsAppRole;
|
|
@@ -240,26 +328,103 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
FsUserRewards queryReward = rewardsMapper.selectByUserIdAndActivityType(fsUserId, activityType);
|
|
FsUserRewards queryReward = rewardsMapper.selectByUserIdAndActivityType(fsUserId, activityType);
|
|
|
if (queryReward == null) {
|
|
if (queryReward == null) {
|
|
|
//TODO 查询当前用户对应的角色配置返回红包、积分、商品弹窗信息(还需要判断当前用户是否满足看课奖励)
|
|
//TODO 查询当前用户对应的角色配置返回红包、积分、商品弹窗信息(还需要判断当前用户是否满足看课奖励)
|
|
|
- fsAppRole = appRoleMapper.selectFsAppRoleById(1L);
|
|
|
|
|
- //从缓存查询当前用户看课天数对比角色要求的看课天数,达标才返回
|
|
|
|
|
- fsAppRole.setShow(true);//弹窗显示
|
|
|
|
|
|
|
+ fsAppRole = getAppRoleConfig(fsUserId);
|
|
|
return fsAppRole;
|
|
return fsAppRole;
|
|
|
}
|
|
}
|
|
|
return fsAppRole;
|
|
return fsAppRole;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //奖品是否过期的判断方法
|
|
|
|
|
- private boolean isExpired(Date createTime) {
|
|
|
|
|
- // TODO 实现过期判断逻辑
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 积分、商品是否过期的判断方法(红包需要另外方法计算)
|
|
|
|
|
+ * @param reward: 奖品
|
|
|
|
|
+ * @return true: 过期 false: 未过期
|
|
|
|
|
+ * */
|
|
|
|
|
+ private boolean isExpired(FsUserRewards reward) {
|
|
|
|
|
+ // 奖品产生时间
|
|
|
|
|
+ Date rewardTime = reward.getCreateTime();
|
|
|
|
|
+ // 获取当前时间
|
|
|
|
|
+ Date currentTime = DateUtils.getNowDate();
|
|
|
|
|
+ // 获取当前角色配置(奖品有效期)
|
|
|
|
|
+ FsAppRole fsAppRole = appRoleMapper.selectHighestLevelAppRoleByUserId(reward.getFsUserId(), "false");
|
|
|
|
|
+
|
|
|
|
|
+ // 有效天数
|
|
|
|
|
+ if (fsAppRole == null) {
|
|
|
|
|
+ log.info("未找到用户:{}对应的角色配置", reward.getFsUserId());
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 奖品有效期(天)
|
|
|
|
|
+ Long validDays = fsAppRole.getCourseDay();
|
|
|
|
|
+ if (validDays == null || validDays <= 0) {
|
|
|
|
|
+ // 如果有效期配置为0或负数,可以认为是永久有效
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 将奖品生成时间加上有效天数,得到过期时间点
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.setTime(rewardTime);
|
|
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, validDays.intValue()); // 加上有效天数
|
|
|
|
|
+
|
|
|
|
|
+ // 获取加完天数后的日期,清零时分秒,只比较日期
|
|
|
|
|
+ Date expirationDate = DateUtils.truncate(calendar.getTime(), Calendar.DAY_OF_MONTH);
|
|
|
|
|
+ // 同样,将当前时间也清零时分秒进行比较
|
|
|
|
|
+ Date currentDate = DateUtils.truncate(currentTime, Calendar.DAY_OF_MONTH);
|
|
|
|
|
+
|
|
|
|
|
+ // 如果过期日期 < 当前日期,则表示已过期
|
|
|
|
|
+ return expirationDate.before(currentDate);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
//根据用户id、角色id获取配置的奖品信息
|
|
//根据用户id、角色id获取配置的奖品信息
|
|
|
private FsAppRole getAppRoleConfig(Long fsUserId){
|
|
private FsAppRole getAppRoleConfig(Long fsUserId){
|
|
|
- //TODO 获取当前用户最高权重角色id
|
|
|
|
|
- FsAppRole fsAppRole = appRoleMapper.selectFsAppRoleAndRewardsById(1L);
|
|
|
|
|
|
|
+ //获取当前用户最高权重角色, "false"表示查询等级数字最小的权重角色
|
|
|
|
|
+ FsAppRole fsAppRole = appRoleMapper.selectHighestLevelAppRoleByUserId(fsUserId,"false");
|
|
|
if (fsAppRole==null){
|
|
if (fsAppRole==null){
|
|
|
|
|
+ log.info("未找到用户:{}对应的角色配置", fsUserId);
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
+ fsAppRole.setShow(true);//弹窗显示
|
|
|
return fsAppRole;
|
|
return fsAppRole;
|
|
|
}
|
|
}
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 计算单个奖品的剩余过期天数
|
|
|
|
|
+ * @param reward 奖品实体
|
|
|
|
|
+ * @return 剩余天数。null: 表示无有效期或查询失败;0: 今天过期或已过期;正数: 剩余天数
|
|
|
|
|
+ */
|
|
|
|
|
+ private Long calculateExpireDays(FsUserRewards reward) {
|
|
|
|
|
+ // 1. 获取奖品创建时间
|
|
|
|
|
+ Date rewardCreateTime = reward.getCreateTime();
|
|
|
|
|
+ if (rewardCreateTime == null) {
|
|
|
|
|
+ return null; // 如果创建时间为空,无法计算
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 获取用户的角色配置,以确定有效期
|
|
|
|
|
+ FsAppRole fsAppRole = appRoleMapper.selectHighestLevelAppRoleByUserId(reward.getFsUserId(), "false");
|
|
|
|
|
+ if (fsAppRole == null) {
|
|
|
|
|
+ // 如果找不到角色配置,可以视为永不过期,返回一个很大的数,或者返回null
|
|
|
|
|
+ // 这里假设没有配置就不过期
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Long validDays = fsAppRole.getCourseDay();
|
|
|
|
|
+ if (validDays == null || validDays <= 0) {
|
|
|
|
|
+ // 如果有效期配置为0或负数,也视为永不过期
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 计算过期日期
|
|
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
|
|
+ cal.setTime(rewardCreateTime);
|
|
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, validDays.intValue());
|
|
|
|
|
+ Date expireDate = DateUtils.truncate(cal.getTime(), Calendar.DAY_OF_MONTH);
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 计算剩余天数
|
|
|
|
|
+ Date now = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
|
|
|
|
|
+
|
|
|
|
|
+ long diffInMillis = expireDate.getTime() - now.getTime();
|
|
|
|
|
+ long daysDiff = diffInMillis / (1000 * 60 * 60 * 24);
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 返回结果
|
|
|
|
|
+ // 如果差值小于0,说明已过期,返回0
|
|
|
|
|
+ // 如果差值>=0,说明未过期,返回剩余天数
|
|
|
|
|
+ return Math.max(0L, daysDiff);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|