package com.qxueyou.scc.teach.live.service.impl;
|
|
import java.util.Calendar;
|
import java.util.Collections;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import com.qxueyou.scc.sdk.MTCloud;
|
import io.swagger.models.auth.In;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Service;
|
|
import com.qxueyou.scc.admin.teacher.service.ITeacherService;
|
import com.qxueyou.scc.base.model.Pager;
|
import com.qxueyou.scc.base.model.Result;
|
import com.qxueyou.scc.base.service.ICacheService;
|
import com.qxueyou.scc.base.service.impl.CommonAppService;
|
import com.qxueyou.scc.base.util.ClientUtils;
|
import com.qxueyou.scc.base.util.CollectionUtils;
|
import com.qxueyou.scc.base.util.TraceUtils;
|
import com.qxueyou.scc.base.util.UUIDUtils;
|
import com.qxueyou.scc.config.SccConfig;
|
import com.qxueyou.scc.msg.model.MsgInfo;
|
import com.qxueyou.scc.msg.service.IMsgInfoService;
|
import com.qxueyou.scc.teach.live.dao.MediaLiveDAO;
|
import com.qxueyou.scc.teach.live.model.MediaVideoLive;
|
import com.qxueyou.scc.teach.live.model.MediaVideoLiveReClass;
|
import com.qxueyou.scc.teach.live.service.IMediaLiveService;
|
import com.qxueyou.scc.teach.subject.model.Subject;
|
import com.qxueyou.scc.user.model.UserTeacher;
|
|
@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) {
|
|
UserTeacher teacher = teacherService.getTeacherByUserId(ClientUtils.getUserId());
|
|
MediaVideoLive live = new MediaVideoLive();
|
TraceUtils.setCreateTrace(live);
|
|
live.setCourseId(courseId);
|
live.setName(name);
|
live.setRemark(content);
|
live.setPreviewImgUrl(imgPath);
|
live.setStartTime(startTime);
|
live.setEndTime(endTime);
|
live.setStatus(MediaVideoLive.STATUS_LIVE_DRAFT);
|
|
if(teacher!=null){
|
live.setAnchorId(teacher.getTeacherId());
|
live.setAnchor(teacher.getName());
|
}else{
|
live.setAnchorId(ClientUtils.getUserId());
|
live.setAnchor(ClientUtils.getUserName());
|
}
|
|
live.setSubjectId(subjectId);
|
live.setSubjectName(subjectName);
|
|
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) {
|
MediaVideoLive live = read(MediaVideoLive.class,liveId);
|
TraceUtils.setUpdateTrace(live);
|
|
live.setName(name);
|
live.setRemark(content);
|
live.setPreviewImgUrl(imgPath);
|
live.setStartTime(startTime);
|
live.setEndTime(endTime);
|
live.setSubjectId(subjectId);
|
live.setSubjectName(subjectName);
|
|
save(live);
|
|
//更新班级课程关联关系
|
this.doClearAndAddLiveReClass(live.getVideoLiveId(), classIds!=null?classIds.toArray(new String[classIds.size()]):null);
|
|
|
// liveDao.saveLiveClasses(liveId, classIds);
|
|
return Result.SUCCESS;
|
}
|
|
/**
|
* 清理并添加直播权限班级
|
* @param liveId
|
* @param classIds
|
*/
|
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);
|
|
//删除关联关系
|
if(reIdLst!=null && reIdLst.size()>0){
|
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);
|
}
|
}
|
}
|
|
@Override
|
public List<String> getLiveClasses(String liveId) {
|
return this.find("select r.classId from MediaVideoLiveReClass r where r.liveId=? and r.deleteFlag is false",
|
CollectionUtils.newList(liveId), String.class);
|
}
|
|
@Override
|
public Result delete(String liveId) {
|
MediaVideoLive live = read(MediaVideoLive.class,liveId);
|
TraceUtils.setUpdateTrace(live);
|
live.setDeleteFlag(true);
|
save(live);
|
return Result.SUCCESS;
|
}
|
|
@Override
|
public Result delete(String[] liveIds,String [] courseIds) throws Exception {
|
for(String liveId:liveIds) {
|
delete(liveId);
|
for(String courseId:courseIds) {
|
MTCloud client = new MTCloud();
|
String s = client.courseDelete(courseId);
|
}
|
}
|
return Result.SUCCESS;
|
}
|
|
|
@Override
|
public Result doIssue(String liveId) {
|
status(liveId,MediaVideoLive.STATUS_LIVE_ORDER);
|
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, "直播马上开始了,请做好直播准备");
|
}
|
}
|
}
|
|
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()));
|
|
msgInfoService.doSendTextMsgToUsers(new String[]{liveInfo.getCreateId()},type,msg,attrs);
|
}
|
|
@Override
|
public Result doCancel(String liveId) {
|
return status(liveId,MediaVideoLive.STATUS_LIVE_DRAFT);
|
}
|
|
@Override
|
public Result doStart(String liveId) {
|
return status(liveId,MediaVideoLive.STATUS_LIVE_LIVE);
|
}
|
|
@Override
|
public Result doPause(String liveId) {
|
return status(liveId,MediaVideoLive.STATUS_LIVE_PAUSE);
|
}
|
|
@Override
|
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);
|
live.setStatus(status);
|
save(live);
|
return Result.SUCCESS;
|
}
|
|
@Override
|
public Result doPraise(String liveId) {
|
return this.bulkUpdate("update MediaVideoLive set praiseTimes = praiseTimes+1 where videoLiveId=?",new Object[]{liveId});
|
}
|
|
@Override
|
public List<MediaVideoLive> list(String keyword, String [] classIds,String subjectId,Short status,Integer pageNum, Integer pageSize) {
|
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);
|
params.put("Type", Subject.TYPE_PUBLIC_SUBJECT);
|
}else{
|
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);
|
}
|
|
if(status!=null){
|
hql.append(" and m.status=:status");
|
params.put("status", status);
|
}else{
|
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);
|
}
|
|
if(status!=null){
|
hql.append(" and m.status=:status");
|
params.put("status", status);
|
}else{
|
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);
|
}else{
|
hql.append(" and s.type=:subjectType ");
|
params.put("subjectType", Subject.TYPE_PUBLIC_SUBJECT);
|
}
|
|
return this.findCountByComplexHql(hql.toString(), params);
|
}
|
|
@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);
|
}
|
|
@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()));
|
}
|
|
@Override
|
public MediaVideoLive read(String liveId) {
|
return read(MediaVideoLive.class,liveId);
|
}
|
|
@Override
|
public MediaVideoLive revamp(int courseId) {
|
String hql = "from MediaVideoLive where deleteFlag is false and courseId=?";
|
MediaVideoLive unique = findUnique(hql, CollectionUtils.newList(courseId), MediaVideoLive.class);
|
return unique ;
|
}
|
|
@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);
|
}
|
|
}
|