派生自 projectDept/qhighschool

EricsHu
2023-11-24 0ad2f07a292895eeb3b9618eb1e275568c63a59e
src/main/java/com/qxueyou/scc/teach/live/service/impl/MediaLiveService.java
@@ -35,29 +35,29 @@
@Service
public class MediaLiveService extends CommonAppService implements IMediaLiveService {
   @Autowired
   MediaLiveDAO liveDao;
   @Autowired
   SccConfig cfg;
   @Autowired
   IMsgInfoService msgInfoService;
   @Autowired
   ICacheService cacheService;
   @Autowired
   private ITeacherService teacherService;
   @Override
   public Result add(String name, String content, String teacherId, String teacherName , String subjectId, String subjectName,
                 String definition, String imgPath, Date startTime, Date endTime, Integer courseId, boolean record, List<String> classIds) {
                 String definition, String imgPath, Date startTime, Date endTime, Integer courseId, boolean record, List<String> classIds,String portalStatus) {
      UserTeacher teacher = teacherService.getTeacherByUserId(ClientUtils.getUserId());
      MediaVideoLive live = new MediaVideoLive();
      TraceUtils.setCreateTrace(live);
@@ -67,8 +67,9 @@
      live.setPreviewImgUrl(imgPath);
      live.setStartTime(startTime);
      live.setEndTime(endTime);
      live.setPortalStatus(portalStatus);
      live.setStatus(MediaVideoLive.STATUS_LIVE_DRAFT);
      if(teacher!=null){
         live.setAnchorId(teacher.getTeacherId());
         live.setAnchor(teacher.getName());
@@ -76,31 +77,32 @@
         live.setAnchorId(ClientUtils.getUserId());
         live.setAnchor(ClientUtils.getUserName());
      }
      live.setSubjectId(subjectId);
      live.setSubjectName(subjectName);
      live.setCreateId(teacher.getTeacherId());
      live.setCreateTime(new Date());
      String uuid = UUIDUtils.UUID();
      live.setWyLiveNumber(uuid);
      live.setPushUrl(cfg.getRtmpService().concat(uuid));
      live.setHlsPullUrl(cfg.getHlsService().concat(uuid).concat(".m3u8"));
      save(live);
      //保存直播权限班级
      if(classIds!=null && classIds.size()>0){
         this.doClearAndAddLiveReClass(live.getVideoLiveId(), classIds.toArray(new String[classIds.size()]));
      }
      return new Result(true,"",CollectionUtils.newObjectMap("videoLiveId",live.getVideoLiveId()));
   }
   @Override
   public Result update(String liveId, String name, String content,String teacherId,String teacherName , String subjectId,String subjectName,
         String definition,String imgPath, Date startTime, Date endTime,boolean record, List<String> classIds) {
                   String definition,String imgPath, Date startTime, Date endTime,boolean record, List<String> classIds) {
      MediaVideoLive live = read(MediaVideoLive.class,liveId);
      TraceUtils.setUpdateTrace(live);
      live.setName(name);
      live.setRemark(content);
      live.setPreviewImgUrl(imgPath);
@@ -108,18 +110,18 @@
      live.setEndTime(endTime);
      live.setSubjectId(subjectId);
      live.setSubjectName(subjectName);
      live.setCreateTime(new Date());
      save(live);
      //更新班级课程关联关系
      this.doClearAndAddLiveReClass(live.getVideoLiveId(), classIds!=null?classIds.toArray(new String[classIds.size()]):null);
//      liveDao.saveLiveClasses(liveId, classIds);
      return Result.SUCCESS;
   }
   /**
    * 清理并添加直播权限班级
    * @param liveId
@@ -127,22 +129,22 @@
    */
   private void doClearAndAddLiveReClass(String liveId, String [] classIds) {
      List<String>  reIdLst =  this.find("select r.liveReClassId from MediaVideoLiveReClass r where r.liveId=?",
               CollectionUtils.newList(liveId), String.class);
            CollectionUtils.newList(liveId), String.class);
      //删除关联关系
      if(reIdLst!=null && reIdLst.size()>0){
         this.bulkUpdateInLoop("delete from MediaVideoLiveReClass  where liveReClassId=?", reIdLst.toArray());
         this.bulkUpdateInLoop("delete from MediaVideoLiveReClass  where liveReClassId=?", reIdLst.toArray());
      }
      if(classIds!=null && classIds.length>0){
         for(String classId:classIds){
            MediaVideoLiveReClass re = new MediaVideoLiveReClass();
             TraceUtils.setCreateTrace(re);
             re.setClassId(classId);
             re.setLiveId(liveId);
             save(re);
            TraceUtils.setCreateTrace(re);
            re.setClassId(classId);
            re.setLiveId(liveId);
            save(re);
         }
      }
   }
   }
   @Override
   public List<String> getLiveClasses(String liveId) {
@@ -178,54 +180,54 @@
      doSendMsg(liveId,MsgInfo.TYPE_LIVE,"发布了直播");
      return new Result(true);
   }
   private void doSendMsg(String liveId,short type,String msg){
      MediaVideoLive  liveInfo = this.read(liveId);
      //查询直播关联的班级
      String hql = "select s.userId from MediaVideoLiveReClass m,StuStudent s where m.classId=s.classId and m.liveId=? and m.deleteFlag is false and s.deleteFlag is false";
      List<String> lstUserIds = this.find(hql, CollectionUtils.newList(liveId),String.class);
      if(lstUserIds!=null && lstUserIds.size()>0){
         Map<String,String> attrs = CollectionUtils.newStringMap("liveId",liveId,"liveName",liveInfo.getName(),"subjectId",liveInfo.getSubjectId(),
               "subjectName",liveInfo.getSubjectName(),"startTime",String.valueOf(liveInfo.getStartTime().getTime()));
         msgInfoService.doSendTextMsgToUsers(lstUserIds.toArray(new String[lstUserIds.size()]),type,msg,attrs);
      }
   }
   @Scheduled(cron = " 0 0/5 * * * ?")
   protected void doTimer() {
//      System.out.println("执行直播提醒定时器");
      Calendar cal = Calendar.getInstance();
      cal.add(Calendar.HOUR, 1);
      Date beginDate = cal.getTime();
      cal.add(Calendar.MINUTE, 5);
      Date endDate = cal.getTime();
      //查询1小时内将开始的直播
       List<MediaVideoLive> lstLives = this.find("from MediaVideoLive where startTime>=? and startTime<? and status=? and deleteFlag is false ",
             CollectionUtils.newList(beginDate,endDate,MediaVideoLive.STATUS_LIVE_REVIEW), MediaVideoLive.class);
      if(lstLives!=null && lstLives.size()>0){
         for(MediaVideoLive live:lstLives){
            this.doLiveRemindMsg(live.getVideoLiveId(),MsgInfo.TYPE_REMIND, "直播马上开始了,请做好直播准备");
         }
      }
   }
//   @Scheduled(cron = " 0 0/5 * * * ?")
//   protected void doTimer() {
////      System.out.println("执行直播提醒定时器");
//      Calendar cal = Calendar.getInstance();
//      cal.add(Calendar.HOUR, 1);
//      Date beginDate = cal.getTime();
//
//      cal.add(Calendar.MINUTE, 5);
//      Date endDate = cal.getTime();
//
//      //查询1小时内将开始的直播
//      List<MediaVideoLive> lstLives = this.find("from MediaVideoLive where startTime>=? and startTime<? and status=? and deleteFlag is false ",
//            CollectionUtils.newList(beginDate,endDate,MediaVideoLive.STATUS_LIVE_REVIEW), MediaVideoLive.class);
//
//      if(lstLives!=null && lstLives.size()>0){
//         for(MediaVideoLive live:lstLives){
//            this.doLiveRemindMsg(live.getVideoLiveId(),MsgInfo.TYPE_REMIND, "直播马上开始了,请做好直播准备");
//         }
//      }
//   }
   private void doLiveRemindMsg(String liveId,short type,String msg){
      MediaVideoLive  liveInfo = this.read(liveId);
      Map<String,String> attrs = CollectionUtils.newStringMap("liveId",liveId,
               "liveName",liveInfo.getName(),
               "remindType","live",
               "status",String.valueOf(liveInfo.getStatus()),
                "subjectId",liveInfo.getSubjectId(),
               "subjectName",liveInfo.getSubjectName(),
               "startTime",String.valueOf(liveInfo.getStartTime().getTime()),
               "endTime",String.valueOf(liveInfo.getEndTime().getTime()));
            "liveName",liveInfo.getName(),
            "remindType","live",
            "status",String.valueOf(liveInfo.getStatus()),
            "subjectId",liveInfo.getSubjectId(),
            "subjectName",liveInfo.getSubjectName(),
            "startTime",String.valueOf(liveInfo.getStartTime().getTime()),
            "endTime",String.valueOf(liveInfo.getEndTime().getTime()));
      msgInfoService.doSendTextMsgToUsers(new String[]{liveInfo.getCreateId()},type,msg,attrs);
   }
@@ -248,11 +250,11 @@
   public Result doStop(String liveId) {
      //发送生成回放消息
      cacheService.lstRightPush(MediaVideoLivePlayBackService.LIVE_PLAYBACK_LST, liveId);
      return status(liveId,MediaVideoLive.STATUS_LIVE_DOWNLOAD);
   }
   private Result status(String liveId,short status) {
      MediaVideoLive live = read(MediaVideoLive.class,liveId);
      TraceUtils.setUpdateTrace(live);
@@ -271,9 +273,9 @@
      StringBuffer hql = new StringBuffer(1000);
      Map<String,Object> params = new HashMap<String,Object>();
      hql.append("from MediaVideoLive m left join fetch m.classes  c left join fetch m.subject s where m.deleteFlag is false  and m.name like :name ");
      params.put("name",keyword + "%");
      if(classIds!=null && classIds.length>0){
         hql.append(" and (c.classId in(:classIds) or s.type=:Type)");
         params.put("classIds", classIds);
@@ -282,12 +284,12 @@
         hql.append(" and  s.type=:subjectType");
         params.put("subjectType", Subject.TYPE_PUBLIC_SUBJECT);
      }
      if(!StringUtils.isEmpty(subjectId)){
          hql.append(" and m.subjectId=:subjectId");
          params.put("subjectId", subjectId);
         hql.append(" and m.subjectId=:subjectId");
         params.put("subjectId", subjectId);
      }
      if(status!=null){
         hql.append(" and m.status=:status");
         params.put("status", status);
@@ -295,24 +297,24 @@
         hql.append(" and m.status!=:status");
         params.put("status",MediaVideoLive.STATUS_LIVE_DRAFT);
      }
      hql.append(" order by m.createTime desc");
      return this.findByComplexHql(hql.toString(),new Pager(pageSize,pageNum),params,MediaVideoLive.class);
   }
   }
   @Override
   public int listCount(String keyword,String [] classIds,String subjectId,Short status) {
      StringBuffer hql = new StringBuffer(1000);
      Map<String,Object> params = new HashMap<String,Object>();
      hql.append("from MediaVideoLive m left join m.subject s where m.deleteFlag is false and m.name like :name ");
      params.put("name","%" + keyword + "%");
      if(!StringUtils.isEmpty(subjectId)){
          hql.append(" and m.subjectId=:subjectId");
          params.put("subjectId", subjectId);
         hql.append(" and m.subjectId=:subjectId");
         params.put("subjectId", subjectId);
      }
      if(status!=null){
         hql.append(" and m.status=:status");
         params.put("status", status);
@@ -320,40 +322,57 @@
         hql.append(" and m.status!=:status");
         params.put("status",MediaVideoLive.STATUS_LIVE_DRAFT);
      }
      if(classIds!=null && classIds.length>0){
          hql.append(" and ( exists (select 1 from MediaVideoLiveReClass r where r.deleteFlag is false and r.liveId= m.videoLiveId and r.classId in (:classIds) ) or s.type=:subjectType )");
          params.put("classIds", classIds);
          params.put("subjectType", Subject.TYPE_PUBLIC_SUBJECT);
         hql.append(" and ( exists (select 1 from MediaVideoLiveReClass r where r.deleteFlag is false and r.liveId= m.videoLiveId and r.classId in (:classIds) ) or s.type=:subjectType )");
         params.put("classIds", classIds);
         params.put("subjectType", Subject.TYPE_PUBLIC_SUBJECT);
      }else{
          hql.append(" and  s.type=:subjectType ");
          params.put("subjectType", Subject.TYPE_PUBLIC_SUBJECT);
         hql.append(" and  s.type=:subjectType ");
         params.put("subjectType", Subject.TYPE_PUBLIC_SUBJECT);
      }
      return this.findCountByComplexHql(hql.toString(), params);
   }
   @Override
   public List<MediaVideoLive> listvideo(String status) {
      String hql = "from MediaVideoLive where deleteFlag is false and status=?  order by createTime desc";
      //是否是老师
      String teacherId = ClientUtils.isAdmin() ? null : teacherService.getTeacherIdByUserId(ClientUtils.getUserId());
      String hql = "from MediaVideoLive where deleteFlag is false and status=? ";
      List<Object> args =CollectionUtils.newList(status);
      if (StringUtils.isNotBlank(teacherId)) {
         hql.concat(" and createId=?");
         args.add(teacherId);
      }
      hql.concat(" order by createTime desc");
      return find(hql, CollectionUtils.newList(status), MediaVideoLive.class);
      return find(hql, args, MediaVideoLive.class);
   }
   @Override
   public List<MediaVideoLive> listOfMine(String keyword, Integer pageNum, Integer pageSize) {
      String hql = "from MediaVideoLive where deleteFlag is false and name like ?  order by createTime desc";
      return findList(hql, new Pager(pageSize, pageNum), CollectionUtils.newList(keyword + "%"), MediaVideoLive.class);
   public List<MediaVideoLive> listOfMine(String keyword,String portalStatus, Integer pageNum, Integer pageSize) {
      //是否是老师
      String teacherId = ClientUtils.isAdmin() ? null : teacherService.getTeacherIdByUserId(ClientUtils.getUserId());
      StringBuffer hql = new StringBuffer("from MediaVideoLive where deleteFlag is false and name like ? ");
      List<Object> args =CollectionUtils.newList(keyword + "%");
      if(portalStatus != ""){
         hql.append(" and portalStatus = ?");
         args.add(portalStatus);
      }
      if (StringUtils.isNotBlank(teacherId)) {
         hql.append(" and createId=?");
         args.add(teacherId);
      }
      hql.append(" order by createTime desc");
      return findList(hql.toString(), new Pager(pageSize, pageNum),args , MediaVideoLive.class);
   }
   @Override
   public int listCountOfMine(String keyword) {
      String hql = "from MediaVideoLive where deleteFlag is false and name like ? and createId=? order by createTime desc";
      return findCount(hql, CollectionUtils.newList(keyword + "%",ClientUtils.getUserId()));
@@ -374,16 +393,16 @@
   @SuppressWarnings("unchecked")
   @Override
   public List<MediaVideoLive> readByStatus(List<String> liveIdLst,short[] status) {
      if(liveIdLst.isEmpty()) {
         return Collections.EMPTY_LIST;
      }
      Object[] _status = new Object[status.length];
      for(int i=0;i<status.length;i++) {
         _status[i]=status[i];
      }
      String hql = "from MediaVideoLive where deleteFlag is false and videoLiveId in (:liveId) and status in (:status)";
      return findByComplexHql(hql, CollectionUtils.newObjectMap("liveId",liveIdLst,"status",_status), MediaVideoLive.class);
   }