派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
package com.qxueyou.scc.school.service.impl;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
 
import com.qxueyou.scc.admin.classes.model.ClsClass;
import com.qxueyou.scc.base.dao.CommonDAO;
import com.qxueyou.scc.base.model.Pager;
import com.qxueyou.scc.base.model.Result;
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.DateTimeUtils;
import com.qxueyou.scc.base.util.TraceUtils;
import com.qxueyou.scc.media.model.MediaVideo;
import com.qxueyou.scc.media.model.MediaVideoReCourse;
import com.qxueyou.scc.media.service.IMediaVideoService;
import com.qxueyou.scc.org.model.OrgText;
import com.qxueyou.scc.org.service.IOrgTextService;
import com.qxueyou.scc.school.dao.LessonDAO;
import com.qxueyou.scc.school.model.SchClassSchedule;
import com.qxueyou.scc.school.model.SchClassSubject;
import com.qxueyou.scc.school.model.SchReLessonVideo;
import com.qxueyou.scc.school.service.ICourseWareService;
import com.qxueyou.scc.school.service.ILessonService;
import com.qxueyou.scc.teach.subject.model.SubjectChapter;
 
/**
 * 课程服务
 * 
 * @author 德虎
 *
 */
@Service
public class LessonService extends CommonAppService implements ILessonService {
 
    @Autowired
    CommonDAO dao;
    
    
    
    @Autowired
    IMediaVideoService mediaVideoService;
 
    
    
    @Autowired
    IOrgTextService orgTextService;
 
    
    private LessonDAO lessonDAO;
    
    @Autowired
    private ICourseWareService courseWareService;
 
    private static Logger log = LogManager.getLogger("LessonService");
    
    private static final String OPERATE_TYPE_ISSUE = "Issue";
    
    private static final String OPERATE_TYPE_STOP = "Stop";
    
    /**
     * 依赖注入
     *
     * @param lessonDAO
     */
    @Autowired(required = false)
    public void setLessonDAO(@Qualifier("lessonDAO") LessonDAO lessonDAO) {
        this.lessonDAO = lessonDAO;
    }
    
    public LessonDAO getLessonDAO() {
        return lessonDAO;
    }
 
    /**
     * 定时发布、停用
     */
    @Override
    public Result  doVideoIssueStop() {
 
        doVideoIssue(); // 发布未发布的视频
        //doVideoStop(); // 停用已过期的视频
 
        return new Result(true);
    }
    
    /**
     * 定时发布
     */
    @SuppressWarnings("unused")
    public Result  doVideoIssue() {
        
        Date now = new Date();
        
        // 查询未发布的视频 ---- 只有草稿状态的视频才能发布
        String hql = "from MediaVideo where deleteFlag is false and status = ? and startTime <= ? and endTime > ? ";
        List<MediaVideo> mediaLst =  this.find(hql, CollectionUtils.newList(MediaVideo.STATUS_DRAFT,now,now), MediaVideo.class);
        
//        for (MediaVideo mediaVideo : mediaLst) {
//            // 发送消息  发布视频
//            sendMsgVideoIssueStop(OPERATE_TYPE_ISSUE, mediaVideo.getVideoId());
//        }
//        
        return new Result(true);
    }
    
    @SuppressWarnings("unused")
    public Result  doVideoStop() {
 
        Date now = new Date();
        
        // 查询已过期的视频 ---- 只有已发布的视频才能停用
        String hql = "from MediaVideo where deleteFlag is false and status = ? and endTime <= ?";
        List<MediaVideo> mediaLst =  this.find(hql, CollectionUtils.newList(MediaVideo.STATUS_PUBLISHED,now), MediaVideo.class);
        
//        for (MediaVideo mediaVideo : mediaLst) {
//            // 发送消息  停用视频
//            sendMsgVideoIssueStop(OPERATE_TYPE_STOP, mediaVideo.getVideoId());
//        }
        return new Result(true);
    }
    
    
    
    /**
     * 单个处理视频发布与上传
     */
    @SuppressWarnings("unused")
    @Override
    public Result  doSingleVideoIssueStop(String videoId,String operateType) {
 
        MediaVideo video = read(MediaVideo.class, videoId);
        if(OPERATE_TYPE_ISSUE.equals(operateType)){ // 发布
            log.info("发布");
            bulkUpdateInLoop("update MediaVideo set status = " + MediaVideo.STATUS_PUBLISHED + " where videoId = ? " ,new Object[] { videoId });
            bulkUpdateInLoop("update SchCourseware set status = " + MediaVideo.STATUS_PUBLISHED + " where id = ? " ,new Object[] { videoId });
            
//            ONSMsg msg = new ONSMsg(onsProducer.getTopic());
//            msg.put("msgType", "VIDEO_PUBLISH"); 
//            msg.put("videoIds", video.getVideoId()); 
//            msg.put("classId", video.getClassId());
//            try {
//                onsProducer.sendMsg(msg);
//            } catch (Exception e) {
//                log.error("Lesson.sendMsg.videoId: " + video.getVideoId(), e);
//            }
            
        }else if(OPERATE_TYPE_STOP.equals(operateType)){ // 停用
            log.info("停用");
            bulkUpdateInLoop("update MediaVideo set status = " + MediaVideo.STATUS_TAKEOFF + " where videoId = ? " ,new Object[] { videoId });
        }
        
        return new Result(true);
    }
    
    /**
     * 添加视频
     */
    @Override
    public Result insertVideo(MediaVideo video, String lessonId, boolean isCopy) {
 
        this.insertVideoSingle(video, lessonId, isCopy);
 
        // 将数据插入转码队列
        mediaVideoService.insertVideoConverTask(video);
        
        // 调用本地转码服务
        //mediaVideoService.doFfmpegMediaTrascode(video.getVideoId(), video.getName());
 
        return new Result(true, video.getVideoId());
    }
    
    /**
     * 添加视频,不调用转码
     */
    @Override
    public Result insertVideoSingle(MediaVideo video, String lessonId, boolean isCopy) {
        video.setVideoId(null);
        TraceUtils.setCreateTrace(video);
        video.setClassId(ClientUtils.getClassId());
        video.setDeleteFlag(false);
        video.setSubmitor(ClientUtils.getUserName());
        video.setSubmitorId(ClientUtils.getUserId());
        video.setPlayTimes(0);
        if(isCopy){
            video.setStatus(video.getStatus() == MediaVideo.STATUS_PUBLISHED ?MediaVideo.STATUS_DRAFT: video.getStatus());
        }else{
            //jgw 暂时直接修改为
//            video.setStatus(MediaVideo.STATUS_LINEUP);
            video.setStatus(MediaVideo.STATUS_PUBLISHED);
        }
        
        // 序号
        String hql = "select MAX(c.videoOrder) from MediaVideo c where c.deleteFlag is false and c.classId = ? ";
        Integer iMax = this.findUnique(hql, CollectionUtils.newList(ClientUtils.getClassId()), Integer.class);
 
        if (iMax == null || iMax == 0) {
            iMax = 1;
        } else {
            iMax = iMax + 1;
        }
        video.setVideoOrder(iMax);
        
        this.mediaVideoService.saveVideo(video);
        
        // 保存OrgText —— 视频详情
        orgTextService.doSaveOrgText(video.getVideoId(), OrgText.TABLE_NAME_VIDEO, video.getRemark());
        
        if(StringUtils.isNotBlank(lessonId)){
            SchReLessonVideo re = new SchReLessonVideo();
            TraceUtils.setCreateTrace(re);
            re.setDeleteFlag(false);
            re.setClassScheduleId(lessonId);
            re.setVideoId(video.getVideoId());
            this.save(re);
        }
        
        //标记班级有视频
        ClsClass cls = read(ClsClass.class, ClientUtils.getClassId());
        if(cls!=null&&cls.getHaveVideo()==ClsClass.NOT_HAVE_VIDEO){
            cls.setHaveVideo(ClsClass.HAVE_VIDEO);
            save(cls);
        }
 
        return new Result(true, video.getVideoId());
    }
 
    /**
     * 批量发布视频
     */
    @Override
    public Result doStartVideos(String videoIds) {
 
        if(StringUtils.isEmpty(videoIds)){
            return new Result(false, "参数错误");
        }
        String[] ids = videoIds.split(",");
        boolean resultFlag = false;
        for (String videoId : ids) {
            Result result = doStartVideo(videoId);
            if(result.isSuccess()){
                resultFlag = true;
            }
        }
        
//        // 发送消息
//        ONSMsg msg = new ONSMsg(onsProducer.getTopic());
//        msg.put("msgType", "VIDEO_PUBLISH"); 
//        msg.put("videoIds", videoIds); 
//        msg.put("classId", ClientUtils.getClassId());
//        try {
//            onsProducer.sendMsg(msg);
//        } catch (Exception e) {
//            log.error("Lesson.sendMsg.videoId: " + videoIds, e);
//        }
        
        return new Result(resultFlag);
    }
    
    /**
     * 发布视频
     */
    public Result doStartVideo(String videoId) {
        
        MediaVideo video = read(MediaVideo.class, videoId);
        
        if( video.getStatus() == MediaVideo.STATUS_DRAFT || video.getStatus() == MediaVideo.STATUS_TAKEOFF || video.getStatus() == MediaVideo.STATUS_PUBLISHED){ // 草稿、已下架
            
            TraceUtils.setUpdateTrace(video);
            video.setStatus(MediaVideo.STATUS_PUBLISHED);
            
            return mediaVideoService.saveVideo(video);
        
        }else{
            
            return new Result(false);
        }
        
    }
    
    /**
     * 批量停用视频
     */
    @Override
    public Result doStopVideos(String videoIds) {
        if(StringUtils.isEmpty(videoIds)){
            return new Result(false, "参数错误");
        }
        boolean resultFlag = false;
 
        String[] ids = videoIds.split(",");
        for (String videoId : ids) {
            
            Result result = doStopVideo(videoId);
            if(result.isSuccess()){
                resultFlag = true;
            }
        }
 
        return new Result(resultFlag);
    }
    
    /**
     * 停用视频
     */
    public Result doStopVideo(String videoId) {
        
        MediaVideo video = read(MediaVideo.class, videoId);
        
        if(video.getStatus() == MediaVideo.STATUS_PUBLISHED){ // 已发布
            
            TraceUtils.setUpdateTrace(video);
            video.setStatus(MediaVideo.STATUS_TAKEOFF);
 
            mediaVideoService.saveVideo(video);
            
            return new Result(true);
        }else{
            
            return new Result(false,"视频已发布状态才能停用视频");
        }
    }
    
    /**
     * 添加视频
     */
    @Override
    public Result insertOrgVideo(MediaVideo video, String collegeCourseId, boolean isCopy) {
        String hql = " from MediaVideoReCourse where  collegeCourseId = ? and deleteFlag is false";
        int count = findCount(hql, CollectionUtils.newList(collegeCourseId));
        TraceUtils.setCreateTrace(video);
        video.setDeleteFlag(false);
        video.setSubmitor(ClientUtils.getUserName());
        video.setSubmitorId(ClientUtils.getUserId());
        
        
        video.setPlayTimes(0);
        if(isCopy){
            video.setStatus(video.getStatus() == MediaVideo.STATUS_PUBLISHED ?MediaVideo.STATUS_LINEUP: video.getStatus());
        }else{
            video.setStatus(MediaVideo.STATUS_LINEUP);
        }
        
        
        mediaVideoService.saveVideo(video);
        video.setOriginVideoId(video.getVideoId());
        mediaVideoService.saveVideo(video);
        
        // 保存OrgText —— 视频详情
        orgTextService.doSaveOrgText(video.getVideoId(), OrgText.TABLE_NAME_VIDEO, video.getRemark());
 
        // 序号
        hql = "select MAX(c.videoOrder) from MediaVideoReCourse c where c.deleteFlag is false and c.collegeCourseId = ?  ";
        Integer iMax = this.findUnique(hql, CollectionUtils.newList(collegeCourseId), Integer.class);
 
        if (iMax == null || iMax == 0) {
            iMax = 1;
        } else {
            iMax = iMax + 1;
        }
 
        MediaVideoReCourse course = new MediaVideoReCourse();
        course.setCollegeCourseId(collegeCourseId);
        course.setDeleteFlag(false);
        course.setVideoId(video.getVideoId());
        course.setOrgId(ClientUtils.getOrgId());
        course.setVideoOrder(iMax);
        TraceUtils.setCreateTrace(course);
        save(course);
 
        //mediaVideoService.doFfmpegMediaTrascode(video.getVideoId(), video.getName());
        // 将数据插入转码队列
        mediaVideoService.insertVideoConverTask(video);
        
        // TODO 不自动指定给自己
        // insertAppointSelfOrg(video.getVideoId(), ClientUtils.getOrgId(),collegeCourseId);
        Result result = new Result(true,video.getVideoId());
        result.addData("count", count+1);
        return result;
    }
 
    public Result insertAppointOrgVideo(String videoIds[], String orgIds[], String classIds[]) {
        if(videoIds.length == 0){
            return new Result(false,"参数错误");
        }
        //预先按videoId和OrgId查询mediavideoreCourse表,需要把当前机构ID也放进lstOrgIds一起查
        List<String> lstOrgIds0 = Arrays.asList(orgIds);
        List<String> lstOrgIds = new ArrayList<String>();
        lstOrgIds.addAll(lstOrgIds0);
        lstOrgIds.add(ClientUtils.getOrgId());
        Map<String,MediaVideoReCourse> videoOrgCourseMap = new HashMap<String, MediaVideoReCourse>();
        Map<String,Object> args = new HashMap<String, Object>();
        String hql = " from MediaVideoReCourse where  videoId in (:videoIds) and deleteFlag is false and orgId in (:orgIds)";
        args.put("videoIds", videoIds);
        args.put("orgIds", lstOrgIds.toArray());
        List<MediaVideoReCourse> lstMediaCourse = findByComplexHql(hql, args, MediaVideoReCourse.class);
        for (MediaVideoReCourse mediaVideoReCourse : lstMediaCourse) {
            videoOrgCourseMap.put(mediaVideoReCourse.getVideoId()+mediaVideoReCourse.getOrgId(), mediaVideoReCourse);
        }
        if(orgIds.length!=0){ // 添加管理员视频
            insertOrgVideo(videoIds,orgIds,videoOrgCourseMap);
        }
        if(classIds.length!=0){ // 添加班级视频
            insertClassVideo(videoIds,classIds,ClientUtils.getOrgId(),videoOrgCourseMap);
        }
        
        return new Result(true);
    }
    
    private void insertOrgVideo(String videoIds[], String orgIds[],Map<String,MediaVideoReCourse> videoOrgCourseMap){
        Map<String,Object> args = new HashMap<String, Object>();
        Map<String,MediaVideoReCourse> videoCourseMap = new HashMap<String, MediaVideoReCourse>();
        String hql = " from MediaVideoReCourse where  videoId in (:videoIds) and deleteFlag is false";
        args.put("videoIds", videoIds);
        List<MediaVideoReCourse> lstCourse = findByComplexHql(hql, args, MediaVideoReCourse.class);
        for (MediaVideoReCourse mediaVideoReCourse : lstCourse) {
            videoCourseMap.put(mediaVideoReCourse.getVideoId(), mediaVideoReCourse);
        }
        
        for (String orgId : orgIds) {
            for (String videoId : videoIds) {
                
                //String hql = " from MediaVideoReCourse where  videoId = ? and deleteFlag is false and orgId = ?";
                
                MediaVideoReCourse mvrc = videoOrgCourseMap.get(videoId+orgId);
                if (mvrc == null) {
                    //String hql = " from MediaVideoReCourse where  videoId = ? and deleteFlag is false";
                    MediaVideoReCourse mvr = videoCourseMap.get(videoId);
                    mvrc = new MediaVideoReCourse();
                    mvrc.setCollegeCourseId(mvr.getCollegeCourseId());
                    mvrc.setDeleteFlag(false);
                    mvrc.setVideoId(videoId);
                    mvrc.setOrgId(orgId);
                    mvrc.setVideoOrder(mvr.getVideoOrder());
                    TraceUtils.setCreateTrace(mvrc);
                    save(mvrc);
                    
                    this.courseWareService.insertOrgCourseware(videoId, ClientUtils.getOrgId(), orgId, mvr.getCollegeCourseId());
                }
            }
        }
    }
    private void insertClassVideo(String videoIds[],String classIds[],String currOrgId,Map<String,MediaVideoReCourse> videoOrgCourseMap){
        Map<String,Object> args = new HashMap<String, Object>();
        List<String> lstOriginVideoIds = new ArrayList<String>();
        List<String> lstSubjectIds = new ArrayList<String>();
        Map<String,MediaVideo> videoMap = new HashMap<String, MediaVideo>();
        Map<String,MediaVideo> originVideoMap = new HashMap<String, MediaVideo>();
        Map<String,SchClassSubject> origSubjectMap = new HashMap<String, SchClassSubject>();
        //章节id
        List<String> lstCharpteIds = new ArrayList<String>();
        Map<String,SubjectChapter> origChapterMap = new HashMap<String, SubjectChapter>();
        List<String> keys = new ArrayList<String>();
        String hql = " from MediaVideo where videoId in (:videoIds) and deleteFlag is false";
        args.put("videoIds", videoIds);
        List<MediaVideo> lstVideo = findByComplexHql(hql, args, MediaVideo.class);
        for (MediaVideo mediaVideo : lstVideo) {
            videoMap.put(mediaVideo.getVideoId(), mediaVideo);
            lstOriginVideoIds.add(mediaVideo.getOriginVideoId());
            lstSubjectIds.add(mediaVideo.getSubjectId());
            if(StringUtils.isNotEmpty(mediaVideo.getChapterId())){
                lstCharpteIds.add(mediaVideo.getChapterId());
            }
            keys.add(mediaVideo.getVideoId());
        }
        //查询所有originVideoId对应的mediaVideo
        hql = " from MediaVideo where originVideoId in (:originVideoIds) and deleteFlag is false and classId in (:classIds)";
        args = new HashMap<String, Object>();
        args.put("originVideoIds", lstOriginVideoIds.toArray());
        args.put("classIds",classIds);
        List<MediaVideo> lstOriginVideo = findByComplexHql(hql, args, MediaVideo.class);
        for (MediaVideo mediaVideo : lstOriginVideo) {
            originVideoMap.put(mediaVideo.getOriginVideoId() + mediaVideo.getClassId(), mediaVideo);
        }
        
        hql = " from SchClassSubject where classId in (:classIds) and origSubjectId in (:origSubjectIds)";
        args = new HashMap<String, Object>();
        args.put("classIds", classIds);
        args.put("origSubjectIds", lstSubjectIds.toArray());
        List<SchClassSubject> subjects = findByComplexHql(hql, args, SchClassSubject.class);
        for (SchClassSubject schClassSubject : subjects) {
            origSubjectMap.put(schClassSubject.getClassId()+schClassSubject.getOrigSubjectId(), schClassSubject);
        }
        
        Map<String, Object> orgTextMap = orgTextService.getOrgTextMap(keys, OrgText.TABLE_NAME_VIDEO);
        
        for (String clsId : classIds) {
            for (String videoId : videoIds) {
                MediaVideo video = videoMap.get(videoId);
                MediaVideo mv = originVideoMap.get(video.getOriginVideoId()+clsId); // 判断当前班级有没有这个视频
                if (mv == null) {
                    SchClassSubject clsSubject = origSubjectMap.get(clsId+video.getSubjectId());
                    mv = new MediaVideo();
                    MediaVideoReCourse videoCourse = videoOrgCourseMap.get(videoId+currOrgId);
                    try {
                        BeanUtils.copyProperties(mv, video);
                        mv.setVideoId(null);
                        mv.setClassId(clsId);
                        mv.setVideoOrder(videoCourse.getVideoOrder());
                        if(clsSubject == null){
                            mv.setSubjectId(null);
                            mv.setSubjectName(null);
                            mv.setCollegeCourseId(this.read(ClsClass.class, clsId).getCollegeCourseId());
                        }else{
                            mv.setSubjectId(clsSubject.getClassSubjectId());
                            mv.setSubjectName(clsSubject.getName());
                            mv.setCollegeCourseId(clsSubject.getCourseId());
                        }
                        if(null != origChapterMap.get(video.getOriginVideoId()+clsId)){
                            mv.setChapterId(origChapterMap.get(video.getOriginVideoId()+clsId).getChapterId());
                        }else{
                            mv.setChapterId(null);
                        }
                    } catch (Exception e) {
                        log.error("视频下发到班级BeanUtils.copyProperties()方法copy失败", e);
                    }
                    TraceUtils.setCreateTrace(mv);
                    this.mediaVideoService.saveVideo(mv);
 
                    OrgText oldOrgText = (OrgText) orgTextMap.get(video.getVideoId()); // 插入OrgText
                    orgTextService.doInsertOrgText(mv.getVideoId(), OrgText.TABLE_NAME_VIDEO, oldOrgText != null ? oldOrgText.getContent() : video.getRemark());
                }
            }
            //标记班级有视频
            ClsClass cls = read(ClsClass.class, clsId);
            if(cls!=null&&cls.getHaveVideo()==ClsClass.NOT_HAVE_VIDEO){
                cls.setHaveVideo(ClsClass.HAVE_VIDEO);
                save(cls);
            }
        }
    }
 
    @Override
    public Result deleteVideos(String videoIds) {
        if(StringUtils.isEmpty(videoIds)){
            return new Result(false, "参数错误");
        }
        
        String[] ids = videoIds.split(",");
        List<String> lst = new ArrayList<String>();
        for (String videoId : ids) {
            Result result = deleteVideo(videoId);
            if (result.isSuccess()) {
                lst.add(videoId);
            }
        }
 
        // 转码记录
        bulkUpdateInLoop("update MediaVideoTrans set deleteFlag = true where videoId = ?", lst.toArray());
        this.bulkUpdateInLoop("update SchCourseware set deleteFlag=1 where id=?", lst.toArray());
 
        return new Result(true);
    }
    
    public Result deleteVideo(String videoId) {
        String hql = " from MediaVideoReCourse where videoId = ? and deleteFlag is false and orgId=?";
        MediaVideoReCourse mvr = findUnique(hql, CollectionUtils.newList(videoId, ClientUtils.getOrgId()), MediaVideoReCourse.class);
        if (mvr == null) {
            MediaVideo video = read(MediaVideo.class, videoId);
            TraceUtils.setUpdateTrace(video);
            video.setDeleteFlag(true);
 
            mediaVideoService.saveVideo(video);
            return new Result(true);
        }
        
        hql = " from SchReLessonVideo where classScheduleId in" + "(select classScheduleId from SchClassSchedule where classId =? and deleteFlag is false)" + " and deleteFlag is false and videoId=?";
        List<SchReLessonVideo> lstVideo = find(hql, CollectionUtils.newList(ClientUtils.getClassId(), videoId), SchReLessonVideo.class);
        for (SchReLessonVideo schReLessonVideo : lstVideo) {
            schReLessonVideo.setDeleteFlag(true);
            TraceUtils.setUpdateTrace(schReLessonVideo);
            save(schReLessonVideo);
        }
 
        return new Result(false);
    }
    
    public Result updateMediaVideo(MediaVideo video){
        MediaVideo v = read(MediaVideo.class, video.getVideoId());
        
        if(MediaVideo.STATUS_TAKEOFF == v.getStatus()){
            if(null != video.getStartTime() && video.getStartTime().before(new Date()) 
                    && null != video.getEndTime() && !video.getEndTime().before(new Date())){ // video.getStartTime()在new Date()之前返回true,否则返回false
                
                v.setStatus(MediaVideo.STATUS_PUBLISHED);
            }
            
            if( null != video.getStartTime() && !video.getStartTime().before(new Date())){
                
                v.setStatus(MediaVideo.STATUS_DRAFT);
            }
        }
        
        if(MediaVideo.STATUS_PUBLISHED == v.getStatus()&&null != video.getEndTime() &&  video.getEndTime().before(new Date()) 
                && null != video.getStartTime() && video.getStartTime().before(new Date())){
             // video.getEndTime()在new Date()之前
            v.setStatus(MediaVideo.STATUS_TAKEOFF);
            
        }
        
        v.setCoverPageUrl(video.getCoverPageUrl());
        //v.setSubjectId(video.getSubjectId());
        //v.setSubjectName(video.getSubjectName());
        v.setStartTime(video.getStartTime());
        v.setEndTime(video.getEndTime());
        v.setValidity(video.getValidity());
        v.setName(video.getName());
        v.setRemark(video.getRemark());
        v.setTeacherName(video.getTeacherName());
        this.mediaVideoService.saveVideo(v);
        
        // 保存OrgText —— 视频详情
        orgTextService.doSaveOrgText(v.getVideoId(), OrgText.TABLE_NAME_VIDEO, video.getRemark());
        
        String hql = " from MediaVideo where videoId!=? and deleteFlag is false and originVideoId = ?";
        List<MediaVideo> lstVideo = find(hql, CollectionUtils.newList(video.getVideoId(),video.getVideoId()), MediaVideo.class);
        for (MediaVideo mediaVideo : lstVideo) {
            hql = " from SchClassSubject where deleteFlag is false and classId = ? and origSubjectId = ?";
            
            SchClassSubject scs = findUnique(hql, CollectionUtils.newList(mediaVideo.getClassId(),video.getSubjectId()), SchClassSubject.class);
            if(scs==null){
                continue;
            }
            mediaVideo.setSubjectId(scs.getClassSubjectId());
            mediaVideo.setSubjectName(scs.getName());
            this.mediaVideoService.saveVideo(mediaVideo);
        }
        return new Result(true);
    }
 
    public Result deleteOrgVideo(String videoId, Integer delAll, String orgIds[], String classIds[]) {
        if ((orgIds != null && orgIds.length != 0)  || (classIds != null && classIds.length != 0)) {
            // 删除需要回撤的视频
            return deleteAppoint(videoId, orgIds, classIds);
        }
 
        String hql = " from MediaVideoReCourse where videoId = ? and deleteFlag is false and orgId = ?";
        List<MediaVideoReCourse> lstMvr = find(hql, CollectionUtils.newList(videoId,ClientUtils.getOrgId()), MediaVideoReCourse.class);
        for (MediaVideoReCourse mediaVideoReCourse : lstMvr) {
            TraceUtils.setUpdateTrace(mediaVideoReCourse);
            mediaVideoReCourse.setDeleteFlag(true);
            save(mediaVideoReCourse);
            
            this.courseWareService.deleteOrgCourseware(videoId, ClientUtils.getOrgId());
        }
/*
        MediaVideo video = this.read(MediaVideo.class, videoId);
        if(video != null){
            TraceUtils.setUpdateTrace(video);
            video.setDeleteFlag(true);
            this.mediaVideoService.saveVideo(video);
        }*/
        // 用户选择同时删除下级视频
        if (delAll == 1) {
            deleteSub(videoId,ClientUtils.getOrgId());
 
        }
        return new Result(true);
    }
 
    @SuppressWarnings("unchecked")
    private void deleteSub(String videoId,String currOrgId) {
        // 机构层级视频是没有重新new ,查询出机构下级ID再删关联表
        String sql = " select oa.organization_id from organization as oa,organization ob " +
                " where " +
                " ob.ORGANIZATION_ID = ? " +
                " and  " +
                " oa.org_code like  CONCAT(ob.org_code,'%' ) " +
                " and oa.delete_flag is false and ob.delete_flag is false "+
                " order by oa.level,oa.org_code asc " ;
        
        List<String> orgIds = this.findBySql(sql, CollectionUtils.newList(currOrgId));
        String hql = " from MediaVideoReCourse where videoId = :videoId and deleteFlag is false and orgId in (:orgIds)";
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("videoId", videoId);
        map.put("orgIds", orgIds.toArray());
        List<MediaVideoReCourse> lstMvr = findByComplexHql(hql, map, MediaVideoReCourse.class);
        
        for (MediaVideoReCourse mediaVideoReCourse : lstMvr) {
            TraceUtils.setUpdateTrace(mediaVideoReCourse);
            mediaVideoReCourse.setDeleteFlag(true);
            save(mediaVideoReCourse);
        }
        
        MediaVideo video = read(MediaVideo.class, videoId);
        // 班主任层面视频指定过后都是new 出来的,通过originVideoId 可以查出所有指定过去的视频
        hql = "select classId from ClsClass where orgId in (:orgIds) and deleteFlag is false";
        map = new HashMap<String, Object>();
        map.put("orgIds", orgIds.toArray());
        List<String> clsIds = findByComplexHql(hql, map, String.class);
        if(!clsIds.isEmpty()){
            hql = " from MediaVideo where originVideoId = :originVideoId and deleteFlag is false and videoId !=originVideoId and classId in (:classIds)";
            map = new HashMap<String, Object>();
            map.put("originVideoId", video.getOriginVideoId());
            map.put("classIds", clsIds.toArray());
            List<MediaVideo> videos = findByComplexHql(hql, map, MediaVideo.class);
            for (MediaVideo mediaVideo : videos) {
                TraceUtils.setUpdateTrace(mediaVideo);
                mediaVideo.setDeleteFlag(true);
                
                this.mediaVideoService.saveVideo(mediaVideo);
            }
        }
        
    }
 
    /**
     * 删除需要回撤的视频
     * 
     * @param videoId
     * @param orgIds
     * @param classIds
     * @return
     */
    private Result deleteAppoint(String videoId, String orgIds[], String classIds[]) {
        Map<String, Object> args = new HashMap<String, Object>();
        // 删除需要回撤的机构视频
        if (orgIds.length != 0) {
            
            String hql = " from MediaVideoReCourse where videoId = :videoId and deleteFlag is false and orgId in (:orgIds) and orgId != :currOrgId";
 
            args.put("videoId", videoId);
            args.put("orgIds", orgIds);
            args.put("currOrgId", ClientUtils.getOrgId());
            List<MediaVideoReCourse> courses = findByComplexHql(hql, args, MediaVideoReCourse.class);
            for (MediaVideoReCourse mediaVideoReCourse : courses) {
                TraceUtils.setUpdateTrace(mediaVideoReCourse);
                mediaVideoReCourse.setDeleteFlag(true);
                save(mediaVideoReCourse);
                
                this.courseWareService.deleteOrgCourseware(mediaVideoReCourse.getVideoId(), mediaVideoReCourse.getOrgId());
            }
        }
 
        // 删除需要回撤的班主任视频
        if (classIds.length != 0) {
            args = new HashMap<String, Object>();
            args.put("videoId", videoId);
            args.put("classIds", classIds);
            String hql = " from MediaVideo where originVideoId = :videoId and deleteFlag is false and classId in (:classIds)";
            List<MediaVideo> videos = findByComplexHql(hql, args, MediaVideo.class);
            for (MediaVideo mediaVideo : videos) {
                TraceUtils.setUpdateTrace(mediaVideo);
                mediaVideo.setDeleteFlag(true);
                
                this.mediaVideoService.saveVideo(mediaVideo);
            }
            
        }
 
        return new Result(true);
    }
 
    /**
     * 课程表列表
     * 
     * @return
     */
    public List<SchClassSchedule> queryLessonList(Date monthFirst, Date monthLast) {
 
        return queryLessonList(ClientUtils.getClassId(), monthFirst, monthLast);
 
    }
 
    @Override
    public List<SchClassSchedule> queryAppLessonList(final String hql, final Pager page, final List<Object> args) {
        return lessonDAO.queryAppLessonList(hql, page, args);
    }
 
    @Override
    public List<SchClassSchedule> queryLessonList(String classId, Date monthFirst, Date monthLast) {
 
        String sql = " select s.class_schedule_id as classScheduleId ,s.name, s.content, "
                + " s.teacher,s.address,s.start_time as startTime, s.end_time as endTime,s.status " 
                + " from sch_class_schedule s  where s.delete_flag = 0 and s.class_id = ? ";
 
        List<Object> arrs = new ArrayList<Object>();
        arrs.add(classId);
        if (monthFirst != null && monthLast != null) {
            sql = sql.concat("and s.start_time >= ? and s.start_time <= ?");
            arrs.add(monthFirst);
            arrs.add(monthLast);
        }
        sql = sql.concat(" order by s.create_time desc ");
 
        return lessonDAO.queryLessonList(sql, arrs);
    }
 
    @Override
    public List<String> queryClassesByTeacher(String orgTeacherId) {
        return lessonDAO.queryClassesByTeacher(orgTeacherId);
    }
    
    /**
        * 根据直播查询讲师
        * 
        * @param orgTeacherId
        * @return
        */
    public List<String> queryClassesByLive(String orgTeacherId) {
        return lessonDAO.queryClassesByLive(orgTeacherId);
    }
 
    /**
     * 分享课程页面的全部课程表
     * 
     * @param classId
     * @return
     */
    @Override
    public Result queryScheduleList(String classId, ModelAndView mv, ClsClass cls) {
        String hql = " from SchClassSchedule where deleteFlag is false and classId = ? order by startTime";
        List<SchClassSchedule> classScheduleLst = find(hql, CollectionUtils.newList(classId), SchClassSchedule.class);
        
        // 组装课表信息内容
        Map<String, Object> nowLesson = new HashMap<String, Object>(5);
        nowLesson.put("beginWeek", "");
        nowLesson.put("lessonCount", classScheduleLst.size());
        nowLesson.put("startTime", cls.getStartTime());
        nowLesson.put("endTime", cls.getEndTime());
        
        if (!classScheduleLst.isEmpty()) {
            // 班级开始时间在课程开始时间之后
            if (cls.getStartTime().compareTo(classScheduleLst.get(0).getStartTime()) > 0) {
                nowLesson.put("startTime", classScheduleLst.get(0).getStartTime());
            }
            // 班级结束时间在课程结束时间之前
            if (cls.getEndTime().compareTo(classScheduleLst.get(classScheduleLst.size() - 1).getEndTime()) < 0) {
                nowLesson.put("endTime", classScheduleLst.get(classScheduleLst.size() - 1).getEndTime());
            }
        }
 
        List<Map<String, Object>> lstMap = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        int index = 0;
        int faceToface = 0;
        int newwork = 0;
 
        for (SchClassSchedule schedule : classScheduleLst) {
            if(SchClassSchedule.SCH_NETWORK.equals(schedule.getMode())){
                newwork++;
            }else{
                faceToface ++;
            }
            map = new HashMap<String, Object>(6);
            map.put("name", schedule.getName());
            map.put("startTime", schedule.getStartTime());
            map.put("endTime", schedule.getEndTime());
            map.put("address", schedule.getAddress());
            map.put("teacher", schedule.getTeacher());
            map.put("mode", schedule.getMode());
 
            if (new Date().compareTo(schedule.getEndTime()) > 0) { // 当前时间在结束时间之后 - 已经上过的课
 
                map.put("classStyle", "go-out");
            } else { // 正在上或还没上的课
                if (index == 0) { // 正在上的课
                    map.put("classStyle", "now");
                    index++;
 
                    // 组装课表信息内容
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(schedule.getStartTime());
                    int begin_week = cal.get(Calendar.DAY_OF_WEEK);
                    String str_begin_week = DateTimeUtils.getWeek(begin_week);
                    nowLesson.put("beginWeek", str_begin_week);
                    nowLesson.put("beginTime", schedule.getStartTime());
 
                } else { // 还没上的课
                    map.put("classStyle", "future");
                }
            }
            lstMap.add(map);
        }
        mv.addObject("nowLesson", nowLesson);
        mv.addObject("lessons", lstMap);
        mv.addObject("mode", newwork>0&&faceToface>0?"线上、线下":newwork>0&&faceToface==0?"线上":"线下");
        return new Result(true);
    }
}