派生自 projectDept/qhighschool

yn147
2023-10-26 3970ced88b5b456f03fe277c254ca761f05492e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package com.qxueyou.scc.controller;
 
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hankcs.hanlp.corpus.tag.Nature;
import com.qxueyou.scc.base.dao.CommonDAO;
import com.qxueyou.scc.config.IpUtils;
import com.qxueyou.scc.sdk.MTCloud;
import com.qxueyou.scc.teach.live.model.AccessLog;
import com.qxueyou.scc.teach.live.utils.RedisCache;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
 
import com.qxueyou.scc.admin.classes.model.ClsClass;
import com.qxueyou.scc.admin.classes.service.IClassService;
import com.qxueyou.scc.admin.teacher.service.ITeacherService;
import com.qxueyou.scc.base.model.Result;
import com.qxueyou.scc.base.util.ClientUtils;
import com.qxueyou.scc.base.util.CollectionUtils;
import com.qxueyou.scc.base.util.QBeanUtils;
import com.qxueyou.scc.teach.live.dao.MediaLiveDAO;
import com.qxueyou.scc.teach.live.model.MediaVideoLive;
import com.qxueyou.scc.teach.live.model.MediaVideoLiveReplay;
import com.qxueyou.scc.teach.live.service.IMediaLiveService;
import com.qxueyou.scc.teach.live.service.IMediaVideoLivePlayBackService;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.spring.web.json.Json;
 
import javax.servlet.http.HttpServletRequest;
 
import static com.hankcs.hanlp.corpus.tag.Nature.r;
import static com.hankcs.hanlp.corpus.tag.Nature.s;
 
@Api(tags="直播管理接口")
@RestController
@Slf4j
@CrossOrigin(origins="*",maxAge=3600)
@RequestMapping(value = "/admin/videoLive")
public class VideoLiveController {
    
    // 分页查询中,默认记录条数和页数
    private static final int DEFAULT_PAGE_SIZE = 10;
    private static final int DEFAULT_PAGE_NUM = 1;
    
    @Autowired
    IMediaLiveService liveService;
    
    @Autowired
    IMediaVideoLivePlayBackService mediaVideoLivePlayBackService;
    
    @Autowired
    IClassService classService;
    
    @Autowired
    MediaLiveDAO dao;
 
    @Autowired
    CommonDAO commonDAO;
 
    @Autowired
    private RedisCache redisCache;
    /**
     * 获取直播列表
     * 
     * @param keyword
     *            搜索关键字
     * @param pageNum
     *            页码
     * @param pageSize
     *            页数
     * @return
     */
    @ApiOperation(value = "获取创建人的直播列表")
    @GetMapping(value = "list")
    public Result list(String keyword,String portalStatus, Integer pageNum, Integer pageSize) {
        
        List<MediaVideoLive> data = liveService.listOfMine(keyword,portalStatus, pageNum, pageSize);
        int count = liveService.listCountOfMine(keyword);
 
        return new Result(true, "success", CollectionUtils.newObjectMap("videoLiveCount", count, 
                "videoLiveLst",QBeanUtils.listBean2ListMap(data, CollectionUtils.newStringMap(
                        "creator","creator",
                        "startTime","startTime",
                        "endTime","endTime",
                        "updateTime","lastUpdateTime",
                        "videoLiveId","videoLiveId",
                        "name","name",
                        "subjectId","subjectId",
                        "subjectName","subjectName",
                        "status","status",
                        "pushUrl","rtmpPushUrl",
                        "hlsPullUrl","hlsPullUrl",
                        "previewImgUrl","previewImgUrl",
                        "courseId","courseId",
                        "portalStatus","portalStatus"
                        ))));
    }
 
    @ApiOperation(value = "获取已结束直播列表")
    @GetMapping(value = "listvideo")
    public Result listvideo() {
 
        List<MediaVideoLive> data = liveService.listvideo("9");
 
        return new Result(true, "success", CollectionUtils.newObjectMap("listVideo",data));
    }
    /**
     * 新增直播/更新直播
     * 
     * @param videoLiveId
     *            直播id(更新的时候才需要传)
     * @param name
     *            直播名称
     * @param content
     *            直播介绍
     * @param imgPath
     *            图片地址
     * @param startTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @param isPlayBack
     *            是否回放
     * @param classIds
     *            班级id
     * @return
     */
    @ApiOperation(value = "创建编辑直播")
    @PostMapping(value = "addOrUpdate")
    public Result addOrUpdate(String videoLiveId, String name, String content,String teacherId,String teacherName,String subjectId,
            String subjectName,String definition,String imgPath, long startTime,long endTime, boolean isPlayBack, String classIds,String portalStatus) throws Exception {
        MTCloud client = new MTCloud();
        HashMap<Object,Object> options = new HashMap<Object,Object>();
        options.put("barrage", 1);
        options.put("isPublic", 1);
        options.put("robotNumber", 1);
        options.put("robotType", 1);
        options.put("pptDisplay", 1);
        options.put("modetype", 6);
        //将long类型转换为String类型
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime1 = simpleDateFormat.format(new Date(startTime));
        String endTime1 = simpleDateFormat.format(new Date(endTime));
        System.out.println(startTime1+"------"+endTime1);
        //调用第三方直播接口
        String s = client.courseAdd(name, ClientUtils.getUserId(), startTime1, endTime1, ClientUtils.getUserName(), "222", options);
        //json转换拿到courseId
        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject data = jsonObject.getJSONObject("data");
        if(jsonObject.getString("code").equals("0")) {
            String course_id = (String) data.get("course_id");
            liveService.add(name, content,teacherId,teacherName,subjectId,subjectName,definition,imgPath, new Date(startTime), new Date(endTime),Integer.parseInt(course_id), isPlayBack, StringUtils.isEmpty(classIds)?null:CollectionUtils.newList(String.class,classIds.split(",")),portalStatus);
            return new Result(true, "成功", s);
        }
        return new Result(false,jsonObject.getString("msg"));
    }
 
    @ApiOperation(value = "开启直播")
    @GetMapping(value = "OpenLive")
    public Result OpenLive(Integer courseId, String logId, HttpServletRequest request) throws Exception {
        MTCloud client = new MTCloud();
        String redisUrl = redisCache.getCacheObject("ZCR" + courseId);
        AccessLog accessLog = new AccessLog();
 
        if (StringUtils.isEmpty(logId)) {
            String ip = IpUtils.getIpAddr(request);
            String cityInfo = null;
            try {
                cityInfo = IpUtils.getCityInfo(ip);
            } catch (Exception e) {
                log.error("获取ip归属地信息失败!");
            }
            accessLog.setCourseId(courseId.toString());
            accessLog.setEnterTime(new Date());
            accessLog.setType("videoLive");
            accessLog.setName(ClientUtils.getUserName());
            accessLog.setIp(ip);
            accessLog.setIpAttribution(cityInfo);
            accessLog.setCreateTime(new Date());
            accessLog.setUpdateTime(new Date());
            //访问留痕
            commonDAO.save(accessLog);
        }
        if (StringUtils.isEmpty(redisUrl)) {
            HashMap<Object, Object> options = new HashMap<Object, Object>();
            options.put("ssl", false);
            String res = client.courseLogin(ClientUtils.getUserId(), MTCloud.ACCOUNT_TYPE_THIRD, options);
            JSONObject jsonObject = JSON.parseObject(res);
            if (jsonObject.getString("code").equals("0")) {
                JSONObject data = jsonObject.getJSONObject("data");
                String protocol = (String) data.get("url");
                redisCache.setCacheObject("ZCR" + courseId, protocol, 1, TimeUnit.DAYS);
                return new Result(true,"suc", CollectionUtils.newObjectMap("url", protocol, "log", accessLog.getLogId()));
            }
        }
        return new Result(true,"suc",CollectionUtils.newObjectMap("url", redisUrl, "log", accessLog.getLogId()));
    }
 
    @ApiOperation(value = "修改直播状态")
    @GetMapping(value = "updateLive")
    public Result updateLive(Integer courseId)  {
 
        MediaVideoLive revamp = liveService.revamp(courseId);
        revamp.setStatus(MediaVideoLive.STATUS_LIVE_STOP);
        commonDAO.update(revamp);
 
        return new Result(true,"关闭成功");
    }
 
    @ApiOperation(value = "在线时长")
    @GetMapping(value = "onlineCommunication")
    public Result onlineCommunication(String courseId,Integer page, Integer size) throws Exception {
        MTCloud client = new MTCloud();
        HashMap<Object,Object> options = new HashMap<Object, Object>();
        options.put("ssl", false);
        String res = client.courseVisitorList(courseId,page,size,options);
        return new Result(true,"开启成功",res);
    }
 
//    @ApiOperation(value = "查看详情")
//    @GetMapping(value = "detailLive")
//    public Result detailLive(String liveId) {
//        commonDAO.find
//        return new Result(true,"开启成功",res);
//    }
    /**
     * 查询直播详情
     * 
     * @param videoLiveId
     *            直播id
     * @return
     */
    @ApiOperation(value = "获取直播详情")
    @GetMapping(value = "queryDetail")
    public Result detail(String videoLiveId) {
        List<String> lstClassIds = null;
        List<Map<String,Object>> lstClassIdNames = null;
        
        MediaVideoLive live = liveService.read(videoLiveId);
        List<ClsClass> lstClassses = live.getClasses();
        
        if(lstClassses!=null){
            lstClassIds = new ArrayList<String>(lstClassses.size());
            lstClassIdNames = new  ArrayList<Map<String,Object>>(lstClassses.size());
            
            for(ClsClass cls: lstClassses){
                lstClassIds.add(cls.getClassId());
                lstClassIdNames.add(CollectionUtils.newObjectMap("classId",cls.getClassId(),"className",cls.getName()));
            }
        }
        
        return new Result(true, "success",
                CollectionUtils.newObjectMap(
                        "videoLiveId",live.getVideoLiveId(),
                        "name", live.getName(), 
                        "content", live.getRemark(), 
                        "imgPath", live.getPreviewImgUrl(), 
                        "isplayBack",1, 
                        "startTime", live.getStartTime(), 
                        "endTime", live.getEndTime(),
                        "rtmpPushUrl", live.getPushUrl(),
                        "hlsPullUrl", live.getHlsPullUrl(),
                        "chatroomId", dao.getChatRoomId(videoLiveId),
                        "watchTimes",live.getWatchTimes(),
                        "praiseTimes",live.getPraiseTimes(),
                        "definition",live.getDefinition(),
                        "subjectId",live.getSubjectId(),
                        "subjectName",live.getSubjectName(),
                        "status",live.getStatus(),
                        "teacherId",live.getAnchorId(),
                        "teacherName",live.getAnchor(),
                        "classIds",lstClassIds ,
                        "classes", lstClassIdNames));
    }
 
    /**
     * 发布/多个以逗号隔开
     * 
     * @param videoLiveIds
     *            直播ids
     * @return
     */
    @ApiOperation(value = "发布直播")
    @PostMapping(value = "release")
    public Result release(String videoLiveIds) {
        for(String liveId:videoLiveIds.split(",")) {
            liveService.doIssue(liveId);
        }
        return Result.SUCCESS;
    }
 
    /**
     * 取消发布/多个以逗号隔开
     * 
     * @param videoLiveIds
     *            直播ids
     * @return
     */
    @ApiOperation(value = "取消发布直播")
    @PostMapping(value = "cancel")
    public Result cance(String videoLiveIds) {
        
        for(String liveId:videoLiveIds.split(",")) {
            liveService.doCancel(liveId);
        }
        return Result.SUCCESS;
    }
 
    /**
     * 删除/多个以逗号隔开
     * 
     * @param videoLiveIds
     * @return
     */
    @ApiOperation(value = "删除创建的直播")
    @PostMapping(value = "delete")
    public Result delete(String videoLiveIds,String    courseIds) throws Exception {
        return liveService.delete(videoLiveIds.split(","),courseIds.split(","));
    }
    
    
    /**
     * 直播开始
     * 
     * @param videoLiveIds
     *            直播id
     */
    @ApiOperation(value = "直播开始")
    @PostMapping(value = "start")
    public Result start(String videoLiveIds) {
 
        for(String liveId:videoLiveIds.split(",")) {
            liveService.doStart(liveId);
        }
        return Result.SUCCESS;
    }
    
    
    /**
     * 直播暂停
     * 
     * @param videoLiveIds
     *            直播id
     */
    @ApiOperation(value = "直播暂停")
    @PostMapping(value = "pause")
    public Result pause(String videoLiveIds) {
        
        for(String liveId:videoLiveIds.split(",")) {
            liveService.doPause(liveId);
        }
        return Result.SUCCESS;
    }
 
    /**
     * 直播结束
     * 
     * @param videoLiveIds
     *            直播id
     */
    @ApiOperation(value = "直播结束")
    @PostMapping(value = "stop")
    public Result stop(String videoLiveIds) {
 
        for(String liveId:videoLiveIds.split(",")) {
            liveService.doStop(liveId);
        }
        return Result.SUCCESS;
    }
    
    
    
    /**
     * 直播点赞
     * 
     * @param videoLiveId
     *            直播id
     */
    @ApiOperation(value = "直播点赞")
    @PostMapping(value = "praise")
    public Result praise(String videoLiveId) {
        return liveService.doPraise(videoLiveId);
    }
    
    @ApiOperation(value = "学生端根据班级,课程过滤直播列表")
    @GetMapping(value = "student/list")
    public Result list(String keyword,String classIds,String subjectId,Short status,Integer pageNum, Integer pageSize) {
        pageSize = pageSize != null && pageSize > 0 ? pageSize : DEFAULT_PAGE_SIZE;
        pageNum = pageNum != null && pageNum > 0 ? pageNum : DEFAULT_PAGE_NUM;
        
        String [] arrClassIds = null;
        if(StringUtils.isNotEmpty(classIds) || StringUtils.isNotBlank(ClientUtils.getClassId())){
            arrClassIds = StringUtils.isEmpty(classIds)?new String[]{ClientUtils.getClassId()}:classIds.split(",");
        }
        
        int count = liveService.listCount(keyword==null?"":keyword.trim() , arrClassIds , subjectId , status);
        List<MediaVideoLive> data = liveService.list(keyword==null?"":keyword.trim(),StringUtils.isEmpty(classIds)?new String[]{ClientUtils.getClassId()}:classIds.split(","),subjectId,status, pageNum, pageSize);
        
        return new Result(true, "success", CollectionUtils.newObjectMap("videoLiveCount", count, 
                "videoLiveLst",QBeanUtils.listBean2ListMap(data, CollectionUtils.newStringMap(
                        "creator","creator",
                        "startTime","startTime",
                        "endTime","endTime",
                        "updateTime","lastUpdateTime",
                        "videoLiveId","videoLiveId",
                        "name","name",
                        "subjectId","subjectId",
                        "subjectName","subjectName",
                        "status","status",
                        "pushUrl","rtmpPushUrl",
                        "hlsPullUrl","hlsPullUrl",
                        "previewImgUrl","previewImgUrl",
                        "courseId","courseId"
                ))));
    }
    
    @ApiOperation(value = "直播回放列表")
    @GetMapping(value = "replay/list")
    public Result listReplay(String videoLiveId) {
        
        List<MediaVideoLiveReplay> data = mediaVideoLivePlayBackService.queryVideoLiveReplay(videoLiveId);
        
        return new Result(true, "success", CollectionUtils.newObjectMap(
                "replayList",QBeanUtils.listBean2ListMap(data, CollectionUtils.newStringMap(
                        "name","replayName",
                        "creator","creator",
                        "beginTime","beginTime",
                        "endTime","endTime",
                        "updateTime","updateTime",
                        "duration","duration",
                        "initialSize","initialSize",
                        "url","url",
                        "videoLiveId","videoLiveId",
                        "status","status",
                        "liveReplayId","liveReplayId"
        ))));
    }
    
    @ApiOperation(value = "测试直播回放")
    @GetMapping(value = "replay/test")
    public Result testReplay(String liveId) {
        mediaVideoLivePlayBackService.testPlayBack(liveId);
        return new Result(true);
    }
 
}