Explorar o código

定时任务 修改 数据 添加5分钟之内开播直播间

yuhongqi hai 1 semana
pai
achega
0c45d7e6ac

+ 33 - 22
fs-live-socket/src/main/java/com/fs/live/task/Task.java

@@ -93,6 +93,7 @@ public class Task {
             return;
         List<Long> liveIdLists = list.stream().map(Live::getLiveId).collect(Collectors.toList());
         List<LiveAutoTask> liveAutoTasks = liveAutoTaskService.selectLiveAutoTaskByLiveIds(liveIdLists);
+        List<Live> liveList = new ArrayList<>();
         LocalDateTime now = LocalDateTime.now().plusSeconds(2L);
         List<Live> startLiveList = new ArrayList<>();
         List<Live> endLiveList = new ArrayList<>();
@@ -101,25 +102,36 @@ public class Task {
                 if (live.getFinishTime() == null) {
                     if (now.isAfter(live.getStartTime().minusSeconds(2L))){
                         if(live.getStatus() != 2){
+                            live.setStatus(2);
                             startLiveList.add(live);
+                            liveList.add(live);
                         }
-                        live.setStatus(2);
                     } else if (now.isBefore(live.getStartTime())) {
-                        live.setStatus(1);
+                        if (live.getStatus() != 1) {
+                            live.setStatus(1);
+                            liveList.add(live);
+                        }
+
                     }
                 } else {
                     if (now.isAfter(live.getStartTime().minusSeconds(2L)) && now.isBefore(live.getFinishTime())) {
                         if(live.getStatus() != 2){
                             startLiveList.add(live);
+                            live.setStatus(2);
+                            liveList.add(live);
                         }
                         live.setStatus(2);
                     } else if (now.isBefore(live.getStartTime().minusSeconds(2L))) {
-                        live.setStatus(1);
+                        if (live.getStatus() != 1) {
+                            live.setStatus(1);
+                            liveList.add(live);
+                        }
                     } else if (now.isAfter(live.getFinishTime().minusSeconds(2L))) {
                         if(live.getStatus() != 3){
                             endLiveList.add(live);
+                            live.setStatus(3);
+                            liveList.add(live);
                         }
-                        live.setStatus(3);
                     }
                 }
             } else {
@@ -127,26 +139,25 @@ public class Task {
                 LiveReplayParam liveReplayParam = JSON.parseObject(live.getLiveConfig(), LiveReplayParam.class);
                 if (liveReplayParam.getIsPlaybackOpen()) {
                     if (liveReplayParam.getFinishTime() != null) {
-                        if (now.isAfter(live.getFinishTime().minusSeconds(2L))) {
+                        if (now.isAfter(liveReplayParam.getFinishTime().minusSeconds(2L))) {
                             if(live.getStatus() != 3){
                                 endLiveList.add(live);
+                                live.setStatus(3);
+                                liveList.add(live);
                             }
-                            live.setStatus(3);
                         }
                     }
                 }
             }
-
         });
         String key = "live:auto_task:";
-
-        if(!list.isEmpty()){
-            for (Live live : list) {
-                liveService.updateLive(live);
-            }
-        }
         if (!startLiveList.isEmpty()) {
             for (Live live : startLiveList) {
+                redisUtil.zSetAdd(LiveKeysConstant.LIVE_HOME_PAGE_LIST, JSON.toJSONString(live), live.getCreateTime().getTime());
+                SendMsgVo sendMsgVo = new SendMsgVo();
+                sendMsgVo.setMsg("开始直播");
+                sendMsgVo.setCmd("live_start");
+                webSocketServer.broadcastMessage(live.getLiveId(), JSONObject.toJSONString(R.ok().put("data",sendMsgVo)));
                 List<LiveAutoTask> collect = liveAutoTasks.stream().filter(liveAutoTask -> liveAutoTask.getLiveId().equals(live.getLiveId())).collect(Collectors.toList());
                 if (!collect.isEmpty()) {
                     collect.forEach(liveAutoTask -> {
@@ -156,15 +167,14 @@ public class Task {
                         redisCache.redisTemplate.expire(key+live.getLiveId(), 30, java.util.concurrent.TimeUnit.MINUTES);
                     });
                 }
-                liveService.asyncToCacheLiveDetail(live.getLiveId());
-                SendMsgVo sendMsgVo = new SendMsgVo();
-                sendMsgVo.setMsg("开始直播");
-                sendMsgVo.setCmd("live_start");
-                webSocketServer.broadcastMessage(live.getLiveId(), JSONObject.toJSONString(R.ok().put("data",sendMsgVo)));
             }
         }
         if (!endLiveList.isEmpty()) {
             for (Live live : endLiveList) {
+                SendMsgVo sendMsgVo = new SendMsgVo();
+                sendMsgVo.setMsg("结束直播");
+                sendMsgVo.setCmd("live_end");
+                webSocketServer.broadcastMessage(live.getLiveId(), JSONObject.toJSONString(R.ok().put("data",sendMsgVo)));
                 List<LiveAutoTask> collect = liveAutoTasks.stream().filter(liveAutoTask -> liveAutoTask.getLiveId().equals(live.getLiveId())).collect(Collectors.toList());
                 if (!collect.isEmpty()) {
                     collect.forEach(liveAutoTask -> {
@@ -173,10 +183,11 @@ public class Task {
                         redisCache.redisTemplate.opsForZSet().remove(key + live.getLiveId(), JSON.toJSONString(liveAutoTask),liveAutoTask.getAbsValue().getTime());
                     });
                 }
-                SendMsgVo sendMsgVo = new SendMsgVo();
-                sendMsgVo.setMsg("开始直播");
-                sendMsgVo.setCmd("live_start");
-                webSocketServer.broadcastMessage(live.getLiveId(), JSONObject.toJSONString(R.ok().put("data",sendMsgVo)));
+            }
+        }
+        if(!liveList.isEmpty()){
+            for (Live live : liveList) {
+                liveService.updateLiveEntity(live);
             }
         }
     }

+ 2 - 0
fs-service-system/src/main/java/com/fs/live/mapper/LiveMapper.java

@@ -127,4 +127,6 @@ public interface LiveMapper
 
     @Update("update live set is_audit = #{isAudit} where live_id = #{liveId}")
     Integer updateLiveIsAudit(Live live);
+
+    List<Live> selectReadyStartLiveList();
 }

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

@@ -76,7 +76,7 @@ public interface ILiveAutoTaskService {
 
     List<LiveAutoTask> selectLiveAutoTaskByLiveIds(List<Long> liveIdLists);
 
-    void recalcLiveAutoTask(Long liveId);
+    void recalcLiveAutoTask(Live live);
 
     void finishLiveAutoTask(LiveAutoTask task);
 

+ 2 - 0
fs-service-system/src/main/java/com/fs/live/service/ILiveService.java

@@ -183,4 +183,6 @@ public interface ILiveService
     R subNotifyLive(HashMap<String, Object> param);
 
     Integer updateLiveIsAudit(Live live);
+
+    int updateLiveEntity(Live live);
 }

+ 3 - 3
fs-service-system/src/main/java/com/fs/live/service/impl/LiveAutoTaskServiceImpl.java

@@ -253,11 +253,11 @@ public class LiveAutoTaskServiceImpl implements ILiveAutoTaskService {
     }
 
     @Override
-    public void recalcLiveAutoTask(Long liveId) {
-        Live live = liveMapper.selectLiveByLiveId(liveId);
+    public void recalcLiveAutoTask(Live live) {
+
         if(live == null) return;
         // 目前只重算 推送商品 弹幕脚本
-        List<LiveAutoTask> liveAutoTasks = baseMapper.recalcLiveAutoTaskQuery(liveId);
+        List<LiveAutoTask> liveAutoTasks = baseMapper.recalcLiveAutoTaskQuery(live.getLiveId());
         Date now = new Date();
         for (LiveAutoTask liveAutoTask : liveAutoTasks) {
             liveAutoTask.setAbsValue(getTriggerValue(liveAutoTask.getTriggerValue(), live.getStartTime()));

+ 17 - 2
fs-service-system/src/main/java/com/fs/live/service/impl/LiveServiceImpl.java

@@ -253,6 +253,8 @@ public class LiveServiceImpl implements ILiveService
         return baseMapper.updateLiveIsAudit(live);
     }
 
+
+
     /**
      * 查询企业直播
      * @param liveId            直播ID
@@ -341,6 +343,12 @@ public class LiveServiceImpl implements ILiveService
         return save > 0 ? 1 : 0;
     }
 
+    @Override
+    @Transactional
+    public int updateLiveEntity(Live live) {
+        return baseMapper.updateLive( live);
+    }
+
     /**
      * 修改直播
      *
@@ -378,7 +386,7 @@ public class LiveServiceImpl implements ILiveService
 
         }
         int result = baseMapper.updateLive(live);
-        liveAutoTaskService.recalcLiveAutoTask(live.getLiveId());
+        liveAutoTaskService.recalcLiveAutoTask(live);
         return result;
     }
 
@@ -407,7 +415,14 @@ public class LiveServiceImpl implements ILiveService
 
     @Override
     public List<Live> liveList() {
-        return baseMapper.liveList();
+        List<Live> livesList = baseMapper.liveList();
+        try {
+            // 查询准备开播的直播间
+            livesList.addAll(baseMapper.selectReadyStartLiveList());
+        }catch (Exception e){
+            log.error("查询准备开播的直播间异常", e);
+        }
+        return livesList;
     }
 
     @Override

+ 14 - 1
fs-service-system/src/main/resources/mapper/live/LiveMapper.xml

@@ -48,10 +48,23 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         a
         left join live_video b on a.live_id = b.live_id
         left join company c on a.company_id = c.company_id
-        where 1=1 and a.is_del = 0 and a.is_show = 1 and a.status in (2,4) and b.video_type in (1,2)
+        where 1=1 and a.is_del = 0 and a.is_audit = 1 and a.is_show = 1 and a.status in (2,4) and b.video_type in (1,2)
 
         order by create_time desc
     </select>
+    <select id="selectReadyStartLiveList" parameterType="Live" resultMap="LiveResult">
+        select a.live_id, a.company_id, a.company_user_id,talent_id, a.live_name, a.is_audit, a.live_desc, a.show_type, a.status, a.anchor_id,
+        a.live_type, a.start_time, a.finish_time, a.live_img_url, a.live_config, a.id_card_url, a.is_show, a.is_del, a.qw_qr_code, a.rtmp_url,
+        a.flv_hls_url, a.create_time, a.create_by, a.update_by, a.update_time, a.remark,config_json, b.video_url,c.company_name,b.file_size
+        from live
+        a
+        left join live_video b on a.live_id = b.live_id
+        left join company c on a.company_id = c.company_id
+        where 1=1 and a.is_del = 0 and a.is_audit = 1 and a.is_show = 1 and a.status in (1) and b.video_type in (1,2)
+          and a.start_time &lt;= NOW() + INTERVAL 5 MINUTE
+          and a.start_time >= NOW()
+        order by create_time desc
+    </select>
 
     <select id="selectLiveList" parameterType="com.fs.live.domain.Live" resultMap="LiveResult">
         select a.live_id, a.company_id, a.company_user_id,talent_id, a.live_name, a.is_audit, a.live_desc, a.show_type, a.status, a.anchor_id,