|
@@ -1,18 +1,27 @@
|
|
|
package com.fs.his.service.impl;
|
|
package com.fs.his.service.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
import com.fs.common.exception.CustomException;
|
|
import com.fs.common.exception.CustomException;
|
|
|
|
|
+import com.fs.common.utils.DateUtils;
|
|
|
|
|
+import com.fs.his.domain.FsUser;
|
|
|
import com.fs.his.domain.FsUserRewards;
|
|
import com.fs.his.domain.FsUserRewards;
|
|
|
import com.fs.his.enums.ActivityTypeEnum;
|
|
import com.fs.his.enums.ActivityTypeEnum;
|
|
|
|
|
+import com.fs.his.mapper.FsUserMapper;
|
|
|
import com.fs.his.mapper.FsUserRewardsMapper;
|
|
import com.fs.his.mapper.FsUserRewardsMapper;
|
|
|
import com.fs.his.service.IAppUserRewardService;
|
|
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 lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@Service
|
|
@Service
|
|
@@ -20,10 +29,103 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private FsUserRewardsMapper rewardsMapper;
|
|
private FsUserRewardsMapper rewardsMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private FsUserMapper fsUserMapper;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private RewardStrategyFactory strategyFactory;
|
|
private RewardStrategyFactory strategyFactory;
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean checkFirstLoginRewardStatus(Long fsUserId) {
|
|
|
|
|
+ // 1. 查用户表判断是否首次登录注册App
|
|
|
|
|
+ FsUser fsUser = fsUserMapper.selectFsUserById(fsUserId);
|
|
|
|
|
+ if (fsUser != null && fsUser.getFirstLoginTime() != null) {
|
|
|
|
|
+ log.info("用户:{}已注册登录过,非首次登录", fsUserId);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 查奖品表判断是否已发放奖励
|
|
|
|
|
+ String activityType = ActivityTypeEnum.FIRST_LOGIN.getCode();
|
|
|
|
|
+ FsUserRewards queryReward = rewardsMapper.selectByUserIdAndActivityType(fsUserId, activityType);
|
|
|
|
|
+ if (queryReward != null) {
|
|
|
|
|
+ log.info("用户:{}已下发过首次注册奖励, status={}", fsUserId, queryReward.getStatus());
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public void addUserFirstLoginRewards(Long fsUserId) {
|
|
|
|
|
+ String activityType = ActivityTypeEnum.FIRST_LOGIN.getCode();
|
|
|
|
|
+
|
|
|
|
|
+ // TODO 根据配置获取奖品类型
|
|
|
|
|
+ Integer rewardType = RandomUtil.randomInt(1, 4);
|
|
|
|
|
+
|
|
|
|
|
+ // 构建奖品记录
|
|
|
|
|
+ FsUserRewards reward = buildFirstLoginReward(fsUserId, activityType, rewardType);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 插入奖品表
|
|
|
|
|
+ int insertResult = rewardsMapper.insertFsUserRewards(reward);
|
|
|
|
|
+ if (insertResult > 0) {
|
|
|
|
|
+ // 更新用户表的首次登录时间
|
|
|
|
|
+ FsUser updateFsUser = new FsUser();
|
|
|
|
|
+ updateFsUser.setUserId(fsUserId);
|
|
|
|
|
+ updateFsUser.setFirstLoginTime(DateUtils.getNowDate());
|
|
|
|
|
+
|
|
|
|
|
+ int updateResult = fsUserMapper.updateFsUser(updateFsUser);
|
|
|
|
|
+
|
|
|
|
|
+ if (updateResult > 0) {
|
|
|
|
|
+ log.info("用户首次注册奖励发放成功: userId={}, rewardType={}, rewardsId={}",
|
|
|
|
|
+ fsUserId, rewardType, reward.getId());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("用户首次登录时间更新失败: userId={}, 但奖品已发放, rewardsId={}",
|
|
|
|
|
+ fsUserId, reward.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
|
|
+ log.info("用户首次注册奖励已存在: userId={}", fsUserId);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("用户首次注册奖励发放异常: userId={}, error={}", fsUserId, e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<FsUserRewards> getMyRewardList(FsUserRewards fsUserRewards) {
|
|
|
|
|
+ List<FsUserRewards> fsUserRewardsList = rewardsMapper.selectFsUserRewardsList(fsUserRewards);
|
|
|
|
|
+ if (CollectionUtils.isEmpty(fsUserRewardsList)){
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ return fsUserRewardsList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private FsUserRewards buildFirstLoginReward(Long fsUserId, String activityType, Integer rewardType) {
|
|
|
|
|
+ FsUserRewards reward = new FsUserRewards();
|
|
|
|
|
+ reward.setFsUserId(fsUserId);
|
|
|
|
|
+ reward.setActivityType(activityType);
|
|
|
|
|
+ reward.setRewardType(rewardType);
|
|
|
|
|
+ reward.setStatus(0); // 待领取
|
|
|
|
|
+ reward.setCreateTime(DateUtils.getNowDate());
|
|
|
|
|
+ reward.setIsFirstLogin(1); // 标记为首次注册奖励(用于虚拟列索引)
|
|
|
|
|
+
|
|
|
|
|
+ if (rewardType == 3) {
|
|
|
|
|
+ // TODO 根据配置设置具体的商品
|
|
|
|
|
+ reward.setProductType(1); // 1-药品
|
|
|
|
|
+ reward.setGoodsId(1L); // 默认商品ID
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return reward;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //为当前用户新增看课奖励
|
|
|
|
|
+ public void addUserWatchCourseRewards(Long fsUserId) {
|
|
|
|
|
+ //看课奖励可以有多个
|
|
|
|
|
+
|
|
|
|
|
+ //TODO 根据用户id从配置获取用户领取奖品类型、产品类型是
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public void claimRewards(Long fsUserId,Long rewardsId) {
|
|
public void claimRewards(Long fsUserId,Long rewardsId) {
|
|
|
FsUserRewards reward = rewardsMapper.selectByUserIdAndRewardsId(fsUserId, rewardsId);
|
|
FsUserRewards reward = rewardsMapper.selectByUserIdAndRewardsId(fsUserId, rewardsId);
|
|
@@ -31,29 +133,29 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
|
|
|
log.info("用户:{}没有奖品:{}", fsUserId, rewardsId);
|
|
log.info("用户:{}没有奖品:{}", fsUserId, rewardsId);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- // 校验是否过期(目前只有看课奖励需要校验过期)
|
|
|
|
|
|
|
+ // 校验是否过期(目前只有看课奖品需要校验过期)
|
|
|
if (ActivityTypeEnum.WATCH_COURSE.getCode().equals(reward.getActivityType())) {
|
|
if (ActivityTypeEnum.WATCH_COURSE.getCode().equals(reward.getActivityType())) {
|
|
|
if (isExpired(reward.getCreateTime())) {
|
|
if (isExpired(reward.getCreateTime())) {
|
|
|
- log.info("奖品已过期: rewardId={}", rewardsId);
|
|
|
|
|
|
|
+ log.info("奖品已过期: rewardsId={}", rewardsId);
|
|
|
// 更新奖品状态为已过期
|
|
// 更新奖品状态为已过期
|
|
|
- rewardsMapper.updateStatus(reward.getId(), 2, null, new Date());
|
|
|
|
|
|
|
+ rewardsMapper.updateStatus(reward.getId(), 2, null, null);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- //执行奖品发放逻辑
|
|
|
|
|
|
|
+ //执行奖品领取逻辑
|
|
|
RewardStrategy strategy = strategyFactory.getStrategy(reward.getActivityType(), reward.getRewardType());
|
|
RewardStrategy strategy = strategyFactory.getStrategy(reward.getActivityType(), reward.getRewardType());
|
|
|
RewardResult result = strategy.process(reward);
|
|
RewardResult result = strategy.process(reward);
|
|
|
if (result.isSuccess()) {
|
|
if (result.isSuccess()) {
|
|
|
// 更新奖品状态
|
|
// 更新奖品状态
|
|
|
- rewardsMapper.updateStatus(rewardsId, 1, result.getOrderCode(), new Date());
|
|
|
|
|
- log.info("奖品发放成功: rewardId={}, orderCode={}", rewardsId, result.getOrderCode());
|
|
|
|
|
|
|
+ rewardsMapper.updateStatus(rewardsId, 1, result.getOrderCode(), DateUtils.getNowDate());
|
|
|
|
|
+ log.info("奖品领取成功: rewardsId={}, orderCode={}", rewardsId, result.getOrderCode());
|
|
|
} else {
|
|
} else {
|
|
|
- log.error("奖品发放失败: rewardId={}, reason={}", rewardsId, result.getMessage());
|
|
|
|
|
|
|
+ log.error("奖品领取失败: rewardsId={}, reason={}", rewardsId, result.getMessage());
|
|
|
throw new CustomException(result.getMessage());
|
|
throw new CustomException(result.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //奖品过期的判断方法
|
|
|
|
|
|
|
+ //奖品是否过期的判断方法
|
|
|
private boolean isExpired(Date createTime) {
|
|
private boolean isExpired(Date createTime) {
|
|
|
// TODO 实现过期判断逻辑
|
|
// TODO 实现过期判断逻辑
|
|
|
return false;
|
|
return false;
|