派生自 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
package com.qxueyou.scc.school.service.impl;
 
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
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.stereotype.Service;
 
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.TraceUtils;
import com.qxueyou.scc.base.util.UUIDUtils;
import com.qxueyou.scc.exercise.model.ExerciseGroup;
import com.qxueyou.scc.exercise.service.IExerciseService;
import com.qxueyou.scc.media.model.MediaVideo;
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.model.SchArticle;
import com.qxueyou.scc.school.model.SchClassSubject;
import com.qxueyou.scc.school.model.SchCourseware;
import com.qxueyou.scc.school.model.SchHandout;
import com.qxueyou.scc.school.service.IArticleService;
import com.qxueyou.scc.school.service.ICourseWareService;
import com.qxueyou.scc.school.service.IHandoutService;
import com.qxueyou.scc.school.service.ILessonService;
import com.qxueyou.scc.teach.subject.model.Subject;
import com.qxueyou.scc.teach.subject.model.SubjectChapter;
 
/**
 * 课件管理
 * 
 * @author lihanqi
 *
 */
@Service
public class CoursewareService extends CommonAppService implements ICourseWareService {
    private static Logger log = LogManager.getLogger("CoursewareService");
    
    @Autowired
    IArticleService articleService;
    
    @Autowired
    IExerciseService exerciseService;
    
    @Autowired
    IHandoutService handoutService;
    
    @Autowired
    ILessonService lessonService;
    
    @Autowired
    IMediaVideoService mediaVideoService;
    
    @Autowired
    IOrgTextService orgTextService;
    
    /**
     * 新增章节
     * 
     * @param name
     * @param parentChapterId
     * @return
     */
    public Result addOrUpdateChapter(String chapterId, String subjectId, String name, String parentChapterId){
        if(StringUtils.isEmpty(subjectId) || StringUtils.isEmpty(name)){
            return new Result(false, "参数错误");
        }
        SubjectChapter objSchChapter = null;
        if(StringUtils.isEmpty(chapterId)){
            objSchChapter = new SubjectChapter();
            String uuid = UUIDUtils.generateUUID().replace("-", "");
            objSchChapter.setChapterId(uuid);
            objSchChapter.setParentChapterId(StringUtils.isEmpty(parentChapterId) ? uuid : parentChapterId);
            objSchChapter.setDeleteFlag(false);
            TraceUtils.setCreateTrace(objSchChapter);
        }else{
            objSchChapter = this.read(SubjectChapter.class, chapterId);
            TraceUtils.setUpdateTrace(objSchChapter);
        }
        
        objSchChapter.setName(name);
        objSchChapter.setParentChapterId(parentChapterId);
        objSchChapter.setSubjectId(subjectId);
        
        this.save(objSchChapter);
        
        return new Result(true,"",objSchChapter);
    }
    
    /**
     * 删除章节
     * 
     * @param chapterId
     * @return
     */
    public Result deleteChapter(String[] chapterIds){
        if(chapterIds.length == 0){
            return new Result(false, "参数错误");
        }
        int iCount = 0;
        for(String chapterId : chapterIds){//级联删除课件
            SubjectChapter chapter = this.read(SubjectChapter.class, chapterId);
            if(chapter != null && StringUtils.isEmpty(chapter.getParentChapterId())){
                iCount += this.findCount("from SchCourseware where chapterId = ? and cType = ?", CollectionUtils.newList(chapterId, 6));
            }
            this.deleteCourseWare(null, chapterId);
        }
        this.bulkUpdateInLoop("update SchChapter set deleteFlag = true where chapterId = ?", chapterIds);
        return new Result(true, "", iCount);
    }
    
    /**
     * 拷贝课件
     * 
     * @param type 课件类型
     * @param id 主键
     * @param subjectId 课程
     * @param collegeCourseId 科目
     * @param chapterId 章节
     * @return
     */
    public Result doCopyCourseware(String type, String id, String subjectId, String collegeCourseId, String chapterId){
        try {
            subjectId = StringUtils.isEmpty(subjectId)?null:subjectId;
            chapterId = StringUtils.isEmpty(chapterId)?null:chapterId;
            collegeCourseId = StringUtils.isEmpty(collegeCourseId)?null:collegeCourseId;
            Subject subject = this.read(Subject.class, subjectId);
            String subjectName = subject == null?null:subject.getName();
            
            if(SchCourseware.COURSEWARE_TYPE_ARTICLE.equals(type)){
                SchArticle article = this.read(SchArticle.class, id);
                SchArticle newArticle = (SchArticle) BeanUtils.cloneBean(article);
                newArticle.setArticleId(null);
                
                newArticle.setChapterId(chapterId);
                newArticle.setSubjectId(subjectId);
                newArticle.setSubjectName(subjectName);
                newArticle.setOrderNum(1000);
                newArticle.setCollegeCourseId(collegeCourseId);
                OrgText text = orgTextService.getOrgText(id, OrgText.TABLE_NAME_ARTICLE);
                newArticle.setContent(text == null?null:text.getContent());
                
                articleService.updateOrgArticle(newArticle, collegeCourseId);
            }else if(SchCourseware.COURSEWARE_TYPE_EXERCISE.equals(type)){
                exerciseService.doCopyExerciseGroup(id, subjectId, collegeCourseId, chapterId);
            }else if(SchCourseware.COURSEWARE_TYPE_HANDOUT.equals(type)){
                handoutService.doCopyHandout(id, subjectId, collegeCourseId, chapterId, subjectName);
            }else if(SchCourseware.COURSEWARE_TYPE_VIDEO.equals(type)){
                MediaVideo video = this.read(MediaVideo.class, id);
                MediaVideo newVideo = (MediaVideo) BeanUtils.cloneBean(video);
                newVideo.setVideoId(null);
                
                //newVideo.setChapterId(chapterId);
                newVideo.setSubjectId(subjectId);
                newVideo.setSubjectName(subjectName);
                newVideo.setCollegeCourseId(collegeCourseId);
                newVideo.setVideoOrder(1000);
                
                lessonService.insertOrgVideo(newVideo, collegeCourseId, true);
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return new Result(true);
    }
    
    /**
     * 移动课件
     * 
     * @param type 课件类型
     * @param id 主键
     * @param subjectId 课程
     * @param collegeCourseId 科目
     * @param chapterId 章节
     * @return
     */
    public Result doMoveCourseware(String type, String id, String subjectId, String collegeCourseId, String chapterId){
        
        boolean isOrg = StringUtils.isEmpty(ClientUtils.getClassId());
        subjectId = StringUtils.isEmpty(subjectId)?null:subjectId;
        chapterId = StringUtils.isEmpty(chapterId)?null:chapterId;
        collegeCourseId = StringUtils.isEmpty(collegeCourseId)?null:collegeCourseId;
        String subjectName = null;
        if(StringUtils.isNoneBlank(subjectId)){
            if(isOrg){
                subjectName = this.read(Subject.class, subjectId).getName();
            }else{
                subjectName = this.read(SchClassSubject.class, subjectId).getName();
            }
        }
        
        if(SchCourseware.COURSEWARE_TYPE_ARTICLE.equals(type)){
            SchArticle article = this.read(SchArticle.class, id);
            article.setSubjectId(subjectId);
            article.setSubjectName(subjectName);
            article.setChapterId(chapterId);
            TraceUtils.setUpdateTrace(article);
            article.setOrderNum(1000);
            
            this.articleService.saveArticle(article);
            
            this.bulkUpdate("update SchArticleReCourse set collegeCourseId = ? where articleId = ?", new Object[]{collegeCourseId, id});
        }else if(SchCourseware.COURSEWARE_TYPE_EXERCISE.equals(type)){
            ExerciseGroup exercise = this.read(ExerciseGroup.class, id);
            exercise.setSubjectId(subjectId);
            exercise.setChapterId(chapterId);
            TraceUtils.setUpdateTrace(exercise);
            exercise.setOrderNum(BigInteger.valueOf(1000));
            
            this.exerciseService.saveExerciseGroup(exercise);
            
            this.bulkUpdate("update ExerciseReCourse set collegeCourseId = ? where groupId = ?", new Object[]{collegeCourseId, id});
        }else if(SchCourseware.COURSEWARE_TYPE_HANDOUT.equals(type)){
            SchHandout handout = this.read(SchHandout.class, id);
            handout.setSubjectId(subjectId);
            handout.setSubjectName(subjectName);
            handout.setChapterId(chapterId);
            TraceUtils.setUpdateTrace(handout);
            handout.setOrderNum(1000);
            
            this.handoutService.saveHandout(handout);
            
            this.bulkUpdate("update SchHandoutReCourse set collegeCourseId = ? where handoutId = ?", new Object[]{collegeCourseId, id});
        }else if(SchCourseware.COURSEWARE_TYPE_VIDEO.equals(type)){
            MediaVideo video = this.read(MediaVideo.class, id);
            video.setSubjectId(subjectId);
            video.setSubjectName(subjectName);
            //video.setChapterId(chapterId);
            TraceUtils.setUpdateTrace(video);
            video.setVideoOrder(1000);
            
            this.mediaVideoService.saveVideo(video);
            
            this.bulkUpdate("update MediaVideoReCourse set collegeCourseId = ? where videoId = ?", new Object[]{collegeCourseId, id});
        }
        if(isOrg){
            this.bulkUpdate("update SchCourseware set collegeCourseId = ? where id = ? and orgId = ?", new Object[]{collegeCourseId, id, ClientUtils.getOrgId()});
        }
        return new Result(true);
    }
    
    /**
     * 删除课件
     * 
     * @param types
     * @param ids
     * @param delAll
     * @param orgIds
     * @param classIds
     * @return
     */
    public Result deleteCourseware(String[] types, String[] ids, Integer delAll, String orgIds[], String classIds[]){
        
        for(String id: ids) {
            this.bulkUpdate("update SchCourseware set deleteFlag = 1 where id=? ", new Object[] {id});
        }
        
        
        Map<String, String[]> result = this.coursewareClassify(types, ids);
        
        this.articleService.deleteOrgArticleIds(result.get(SchCourseware.COURSEWARE_TYPE_ARTICLE), delAll, orgIds, classIds);
        String [] groupIds = result.get(SchCourseware.COURSEWARE_TYPE_EXERCISE);
        if(groupIds != null && groupIds.length > 0){
            String [] groupTypes = new String[groupIds.length];
            for(int i=0;i<groupIds.length;i++){
                ExerciseGroup objExerciseGroup = this.read(ExerciseGroup.class, groupIds[i]);
                groupTypes[i] = String.valueOf(objExerciseGroup.getType());
            }
            this.exerciseService.deleteExerciseGroup(StringUtils.join(groupIds, ","), StringUtils.join(groupTypes, ","), "org", delAll, orgIds, classIds);
        }
        this.handoutService.deleteOrgHandouts(result.get(SchCourseware.COURSEWARE_TYPE_HANDOUT), delAll, orgIds, classIds);
        String [] videoIds = result.get(SchCourseware.COURSEWARE_TYPE_VIDEO);
        if(videoIds != null && videoIds.length > 0){
            for(int i=0;i<videoIds.length;i++){
                this.lessonService.deleteOrgVideo(videoIds[i], delAll, orgIds, classIds);
            }
        }
        return new Result(true);
    }
    
    /**
     * 下发课件
     * 
     * @param types
     * @param ids
     * @param orgIds
     * @param classIds
     * @return
     */
    public Result doAppointCourseware(String[] types, String[] ids, String orgIds[], String classIds[], String collegeCourseId){
        Map<String, String[]> result = this.coursewareClassify(types, ids);
        
        this.articleService.insertAppointArticle(result.get(SchCourseware.COURSEWARE_TYPE_ARTICLE), orgIds, classIds, collegeCourseId);
        this.exerciseService.insertAppointExercise(result.get(SchCourseware.COURSEWARE_TYPE_EXERCISE), orgIds, classIds);
        this.handoutService.insertAppointHandout(result.get(SchCourseware.COURSEWARE_TYPE_HANDOUT), orgIds, classIds, collegeCourseId);
        this.lessonService.insertAppointOrgVideo(result.get(SchCourseware.COURSEWARE_TYPE_VIDEO), orgIds, classIds);
        
        return new Result(true);
    }
    
    /**
     * 下发课件到子机构
     * 
     * @param id
     * @param sourceOrgId
     * @param targetOrgId
     * @param collegeCourseId
     * @return
     */
    public Result insertOrgCourseware(String id, String sourceOrgId, String targetOrgId, String collegeCourseId){
        SchCourseware courseware = this.findUnique("from SchCourseware where id = ? and orgId = ? and deleteFlag is false",
                CollectionUtils.newList(id, sourceOrgId), SchCourseware.class);
        
        if(courseware == null){
            return new Result(false, "原课件不存在");
        }
        SchCourseware newCourseware = new SchCourseware();
        try {
            BeanUtils.copyProperties(newCourseware, courseware);
            
            newCourseware.setCoursewareId(null);
            newCourseware.setOrgId(targetOrgId);
            TraceUtils.setCreateTrace(newCourseware);
            
            this.save(newCourseware);
        } catch (Exception e) {
            log.error("机构下发课件报错:课件id:"+id+",子机构id:"+targetOrgId, e);
        }
        
        return new Result(true);
    }
    
    /**
     * 撤回
     * 
     * @param id
     * @param orgId
     * @return
     */
    public Result deleteOrgCourseware(String id, String orgId){
        SchCourseware courseware = this.findUnique("from SchCourseware where id = ? and orgId = ? and deleteFlag is false",
                CollectionUtils.newList(id, orgId), SchCourseware.class);
        if(courseware != null){
            courseware.setDeleteFlag(true);
            TraceUtils.setUpdateTrace(courseware);
            
            this.save(courseware);
        }
        return new Result(true);
    }
    
    /**
     * 查询已下发的班级
     * 
     * @param types
     * @param ids
     * @return
     */
    public Result findAlready(String[] types, String[] ids){
        Map<String, String[]> classify = this.coursewareClassify(types, ids);
        
        String[] articleId = classify.get(SchCourseware.COURSEWARE_TYPE_ARTICLE);
        String[] groupId = classify.get(SchCourseware.COURSEWARE_TYPE_EXERCISE);
        String[] handoutId = classify.get(SchCourseware.COURSEWARE_TYPE_HANDOUT);
        String[] videoId = classify.get(SchCourseware.COURSEWARE_TYPE_VIDEO);
        
        List<String> lstOrg = new ArrayList<String>();
        List<String> lstClass = new ArrayList<String>();
        if(articleId.length > 0){
            String hql = " select c.orgId from SchArticleReCourse c" 
                       + " where c.deleteFlag is false" 
                       + " and c.articleId in(:articleId)";
            lstOrg.addAll(this.findByComplexHql(hql, CollectionUtils.newObjectMap("articleId", articleId), String.class));
            lstClass.addAll(this.findByComplexHql("select distinct classId from SchArticle where deleteFlag is false and originArticleId in(:articleId) and classId is not null", CollectionUtils.newObjectMap("articleId", articleId), String.class));
        }
        if(groupId.length > 0){
            String hql = " select c.orgId from ExerciseReCourse c"
                    + " where c.deleteFlag is false"
                    + " and c.groupId in (:groupId)";
            lstOrg.addAll(this.findByComplexHql(hql, CollectionUtils.newObjectMap("groupId", groupId), String.class));
            lstClass.addAll(this.findByComplexHql("select distinct classId from ExerciseGroup where  deleteFlag is false and originExerciseId in (:groupId) and classId is not null", CollectionUtils.newObjectMap("groupId", groupId), String.class));    
        }
        if(handoutId.length > 0){
            String hql = " select c.orgId from SchHandoutReCourse c" 
                       + " where c.deleteFlag is false" 
                       + " and c.handoutId in(:handoutId)";
            lstOrg.addAll(this.findByComplexHql(hql, CollectionUtils.newObjectMap("handoutId", handoutId), String.class));
            lstClass.addAll(this.findByComplexHql("select distinct classId from SchHandout where deleteFlag is false and originHandoutId in(:handoutId) and classId is not null", CollectionUtils.newObjectMap("handoutId", handoutId), String.class));
        }
        if(videoId.length > 0){
            String hql = " select c.orgId from MediaVideoReCourse c"
                    + " where c.deleteFlag is false"
                    + " and c.videoId in(:videoId)";
            lstOrg.addAll(this.findByComplexHql(hql, CollectionUtils.newObjectMap("videoId", videoId), String.class));
            lstClass.addAll(this.findByComplexHql("select distinct classId from MediaVideo where deleteFlag is false and originVideoId in(:videoId) and classId is not null", CollectionUtils.newObjectMap("videoId", videoId), String.class));
        }
        return new Result(true, "", CollectionUtils.newObjectMap("orgs", lstOrg, "classes", lstClass));
    }
    
    /**
     * 课件分类
     * 
     * @param types
     * @param ids
     * @return
     */
    private Map<String, String[]> coursewareClassify(String[] types, String[] ids){
        List<String> articleId = new ArrayList<String>();
        List<String> groupId = new ArrayList<String>();
        List<String> handoutId = new ArrayList<String>();
        List<String> videoId = new ArrayList<String>();
        for(int i=0;i<ids.length;i++){
            String type = types[i];
            if(SchCourseware.COURSEWARE_TYPE_ARTICLE.equals(type)){
                articleId.add(ids[i]);
            }else if(SchCourseware.COURSEWARE_TYPE_EXERCISE.equals(type)){
                groupId.add(ids[i]);
            }else if(SchCourseware.COURSEWARE_TYPE_HANDOUT.equals(type)){
                handoutId.add(ids[i]);
            }else if(SchCourseware.COURSEWARE_TYPE_VIDEO.equals(type)){
                videoId.add(ids[i]);
            }
        }
        Map<String, String[]> result = new HashMap<String, String[]>();
        result.put(SchCourseware.COURSEWARE_TYPE_ARTICLE, articleId.toArray(new String[articleId.size()]));
        result.put(SchCourseware.COURSEWARE_TYPE_EXERCISE, groupId.toArray(new String[groupId.size()]));
        result.put(SchCourseware.COURSEWARE_TYPE_HANDOUT, handoutId.toArray(new String[handoutId.size()]));
        result.put(SchCourseware.COURSEWARE_TYPE_VIDEO, videoId.toArray(new String[videoId.size()]));
        
        return result;
    }
    
    /**
     * 课件树排序
     * 
     * @param ids[] 排序的id
     * @param type 排序类型
     * @param parentId 父节点id
     * @return
     */
    public Result doOrder(String[] ids, String type, String parentId){
        for(int i=0;i<ids.length;i++){
            int index = ids.length - i;
            if("courseCategory".equals(type)){//科目类别
                this.bulkUpdate("update OrgCategoryLevel set orderNum = ? where categoryLevelId = ?", new Object[]{index, ids[i]});
            }else if("course".equals(type)){//科目
                this.bulkUpdate("update OrgCollegeCourse set orderNum = ? where collegeCourseId = ? and courseCategoryId = ?", new Object[]{index, ids[i], parentId});
            }else if("subject".equals(type)){//课程
                this.bulkUpdate("update SchSubject set orderNum = ? where subjectId = ? ", new Object[]{index, ids[i]});
            }else if("chapter".equals(type)){//章
                this.bulkUpdate("update SchChapter set orderNum = ? where chapterId = ? and subjectId = ?", new Object[]{index, ids[i], parentId});
            }else if("section".equals(type)){//节
                this.bulkUpdate("update SchChapter set orderNum = ? where chapterId = ? and parentChapterId = ?", new Object[]{index, ids[i], parentId});
            }else if("classSubject".equals(type)){//班级课程
                this.bulkUpdate("update SchClassSubject set orderNum = ? where classSubjectId = ? ", new Object[]{index, ids[i]});
            }
        }
        
        return new Result(true);
    }
    
    /*****************************************************班主任课件管理**************************************************************************************************/
    
    /**
     * 删除课件
     * 
     * @param types
     * @param ids
     * @return
     */
    public Result deleteClassCourseware(String[] types, String[] ids){
        Map<String, String[]> result = this.coursewareClassify(types, ids);
        
        for(String id:ids) {
            bulkUpdate("update SchCourseware set deleteFlag=true where id=?", new Object[] {id});
        }
        
        this.articleService.deleteArticle(StringUtils.join(result.get(SchCourseware.COURSEWARE_TYPE_ARTICLE), ","));
        this.exerciseService.deleteExerciseGroup(StringUtils.join(result.get(SchCourseware.COURSEWARE_TYPE_EXERCISE), ","));
        this.handoutService.deleteHandouts(result.get(SchCourseware.COURSEWARE_TYPE_HANDOUT));
        this.lessonService.deleteVideos(StringUtils.join(result.get(SchCourseware.COURSEWARE_TYPE_VIDEO), ","));
        
        return new Result(true);
    }
    
    /**
     * 拷贝课件
     * 
     * @param type 课件类型
     * @param id 主键
     * @param subjectId 课程
     * @param collegeCourseId 科目
     * @param chapterId 章节
     * @return
     */
    public Result doCopyClassCourseware(String type, String id, String subjectId, String chapterId){
        try {
            subjectId = StringUtils.isEmpty(subjectId)?null:subjectId;
            chapterId = StringUtils.isEmpty(chapterId)?null:chapterId;
            SchClassSubject subject = this.read(SchClassSubject.class, subjectId);
            String subjectName = subject == null?null:subject.getName();
            
            if(SchCourseware.COURSEWARE_TYPE_ARTICLE.equals(type)){
                SchArticle article = this.read(SchArticle.class, id);
                SchArticle newArticle = (SchArticle) BeanUtils.cloneBean(article);
                newArticle.setArticleId(null);
                
                newArticle.setChapterId(chapterId);
                newArticle.setSubjectId(subjectId);
                newArticle.setSubjectName(subjectName);
                newArticle.setCollegeCourseId(ClientUtils.getCourseId());
                OrgText text = orgTextService.getOrgText(id, OrgText.TABLE_NAME_ARTICLE);
                newArticle.setContent(text == null?null:text.getContent());
                
                articleService.updateArticle(newArticle);
            }else if(SchCourseware.COURSEWARE_TYPE_EXERCISE.equals(type)){
                exerciseService.doCopyExerciseGroup(id, subjectId, ClientUtils.getCourseId(), chapterId);
            }else if(SchCourseware.COURSEWARE_TYPE_HANDOUT.equals(type)){
                handoutService.doCopyHandout(id, subjectId, ClientUtils.getCourseId(), chapterId, subjectName);
            }else if(SchCourseware.COURSEWARE_TYPE_VIDEO.equals(type)){
                MediaVideo video = this.read(MediaVideo.class, id);
                MediaVideo newVideo = (MediaVideo) BeanUtils.cloneBean(video);
                newVideo.setVideoId(null);
                
                //newVideo.setChapterId(chapterId);
                newVideo.setSubjectId(subjectId);
                newVideo.setSubjectName(subjectName);
                newVideo.setCollegeCourseId(ClientUtils.getCourseId());
                
                lessonService.insertVideo(newVideo,"", true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true);
    }
    
    /**
     * 发布和停用
     * 
     * @param types
     * @param ids
     * @param status
     * @return
     */
    public Result updateStatus(String[] types, String[] ids, int status){
        Map<String, String[]> result = this.coursewareClassify(types, ids);
        if(status == 1){
            if(result.get(SchCourseware.COURSEWARE_TYPE_ARTICLE).length > 0){
                this.bulkUpdateInLoop("update SchArticle set status = 2 where articleId = ?", result.get(SchCourseware.COURSEWARE_TYPE_ARTICLE));
                this.bulkUpdateInLoop("update SchCourseware set status = 2 where id = ?", result.get(SchCourseware.COURSEWARE_TYPE_ARTICLE));
            }
            if(result.get(SchCourseware.COURSEWARE_TYPE_EXERCISE).length > 0){
                this.bulkUpdateInLoop("update ExerciseGroup set status = 2 where groupId = ?", result.get(SchCourseware.COURSEWARE_TYPE_EXERCISE));
                this.bulkUpdateInLoop("update SchCourseware set status = 2 where id = ?", result.get(SchCourseware.COURSEWARE_TYPE_EXERCISE));
            }
            if(result.get(SchCourseware.COURSEWARE_TYPE_HANDOUT).length > 0){
                this.handoutService.doStartHandouts(StringUtils.join(result.get(SchCourseware.COURSEWARE_TYPE_HANDOUT), ","));
            }
            if(result.get(SchCourseware.COURSEWARE_TYPE_VIDEO).length > 0){
                this.lessonService.doStartVideos(StringUtils.join(result.get(SchCourseware.COURSEWARE_TYPE_VIDEO), ","));
            }
            
        }else if(status == 2){
            if(result.get(SchCourseware.COURSEWARE_TYPE_ARTICLE).length > 0){
                this.bulkUpdateInLoop("update SchArticle set status = 3 where articleId = ?", result.get(SchCourseware.COURSEWARE_TYPE_ARTICLE));
                this.bulkUpdateInLoop("update SchCourseware set status = 3 where id = ?", result.get(SchCourseware.COURSEWARE_TYPE_ARTICLE));
            }
            if(result.get(SchCourseware.COURSEWARE_TYPE_EXERCISE).length > 0){
                this.bulkUpdateInLoop("update ExerciseGroup set status = 3 where groupId = ?", result.get(SchCourseware.COURSEWARE_TYPE_EXERCISE));
                this.bulkUpdateInLoop("update SchCourseware set status = 3 where id = ?", result.get(SchCourseware.COURSEWARE_TYPE_EXERCISE));
            }
            if(result.get(SchCourseware.COURSEWARE_TYPE_HANDOUT).length > 0){
                this.handoutService.doStopHandouts(result.get(SchCourseware.COURSEWARE_TYPE_HANDOUT));
            }
            if(result.get(SchCourseware.COURSEWARE_TYPE_VIDEO).length > 0){
                this.lessonService.doStopVideos(StringUtils.join(result.get(SchCourseware.COURSEWARE_TYPE_VIDEO), ","));
            }
        }
        return new Result(true);
    }
    
    /**
     * 删除课程和章节级联删除课件
     *  
     * @param subjectId 机构课程id
     * @param chapterId 章节id
     * @param classSubjectId 班级课程id
     * @return
     */
    public Result deleteCourseWare(String subjectId, String chapterId){
        if(StringUtils.isNotEmpty(chapterId)){
            SubjectChapter chapter = this.read(SubjectChapter.class, chapterId);
            String newChapterId = null;
            if(StringUtils.isNotEmpty(chapter.getParentChapterId())){
                newChapterId = chapter.getParentChapterId();
            }else{
                //删除章节练习
                this.bulkUpdate("update ExerciseGroup set deleteFlag = ? where chapterId = ? and type = ?", new Object[]{true, chapterId, ExerciseGroup.TYPE_CHAPTER_ITEM});
                this.bulkUpdate("update SchCourseware set deleteFlag = ? where chapterId = ? and cType = ?", new Object[]{true, chapterId, 6});
            }
            
            this.bulkUpdate("update MediaVideo set chapterId = ? where chapterId = ?", new Object[]{newChapterId, chapterId});
            this.bulkUpdate("update SchHandout set chapterId = ? where chapterId = ?", new Object[]{newChapterId, chapterId});
            this.bulkUpdate("update ExerciseGroup set chapterId = ? where chapterId = ?", new Object[]{newChapterId, chapterId});
            this.bulkUpdate("update SchArticle set chapterId = ? where chapterId = ?", new Object[]{newChapterId, chapterId});
            
            this.bulkUpdate("update SchCourseware set chapterId = ? where chapterId = ?", new Object[]{newChapterId, chapterId});
        }
        if(StringUtils.isNotEmpty(subjectId)){
            this.bulkUpdate("update MediaVideo set subjectId = null,subjectName = null where subjectId = ?", new Object[]{subjectId});
            this.bulkUpdate("update SchHandout set subjectId = null,subjectName = null where subjectId = ?", new Object[]{subjectId});
            this.bulkUpdate("update ExerciseGroup set subjectId = null where subjectId = ?", new Object[]{subjectId});
            this.bulkUpdate("update SchArticle set subjectId = null,subjectName = null where subjectId = ?", new Object[]{subjectId});
            
            this.bulkUpdate("update SchCourseware set subjectId = null where subjectId = ?", new Object[]{subjectId});
            
            //删除章节练习
            this.bulkUpdate("update ExerciseGroup set deleteFlag = ? where subjectId = ? and type = ?", new Object[]{true, subjectId, ExerciseGroup.TYPE_CHAPTER_ITEM});
            this.bulkUpdate("update SchCourseware set deleteFlag = ? where subjectId = ? and cType = ?", new Object[]{true, subjectId, 6});
        }
        return new Result(true);
    }
    
    /**
     * 同步更新课件课程名称
     * 
     * @param subjectId
     * @param subjectName
     * @return
     */
    public Result updateCourseWare(String subjectId, String subjectName){
        if(StringUtils.isNotEmpty(subjectId)){
            this.bulkUpdate("update MediaVideo set subjectName = ? where subjectId = ?", new Object[]{subjectName, subjectId});
            this.bulkUpdate("update SchHandout set subjectName = ? where subjectId = ?", new Object[]{subjectName, subjectId});
            this.bulkUpdate("update SchArticle set subjectName = ? where subjectId = ?", new Object[]{subjectName, subjectId});
        }
        return new Result(true);
    }
 
    /**
     * 设置学分
     */
    public Result updateCoursewareScore(String id, int score) {
        if(StringUtils.isNotEmpty(id)){
            this.bulkUpdate("update Schchapter set score = ? where id = ?", new Object[]{score, id});
        }
        return new Result(true);
    }
 
 
    /**
     * 查询课件
     */
    public List<Map<String, Object>> queryCourseware(String id, String type) {
         if(type.equals("category")) {
                 
             List<Map<String, Object>> items=new ArrayList<Map<String, Object>>();
             //查询项目列表
                String hql1="select collegeCourseId as id from OrgCollegeCourse where courseCategoryId=? and deleteFlag is false ";
                List<String> courseList =find(hql1, CollectionUtils.newList(id),String.class);
             //查询机构课程
                for (String courseId : courseList) {
                    List<Map<String, Object>> result = queryCoursewareByCourseId(courseId);
                    items.addAll(result);
                }
                return items;
        }else if(type.equals("course"))
        {
            return queryCoursewareByCourseId(id);
        }else {
            return queryCoursewareById( id ,type);
        }
 
    }
    
    
    
    /**
     * 根据项目id查询所有机构课程的课件
     * @param id
     * @return
     */
    public List<Map<String, Object>> queryCoursewareByCourseId(String id){
        List<Map<String, Object>> items=new ArrayList<Map<String, Object>>();
        
        String hql="select subjectId from  SchSubject where  collegeCourseId=?  and  deleteFlag is false";
        List<String> subjectList  =find(hql, CollectionUtils.newList(id),String.class);
        
        for (String subjectId : subjectList) {
             List<Map<String, Object>> courseware = queryCoursewareById(subjectId ,"subject");
             items.addAll(courseware);
        }
        return items;
    }
    
    /**
     * 根据项目id查询课件
     * @param courseId
     * @return
     */
    public  List<Map<String, Object>> queryCoursewareById(String id ,String type ){
        String hql="select id as id,name as name, describe as desc,type as type,score as score from SchCourseware "
                + "where deleteFlag is false";
        
         
        if(type.equals("course")) {
            hql+=" and collegeCourseId=:courseId";
             
               
            return findListWithMapByHql(hql, CollectionUtils.newObjectMap("courseId",id));
        }
        
        if(type.equals("subject")) {
            hql+=" and subjectId=:subjectId";
             
    
            return findListWithMapByHql(hql, CollectionUtils.newObjectMap("subjectId",id));
        }
        
        if(type.equals("chapter")) {
            hql+=" and chapterId=:chapterId or parentChapterId=:parentChapterId";
 
               
            return  findListWithMapByHql(hql, CollectionUtils.newObjectMap("chapterId",id,"parentChapterId",id));
        }
        
        if(type.equals("section")) {
            hql+=" and chapterId=:chapterId";
               
            return findListWithMapByHql(hql, CollectionUtils.newObjectMap("chapterId",id));
        }
        
        return null;
        
    }
 
    @Override
    public Result updateCoursewareScoreValue(String id,BigDecimal scoreValue) {
        String hql = "from SchCourseware c where c.id=? and c.deleteFlag is false";
        SchCourseware  courseWare = this.findUnique(hql, CollectionUtils.newList(id), SchCourseware.class);
        courseWare.setScoreValue(scoreValue);
        this.save(courseWare);
        return new Result(true);
    }
    
 
    /**
     * 获取排序
     * 
     * @param isOrg
     * @param subjectId
     * @param chapterId
     * @return
     */
    @Override
    public Map<String, Integer> getOrder(boolean isOrg, String subjectId, String chapterId){
        Map<String, Integer> result = new HashMap<>();
        //同步排序
        int subjectOrder = 0;
        int chapterOrder = 0;
        int partOrder = 0;
        if(StringUtils.isNoneBlank(subjectId)){
            if(isOrg){
                Subject subject = this.read(Subject.class, subjectId);
                subjectOrder = subject.getOrderNum();
            }else{
                SchClassSubject subject = this.read(SchClassSubject.class, subjectId);
                subjectOrder = subject.getOrderNum();
            }
        }
        if(StringUtils.isNoneBlank(chapterId)){
            SubjectChapter chapter = this.read(SubjectChapter.class, chapterId);
            if(StringUtils.isEmpty(chapter.getParentChapterId())){
                chapterOrder = chapter.getOrderNum();
            }else{
                SubjectChapter parentChapter = this.read(SubjectChapter.class, chapter.getParentChapterId());
                chapterOrder = parentChapter.getOrderNum();
                partOrder = chapter.getOrderNum();
            }
        }
        result.put("subjectOrder", subjectOrder);
        result.put("chapterOrder", chapterOrder);
        result.put("partOrder", partOrder);
        
        return result;
    }
    
    
    
}