Ver código fonte

益寿缘app-优化奖品-积分发放接口

cgp 1 semana atrás
pai
commit
5dd3d8fe71

+ 1 - 1
fs-service/src/main/java/com/fs/his/domain/FsUserIntegralLogs.java

@@ -24,7 +24,7 @@ public class FsUserIntegralLogs extends BaseEntity
     private Long userId;
 
 
-    @Excel(name = "1 签到获得积分 2购买商品获得积分 3商品退款减去积分 4使用购买积分商品 ")
+    @Excel(name = "1 签到获得积分 2购买商品获得积分 3商品退款减去积分 4使用购买积分商品 101首次注册App积分,102看课积分 ")
     private Integer logType;
 
     /** 积分 正数表示增加积分,负数表示减少积分 */

+ 6 - 2
fs-service/src/main/java/com/fs/his/domain/FsUserRewards.java

@@ -50,12 +50,16 @@ public class FsUserRewards {
     private Long goodsId;
 
     /**
-     * 新增:奖励金额/数量
+     * 新增:奖励红包金额
      * 红包:金额(元),可含小数
-     * 积分:数量(分),整数
      */
     private BigDecimal rewardAmount;
 
+    /**
+     * 积分专用
+     * */
+    private Long rewardPoints;
+
     /**
      * 奖品实际领取时间
      */

+ 6 - 0
fs-service/src/main/java/com/fs/his/mapper/FsUserIntegralLogsMapper.java

@@ -112,6 +112,12 @@ public interface FsUserIntegralLogsMapper
     @Select("select * from fs_user_integral_logs where log_type=12 and user_id = #{userId} limit 1")
     FsUserIntegralLogs selectFsUserIntegralLogsAddUserAddress(@Param("userId")Long userId);
 
+    @Select("select * from fs_user_integral_logs where log_type=#{logType} and user_id = #{userId} limit 1")
+    FsUserIntegralLogs selectFsUserIntegralLogsByLogType(@Param("logType")Integer logType,@Param("userId")Long userId);
+
+    @Select("select * from fs_user_integral_logs where user_id = #{userId} order by  create_time desc limit 1")
+    FsUserIntegralLogs selectFsUserIntegralLastRecordByUserId(@Param("userId")Long userId);
+
     @Select({"<script> " +
             "SELECT SUM(integral) AS todayTotalIntegral FROM fs_user_integral_logs " +
             "WHERE user_id = #{userId} AND log_type IN " +

+ 13 - 7
fs-service/src/main/java/com/fs/his/service/impl/AppUserRewardServiceImpl.java

@@ -66,8 +66,9 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
         Integer productType=1;
         Long goodsId=1L;
         BigDecimal rewardAmount=new BigDecimal("10");
+        Long rewardPoints=600L;
         // 构建奖品记录
-        FsUserRewards reward = buildFirstLoginReward(fsUserId, activityType, rewardType, productType, goodsId, rewardAmount);
+        FsUserRewards reward = buildFirstLoginReward(fsUserId, activityType, rewardType, productType, goodsId, rewardAmount,rewardPoints);
         try {
             // 插入奖品表
             int insertResult = rewardsMapper.insertFsUserRewards(reward);
@@ -109,9 +110,10 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
      * @param rewardType 奖品类型
      * @param productType  产品类型 1-药品 2-套餐包
      * @param goodsId  1-红包 2-积分 3-商品
-     * @param rewardAmount 红包或积分金额
+     * @param rewardAmount 红包
+     * @param rewardPoints 积分
      * */
-    private FsUserRewards buildFirstLoginReward(Long fsUserId, String activityType, Integer rewardType, Integer productType, Long goodsId,BigDecimal rewardAmount) {
+    private FsUserRewards buildFirstLoginReward(Long fsUserId, String activityType, Integer rewardType, Integer productType, Long goodsId,BigDecimal rewardAmount,Long rewardPoints) {
         FsUserRewards reward = new FsUserRewards();
         reward.setFsUserId(fsUserId);
         reward.setActivityType(activityType);
@@ -121,11 +123,14 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
         if (ActivityTypeEnum.FIRST_LOGIN.getCode().equals(activityType)){
             reward.setIsFirstLogin(1); // 标记为首次注册奖励(用于虚拟列索引),首次登录奖励 必传1!!!
         }
-        if (rewardType == 3){//实物商品类型
+        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
-        }else {
-            reward.setRewardAmount(rewardAmount);//红包或积分
         }
         return reward;
     }
@@ -139,8 +144,9 @@ public class AppUserRewardServiceImpl implements IAppUserRewardService {
         Integer productType=1;
         Long goodsId=1L;
         BigDecimal rewardAmount=new BigDecimal("10");
+        Long rewardPoints=600L;
         // 构建奖品记录
-        FsUserRewards reward = buildFirstLoginReward(fsUserId, activityType, rewardType, productType, goodsId, rewardAmount);
+        FsUserRewards reward = buildFirstLoginReward(fsUserId, activityType, rewardType, productType, goodsId, rewardAmount,rewardPoints);
         // 插入奖品表
         int insertResult = rewardsMapper.insertFsUserRewards(reward);
         if (insertResult > 0) {

+ 44 - 1
fs-service/src/main/java/com/fs/his/strategy/impl/FirstLoginPointsStrategy.java

@@ -1,20 +1,40 @@
 package com.fs.his.strategy.impl;
 
+import com.fs.his.domain.FsUser;
+import com.fs.his.domain.FsUserIntegralLogs;
 import com.fs.his.domain.FsUserRewards;
 import com.fs.his.enums.ActivityTypeEnum;
+import com.fs.his.mapper.FsUserIntegralLogsMapper;
+import com.fs.his.mapper.FsUserMapper;
 import com.fs.his.strategy.RewardResult;
 import com.fs.his.strategy.RewardStrategy;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.aop.framework.AopContext;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.support.TransactionSynchronizationManager;
+
+import java.util.Date;
 
 @Slf4j
 @Component
 public class FirstLoginPointsStrategy  implements RewardStrategy {
+
+    @Autowired
+    private FsUserMapper fsUserMapper;
+
+    @Autowired
+    private FsUserIntegralLogsMapper fsUserIntegralLogsMapper;
+
+
     @Override
     public RewardResult process(FsUserRewards reward) {
         log.info("处理首次登录积分发放: rewardId={}", reward.getId());
         try {
-            //TODO 调用发送积分逻辑
+            //调用发送积分逻辑
+            FirstLoginPointsStrategy proxy=(FirstLoginPointsStrategy) AopContext.currentProxy();
+            proxy.addUserAddressIntegral(reward.getFsUserId(), reward.getRewardPoints());
             return RewardResult.success();
 
         } catch (Exception e) {
@@ -22,6 +42,29 @@ public class FirstLoginPointsStrategy  implements RewardStrategy {
             return RewardResult.fail("发送积分失败:" + e.getMessage());
         }
     }
+    @Transactional(rollbackFor = Exception.class)
+    public void addUserAddressIntegral(Long userId, Long points) {
+        //log.debug("当前是否在事务中: " + TransactionSynchronizationManager.isActualTransactionActive());
+        FsUser user = fsUserMapper.selectFsUserById(userId);
+        if (user!=null){
+            //获取首次注册类型的积分记录,没有再添加积分
+            FsUserIntegralLogs integralLogs = fsUserIntegralLogsMapper.selectFsUserIntegralLogsByLogType(101,userId);
+            if (integralLogs==null){
+                FsUser updateFsUser=new FsUser();
+                updateFsUser.setUserId(user.getUserId());
+                updateFsUser.setIntegral(user.getIntegral()+points);
+                fsUserMapper.updateFsUser(updateFsUser);
+                integralLogs = new FsUserIntegralLogs();
+                integralLogs.setIntegral(points);
+                integralLogs.setUserId(userId);
+                integralLogs.setBalance(updateFsUser.getIntegral());
+                integralLogs.setLogType(101);
+                integralLogs.setBusinessId(null);//这里暂时设置为null
+                integralLogs.setCreateTime(new Date());
+                fsUserIntegralLogsMapper.insertFsUserIntegralLogs(integralLogs);
+            }
+        }
+    }
 
     @Override
     public String getActivityType() {

+ 37 - 1
fs-service/src/main/java/com/fs/his/strategy/impl/WatchCoursePointsStrategy.java

@@ -1,20 +1,37 @@
 package com.fs.his.strategy.impl;
 
+import com.fs.his.domain.FsUser;
+import com.fs.his.domain.FsUserIntegralLogs;
 import com.fs.his.domain.FsUserRewards;
 import com.fs.his.enums.ActivityTypeEnum;
+import com.fs.his.mapper.FsUserIntegralLogsMapper;
+import com.fs.his.mapper.FsUserMapper;
 import com.fs.his.strategy.RewardResult;
 import com.fs.his.strategy.RewardStrategy;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.aop.framework.AopContext;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.Date;
 
 @Slf4j
 @Component
 public class WatchCoursePointsStrategy  implements RewardStrategy {
+
+    @Autowired
+    private FsUserMapper fsUserMapper;
+
+    @Autowired
+    private FsUserIntegralLogsMapper fsUserIntegralLogsMapper;
     @Override
     public RewardResult process(FsUserRewards reward) {
         log.info("处理看课积分发放: rewardId={}", reward.getId());
         try {
-            //TODO 调用发送积分逻辑
+            //调用发送积分逻辑
+            WatchCoursePointsStrategy proxy=(WatchCoursePointsStrategy) AopContext.currentProxy();
+            proxy.addUserAddressIntegral(reward.getFsUserId(), reward.getRewardPoints());
             return RewardResult.success();
 
         } catch (Exception e) {
@@ -23,6 +40,25 @@ public class WatchCoursePointsStrategy  implements RewardStrategy {
         }
     }
 
+    @Transactional(rollbackFor = Exception.class)
+    public void addUserAddressIntegral(Long userId, Long points) {
+        FsUser user = fsUserMapper.selectFsUserById(userId);
+        if (user != null) {
+            FsUser updateFsUser = new FsUser();
+            updateFsUser.setUserId(user.getUserId());
+            updateFsUser.setIntegral(user.getIntegral() + points);
+            fsUserMapper.updateFsUser(updateFsUser);
+            FsUserIntegralLogs integralLogs = new FsUserIntegralLogs();
+            integralLogs.setIntegral(points);
+            integralLogs.setUserId(userId);
+            integralLogs.setBalance(updateFsUser.getIntegral());
+            integralLogs.setLogType(102);//102看课积分
+            integralLogs.setBusinessId(null);//这里暂时设置为null
+            integralLogs.setCreateTime(new Date());
+            fsUserIntegralLogsMapper.insertFsUserIntegralLogs(integralLogs);
+        }
+    }
+
     @Override
     public String getActivityType() {
         return ActivityTypeEnum.WATCH_COURSE.getCode();

+ 1 - 1
fs-service/src/main/java/com/fs/his/vo/FsUserIntegralLogsListVO.java

@@ -15,7 +15,7 @@ public class FsUserIntegralLogsListVO {
 
     private Long userId;
 
-    /** 1 签到获得积分 2购买商品获得积分 3购买积分商品  */
+    /** 1 签到获得积分 2购买商品获得积分 3购买积分商品  101首次注册App获得积分,102App看课获得积分*/
     @Excel(name = "类别",dictType = "sys_integral_log_type")
     private String logType;
 

+ 7 - 4
fs-service/src/main/resources/mapper/his/FsUserRewardsMapper.xml

@@ -14,16 +14,17 @@
         <result property="goodsId" column="goods_id"/>
         <result property="productType" column="product_type"/>
         <result property="rewardAmount" column="reward_amount"/>
+        <result property="rewardPoints" column="reward_points"/>
         <result property="grantTime" column="grant_time"/>
         <result property="createTime" column="create_time"/>
         <result property="updateTime" column="update_time"/>
         <result property="isFirstLogin" column="is_first_login"/>
     </resultMap>
 
-    <!-- 基础字段列表 - 添加 reward_amount 和 is_first_login -->
+    <!-- 基础字段列表 -->
     <sql id="selectFsUserRewardsVo">
         select id, fs_user_id, activity_type, reward_type, status,
-               order_code, goods_id, product_type, reward_amount,
+               order_code, goods_id, product_type, reward_amount,reward_points,
                grant_time, create_time, update_time, is_first_login
         from fs_user_rewards
     </sql>
@@ -79,6 +80,7 @@
             <if test="goodsId != null"> and goods_id = #{goodsId}</if>
             <if test="productType != null"> and product_type = #{productType}</if>
             <if test="rewardAmount != null"> and reward_amount = #{rewardAmount}</if>
+            <if test="rewardPoints != null"> and reward_points = #{rewardPoints}</if>
             <if test="grantTime != null"> and grant_time = #{grantTime}</if>
             <if test="createTime != null"> and create_time = #{createTime}</if>
             <if test="updateTime != null"> and update_time = #{updateTime}</if>
@@ -99,9 +101,9 @@
             <if test="goodsId != null">goods_id,</if>
             <if test="productType != null">product_type,</if>
             <if test="rewardAmount != null">reward_amount,</if>
+            <if test="rewardPoints != null">reward_points,</if>
             <if test="grantTime != null">grant_time,</if>
             <if test="isFirstLogin != null">is_first_login,</if>
-            create_time,
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="fsUserId != null">#{fsUserId},</if>
@@ -112,9 +114,9 @@
             <if test="goodsId != null">#{goodsId},</if>
             <if test="productType != null">#{productType},</if>
             <if test="rewardAmount != null">#{rewardAmount},</if>
+            <if test="rewardPoints != null">#{rewardPoints},</if>
             <if test="grantTime != null">#{grantTime},</if>
             <if test="isFirstLogin != null">#{isFirstLogin},</if>
-            sysdate(),
         </trim>
     </insert>
 
@@ -130,6 +132,7 @@
             <if test="goodsId != null">goods_id = #{goodsId},</if>
             <if test="productType != null">product_type = #{productType},</if>
             <if test="rewardAmount != null">reward_amount = #{rewardAmount},</if>
+            <if test="rewardPoints != null">reward_points = #{rewardPoints},</if>
             <if test="grantTime != null">grant_time = #{grantTime},</if>
             <if test="isFirstLogin != null">is_first_login = #{isFirstLogin},</if>
             update_time = sysdate()

+ 2 - 0
fs-user-app/src/main/java/com/fs/FsUserAppApplication.java

@@ -5,6 +5,7 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
 import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
@@ -12,6 +13,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
 /**
  * 启动程序
  */
+@EnableAspectJAutoProxy(exposeProxy = true)
 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
 @EnableTransactionManagement
 @EnableSwaggerBootstrapUI