|
@@ -1,18 +1,28 @@
|
|
|
package com.fs.his.service.impl;
|
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.exception.ServiceException;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
+import com.fs.course.config.CourseConfig;
|
|
|
+import com.fs.course.domain.FsCourseAnswerReward;
|
|
|
import com.fs.his.domain.FsChineseMedicine;
|
|
|
import com.fs.his.domain.FsIntegralGoods;
|
|
|
+import com.fs.his.domain.FsUser;
|
|
|
import com.fs.his.mapper.FsIntegralGoodsMapper;
|
|
|
+import com.fs.his.mapper.FsUserMapper;
|
|
|
import com.fs.his.param.FsIntegralGoodsListUParam;
|
|
|
import com.fs.his.service.IFsIntegralGoodsService;
|
|
|
import com.fs.his.vo.FsIntegralGoodsListUVO;
|
|
|
import com.fs.his.vo.FsIntegralGoodsListVO;
|
|
|
+import com.fs.system.service.ISysConfigService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Random;
|
|
|
|
|
|
/**
|
|
|
* 积分商品Service业务层处理
|
|
@@ -21,10 +31,16 @@ import java.util.List;
|
|
|
* @date 2023-11-02
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class FsIntegralGoodsServiceImpl implements IFsIntegralGoodsService
|
|
|
{
|
|
|
@Autowired
|
|
|
private FsIntegralGoodsMapper fsIntegralGoodsMapper;
|
|
|
+ @Autowired
|
|
|
+ private FsUserMapper fsUserMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysConfigService configService;
|
|
|
|
|
|
/**
|
|
|
* 查询积分商品
|
|
@@ -149,4 +165,82 @@ public class FsIntegralGoodsServiceImpl implements IFsIntegralGoodsService
|
|
|
}
|
|
|
return successMsg.toString();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R getCourseIntegralGoods(Long userId) {
|
|
|
+ log.info("获取答题奖励信息, userId:{}", userId);
|
|
|
+ String json = configService.selectConfigByKey("course.config");
|
|
|
+ CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
|
|
|
+ FsUser user = fsUserMapper.selectFsUserByUserId(userId);
|
|
|
+
|
|
|
+ // 1. 创建返回对象
|
|
|
+ FsCourseAnswerReward reward = new FsCourseAnswerReward();
|
|
|
+
|
|
|
+ // 2. 设置当前积分
|
|
|
+ Integer currentPoints = user.getIntegral().intValue();
|
|
|
+ reward.setCurrentPoints(currentPoints);
|
|
|
+
|
|
|
+ // 3. 计算用户所在阶段(每5000积分一个阶段)
|
|
|
+ int currentStage = (currentPoints / 5000) + 1;
|
|
|
+ int stageMaxPoints = currentStage * 5000;
|
|
|
+
|
|
|
+ // 4. 获取当前阶段的积分商品(随机获取3个)
|
|
|
+ FsIntegralGoodsListUParam param = new FsIntegralGoodsListUParam();
|
|
|
+ param.setMinPoints(currentPoints);
|
|
|
+ param.setMaxPoints(stageMaxPoints);
|
|
|
+ List<FsIntegralGoods> stageGoods = selectRandomStageGoods(param);
|
|
|
+
|
|
|
+ // 5. 转换为奖励商品列表
|
|
|
+ List<FsCourseAnswerReward.RewardProduct> products = new ArrayList<>();
|
|
|
+ int minRequiredPoints = stageMaxPoints; // 用于计算进度的最小积分要求
|
|
|
+
|
|
|
+ for (FsIntegralGoods goods : stageGoods) {
|
|
|
+ FsCourseAnswerReward.RewardProduct product = new FsCourseAnswerReward.RewardProduct();
|
|
|
+ product.setProductId(goods.getGoodsId());
|
|
|
+ product.setProductName(goods.getGoodsName());
|
|
|
+ product.setRequiredPoints(goods.getIntegral().intValue());
|
|
|
+ product.setImageUrl(goods.getImgUrl());
|
|
|
+ product.setOtPrice(goods.getOtPrice());
|
|
|
+ products.add(product);
|
|
|
+
|
|
|
+ // 更新最小所需积分
|
|
|
+ if (goods.getIntegral().intValue() < minRequiredPoints) {
|
|
|
+ minRequiredPoints = goods.getIntegral().intValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ reward.setProducts(products);
|
|
|
+
|
|
|
+ // 6. 计算兑换进度(基于最低积分商品)
|
|
|
+ int exchangeProgress = (int) ((currentPoints * 100.0) / minRequiredPoints);
|
|
|
+ // 进度最大显示100%
|
|
|
+ reward.setExchangeProgress(Math.min(exchangeProgress, 100));
|
|
|
+ reward.setAvailableCoins(config.getAnswerIntegral());
|
|
|
+ log.info("答题奖励信息: {}", reward);
|
|
|
+ return R.ok().put("data", reward);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 随机获取当前阶段的积分商品
|
|
|
+ */
|
|
|
+ private List<FsIntegralGoods> selectRandomStageGoods(FsIntegralGoodsListUParam param) {
|
|
|
+ // 查询当前阶段的所有商品
|
|
|
+ List<FsIntegralGoods> allGoods = fsIntegralGoodsMapper.selectFsIntegralGoodsByStage(param);
|
|
|
+
|
|
|
+ // 如果商品数量不足3个,直接返回全部
|
|
|
+ if (allGoods.size() <= 3) {
|
|
|
+ return allGoods;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 随机选择3个商品
|
|
|
+ List<FsIntegralGoods> selectedGoods = new ArrayList<>();
|
|
|
+ Random random = new Random();
|
|
|
+
|
|
|
+ while (selectedGoods.size() < 3 && !allGoods.isEmpty()) {
|
|
|
+ int index = random.nextInt(allGoods.size());
|
|
|
+ selectedGoods.add(allGoods.get(index));
|
|
|
+ allGoods.remove(index);
|
|
|
+ }
|
|
|
+
|
|
|
+ return selectedGoods;
|
|
|
+ }
|
|
|
}
|