派生自 projectDept/qhighschool

Administrator
2022-11-29 8c99e2d8b6c1e0d9cde6abbe80b4df75be19f6d1
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
package com.qxueyou.scc.admin.classes.service.impl;
 
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
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.querydsl.core.QueryResults;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.jpa.impl.JPAQuery;
import com.qxueyou.scc.admin.classes.model.ClsSubjectChapter;
import com.qxueyou.scc.admin.classes.model.ClsSubjectLecture;
import com.qxueyou.scc.admin.classes.service.IClassLectureService;
import com.qxueyou.scc.admin.progress.dao.ProgressDAO;
import com.qxueyou.scc.admin.progress.model.Progress;
import com.qxueyou.scc.admin.progress.model.QProgress;
import com.qxueyou.scc.admin.progress.model.view.SubjectProgressTreeV;
import com.qxueyou.scc.admin.progress.service.IProgressService;
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.QBeanUtils;
import com.qxueyou.scc.base.util.QueryDslOptionBuilder;
import com.qxueyou.scc.base.util.TraceUtils;
import com.qxueyou.scc.msg.model.MsgInfo;
import com.qxueyou.scc.msg.service.IMsgInfoService;
import com.qxueyou.scc.school.model.SchHandout;
import com.qxueyou.scc.teach.res.model.Res;
import com.qxueyou.scc.teach.res.model.ResItemDoc;
import com.qxueyou.scc.teach.res.service.IResService;
import com.qxueyou.scc.teach.subject.model.QSubjectLecture;
import com.qxueyou.scc.teach.subject.model.Subject;
import com.qxueyou.scc.teach.subject.model.SubjectChapter;
import com.qxueyou.scc.teach.subject.model.SubjectLecture;
import com.qxueyou.scc.teach.subject.model.view.MyLectureV;
import com.qxueyou.scc.teach.subject.model.view.QMyLectureV;
import com.qxueyou.scc.teach.subject.service.ILectureService;
import com.qxueyou.scc.teach.subject.service.ISubjectService;
 
@Service
public class ClassLectureService extends CommonAppService implements IClassLectureService {
 
    private final Logger log = LogManager.getLogger(ClassLectureService.class);
 
    @Autowired
    IResService resService;
 
    @Autowired
    ILectureService lectureService;
 
    @Autowired
    IProgressService progressService;
 
    @Autowired
    ISubjectService subjectService;
 
    @Autowired
    ProgressDAO progressDao;
 
    @Autowired
    IMsgInfoService msgInfoService;
 
    @Override
    public Result addChapter(String parentChapterId, String name) {
 
        ClsSubjectChapter parent = read(ClsSubjectChapter.class, parentChapterId);
 
        ClsSubjectChapter chapter = new ClsSubjectChapter();
 
        TraceUtils.setCreateTrace(chapter);
        chapter.setName(name);
        chapter.setParentChapterId(parentChapterId);
        chapter.setSubjectId(parent.getSubjectId());
        chapter.setClassId(parent.getClassId());
        save(chapter);
 
        return new Result(true, "success",
                CollectionUtils.newObjectMap("id", chapter.getChapterId(), "name", chapter.getName()));
 
    }
 
    @Override
    public List<MyLectureV> listLectureV(String learnerId, String chapterId, String keyword, Integer pageSize,
                                         Integer pageNum, String type) {
        StringBuffer hql = new StringBuffer("from MyLectureV where subjectId=? and id.userId=?");
        List<Object> args = CollectionUtils.newList(chapterId, learnerId);
        List<MyLectureV> myLectureVList = findList(hql.toString(), new Pager(pageSize, pageNum), args, MyLectureV.class);
 
        if(myLectureVList.size() < 1){
 
        }else {
            hql = new StringBuffer("from MyLectureV where chapterId=? and id.userId=? order by lectureCreateTime desc");
            args = CollectionUtils.newList(myLectureVList.get(0).getChapterId(), learnerId);
        }
 
        if (StringUtils.isNotEmpty(type)) {
            hql.append(" and lectureType=?");
            args.add(type);
        }
        List<MyLectureV> result = findList(hql.toString(), new Pager(pageSize, pageNum), args, MyLectureV.class);
        return result;
    }
 
    /**
     * ¿Î³Ì¿Î¼þ
     *
     */
    @Override
    public Map<String,Object> listLectureBySubjectOrderCreateTime(String subjectId, String classId, String sort, Pager pager) {
        Subject subject = this.read(Subject.class, subjectId);
        if(Subject.TYPE_PUBLIC_SUBJECT == subject.getType()) {//¹«¿ª¿Î¿Î¼þ
            return this.listOpenLectureBySubjectOrderCreateTime(subjectId, sort, pager);
        }
 
        QMyLectureV qMyLectureV = QMyLectureV.myLectureV;
 
        OrderSpecifier<Date> order = "desc".equals(sort)?new OrderSpecifier<Date>(Order.DESC, qMyLectureV.lectureCreateTime):
            new OrderSpecifier<Date>(Order.ASC, qMyLectureV.lectureCreateTime);
 
        JPAQuery<MyLectureV> query = this.getQueryFactory().selectFrom(qMyLectureV)
                .where(qMyLectureV.subjectId.eq(subjectId).
                        and(qMyLectureV.classId.eq(classId)).and(qMyLectureV.id.userId.eq(ClientUtils.getUserId())));
 
        long count = query.fetchCount();
 
        return CollectionUtils.newObjectMap("count", count, "listData", query.limit(pager.getPageSize()).offset(pager.getOffset())
                .orderBy(order)
                .fetch().stream()
                .map(tuple -> {
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("lectureName", tuple.getLectureName());
                    map.put("chapterId", tuple.getChapterId());
                    map.put("lectureType", tuple.getLectureType());
                    map.put("progressPercent", tuple.getPercent());
                    map.put("lectureId", tuple.getId().getLectureId());
                    map.put("createTime", tuple.getLectureCreateTime());
                    return map;
                }).collect(Collectors.toList()));
    }
 
    /**
     * ¹«¿ª¿Î¿Î¼þ
     *
     * @param subjectId
     * @param sort
     * @param pager
     * @return
     */
    @Override
    public Map<String,Object> listOpenLectureBySubjectOrderCreateTime(String subjectId, String sort, Pager pager) {
        QSubjectLecture qSubjectLecture = QSubjectLecture.subjectLecture;
        QProgress qProgress = QProgress.progress;
 
        OrderSpecifier<Date> order = "desc".equals(sort)?new OrderSpecifier<Date>(Order.DESC, qSubjectLecture.createTime):
            new OrderSpecifier<Date>(Order.ASC, qSubjectLecture.createTime);
 
        JPAQuery<SubjectLecture> query = this.getQueryFactory().selectFrom(qSubjectLecture).where(qSubjectLecture.subjectId.eq(subjectId).and(qSubjectLecture.deleteFlag.isFalse()).and(qSubjectLecture.status.eq(SubjectLecture.STATUS_DRAFT)));
 
        long count = query.fetchCount();
 
        return CollectionUtils.newObjectMap("count", count, "listData", query.limit(pager.getPageSize()).offset(pager.getOffset())
                .orderBy(order)
                .fetch().stream()
                .map(tuple -> {
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("lectureName", tuple.getName());
                    map.put("chapterId", tuple.getChapterId());
                    map.put("lectureType", tuple.getLectureType());
                    map.put("progressPercent", this.getQueryFactory().select(qProgress.progressPercent).from(qProgress).where(
                            qProgress.deleteFlag.isFalse().and(qProgress.targetId.eq(tuple.getLectureId())
                                    .and(qProgress.learnerId.eq(ClientUtils.getUserId())))).fetchFirst());
                    map.put("lectureId", tuple.getLectureId());
                    map.put("createTime", tuple.getCreateTime());
                    return map;
                }).collect(Collectors.toList()));
    }
 
    @Override
    public Result updateChapter(String chapterId, String name) {
 
        ClsSubjectChapter chapter = read(ClsSubjectChapter.class, chapterId);
        TraceUtils.setUpdateTrace(chapter);
        chapter.setName(name);
        save(chapter);
        return new Result(true, "success", CollectionUtils.newObjectMap("id", chapter.getChapterId(), "name", name));
    }
 
    @Override
    public Result deleteChapter(String[] chapterIds) {
        for (String chapterId : chapterIds) {
            deleteChapter(chapterId);
        }
        return new Result(true, "success");
    }
 
    /**
     * É¾³ýµ¥¸öÕ½Ú
     *
     * @param chapterId Õ½Úid
     * @return
     */
    private Result deleteChapter(String chapterId) {
 
        ClsSubjectChapter chapter = read(ClsSubjectChapter.class, chapterId);
 
        TraceUtils.setUpdateTrace(chapter);
        chapter.setDeleteFlag(true);
        save(chapter);
        Result result = deleteCourseware4Chapter(chapterId);
        if (!result.isSuccess()) {
            return result;
        }
        return new Result(true, "success");
    }
 
    @Override
    public Result addClsLecture(String chapterId, String resId) {
        ClsSubjectChapter chapter = read(ClsSubjectChapter.class, chapterId);
 
        Res res = resService.read(resId);
        ClsSubjectLecture lecture = new ClsSubjectLecture();
        TraceUtils.setCreateTrace(lecture);
        lecture.setChapterId(chapterId);
        lecture.setLectureType(res.getType());
        lecture.setName(res.getName());
        lecture.setRemark(res.getRemark());
        lecture.setSubjectId(chapter.getSubjectId());
        lecture.setResItemId(res.getResId());
        lecture.setClassId(chapter.getClassId());
        lecture.setStatus(res.getStatus());
        save(lecture);
 
        // ·¢ËÍÏûÏ¢
        doPublishMsg(lecture);
 
        return new Result(true, "success");
    }
 
    //Ìí¼Ó¿Î³Ì£¬·¢ËÍÏûÏ¢
    private void doPublishMsg(ClsSubjectLecture lecture){
        Subject subject = this.read(Subject.class, lecture.getSubjectId());
 
        if(!subject.getStatus().equals(Subject.STATUS_ISSUED)){
            return ;
        }
 
        //²éѯֱ²¥¹ØÁªµÄ°à¼¶
        String hql = "select userId from StuStudent  where classId =:classId and deleteFlag is false ";
 
        List<String> lstUserIds = this.findByComplexHql(hql, CollectionUtils.newObjectMap("classId",lecture.getClassId()), String.class);
 
        if(lstUserIds!=null && lstUserIds.size()>0){
            Map<String,String> attrs = CollectionUtils.newStringMap("lectureId",lecture.getLectureId(),"lectureName",lecture.getName(),"subjectId",subject.getSubjectId(),
                    "lectureType",lecture.getLectureType(),"subjectName",subject.getName());
 
            msgInfoService.doSendTextMsgToUsers(lstUserIds.toArray(new String[lstUserIds.size()]),MsgInfo.TYPE_COURSEWARE,"·¢²¼Á˿μþ",attrs);
        }
    }
 
    public Result updateLecturetime(String targetId) {
 
        ClsSubjectLecture lecture = read(ClsSubjectLecture.class, targetId);
 
 
        TraceUtils.setUpdateTrace(lecture);
 
        lecture.setUpdateTime(new Date());
        lecture.setUpdator(ClientUtils.getUserId());
        save(lecture);
 
        return new Result(true, "success");
    }
    @Override
    public Result updateLecture(String lectureId, String resId) {
 
        ClsSubjectLecture lecture = read(ClsSubjectLecture.class, lectureId);
 
        Res res = resService.read(resId);
 
        TraceUtils.setUpdateTrace(lecture);
        lecture.setLectureType(res.getType());
        lecture.setName(res.getName());
        lecture.setRemark(res.getRemark());
        lecture.setResItemId(res.getResId());
 
        save(lecture);
 
        return new Result(true, "success");
    }
 
    @Override
    public Result deleteLecture(String[] lectureIds) {
        for (String lectureId : lectureIds) {
            deleteLecture(lectureId);
        }
 
        return new Result(true, "success");
    }
 
    /**
     * @param lectureId ¿Î¼þid
     * @return
     */
    private Result deleteLecture(String lectureId) {
        ClsSubjectLecture lecture = read(ClsSubjectLecture.class, lectureId);
 
        TraceUtils.setUpdateTrace(lecture);
        lecture.setDeleteFlag(true);
        save(lecture);
        deleteProgress(lectureId);// É¾³ý¶ÔÓ¦µÄ½ø¶È
        return new Result(true, "success");
    }
 
    /**
     *
     */
    private Result deleteProgress(String targetId) {
        String hql = "update Progress set deleteFlag = true where deleteFlag is  false and targetId = ?";
        bulkUpdate(hql, new String[]{targetId});
        return new Result(true);
    }
 
    @Override
    public Result doCopyLecture(String lectureId, String destChapterId, String name) {
 
        return null;
    }
 
    @Override
    public Result doMoveLecture(String lectureId, String destChapterId, String name) {
 
        return null;
    }
 
    @Override
    public List<ClsSubjectLecture> listLecture(String chapterId, String keyword, Integer pageSize, Integer pageNum,
                                               String type, String classId) {
        StringBuffer hql = new StringBuffer("from ClsSubjectLecture where chapterId = ? and deleteFlag is false");
 
        List<Object> args = CollectionUtils.newList(chapterId);
 
        if (StringUtils.isNotEmpty(type)) {
            hql.append(" and lectureType=? order by createTime desc");
            args.add(type);
        } else {
            hql.append(" order by createTime desc");
        }
 
        List<ClsSubjectLecture> result = findList(hql.toString(), new Pager(pageSize, pageNum), args,
                ClsSubjectLecture.class);
 
        return result;
    }
 
    @Override
    public List<Map<String, Object>> listLectureBySubject(String subjectId, String type, String classId) {
        StringBuffer hql = new StringBuffer("select name as lectureName, "
                + " lectureId as lectureId, "
                + " lectureType as lectureType, "
                + " chapterId as chapterId, "
                + " status as status, "
                + " updateTime as updateTime "
                + " from ClsSubjectLecture where subjectId = :subjectId and classId = :classId and deleteFlag is false and status = :status");
 
        Map<String, Object> args = CollectionUtils.newObjectMap("subjectId", subjectId, "classId", classId, "status", Res.STATUS_DRAFT);
 
        if (StringUtils.isNotEmpty(type)) {
            hql.append(" and lectureType= :lectureType");
            args.put("lectureType", type);
        }
        hql.append(" order by createTime desc");
 
        return this.findListWithMapByHql(hql.toString(), args);
    }
 
 
    @Override
    public List<Map<String, Object>> listStuLectureBySubject(String subjectId, String classId) {
        if(StringUtils.isEmpty(subjectId) || StringUtils.isEmpty(classId)) {
            return new ArrayList<>();
        }
        QMyLectureV qMyLectureV = QMyLectureV.myLectureV;
 
        return this.getQueryFactory().selectFrom(qMyLectureV)
                .where(qMyLectureV.classId.eq(classId).and(
                        qMyLectureV.subjectId.eq(subjectId).and(qMyLectureV.id.userId.eq(ClientUtils.getUserId()))))
                .orderBy(qMyLectureV.lectureCreateTime.desc()).fetch().stream().map(tuple -> {
                    Map<String, Object> map = new HashMap<String, Object>(6);
                    map.put("lectureName", tuple.getLectureName());
                    map.put("lectureId", tuple.getId().getLectureId());
                    map.put("lectureType", tuple.getLectureType());
                    map.put("chapterId", tuple.getChapterId());
                    map.put("subjectId", tuple.getSubjectId());
                    map.put("progressPercent", tuple.getPercent());
                    map.put("createTime", tuple.getLectureCreateTime());
                    return map;
                }).collect(Collectors.toList());
    }
 
    /**
     * ÎҵĹ«¿ª¿Î¿Î¼þ²éѯ
     *
     * @param subjectId
     * @return
     */
    public List<Map<String, Object>> listStuLectureByOpenSubject(String subjectId) {
        QSubjectLecture qSubjectLecture = QSubjectLecture.subjectLecture;
        QProgress qProgress = QProgress.progress;
        return this.getQueryFactory().selectFrom(qSubjectLecture)
                .where(qSubjectLecture.subjectId.eq(subjectId).and(qSubjectLecture.status.eq(SubjectLecture.STATUS_DRAFT)).and(qSubjectLecture.deleteFlag.isFalse()))
                .orderBy(qSubjectLecture.createTime.desc()).fetch().stream().map(tuple -> {
                    Map<String, Object> map = new HashMap<String, Object>(6);
                    map.put("lectureName", tuple.getName());
                    map.put("lectureId", tuple.getLectureId());
                    map.put("lectureType", tuple.getLectureType());
                    map.put("chapterId", tuple.getChapterId());
                    map.put("subjectId", tuple.getSubjectId());
                    map.put("progressPercent",
                            this.getQueryFactory().select(qProgress.progressPercent).from(qProgress).where(
                                    qProgress.deleteFlag.isFalse().and(qProgress.targetId.eq(tuple.getLectureId())
                                            .and(qProgress.learnerId.eq(ClientUtils.getUserId())))).fetchFirst());
                    map.put("createTime", tuple.getCreateTime());
                    map.put("updateTime", tuple.getUpdateTime());
                    return map;
                }).collect(Collectors.toList());
    }
 
    /**
     * ²éѯÊýÁ¿
     *
     * @param subjectId
     * @param classId
     * @return
     */
    @Override
    public long stuLectureCountBySubject(String subjectId, String classId) {
        QMyLectureV qMyLectureV = QMyLectureV.myLectureV;
 
        return this.getQueryFactory().selectFrom(qMyLectureV)
                .where(qMyLectureV.classId.eq(classId).and(
                        qMyLectureV.subjectId.eq(subjectId).and(qMyLectureV.id.userId.eq(ClientUtils.getUserId())))).fetchCount();
    }
 
    @Override
    public Result readLectureAccessPath(String lectureId, String attribute) {
        ClsSubjectLecture lecture = read(ClsSubjectLecture.class, lectureId);
        String resItemId = null;
        if(lecture == null) {
            resItemId = read(SubjectLecture.class, lectureId).getResItemId();//°à¼¶¿Î¼þ
        }else {
            resItemId = lecture.getResItemId();
        }
        return resService.readAccessPath(resItemId, attribute);
    }
 
    @Override
    public ClsSubjectLecture readClsLecture(String id) {
 
        return read(ClsSubjectLecture.class, id);
    }
 
    @Override
    public ClsSubjectChapter readChapter(String chapterId) {
 
        return read(ClsSubjectChapter.class, chapterId);
    }
 
    @Override
    public int listLectureCount(String chapterId, String keyword, String type) {
        String hql = "from ClsSubjectLecture where deleteFlag is false and chapterId =:chapterId ";
        Map<String, Object> args = CollectionUtils.newObjectMap("chapterId", chapterId);
        if (StringUtils.isNotBlank(type)) {
            hql = hql.concat(" and lectureType =:type");
            args.put("type", type);
        }
        int count = findCountByComplexHql(hql, args);
        return count;
    }
 
    @Override
    public List<ClsSubjectChapter> getListChapter(String classId, String subjectId, String parentChapterId) {
 
        StringBuffer hql = new StringBuffer(
                "from ClsSubjectChapter where classId=? and subjectId=? and deleteFlag is false ");
 
        List<Object> args = CollectionUtils.newList(classId,subjectId);
 
        if (StringUtils.isNotEmpty(parentChapterId)) {
            hql = hql.append(" and parentChapterId=? ");
            args.add(parentChapterId);
        }
 
        List<ClsSubjectChapter> result = find(hql.toString(), args, ClsSubjectChapter.class);
 
        return result;
    }
 
    public List<SubjectChapter> getListOpenChapter(String subjectId) {
 
        StringBuffer hql = new StringBuffer(
                "from SubjectChapter where subjectId=? and deleteFlag is false ");
 
        List<Object> args = CollectionUtils.newList(subjectId);
 
        return find(hql.toString(), args, SubjectChapter.class);
    }
 
    @Override
    public List<ClsSubjectChapter> doGetListChapter(String classId,String subjectId, String parentChapterId) {
 
        List<ClsSubjectChapter> result = getListChapter(classId,subjectId, StringUtils.isEmpty(parentChapterId)?ClsSubjectChapter.ROOT_CHAPTER_ID:parentChapterId);
 
        return result;
    }
 
    @Override
    public Result doClearLecturesToClass( String classId,String subjectId) {
        this.bulkUpdate("update ClsSubjectLecture set deleteFlag = true where classId=? and  subjectId = ? ",
                   new String[]{classId,subjectId});
 
        this.bulkUpdate("update ClsSubjectChapter set deleteFlag = true where classId=? and  subjectId = ? ",
                new String[]{classId,subjectId});
 
        return new Result(true);
    }
 
    @Override
    public Result doCopyLecturesToClass(String origSubjectId, String subjectId, String classId) {
        Map<String, String> subjectIdMap = CollectionUtils.newStringMap(SubjectChapter.ROOT_CHAPTER_ID,
                SubjectChapter.ROOT_CHAPTER_ID);
 
        List<SubjectChapter> chapterLst = lectureService.getAllChapterBySubjectId(origSubjectId);
        ClsSubjectChapter clsChapter = null;
 
        for (SubjectChapter subjectChapter : chapterLst) {
            clsChapter = new ClsSubjectChapter();
            clsChapter.setClassId(classId);
            clsChapter.setName(subjectChapter.getName());
            clsChapter.setOrderNum(subjectChapter.getOrderNum());
            clsChapter.setParentChapterId(subjectIdMap.get(subjectChapter.getParentChapterId()));
            clsChapter.setSubjectId(subjectId);
            TraceUtils.setCreateTrace(clsChapter);
            clsChapter.setCreateTime(subjectChapter.getCreateTime());
            save(clsChapter);
            subjectIdMap.put(subjectChapter.getChapterId(), clsChapter.getChapterId());
        }
 
        List<SubjectLecture> lectureLst = lectureService.listLectureBySubjectId(origSubjectId);
        ClsSubjectLecture clsLecture = null;
        for (SubjectLecture lecture : lectureLst) {
            clsLecture = new ClsSubjectLecture();
            clsLecture.setChapterId(subjectIdMap.get(lecture.getChapterId()));
            clsLecture.setClassId(classId);
            clsLecture.setLectureType(lecture.getLectureType());
            clsLecture.setName(lecture.getName());
            clsLecture.setRemark(lecture.getRemark());
            clsLecture.setResItemId(lecture.getResItemId());
            clsLecture.setSubjectId(subjectId);
            clsLecture.setDeleteFlag(false);
            clsLecture.setStatus(lecture.getStatus());
            TraceUtils.setCreateTrace(clsLecture);
            save(clsLecture);
        }
 
        return new Result(true);
    }
 
    @Override
    public Result doStudy(String clsLectureId, Double from, Double to) {
        return progressService.addProgress(Progress.PROGRESS_TYPE_LECTURE, clsLectureId, from.intValue(), to.intValue(),
                ClientUtils.getUserId());
    }
 
    @Override
    public List<ClsSubjectLecture> listLecture(String subjectId) {
 
        StringBuffer hql = new StringBuffer("from ClsSubjectLecture where subjectId = ? and deleteFlag is false");
 
        List<Object> args = CollectionUtils.newList(subjectId);
 
        return find(hql.toString(), args, ClsSubjectLecture.class);
 
    }
 
    @Override
    public Result listSubjectLecture(String classId, String sort, Pager pager) {
        if(StringUtils.isEmpty(classId)) {
            return new Result(false, "°à¼¶id²»ÄÜΪ¿Õ");
        }
        QMyLectureV qMyLectureV = QMyLectureV.myLectureV;
 
        OrderSpecifier<Date> order = "desc".equals(sort)?new OrderSpecifier<Date>(Order.DESC, qMyLectureV.lectureCreateTime):
            new OrderSpecifier<Date>(Order.ASC, qMyLectureV.lectureCreateTime);
 
        JPAQuery<MyLectureV> query = this.getQueryFactory()
                .selectFrom(qMyLectureV)
                .where(new QueryDslOptionBuilder()
                        .and(qMyLectureV.classId::eq, classId).and(qMyLectureV.id.userId::eq,ClientUtils.getUserId())
                        .build()
                        );
 
        long count = query.fetchCount();
 
        // »ñÈ¡²éѯ½á¹û¼¯ºÏ
        QueryResults<MyLectureV> results = query.limit(pager.getPageSize()).offset(pager.getOffset()).orderBy(order).fetchResults();
 
        List<Map<String,Object>> resultLst =
                results.getResults()
                .stream()
                .map(tuple -> {
                    Map<String,Object> map = new HashMap<String,Object>(8);
                    map.put("lectureName", tuple.getLectureName());
                    map.put("lectureId", tuple.getId().getLectureId());
                    map.put("lectureType", tuple.getLectureType());
                    map.put("chapterId", tuple.getChapterId());
                    map.put("subjectName", tuple.getSubjectName());
                    map.put("subjectId", tuple.getSubjectId());
                    map.put("progressPercent", tuple.getPercent());
                    map.put("createTime", tuple.getLectureCreateTime());
                    return map;
                }).collect(Collectors.toList());
 
        return new Result(true, "", CollectionUtils.newObjectMap("count", count, "listData", resultLst));
 
    }
 
    @Override
    public List<SubjectProgressTreeV> getChapterTreeVList(String subjectId, String parentChapterId) {
 
        if (parentChapterId == null) {
            return progressService.getSubjectChapterTreeVList(subjectId, ClientUtils.getUserId());
        }
 
        return progressService.getSubjectChapterTreeVList(parentChapterId, ClientUtils.getUserId());
    }
 
    /**
     * É¾³ý¶ÔÓ¦µÄ¿Î¼þ
     *
     * @param chapterId
     * @return
     */
    private Result deleteCourseware4Chapter(String chapterId) {
 
        this.bulkUpdate("update ClsSubjectLecture set deleteFlag = true where chapterId = ?",
                new String[]{chapterId});
        return new Result(true);
 
    }
 
 
    /**
     * »ñȡսÚϵĿμþ
     *
     * @param chapterLst
     * @param lectures
     * @return
     */
    public List<ClsSubjectChapter> getChapterLectures(List<ClsSubjectChapter> chapterLst, List<Map<String, Object>> lectures){
        if (chapterLst != null && chapterLst.size() > 0 && lectures != null) {
            for (ClsSubjectChapter objClsSubjectChapter : chapterLst) {
                for (Map<String, Object> objClsSubjectLecture : lectures) {
                    if (objClsSubjectChapter.getChapterId().equals(objClsSubjectLecture.get("chapterId"))) {
                        objClsSubjectChapter.getLectures().add(objClsSubjectLecture);
                    }
                }
            }
 
            this.getTopChapters(chapterLst);
 
            for (int i=chapterLst.size()-1;i>=0;i--) {
                ClsSubjectChapter objClsSubjectChapter = chapterLst.get(i);
                if(objClsSubjectChapter.getLectures() == null || objClsSubjectChapter.getLectures().isEmpty()) {
                    chapterLst.remove(i);
                }
            }
        }
 
        return chapterLst;
    }
 
    /**
     * »ñȡսڵĵÚÒ»²ãÊý¾Ý
     *
     * @param chapterLst
     * @return
     */
    private List<ClsSubjectChapter> getTopChapters(List<ClsSubjectChapter> chapterLst){
        int count = 0;
        if (chapterLst != null && chapterLst.size() > 0) {
            for (int i=chapterLst.size()-1;i>=0;i--) {
                ClsSubjectChapter objClsSubjectChapter = chapterLst.get(i);
                for (ClsSubjectChapter objClsSubjectChapter2 : chapterLst) {
                    if(StringUtils.isNotBlank(objClsSubjectChapter.getChapterId()) && !ClsSubjectChapter.ROOT_CHAPTER_ID.equals(objClsSubjectChapter.getParentChapterId())
                            && objClsSubjectChapter.getChapterId().equals(objClsSubjectChapter2.getParentChapterId())
                            && objClsSubjectChapter2.getLectures() != null) {
                        if(objClsSubjectChapter.getLectures() == null) {
                            objClsSubjectChapter.setLectures(new ArrayList<Map<String,Object>>());
                        }
                        objClsSubjectChapter.getLectures().addAll(objClsSubjectChapter2.getLectures());
                        count++;
                        objClsSubjectChapter2.setLectures(null);
                        break;
                    }
                }
            }
            if(count != 0) {
                chapterLst = this.getTopChapters(chapterLst);
            }
        }
 
        return chapterLst;
    }
 
 
    @Override
    public int readDocPageCount(String resItemId) {
        ResItemDoc resItemDoc = read(ResItemDoc.class, resItemId);
        if (resItemDoc == null || ResItemDoc.STATUS_DRAFT != resItemDoc.getStatus()) {
            return 0;
        }
        SchHandout schHandout = read(SchHandout.class, resItemDoc.getHandoutId());
 
        if (schHandout == null) {
            return 0;
        }
 
        return schHandout.getPageCount();
    }
 
     /**
     * ¿Î¼þ°´Ä¿Â¼·ÖÀà
     *
     * @param classId
     * @param subjectId
     * @param type
     * @return
     */
    public Result coursewareChapterList(String classId,String subjectId, String type) {
        Subject subject = this.read(Subject.class, subjectId);
 
        if(Subject.TYPE_PUBLIC_SUBJECT == subject.getType() || Subject.TYPE_ORG_SUBJECT == subject.getType()) {//¹«¿ª¿ÎºÍ¹ÜÀíÔ±¿Î³Ì
            QSubjectLecture qSubjectLecture = QSubjectLecture.subjectLecture;
            //¿Î¼þ
            List<Map<String, Object>> lectures = this.listStuLectureByOpenSubject(subjectId);
 
            List<SubjectChapter> lstSubjectChapterLst = this.getListOpenChapter(subjectId);
            List<ClsSubjectChapter> lstClsSubjectChapter = new ArrayList<>();
            if(lstSubjectChapterLst != null && !lstSubjectChapterLst.isEmpty()) {
                ClsSubjectChapter objClsSubjectChapter = null;
                for(SubjectChapter objSubjectChapter : lstSubjectChapterLst) {
                    objClsSubjectChapter = new ClsSubjectChapter();
                    try {
                        BeanUtils.copyProperties(objClsSubjectChapter, objSubjectChapter);
                    } catch (IllegalAccessException e) {
                        log.error("¿½±´ÊôÐÔʧ°ÜBeanUtils.copyProperties(objClsSubjectChapter, objSubjectChapter)", e);
                    } catch (InvocationTargetException e) {
                        log.error("¿½±´ÊôÐÔʧ°ÜBeanUtils.copyProperties(objClsSubjectChapter, objSubjectChapter)", e);
                    }
 
                    lstClsSubjectChapter.add(objClsSubjectChapter);
                }
            }
            //Õ½Ú
            List<ClsSubjectChapter> chapterLst = this.getChapterLectures(lstClsSubjectChapter, lectures);
 
            long count =  this.getQueryFactory().selectFrom(qSubjectLecture)
                    .where(qSubjectLecture.subjectId.eq(subjectId).and(qSubjectLecture.deleteFlag.isFalse()).
                            and(qSubjectLecture.status.eq(SubjectLecture.STATUS_DRAFT))).fetchCount();
 
            return new Result(true, "success", CollectionUtils.newObjectMap("count",count, "listData", QBeanUtils.listBean2ListMap(chapterLst,
                    CollectionUtils.newStringMap("name", "name", "chapterId", "chapterId", "lectures", "lectures"))));
 
        }else{
            //¿Î¼þ
            List<Map<String, Object>> lectures = this.listStuLectureBySubject(subjectId, classId);
            //Õ½Ú
            List<ClsSubjectChapter> chapterLst = this.getChapterLectures(this.getListChapter(classId, subjectId, null), lectures);
 
            return new Result(true, "success", CollectionUtils.newObjectMap("count",this.stuLectureCountBySubject(subjectId, classId), "listData", QBeanUtils.listBean2ListMap(chapterLst,
                    CollectionUtils.newStringMap("name", "name", "chapterId", "chapterId", "lectures", "lectures"))));
        }
    }
 
}