zyp il y a 1 semaine
Parent
commit
bbed9ff092

+ 58 - 0
fs-service/src/main/java/com/fs/course/domain/FsCourseAnswerReward.java

@@ -0,0 +1,58 @@
+package com.fs.course.domain;
+
+import lombok.Data;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+@Data
+public class FsCourseAnswerReward {
+
+    /**
+     * 用户当前积分
+     */
+    private Integer currentPoints;
+
+    /**
+     * 奖励兑换进度(百分比)
+     */
+    private Integer exchangeProgress;
+
+    /**
+     * 可兑换的商品列表
+     */
+    private List<RewardProduct> products;
+
+    /**
+     * 本次获得的芳华币数量
+     */
+    private Integer availableCoins;
+
+    @Data
+    public static class RewardProduct {
+        /**
+         * 商品ID
+         */
+        private Long productId;
+
+        /**
+         * 商品名称
+         */
+        private String productName;
+
+        /**
+         * 所需积分
+         */
+        private Integer requiredPoints;
+
+        /**
+         * 商品图片URL
+         */
+        private String imageUrl;
+
+        /**
+         * 商品图片URL
+         */
+        private BigDecimal otPrice;
+    }
+}

+ 7 - 0
fs-service/src/main/java/com/fs/his/mapper/FsIntegralGoodsMapper.java

@@ -83,4 +83,11 @@ public interface FsIntegralGoodsMapper
             " order by g.goods_id desc "+
             "</script>"})
     List<FsIntegralGoodsListUVO> selectFsIntegralGoodsListUVO(@Param("maps")FsIntegralGoodsListUParam param);
+
+    /**
+     * 查询指定积分区间的商品
+     * @param param 查询参数
+     * @return 商品列表
+     */
+    List<FsIntegralGoods> selectFsIntegralGoodsByStage(FsIntegralGoodsListUParam param);
 }

+ 15 - 0
fs-service/src/main/java/com/fs/his/param/FsIntegralGoodsListUParam.java

@@ -13,4 +13,19 @@ public class FsIntegralGoodsListUParam  implements Serializable {
     @ApiModelProperty(value = "页大小,默认为10")
     private Integer pageSize = 10;
     private String keyword;
+
+    /**
+     * 最小积分
+     */
+    private Integer minPoints;
+
+    /**
+     * 最大积分
+     */
+    private Integer maxPoints;
+
+    /**
+     * 商品状态(1:上架 2:下架)
+     */
+    private Integer status;
 }

+ 3 - 0
fs-service/src/main/java/com/fs/his/service/IFsIntegralGoodsService.java

@@ -1,5 +1,6 @@
 package com.fs.his.service;
 
+import com.fs.common.core.domain.R;
 import com.fs.his.domain.FsIntegralGoods;
 import com.fs.his.param.FsIntegralGoodsListUParam;
 import com.fs.his.vo.FsIntegralGoodsListUVO;
@@ -68,4 +69,6 @@ public interface IFsIntegralGoodsService
     List<FsIntegralGoodsListUVO> selectFsIntegralGoodsListUVO(FsIntegralGoodsListUParam param);
 
     String importIntegralGoodsService(List<FsIntegralGoods> list);
+
+    R getCourseIntegralGoods(Long userId);
 }

+ 94 - 0
fs-service/src/main/java/com/fs/his/service/impl/FsIntegralGoodsServiceImpl.java

@@ -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;
+    }
 }

+ 20 - 0
fs-service/src/main/resources/mapper/his/FsIntegralGoodsMapper.xml

@@ -103,4 +103,24 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             #{goodsId}
         </foreach>
     </delete>
+
+
+
+    <!-- 查询指定积分区间的商品 -->
+    <select id="selectFsIntegralGoodsByStage" parameterType="com.fs.his.param.FsIntegralGoodsListUParam" resultMap="FsIntegralGoodsResult">
+        SELECT
+            g.goods_id,
+            g.goods_name,
+            g.img_url ,
+            g.integral,
+            g.status,
+            g.ot_price
+        FROM fs_integral_goods g
+        WHERE g.status = 1
+        <![CDATA[
+          AND g.integral >= #{minPoints}
+          AND g.integral <= #{maxPoints}
+        ]]>
+        ORDER BY RAND()
+    </select>
 </mapper>

+ 12 - 0
fs-user-app/src/main/java/com/fs/app/controller/CourseController.java

@@ -16,6 +16,7 @@ import com.fs.course.param.*;
 import com.fs.course.service.*;
 import com.fs.course.service.impl.TencentCloudCosService;
 import com.fs.course.vo.*;
+import com.fs.his.service.IFsIntegralGoodsService;
 import com.fs.his.vo.OptionsVO;
 import com.fs.system.service.ISysConfigService;
 import com.github.pagehelper.PageHelper;
@@ -81,6 +82,9 @@ public class CourseController extends  AppBaseController{
     @Autowired
     private IFsCourseWatchCommentService courseWatchCommentService;
 
+    @Autowired
+    private IFsIntegralGoodsService goodsService;
+
 
     @Cacheable(value="getCourseCate" )
     @ApiOperation("获取分类")
@@ -505,6 +509,14 @@ public class CourseController extends  AppBaseController{
     }
 
 
+    @Login
+    @ApiOperation("获取积分奖励信息")
+    @GetMapping("/getCourseIntegralGoods")
+    public R getCourseIntegralGoods() {
+        return goodsService.getCourseIntegralGoods(Long.parseLong(getUserId()));
+    }
+
+
     @Autowired
     TencentCloudCosService tencentCloudCosService;
     @Autowired