Преглед изворни кода

完善直播间展示相关数据api接口和逻辑

chenguo пре 5 дана
родитељ
комит
68a8e7fa5d

+ 17 - 6
fs-live-app/src/main/java/com/fs/app/controller/LiveDataController.java

@@ -4,17 +4,15 @@ import com.fs.app.annotation.Login;
 import com.fs.common.core.domain.R;
 import com.fs.live.domain.LiveData;
 import com.fs.live.service.ILiveDataService;
+import com.fs.live.service.ILiveWatchUserService;
+import com.fs.live.vo.LiveWatchUserVO;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import static com.fs.common.utils.SecurityUtils.getUserId;
-
 @RestController
 @RequestMapping("/app/live/liveData")
 public class LiveDataController extends AppBaseController{
@@ -22,6 +20,8 @@ public class LiveDataController extends AppBaseController{
     @Autowired
     private ILiveDataService liveDataService;
 
+    @Autowired
+    private ILiveWatchUserService liveWatchUserService;
     /**
      * 查询直播数据列表
      * */
@@ -85,5 +85,16 @@ public class LiveDataController extends AppBaseController{
         return R.ok(liveViewData);
     }
 
+    /**
+     * 获取发言最多的三个用户
+     * */
+    @GetMapping("/getRecentLiveViewers/{liveId}")
+    public R getRecentLiveViewers(@PathVariable  Long liveId) {
+        Map<String, Object> params = new HashMap<>();
+        params.put("liveId", liveId);
+        List<LiveWatchUserVO> recentLiveViewers = liveWatchUserService.selectWatchUserList(params);
+        return R.ok().put("recentLiveViewers", recentLiveViewers);
+    }
+
 
 }

+ 16 - 1
fs-live-app/src/main/java/com/fs/app/controller/LiveGoodsController.java

@@ -7,6 +7,7 @@ import com.fs.common.core.domain.R;
 import com.fs.common.core.page.TableDataInfo;
 import com.fs.common.enums.BusinessType;
 import com.fs.common.utils.poi.ExcelUtil;
+import com.fs.his.service.IFsStoreProductService;
 import com.fs.live.domain.LiveGoods;
 import com.fs.live.service.ILiveGoodsService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -28,6 +29,9 @@ public class LiveGoodsController extends BaseController
     @Autowired
     private ILiveGoodsService liveGoodsService;
 
+    @Autowired
+    private IFsStoreProductService fsStoreProductService;
+
     /**
      * 查询直播商品列表
      */
@@ -102,6 +106,17 @@ public class LiveGoodsController extends BaseController
     @GetMapping("/liveStore/{liveId}")
     public R liveGoodsStore(@PathVariable Long liveId)
     {
-        return R.ok().put("store", liveGoodsService.getStoreByLiveId(liveId));
+        return R.ok(liveGoodsService.getStoreByLiveId(liveId));
     }
+
+    /**
+     *商品详情
+     */
+    @GetMapping("/liveGoodsDetail/{productId}")
+    public R liveGoodsDetail(@PathVariable Long productId)
+    {
+        return R.ok().put("data",fsStoreProductService.selectFsStoreProductById(productId));
+    }
+
+
 }

+ 16 - 4
fs-live-app/src/main/java/com/fs/app/controller/LiveOrderController.java

@@ -151,16 +151,28 @@ public class LiveOrderController extends AppBaseController
     }
 
     /**
-     * 取消订单确认
+     * 订单确认
      * */
     @Login
-    @GetMapping(value = "/cancelConfirm/{orderId}")
-    public R cancelConfirm(@PathVariable String orderId)
+    @GetMapping(value = "/updateConfirm/{orderId}/{type}")
+    public R cancelConfirm(@PathVariable String orderId,@PathVariable String type)
     {
         LiveOrder byId = liveOrderService.getById(orderId);
-        List<Map<String, String>> allCodeDescMap = LiveOrderCancleReason.getAllCodeDescMap();
+        List<Map<String, String>> allCodeDescMap = null;
+        if (type.equals("0"))
+            allCodeDescMap = LiveOrderCancleReason.getAllCodeDescMap();
         return R.ok().put("reason",allCodeDescMap).put("data",byId);
     }
 
+    /**
+     *正在购买的用户
+     * */
+    @GetMapping(value = "/liveOrderUser/{liveId}")
+    public R liveOrderUser(@PathVariable  String liveId)
+    {
+        return liveOrderService.liveOrderUser(liveId);
+    }
+
+
 
 }

+ 5 - 16
fs-live-app/src/main/java/com/fs/app/task/Task.java

@@ -24,10 +24,13 @@ public class Task {
 
     private final RedisCache redisCache;
 
-    //@Scheduled(cron = "0 0/1 * * * ?")
-    public void selectSopUserLogsListByTime() {
+    @Scheduled(cron = "0 0/1 * * * ?")
+    //public void selectSopUserLogsListByTime() {
+    public void updateLiveStatusByTime() {
         LocalDateTime now = LocalDateTime.now();
         List<Live> list = liveService.list(new QueryWrapper<Live>().ne("status", 3));
+        if (list.isEmpty())
+            return;
         list.forEach(live -> {
             if (now.isAfter(live.getStartTime()) && now.isBefore(live.getFinishTime())) {
                 live.setStatus(2);
@@ -41,19 +44,5 @@ public class Task {
             liveService.updateBatchById(list);
         }
     }
-    /**
-     *同步点赞数据到数据库
-     */
-    @Scheduled(cron = "0 0/1 * * * ?")
-    public void syncLikeData() {
-        List<LiveData>  liveDatas = liveDataService.selectLiveDataList(new LiveData());
-        liveDatas.forEach(liveData ->{
-            Integer like_num = redisCache.getCacheObject("live:like:" + liveData.getLiveId());
-            if (like_num != null)
-                liveData.setLikes(((Number)like_num).longValue());
-        });
-        if(!liveDatas.isEmpty())
-            liveDataService.updateBatchById(liveDatas);
-    }
 
 }

+ 27 - 13
fs-live-app/src/main/java/com/fs/app/websocket/service/WebSocketServer.java

@@ -93,6 +93,7 @@ public class WebSocketServer {
             // 判断是否是该直播间的首次访客(独立访客统计)
             boolean isFirstVisit = redisCache.setIfAbsent(USER_VISIT_KEY + userId, 1, 1, TimeUnit.DAYS);
             if (isFirstVisit) {
+
                 redisCache.increment(UNIQUE_VISITORS_KEY + liveId, 1);
             }
 
@@ -198,6 +199,7 @@ public class WebSocketServer {
                             sendMessage(session, JSONObject.toJSONString(R.error("你已被禁言")));
                             return;
                         }
+
                         liveMsgService.save(liveMsg);
                     }
 
@@ -260,41 +262,53 @@ public class WebSocketServer {
      */
     @Scheduled(fixedRate = 60000) // 每分钟执行一次
     public void syncLiveDataToDB() {
-        List<Long> liveIds = liveDataService.getAllLiveIds(); // 获取所有正在直播的直播间ID
-        for (Long liveId : liveIds) {
+        List<LiveData> liveDatas = liveDataService.getAllLiveDatas(); // 获取所有正在直播的直播间数据
+        if(liveDatas == null)
+            return;
+        liveDatas.forEach(liveData ->{
+            liveData.setLikes(
+                    Optional.ofNullable(redisCache.incrementCacheValue("live:like:" + liveData.getLiveId(),0 )).orElse(0L)
+            );
+
+       /* for (Long liveId : liveIds) {
             LiveData liveData = liveDataService.selectLiveDataByLiveId(liveId);
             if (liveData == null) {
                 continue; // 防止空指针异常
-            }
+            }*/
 
 
             // 从 redis 获取数据,并提供默认值,避免 NPE
             liveData.setPageViews(
-                    Optional.ofNullable(redisCache.incrementCacheValue(PAGE_VIEWS_KEY + liveId,0)).orElse(0L)
+                    Optional.ofNullable(redisCache.incrementCacheValue(PAGE_VIEWS_KEY + liveData.getLiveId(),0)).orElse(0L)
             );
             liveData.setTotalViews(
-                    Optional.ofNullable(redisCache.incrementCacheValue(TOTAL_VIEWS_KEY + liveId,0)).orElse(0L)
+                    Optional.ofNullable(redisCache.incrementCacheValue(TOTAL_VIEWS_KEY + liveData.getLiveId(),0)).orElse(0L)
             );
             liveData.setUniqueVisitors(
-                    Optional.ofNullable(redisCache.getCacheSet(UNIQUE_VISITORS_KEY + liveId))
+                    /*Optional.ofNullable(redisCache.getCacheSet(UNIQUE_VISITORS_KEY + liveId))
                             .map(Set::size)  // 获取集合大小
                             .map(Long::valueOf)  // 转换为 Long 类型
-                            .orElse(0L)
+                            .orElse(0L)*/
+                    Optional.ofNullable(redisCache.incrementCacheValue(UNIQUE_VISITORS_KEY + liveData.getLiveId(),0)).orElse(0L)
             );
             liveData.setUniqueViewers(
-                    Optional.ofNullable(redisCache.getCacheSet(UNIQUE_VIEWERS_KEY + liveId))
+                    /*Optional.ofNullable(redisCache.getCacheSet(UNIQUE_VIEWERS_KEY + liveId))
                             .map(Set::size)  // 获取集合大小
                             .map(Long::valueOf)  // 转换为 Long 类型
-                            .orElse(0L)
+                            .orElse(0L)*/
+                    Optional.ofNullable(redisCache.incrementCacheValue(UNIQUE_VIEWERS_KEY + liveData.getLiveId(),0)).orElse(0L)
             );
             liveData.setPeakConcurrentViewers(
-                    Optional.ofNullable(redisCache.incrementCacheValue(MAX_ONLINE_USERS_KEY + liveId,0)).orElse(0L)
+                    Optional.ofNullable(redisCache.incrementCacheValue(MAX_ONLINE_USERS_KEY + liveData.getLiveId(),0)).orElse(0L)
             );
+        });
+        if(!liveDatas.isEmpty())
+            liveDataService.updateBatchById(liveDatas);
 
-            // 更新数据库
-            liveDataService.updateLiveData(liveData);
-        }
+            /*// 更新数据库
+            liveDataService.updateLiveData(liveData);*/
     }
 
 
+
 }

+ 1 - 0
fs-service/src/main/java/com/fs/live/mapper/LiveDataMapper.java

@@ -110,4 +110,5 @@ public interface LiveDataMapper extends BaseMapper<LiveData>{
      */
     TrendDataVO getPreviousData(@Param("prevStartDate") String prevStartDate, @Param("prevEndDate") String prevEndDate);
 
+    List<LiveData> getAllLiveDatas();
 }

+ 3 - 0
fs-service/src/main/java/com/fs/live/service/ILiveDataService.java

@@ -66,6 +66,7 @@ public interface ILiveDataService extends IService<LiveData>{
 
     /**
      * 查询所有正在直播的直播间iD
+     *
      * @return
      */
     List<Long> getAllLiveIds();
@@ -107,4 +108,6 @@ public interface ILiveDataService extends IService<LiveData>{
     String collectStore(Long storeId, Long productId, Long liveId, Long userId);
 
     Map<String, Object> getLiveViewData(Long liveId);
+
+    List<LiveData> getAllLiveDatas();
 }

+ 2 - 1
fs-service/src/main/java/com/fs/live/service/ILiveGoodsService.java

@@ -73,7 +73,7 @@ public interface ILiveGoodsService extends IService<LiveGoods>{
      */
     int insertLiveGoods(Map<String, Object> payload, CompanyUser user);
 
-    FsStore getStoreByLiveId(Long liveId);
+    //FsStore getStoreByLiveId(Long liveId);
 
     /**
      * 查询直播商品列表
@@ -82,4 +82,5 @@ public interface ILiveGoodsService extends IService<LiveGoods>{
      * @return 直播商品集合
      */
     List<LiveGoodsVo> selectProductListByLiveId(LiveGoods liveGoods);
+    Map<String, Object> getStoreByLiveId(Long liveId);
 }

+ 3 - 0
fs-service/src/main/java/com/fs/live/service/ILiveOrderService.java

@@ -2,6 +2,7 @@ package com.fs.live.service;
 
 import java.util.List;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.fs.common.core.domain.R;
 import com.fs.live.domain.LiveOrder;
 
 /**
@@ -60,4 +61,6 @@ public interface ILiveOrderService extends IService<LiveOrder>{
     //int deleteLiveOrderByOrderId(String orderId);
 
     void handlePay(LiveOrder liveOrder);
+
+    R liveOrderUser(String liveId);
 }

+ 12 - 5
fs-service/src/main/java/com/fs/live/service/impl/LiveDataServiceImpl.java

@@ -129,13 +129,20 @@ public class LiveDataServiceImpl extends ServiceImpl<LiveDataMapper, LiveData> i
     {
         return baseMapper.deleteLiveDataByLiveId(liveId);
     }
+
+    @Override
+    public List<Long> getAllLiveIds() {
+        return Collections.emptyList();
+    }
+
     /**
-     * 查询所有正在直播的直播间iD
+     * 查询所有正在直播的直播间
+     *
      * @return
      */
     @Override
-    public List<Long> getAllLiveIds() {
-        return baseMapper.getAllLiveIds();
+    public List<LiveData> getAllLiveDatas() {
+        return baseMapper.getAllLiveDatas();
     }
 
     @Override
@@ -365,7 +372,7 @@ public class LiveDataServiceImpl extends ServiceImpl<LiveDataMapper, LiveData> i
                 liveUserLike.setLiveId(liveId);
                 liveUserLike.setCreateTime(new Date());
                 liveUserLikeService.insertLiveUserLike(liveUserLike);
-                return "点赞成功";
+                return redisCache.getCacheObject(key).toString();
             }
         }
         Integer count =redisCache.getCacheObject(key);
@@ -383,7 +390,7 @@ public class LiveDataServiceImpl extends ServiceImpl<LiveDataMapper, LiveData> i
         redisCache.increment(key,1);
         //直播间总点赞数
         redisCache.increment("live:like:" + liveId,1);
-        return "点赞成功";
+        return redisCache.getCacheObject(key).toString();
     }
 
     @Override

+ 20 - 3
fs-service/src/main/java/com/fs/live/service/impl/LiveGoodsServiceImpl.java

@@ -1,5 +1,8 @@
 package com.fs.live.service.impl;
 
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -7,8 +10,10 @@ import com.fs.common.utils.DateUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fs.company.domain.CompanyUser;
 import com.fs.his.domain.FsStoreProduct;
+import com.fs.his.domain.FsStoreProduct;
 import com.fs.his.mapper.FsStoreProductMapper;
 import com.fs.his.domain.FsStore;
+import com.fs.his.service.IFsStoreProductService;
 import com.fs.his.service.IFsStoreService;
 import com.fs.live.vo.LiveGoodsVo;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -26,10 +31,12 @@ import com.fs.live.service.ILiveGoodsService;
 @Service
 public class LiveGoodsServiceImpl extends ServiceImpl<LiveGoodsMapper, LiveGoods> implements ILiveGoodsService {
 
-
     @Autowired
     private FsStoreProductMapper fsStoreProductMapper;
 
+    @Autowired
+    private IFsStoreProductService fsStoreProductService;
+
     @Autowired
     private IFsStoreService fsStoreService;
 
@@ -108,11 +115,21 @@ public class LiveGoodsServiceImpl extends ServiceImpl<LiveGoodsMapper, LiveGoods
     }
 
     @Override
-    public FsStore getStoreByLiveId(Long liveId) {
+    public Map<String, Object> getStoreByLiveId(Long liveId) {
+        Map<String, Object> map = new HashMap<>();
         //获取店铺id
         Long storeId = baseMapper.selectStoreIdByLiveId(liveId);
+        if(storeId == null)
+            return null;
         //获取店铺信息
-        return fsStoreService.selectFsStoreByStoreId(storeId);
+        FsStore fsStore = fsStoreService.selectFsStoreByStoreId(storeId);
+        //获取店铺商品信息
+        FsStoreProduct fsStoreProduct = new FsStoreProduct();
+        fsStoreProduct.setStoreId(fsStore.getStoreId());
+        List<FsStoreProduct> fsStoreProducts = fsStoreProductService.selectFsStoreProductList(fsStoreProduct);
+        map.put("store",fsStore);
+        map.put("products",fsStoreProducts);
+        return map;
     }
 
     @Override

+ 35 - 0
fs-service/src/main/java/com/fs/live/service/impl/LiveOrderServiceImpl.java

@@ -1,8 +1,14 @@
 package com.fs.live.service.impl;
 
 import java.util.List;
+import java.util.concurrent.TimeUnit;
+
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fs.common.core.domain.R;
+import com.fs.common.core.redis.RedisCache;
 import com.fs.common.utils.DateUtils;
+import com.fs.his.service.IFsUserService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.fs.live.mapper.LiveOrderMapper;
 import com.fs.live.domain.LiveOrder;
@@ -18,6 +24,15 @@ import org.springframework.transaction.annotation.Transactional;
 @Service
 public class LiveOrderServiceImpl extends ServiceImpl<LiveOrderMapper, LiveOrder> implements ILiveOrderService {
 
+    private final RedisCache redisCache;
+
+    public LiveOrderServiceImpl(RedisCache redisCache) {
+        this.redisCache = redisCache;
+    }
+
+    @Autowired
+    private IFsUserService fsUserService;
+
     /**
      * 查询订单
      *
@@ -52,7 +67,14 @@ public class LiveOrderServiceImpl extends ServiceImpl<LiveOrderMapper, LiveOrder
     @Transactional
     public Long insertLiveOrder(LiveOrder liveOrder)
     {
+        //创建时间
         liveOrder.setCreateTime(DateUtils.getNowDate());
+        //订单状态 默认1
+        liveOrder.setStatus(1);
+        //支付状态 默认0
+        liveOrder.setIsPay("0");
+        //支付类型 默认1微信
+        liveOrder.setPayType("1");
         baseMapper.insertLiveOrder(liveOrder);
         return liveOrder.getOrderId();
     }
@@ -104,6 +126,19 @@ public class LiveOrderServiceImpl extends ServiceImpl<LiveOrderMapper, LiveOrder
 
     }
 
+    @Override
+    public R liveOrderUser(String liveId) {
+        LiveOrder liveOrder = new LiveOrder();
+        liveOrder.setLiveId(Long.parseLong(liveId));
+        liveOrder.setStatus(1);
+        List<LiveOrder>  list =baseMapper.selectLiveOrderList(liveOrder);
+        if (list == null || list.isEmpty())
+            return R.ok().put("count", 0);
+        int count = list.size();
+        String userName = list.get(0).getUserName();
+        return R.ok().put("count",count).put("userName",userName);
+    }
+
     /**
      * 订单操作记录
      * */

+ 2 - 2
fs-service/src/main/java/com/fs/live/service/impl/LiveWatchUserServiceImpl.java

@@ -124,7 +124,7 @@ public class LiveWatchUserServiceImpl extends ServiceImpl<LiveWatchUserMapper, L
         LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
         if(liveWatchUser != null) {
             liveWatchUser.setUpdateTime(DateUtils.getNowDate());
-            liveWatchUser.setOnline(0);
+            liveWatchUser.setOnline(1);
             liveWatchUser.setUpdateTime(DateUtils.getNowDate());
         }else{
             liveWatchUser = new LiveWatchUser();
@@ -141,7 +141,7 @@ public class LiveWatchUserServiceImpl extends ServiceImpl<LiveWatchUserMapper, L
     public LiveWatchUser close(long liveId, long userId) {
         LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
         liveWatchUser.setUpdateTime(DateUtils.getNowDate());
-        liveWatchUser.setOnline(1);
+        liveWatchUser.setOnline(0);
         super.updateById(liveWatchUser);
         return liveWatchUser;
     }

+ 11 - 1
fs-service/src/main/resources/mapper/live/LiveDataMapper.xml

@@ -38,7 +38,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         where live_id = #{liveId}
     </select>
     <select id="getAllLiveIds" resultType="java.lang.Long">
-        select B.live_id from  live_data A left join live B on A.live_id = B.live_id where B.status = '2'
+        SELECT B.live_id
+        FROM live B
+        where B.status = '2'
+    </select>
+
+    <select id="getAllLiveDatas" resultMap="LiveDataResult">
+        SELECT A.favourite_num,A.follow_num,A.likes,A.live_id,A.page_views,
+               A.peak_concurrent_viewers,A.unique_viewers,A.unique_visitors,A.total_views
+        FROM live_data A
+                 inner join live B on A.live_id = B.live_id
+        where B.status = '2'
     </select>
     <select id="getRecentLive" resultType="com.fs.live.vo.RecentLiveDataVo">
         SELECT A.page_views AS pageViews,A.unique_visitors AS uniqueVisitors, B.live_id AS liveId,B.live_name AS liveName,B.`status` AS status,B.live_img_url AS liveImgUrl,B.start_time AS startTime

+ 1 - 1
fs-service/src/main/resources/mapper/live/LiveGoodsMapper.xml

@@ -109,7 +109,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         </foreach>
     </delete>
 
-    <select id="selectStoreIdByLiveId" parameterType="Long">
+    <select id="selectStoreIdByLiveId" parameterType="Long" resultType="Long">
         select distinct store_id from live_goods where live_id = #{liveId}
     </select>
     <insert id="insertLiveGoodsList">

+ 6 - 5
fs-service/src/main/resources/mapper/live/LiveWatchUserMapper.xml

@@ -3,7 +3,7 @@
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.fs.live.mapper.LiveWatchUserMapper">
-    
+
     <resultMap type="LiveWatchUser" id="LiveWatchUserResult">
         <result property="id"    column="id"    />
         <result property="liveId"    column="live_id"    />
@@ -23,14 +23,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 
     <select id="selectLiveWatchUserList" parameterType="LiveWatchUser" resultMap="LiveWatchUserResult">
         <include refid="selectLiveWatchUserVo"/>
-        <where>  
+        <where>
             <if test="liveId != null "> and live_id = #{liveId}</if>
             <if test="userId != null "> and user_id = #{userId}</if>
             <if test="msgStatus != null "> and msg_status = #{msgStatus}</if>
             <if test="online != null "> and online = #{online}</if>
         </where>
     </select>
-    
+
     <select id="selectLiveWatchUserById" parameterType="Long" resultMap="LiveWatchUserResult">
         <include refid="selectLiveWatchUserVo"/>
         where id = #{id}
@@ -47,6 +47,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         from live_watch_user lwu
         left join fs_user fu on lwu.user_id = fu.user_id
         where lwu.live_id = #{params.liveId}
+        order by lwu.create_time desc
     </select>
 
     <select id="selectLiveWatchUserByIdAndCompanyIdAndCompanyUserId" resultMap="LiveWatchUserResult">
@@ -114,9 +115,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     </delete>
 
     <delete id="deleteLiveWatchUserByIds" parameterType="String">
-        delete from live_watch_user where id in 
+        delete from live_watch_user where id in
         <foreach item="id" collection="array" open="(" separator="," close=")">
             #{id}
         </foreach>
     </delete>
-</mapper>
+</mapper>