yuhongqi 1 vecka sedan
förälder
incheckning
9d4514f805

+ 9 - 0
fs-live-socket/src/main/java/com/fs/live/websocket/service/WebSocketServer.java

@@ -308,6 +308,15 @@ public class WebSocketServer {
                     broadcastMessage(liveId, JSONObject.toJSONString(R.ok().put("data", msg)));
                     break;
                 case "sendGift":
+                    liveMsg = new LiveMsg();
+                    liveMsg.setLiveId(msg.getLiveId());
+                    liveMsg.setUserId(msg.getUserId());
+                    liveMsg.setNickName(msg.getNickName());
+                    liveMsg.setAvatar(msg.getAvatar());
+                    liveMsg.setMsg(msg.getMsg());
+                    msg.setOn(true);
+                    msg.setData(JSONObject.toJSONString(liveMsg));
+                    broadcastMessage(liveId, JSONObject.toJSONString(R.ok().put("data", msg)));
                     break;
                 case "sendTopMsg":
                     msg.setMsg(productionWordFilter.filter(msg.getMsg()).getFilteredText());

+ 7 - 0
fs-service-system/src/main/java/com/fs/live/mapper/LiveGiftMapper.java

@@ -59,4 +59,11 @@ public interface LiveGiftMapper {
      * @return 结果
      */
     int deleteLiveGiftByGiftIds(Long[] giftIds);
+
+    /**
+     * 查询正常状态的礼物列表,按价格升序排列
+     *
+     * @return 礼物列表
+     */
+    List<LiveGift> selectActiveLiveGiftList();
 }

+ 1 - 1
fs-service-system/src/main/java/com/fs/live/mapper/LiveUserGiftMapper.java

@@ -15,6 +15,6 @@ import org.apache.ibatis.annotations.Mapper;
 public interface LiveUserGiftMapper {
 
 
-    @Insert("insert into live_user_gift(user_id,gift_id,gift_name,gift_time,live_id,cn) values(#{userId},#{giftId},#{giftName},#{giftTime},#{liveId},#{cn},)")
+    @Insert("insert into live_user_gift(user_id,gift_id,gift_name,gift_time,live_id,cn) values(#{userId},#{giftId},#{giftName},#{giftTime},#{liveId},#{cn})")
     int insert(LiveUserGift liveUserGift);
 }

+ 21 - 0
fs-service-system/src/main/java/com/fs/live/param/SendGiftParam.java

@@ -0,0 +1,21 @@
+package com.fs.live.param;
+
+import lombok.Data;
+
+@Data
+public class SendGiftParam {
+    /**
+     * 直播间id
+     * */
+    private Long liveId;
+
+    /**
+     * 礼物id
+     * */
+    private Long giftId;
+
+    /**
+     * 数量id
+     * */
+    private Long cn;
+}

+ 7 - 0
fs-service-system/src/main/java/com/fs/live/service/ILiveGiftService.java

@@ -59,4 +59,11 @@ public interface ILiveGiftService {
      * @return 结果
      */
     int deleteLiveGiftByGiftId(Long giftId);
+
+    /**
+     * 查询正常状态的礼物列表,按价格升序排列
+     *
+     * @return 礼物列表
+     */
+    List<LiveGift> selectActiveLiveGiftList();
 }

+ 91 - 4
fs-service-system/src/main/java/com/fs/live/service/impl/LiveGiftServiceImpl.java

@@ -7,8 +7,10 @@ import com.fs.live.mapper.LiveGiftMapper;
 import com.fs.live.service.ILiveGiftService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * 直播间礼物配置Service业务层处理
@@ -19,6 +21,9 @@ import java.util.List;
 @Service
 public class LiveGiftServiceImpl implements ILiveGiftService {
 
+    private static final String ACTIVE_GIFT_CACHE_KEY = "live:gift:active";
+    private static final int MAX_ACTIVE_GIFT_COUNT = 8;
+
     @Autowired
     private RedisCache redisCache;
     @Autowired
@@ -65,9 +70,46 @@ public class LiveGiftServiceImpl implements ILiveGiftService {
      * @return 结果
      */
     @Override
+    @Transactional
     public int insertLiveGift(LiveGift liveGift)
     {
-        return baseMapper.insertLiveGift(liveGift);
+        // 如果新增的礼物状态是正常状态,需要检查正常状态礼物数量
+        if ("1".equals(liveGift.getStatus())) {
+            manageActiveGiftCount(liveGift);
+        }
+        int result = baseMapper.insertLiveGift(liveGift);
+        // 清除缓存
+        clearActiveGiftCache();
+        return result;
+    }
+
+    /**
+     * 管理正常状态礼物数量,确保不超过8个
+     */
+    private void manageActiveGiftCount(LiveGift newGift) {
+        // 查询当前正常状态的礼物(排除当前要新增/修改的礼物)
+        LiveGift query = new LiveGift();
+        query.setStatus("1");
+        List<LiveGift> activeGifts = baseMapper.selectLiveGiftList(query);
+        
+        // 如果是更新操作,需要排除当前礼物本身
+        if (newGift.getGiftId() != null) {
+            activeGifts = activeGifts.stream()
+                    .filter(gift -> !gift.getGiftId().equals(newGift.getGiftId()))
+                    .collect(Collectors.toList());
+        }
+        
+        // 如果正常状态礼物数量已经达到或超过上限,需要停用超出部分的礼物
+        // 确保新增/修改后正常状态礼物不超过8个
+        if (activeGifts.size() >= MAX_ACTIVE_GIFT_COUNT) {
+            // 停用超出部分的礼物(保留前面的,停用后面的)
+            int needToDisable = activeGifts.size() - MAX_ACTIVE_GIFT_COUNT + 1;
+            for (int i = activeGifts.size() - needToDisable; i < activeGifts.size(); i++) {
+                LiveGift giftToDisable = activeGifts.get(i);
+                giftToDisable.setStatus("0");
+                baseMapper.updateLiveGift(giftToDisable);
+            }
+        }
     }
 
     /**
@@ -77,9 +119,17 @@ public class LiveGiftServiceImpl implements ILiveGiftService {
      * @return 结果
      */
     @Override
+    @Transactional
     public int updateLiveGift(LiveGift liveGift)
     {
-        return baseMapper.updateLiveGift(liveGift);
+        // 如果修改后的礼物状态是正常状态,需要检查正常状态礼物数量
+        if ("1".equals(liveGift.getStatus())) {
+            manageActiveGiftCount(liveGift);
+        }
+        int result = baseMapper.updateLiveGift(liveGift);
+        // 清除缓存
+        clearActiveGiftCache();
+        return result;
     }
 
     /**
@@ -91,7 +141,10 @@ public class LiveGiftServiceImpl implements ILiveGiftService {
     @Override
     public int deleteLiveGiftByGiftIds(Long[] giftIds)
     {
-        return baseMapper.deleteLiveGiftByGiftIds(giftIds);
+        int result = baseMapper.deleteLiveGiftByGiftIds(giftIds);
+        // 清除缓存
+        clearActiveGiftCache();
+        return result;
     }
 
     /**
@@ -103,6 +156,40 @@ public class LiveGiftServiceImpl implements ILiveGiftService {
     @Override
     public int deleteLiveGiftByGiftId(Long giftId)
     {
-        return baseMapper.deleteLiveGiftByGiftId(giftId);
+        int result = baseMapper.deleteLiveGiftByGiftId(giftId);
+        // 清除缓存
+        clearActiveGiftCache();
+        return result;
+    }
+
+    /**
+     * 查询正常状态的礼物列表,按价格升序排列
+     *
+     * @return 礼物列表
+     */
+    @Override
+    public List<LiveGift> selectActiveLiveGiftList() {
+        // 先查缓存
+        List<LiveGift> cachedGifts = redisCache.getCacheList(ACTIVE_GIFT_CACHE_KEY);
+        if (cachedGifts != null && !cachedGifts.isEmpty()) {
+            return cachedGifts;
+        }
+        
+        // 缓存不存在,查询数据库
+        List<LiveGift> gifts = baseMapper.selectActiveLiveGiftList();
+        
+        // 存入缓存
+        if (gifts != null && !gifts.isEmpty()) {
+            redisCache.setCacheList(ACTIVE_GIFT_CACHE_KEY, gifts);
+        }
+        
+        return gifts;
+    }
+
+    /**
+     * 清除正常状态礼物缓存
+     */
+    private void clearActiveGiftCache() {
+        redisCache.deleteObject(ACTIVE_GIFT_CACHE_KEY);
     }
 }

+ 6 - 0
fs-service-system/src/main/resources/mapper/live/LiveGiftMapper.xml

@@ -73,4 +73,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             #{giftId}
         </foreach>
     </delete>
+
+    <select id="selectActiveLiveGiftList" resultMap="LiveGiftResult">
+        <include refid="selectLiveGiftVo"/>
+        where status = '1'
+        order by price asc
+    </select>
 </mapper>

+ 160 - 3
fs-user-app/src/main/java/com/fs/app/controller/LiveGiftController.java

@@ -1,13 +1,170 @@
 package com.fs.app.controller;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.fs.app.annotation.Login;
+import com.fs.common.core.domain.R;
+import com.fs.live.domain.Live;
+import com.fs.live.domain.LiveGift;
+import com.fs.live.domain.LiveRewardRecord;
+import com.fs.live.domain.LiveUserGift;
+import com.fs.live.enums.FsUserIntegralLogTypeEnum;
+import com.fs.live.param.SendGiftParam;
+import com.fs.live.service.ILiveGiftService;
+import com.fs.live.service.ILiveRewardRecordService;
+import com.fs.live.service.ILiveService;
+import com.fs.live.service.ILiveUserGiftService;
+import com.fs.store.domain.FsUser;
+import com.fs.store.service.IFsUserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.math.BigDecimal;
+import java.util.*;
+
+/**
+ * 直播间礼物Controller
+ */
 @RestController
 @RequestMapping("/app/live/liveGift")
-public class LiveGiftController {
+public class LiveGiftController extends AppBaseController {
+
+    @Autowired
+    private ILiveGiftService liveGiftService;
+
+    @Autowired
+    private ILiveUserGiftService liveUserGiftService;
+
+    @Autowired
+    private IFsUserService fsUserService;
+
+    @Autowired
+    private ILiveRewardRecordService liveRewardRecordService;
+
+    @Autowired
+    private ILiveService liveService;
+
+
+    /**
+     * 接口①:查询正常状态的礼物列表,按价格升序排列
+     */
+    @GetMapping("/activeList")
+    public R getActiveGiftList() {
+        List<LiveGift> gifts = liveGiftService.selectActiveLiveGiftList();
+        return R.ok().put("data", gifts);
+    }
 
     /**
-     * 送礼物
-     * */
+     * 接口②:用户送礼物
+     * @param param 包含giftId和liveId
+     */
+    @Login
+    @PostMapping("/send")
+    @Transactional
+    public R sendGift(@RequestBody SendGiftParam param) {
+        try {
+            // 获取当前用户ID
+            String userIdStr = getUserId();
+            if (userIdStr == null) {
+                return R.error("用户未登录");
+            }
+            Long userId = Long.parseLong(userIdStr);
+            Long giftId = param.getGiftId();
+            Long liveId = param.getLiveId();
+
+            // 1. 查询礼物信息
+            LiveGift gift = liveGiftService.selectLiveGiftByGiftId(giftId);
+            if (gift == null) {
+                return R.error("礼物不存在");
+            }
+            if (!"1".equals(gift.getStatus())) {
+                return R.error("礼物已停用");
+            }
+
+            // 2. 查询直播间信息
+            Live live = liveService.selectLiveByLiveId(liveId);
+            if (live == null) {
+                return R.error("直播间不存在");
+            }
+
+            // 3. 查询用户信息
+            FsUser user = fsUserService.selectFsUserById(userId);
+            if (user == null) {
+                return R.error("用户不存在");
+            }
+
+            // 4. 计算需要消耗的积分(礼物价格,默认数量为1)
+            BigDecimal giftPrice = gift.getPrice();
+            if (giftPrice == null || giftPrice.compareTo(BigDecimal.ZERO) <= 0) {
+                return R.error("礼物价格无效");
+            }
+            Long integralCost = giftPrice.longValue();
+            if (param.getCn() != null && param.getCn() > 1) {
+                integralCost = giftPrice.multiply(BigDecimal.valueOf(param.getCn())).longValue();
+            }
+
+            // 5. 判断用户积分是否足够
+            BigDecimal userIntegral = user.getIntegral();
+            if (userIntegral == null || userIntegral.compareTo(BigDecimal.valueOf(integralCost)) < 0) {
+                return R.error("积分不足,无法送礼物");
+            }
+
+            // 6. 记录积分消耗前的余额
+            BigDecimal beforeIntegral = userIntegral;
+            BigDecimal afterIntegral = userIntegral.subtract(BigDecimal.valueOf(integralCost));
+
+            // 7. 扣除用户积分
+            fsUserService.decIntegral(userId, integralCost.doubleValue());
+
+            // 8. 保存用户送礼物记录
+            LiveUserGift liveUserGift = new LiveUserGift();
+            liveUserGift.setUserId(userId);
+            liveUserGift.setGiftId(giftId);
+            liveUserGift.setGiftName(gift.getGiftName());
+            liveUserGift.setLiveId(liveId);
+            liveUserGift.setGiftTime(new Date());
+            liveUserGift.setCn(param.getCn() != null ? param.getCn() : 1L);
+            liveUserGiftService.save(liveUserGift);
+
+            // 9. 记录积分消耗记录
+            LiveRewardRecord record = new LiveRewardRecord();
+            record.setLiveId(liveId);
+            record.setUserId(userId);
+            record.setIncomeType(2L); // 支出
+            record.setSourceType(4L); // 来源类型:4送礼物(1观看奖励 2答题红包 3观看积分 4送礼物)
+            record.setSourceId(giftId);
+            record.setRewardType(2L); // 积分
+            record.setNum(BigDecimal.valueOf(integralCost));
+            record.setBeforeNum(beforeIntegral);
+            record.setAfterNum(afterIntegral);
+            record.setCreateTime(new Date());
+            record.setCreateBy(String.valueOf(userId));
+            liveRewardRecordService.insertLiveRewardRecord(record);
+
+            // 10. 调用开放平台接口更新用户积分(异步)
+            // 注意:传递负数表示扣除积分,unifyIntegral传0表示使用userIntegralList中的值
+            FsUser userForPlatform = new FsUser();
+            userForPlatform.setUserId(userId);
+            userForPlatform.setIntegral(BigDecimal.valueOf(-integralCost)); // 负数表示扣除
+            List<FsUser> result = new ArrayList<>();
+            result.add(userForPlatform);
+            fsUserService.openPlatformGeneralUserIntegralHandle(
+                    result,
+                    liveId,
+                    0L, // unifyIntegral传0,使用userIntegralList中的值
+                    FsUserIntegralLogTypeEnum.TYPE_26.getValue() // 赠送礼物扣除积分
+            );
+
+            return R.ok("送礼物成功");
+        } catch (Exception e) {
+            e.printStackTrace();
+            return R.error("送礼物失败:" + e.getMessage());
+        }
+    }
+
 }