Ver Fonte

直播相关功能

hgw há 3 anos atrás
pai
commit
6e4a728deb

+ 11 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/entity/LiveRoom.java

@@ -66,6 +66,10 @@ public class LiveRoom implements Serializable {
     @ApiModelProperty(value = "房间类型 live直播课  temp临时直播间")
     private String type;
 
+    @TableField("cover_pic_")
+    @ApiModelProperty(value = "封面图片")
+    private String coverPic;
+
     @TableField("created_by_")
     @ApiModelProperty(value = "创建人")
     private Long createdBy;
@@ -210,5 +214,12 @@ public class LiveRoom implements Serializable {
         this.updatedTime = updatedTime;
     }
 
+    public String getCoverPic() {
+        return coverPic;
+    }
+
+    public void setCoverPic(String coverPic) {
+        this.coverPic = coverPic;
+    }
 }
 

+ 25 - 2
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/LiveRoomService.java

@@ -6,6 +6,7 @@ import com.yonge.cooleshow.biz.dal.entity.LiveRoom;
 import com.yonge.cooleshow.biz.dal.entity.RoomUserInfoCache;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 直播房间与课程的关系表表(LiveRoom)表服务接口
@@ -17,12 +18,34 @@ public interface LiveRoomService extends IService<LiveRoom> {
 
     LiveRoomDao getDao();
 
+    /**
+     * 定时任务创建直播间
+     */
     void createCourseLiveRoom();
 
-    void createTempLiveRoom(LiveRoom room);
-
+    /**
+     * 定时任务-清理过期的房间
+     */
+    void destroyExpiredLiveRoom();
+
+    /**
+     * 创建临时房间-直播间
+     */
+    void createTempLiveRoom(Map<String, Object> param);
+
+    /**
+     * 进入房间
+     *
+     * @param roomUid 房间uid
+     * @param userId  用户id 主讲人或者用户的id
+     */
     void joinRoom(String roomUid, Long userId);
 
+    /**
+     * 查询在观看直播的用户信息
+     *
+     * @param roomUid 直播间uid
+     */
     List<RoomUserInfoCache> queryRoomUserInfo(String roomUid);
 
 }

+ 27 - 4
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/CourseScheduleServiceImpl.java

@@ -6,9 +6,8 @@ import com.yonge.cooleshow.biz.dal.dao.CourseScheduleDao;
 import com.yonge.cooleshow.biz.dal.entity.CourseSchedule;
 import com.yonge.cooleshow.biz.dal.enums.CourseScheduleTypeEnum;
 import com.yonge.cooleshow.biz.dal.service.CourseScheduleService;
+import com.yonge.cooleshow.common.exception.BizException;
 import org.apache.commons.collections.CollectionUtils;
-import org.joda.time.LocalDate;
-import org.joda.time.LocalDateTime;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
@@ -18,6 +17,7 @@ import org.springframework.validation.annotation.Validated;
 import javax.validation.Valid;
 import java.util.Date;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * 老师课程表(CourseSchedule)表服务实现类
@@ -37,6 +37,28 @@ public class CourseScheduleServiceImpl extends ServiceImpl<CourseScheduleDao, Co
     }
 
     /**
+     * 根据老师id查询直播课程
+     */
+    public void queryCourseList(Long teacherId) {
+        List<CourseSchedule> list = this.list(new QueryWrapper<>(new CourseSchedule()).lambda()
+                .eq(CourseSchedule::getTeacherId, teacherId)
+        );
+        if (CollectionUtils.isEmpty(list)) {
+            throw new BizException("没有课程");
+        }
+    }
+
+    /**
+     * 根据课程id查询直播间信息
+     */
+    public void getCourseInfo(Long courseId) {
+        CourseSchedule course = this.getById(courseId);
+        if (Objects.isNull(course)) {
+            throw new BizException("该课程没有直播间");
+        }
+    }
+
+    /**
      * 查询这个时间段有没有被占用-校验课时
      *
      * @param teacherId 老师id
@@ -94,10 +116,11 @@ public class CourseScheduleServiceImpl extends ServiceImpl<CourseScheduleDao, Co
 
     /**
      * 课程日历
+     *
      * @param teacherId 老师id
-     * @param date 年月日 yyyy-MM-dd 都是1号
+     * @param date      年月日 yyyy-MM-dd 都是1号
      */
-    public void courseCalendar(Long teacherId,String date) {
+    public void courseCalendar(Long teacherId, String date) {
 
     }
 

+ 51 - 37
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/LiveRoomServiceImpl.java

@@ -14,6 +14,7 @@ import com.yonge.cooleshow.biz.dal.service.CourseScheduleService;
 import com.yonge.cooleshow.biz.dal.service.LiveRoomService;
 import com.yonge.cooleshow.biz.dal.service.SysConfigService;
 import com.yonge.cooleshow.biz.dal.support.IMHelper;
+import com.yonge.cooleshow.biz.dal.support.WrapperUtil;
 import com.yonge.cooleshow.common.exception.BizException;
 import com.yonge.toolset.utils.date.DateUtil;
 import org.apache.commons.collections.CollectionUtils;
@@ -29,7 +30,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.util.*;
 import java.util.concurrent.TimeUnit;
-import java.util.function.Function;
+import java.util.function.BiFunction;
 import java.util.stream.Collectors;
 
 import static com.yonge.cooleshow.biz.dal.constant.SysConfigConstant.DESTROY_EXPIRED_LIVE_ROOM_MINUTE;
@@ -71,7 +72,7 @@ public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoom> impl
     //房间点赞数
     public static final String LIVE_ROOM_LIKE = COOLESHOW + ":LIVE_ROOM_LIKE:" + ROOM_UID;
     //生成房间UID
-    public static Function<Long, String> GenRoomUid = (userId) -> COOLESHOW + "-" + userId + "-" + new Date().getTime();
+    public static BiFunction<Long, RoomTypeEnum, String> GenRoomUid = (userId, en) -> String.join("-", COOLESHOW, en.getCode(), userId.toString(), new Date().getTime() + "");
     //房间的信息
     public static final String LIVE_ROOM_INFO = COOLESHOW + ":LIVE_ROOM_INFO:" + ROOM_UID;
 
@@ -81,19 +82,10 @@ public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoom> impl
     }
 
     /**
-     * 根据课程id查询直播间信息
-     */
-    public void getLiveRoomByCourseId(Long courseId) {
-        CourseSchedule course = courseScheduleService.getById(courseId);
-        if (Objects.isNull(course)) {
-            throw new BizException("该课程没有直播间");
-        }
-    }
-
-    /**
      * 定时任务创建直播间
      */
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void createCourseLiveRoom() {
         Date now = new Date();
         //查询房间提前创建的时间
@@ -120,31 +112,24 @@ public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoom> impl
                 .filter(Objects::nonNull)
                 .collect(Collectors.toMap(CourseGroup::getId, CourseGroup::getName));
         //生成课程对应的房间
+        RoomTypeEnum en = RoomTypeEnum.LIVE;
         courseScheduleList.forEach(c -> {
             LiveRoom room = new LiveRoom();
             room.setCourseGroupId(c.getCourseGroupId());
             room.setCourseId(c.getId());
-            room.setRoomUid(GenRoomUid.apply(c.getTeacherId()));
+            room.setRoomUid(GenRoomUid.apply(c.getTeacherId(), en));
             room.setRoomTitle(titleMap.get(c.getCourseGroupId()));
             room.setSpeakerId(c.getTeacherId());
             room.setLiveStartTime(c.getStartTime());
             room.setLiveEndTime(c.getEndTime());
             room.setLiveState(0);
             room.setRoomState(0);
-            room.setType(RoomTypeEnum.LIVE.getCode());
+            room.setType(en.getCode());
             room.setCreatedBy(-2L);
             room.setCreatedTime(now);
             this.save(room);
-            try {
-                //生成主讲人信息
-                createRoomInfoCache(room);
-                //去融云创建房间
-                createLiveRoom(room.getRoomUid(), room.getRoomTitle());
-            } catch (Exception e) {
-                //为什么要删除,因为如果融云创建失败,那么就不会有房间了,需要删除
-                this.removeById(room.getId());
-                log.error("创建直播间失败", e.getCause());
-            }
+            //去融云创建房间及创建房间缓存信息
+            createLiveRoomInfo(room);
         });
 
     }
@@ -154,20 +139,47 @@ public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoom> impl
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public void createTempLiveRoom(LiveRoom room) {
+    public void createTempLiveRoom(Map<String, Object> param) {
+        String roomTitle = WrapperUtil.toStr(param, "roomTitle", "房间标题不能为空!");
+        String liveRemark = WrapperUtil.toStr(param, "liveRemark", "直播间描述不能为空!");
+        String coverPic = WrapperUtil.toStr(param, "coverPic", "直播间封面不能为空!");
         //查询主讲人信息
         SysUser sysUser = getSysUser();
-
-        room.setCourseGroupId(0L);
-        room.setCourseId(0L);
-        room.setType(RoomTypeEnum.TEMP.getCode());
-        room.setRoomUid(GenRoomUid.apply(sysUser.getId()));
+        Long id = sysUser.getId();
+        Date now = new Date();
+        RoomTypeEnum en = RoomTypeEnum.TEMP;
+        LiveRoom room = new LiveRoom();
+        room.setCourseGroupId(-1L);
+        room.setCourseId(-1L);
+        room.setRoomUid(GenRoomUid.apply(id, en));
+        room.setRoomTitle(roomTitle);
+        room.setLiveRemark(liveRemark);
+        room.setCoverPic(coverPic);
+        room.setSpeakerId(id);
+        room.setLiveStartTime(now);
+        room.setLiveState(0);
+        room.setRoomState(0);
+        room.setCreatedBy(id);
+        room.setCreatedTime(now);
+        room.setType(en.getCode());
         this.save(room);
         log.info("createTempLiveRoom>>>>>>room:{}", room.getRoomUid());
-        //生成主讲人信息到缓存
-        createRoomInfoCache(room, sysUser);
-        //去融云创建房间
-        createLiveRoom(room.getRoomUid(), room.getRoomTitle());
+        //去融云创建房间及创建房间缓存信息
+        createLiveRoomInfo(room);
+    }
+
+    /**
+     * 去融云创建房间及创建房间缓存信息-直播间
+     */
+    private void createLiveRoomInfo(LiveRoom room) {
+        try {
+            //生成主讲人信息
+            createRoomInfoCache(room);
+            //去融云创建房间
+            createLiveRoom(room.getRoomUid(), room.getRoomTitle());
+        } catch (Exception e) {
+            throw new BizException("创建直播间失败!", e.getCause());
+        }
     }
 
     //生成主讲人信息
@@ -231,8 +243,10 @@ public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoom> impl
         Date now = new Date();
         //查询已经开始并且没有删除及销毁的直播间
         List<LiveRoom> list = this.list(new QueryWrapper<LiveRoom>().lambda()
+                .eq(LiveRoom::getRoomState, 0)
+                .eq(LiveRoom::getLiveState, 1)
                 .eq(LiveRoom::getType, RoomTypeEnum.LIVE.getCode())
-                .ge(LiveRoom::getLiveEndTime, now));
+                .le(LiveRoom::getLiveEndTime, now));
         if (CollectionUtils.isEmpty(list)) {
             return;
         }
@@ -283,10 +297,11 @@ public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoom> impl
         } else {
             throw new BizException("房间不存在!");
         }
+        Date now = new Date();
         //进入房间的是主讲人
         if (roomInfo.getSpeakerId().equals(userId)) {
             roomInfo.setSpeakerState(0);
-            roomInfo.setJoinRoomTime(new Date());
+            roomInfo.setJoinRoomTime(now);
             roomInfoCache.set(roomInfo, 2L, TimeUnit.DAYS);
             return;
         }
@@ -296,7 +311,6 @@ public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoom> impl
         RMap<Long, RoomUserInfoCache> roomTotalUser = redissonClient.getMap(LIVE_ROOM_TOTAL_USER_LIST.replace(ROOM_UID, roomUid));
         //判断是否第一次进房间
         RoomUserInfoCache userInfo;
-        Date now = new Date();
         if (roomTotalUser.containsKey(userId)) {
             //多次进入更新动态进入时间
             userInfo = roomTotalUser.get(userId);

+ 6 - 5
cooleshow-user/user-biz/src/main/resources/config/mybatis/LiveRoomMapper.xml

@@ -18,24 +18,25 @@
         <result column="created_time_" jdbcType="TIMESTAMP" property="createdTime"/>
         <result column="updated_by_" jdbcType="INTEGER" property="updatedBy"/>
         <result column="updated_time_" jdbcType="TIMESTAMP" property="updatedTime"/>
+        <result column="cover_pic_" jdbcType="VARCHAR" property="coverPic"/>
     </resultMap>
 
     <sql id="Base_Column_List">
         id_
-        , course_group_id_, course_id_, speaker_id_, room_uid_, room_title_, live_start_time_, live_end_time_, live_remark_, live_state_, room_state_, type_, created_by_, created_time_, updated_by_, updated_time_
+        , course_group_id_, course_id_, speaker_id_, room_uid_, room_title_, live_start_time_, live_end_time_, live_remark_, live_state_, room_state_, type_, cover_pic_, created_by_, created_time_, updated_by_, updated_time_
     </sql>
 
     <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
             parameterType="com.yonge.cooleshow.biz.dal.entity.LiveRoom">
         insert into live_room(course_group_id_, course_id_, speaker_id_, room_uid_, room_title_, live_start_time_,
-        live_end_time_, live_remark_, live_state_, room_state_, type_, created_by_, created_time_, updated_by_,
-        updated_time_)
+        live_end_time_, live_remark_, live_state_, room_state_, type_, cover_pic_, created_by_, created_time_,
+        updated_by_, updated_time_)
         values
         <foreach collection="entities" item="entity" separator=",">
             (#{entity.courseGroupId}, #{entity.courseId}, #{entity.speakerId}, #{entity.roomUid}, #{entity.roomTitle},
             #{entity.liveStartTime}, #{entity.liveEndTime}, #{entity.liveRemark}, #{entity.liveState},
-            #{entity.roomState}, #{entity.type}, #{entity.createdBy}, #{entity.createdTime}, #{entity.updatedBy},
-            #{entity.updatedTime})
+            #{entity.roomState}, #{entity.type}, #{entity.coverPic}, #{entity.createdBy}, #{entity.createdTime},
+            #{entity.updatedBy}, #{entity.updatedTime})
         </foreach>
     </insert>
 

+ 36 - 2
cooleshow-user/user-teacher/src/main/java/com/yonge/cooleshow/teacher/controller/LiveRoomController.java

@@ -1,13 +1,17 @@
 package com.yonge.cooleshow.teacher.controller;
 
 
-import com.yonge.cooleshow.biz.dal.entity.LiveRoom;
 import com.yonge.cooleshow.biz.dal.service.LiveRoomService;
-import org.springframework.web.bind.annotation.*;
 import com.yonge.cooleshow.common.controller.BaseController;
+import com.yonge.cooleshow.common.entity.HttpResponseResult;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import java.util.Map;
 
 /**
  * 直播房间与课程的关系表表(LiveRoom)表控制层
@@ -25,5 +29,35 @@ public class LiveRoomController extends BaseController {
     @Resource
     private LiveRoomService liveRoomService;
 
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "roomTitle", dataType = "String", value = "房间标题"),
+            @ApiImplicitParam(name = "liveRemark", dataType = "String", value = "直播内容/最多200个字"),
+            @ApiImplicitParam(name = "coverPic", dataType = "String", value = "封面图片"),
+    })
+    @ApiOperation("创建临时房间-直播间")
+    @PostMapping("/createTempLiveRoom")
+    public HttpResponseResult<Object> createTempLiveRoom(@RequestBody Map<String, Object> param) {
+        liveRoomService.createTempLiveRoom(param);
+        return succeed();
+    }
+
+    @ApiOperation("进入房间")
+    @GetMapping(value = "/joinRoom")
+    public HttpResponseResult<Object> joinRoom(String roomUid, Long userId) {
+        liveRoomService.joinRoom(roomUid, userId);
+        return succeed();
+    }
+
+    @ApiOperation("定时任务-创建房间-直播间")
+    @GetMapping("/createCourseLiveRoom")
+    public void createCourseLiveRoom() {
+        liveRoomService.createCourseLiveRoom();
+    }
+
+    @ApiOperation("定时任务-销毁房间-直播间")
+    @GetMapping("/createCourseLiveRoom")
+    public void destroyExpiredLiveRoom() {
+        liveRoomService.destroyExpiredLiveRoom();
+    }
 }
 

+ 19 - 2
cooleshow-user/user-teacher/src/main/java/com/yonge/cooleshow/teacher/controller/LiveRoomVideoController.java

@@ -1,11 +1,17 @@
 package com.yonge.cooleshow.teacher.controller;
 
 
-import com.yonge.cooleshow.biz.dal.entity.LiveRoomVideo;
+import com.alibaba.fastjson.JSONObject;
+import com.yonge.cooleshow.biz.dal.entity.RecordNotify;
 import com.yonge.cooleshow.biz.dal.service.LiveRoomVideoService;
-import org.springframework.web.bind.annotation.*;
 import com.yonge.cooleshow.common.controller.BaseController;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
 
@@ -19,11 +25,22 @@ import javax.annotation.Resource;
 @RestController
 @RequestMapping("/liveRoomVideo")
 public class LiveRoomVideoController extends BaseController {
+
+    private final static Logger log = LoggerFactory.getLogger(LiveRoomVideoController.class);
+
     /**
      * 服务对象
      */
     @Resource
     private LiveRoomVideoService liveRoomVideoService;
 
+    @ApiOperation("录制结果回调")
+    @RequestMapping(value = "/recordSync")
+    public void recordSync(@RequestBody String body) {
+        log.info("recordSync body:{}", body);
+        RecordNotify recordNotify = JSONObject.parseObject(body, RecordNotify.class);
+        liveRoomVideoService.recordSync(recordNotify);
+    }
+
 }