派生自 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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
package com.qxueyou.scc.school.service.impl;
 
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.qxueyou.scc.admin.classes.model.ClsClass;
import com.qxueyou.scc.base.model.Pager;
import com.qxueyou.scc.base.model.Result;
import com.qxueyou.scc.base.model.UserInfoWrapper;
//import com.qxueyou.scc.base.service.ICacheService;
import com.qxueyou.scc.base.service.impl.CommonAppService;
//import com.qxueyou.scc.base.util.CacheUtils;
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.operation.comment.mode.Comment;
import com.qxueyou.scc.operation.comment.mode.CommentPraise;
//import com.qxueyou.scc.sys.service.IOssService;
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.SchArticleReCourse;
import com.qxueyou.scc.school.model.SchArticleStatistic;
import com.qxueyou.scc.school.model.SchClassCircleMsg;
import com.qxueyou.scc.school.model.SchClassSubject;
import com.qxueyou.scc.school.model.SchCourseware;
import com.qxueyou.scc.school.model.SchFile;
import com.qxueyou.scc.school.model.SchImg;
import com.qxueyou.scc.school.service.IArticleService;
import com.qxueyou.scc.school.service.ICourseWareService;
import com.qxueyou.scc.teach.subject.model.SubjectChapter;
 
/**
 * 文章实现类
 * 
 * @author zhiyong
 *
 */
@Service
public class ArticleService extends CommonAppService implements IArticleService {
 
    // @Autowired
    // ICacheService cache;
    //
    // @Autowired
    // IOssService ossService;
 
    @Autowired
    IOrgTextService orgTextService;
 
    @Autowired
    private ICourseWareService courseWareService;
 
    // 缓存
    @SuppressWarnings("unused")
    private static final String CACHE_MSG_IMGS = "CACHE_MSG_IMGS_";
    @SuppressWarnings("unused")
    private static final String CACHE_MSG_FILES = "CACHE_MSG_FILES_";
    @SuppressWarnings("unused")
    private static final String CACHE_ARTICLE_COMMENTS = "CACHE_ARTICLE_COMMENTS";
 
    @SuppressWarnings("unused")
    private static final int ONE_DAY = 60 * 60 * 24;
 
    /**
     * 查询列表数据
     * 
     * @param type
     * @return
     */
    @Override
    public List<SchArticle> queryClassListData() {
 
        List<SchArticle> resultLst = this.find(
                "from SchArticle where deleteFlag is false and classId=? order by orderNum ",
                CollectionUtils.newList(ClientUtils.getClassId()), SchArticle.class);
 
        return resultLst;
    }
 
    /**
     * 新增或更新列表数据
     * 
     * @param articleId
     * @param name
     * @return
     */
    @SuppressWarnings("unused")
    @Override
    public Result updateArticle(SchArticle article) {
 
        String articleId = article.getArticleId();
        if (StringUtils.isBlank(articleId)) {// 新增
            article.setArticleId(null);
            article.setClassId(ClientUtils.getClassId());
            article.setOrgId(ClientUtils.getOrgId());
            article.setDeleteFlag(false);
            TraceUtils.setCreateTrace(article);
 
            // 序号
            String hql = "select MAX(c.orderNum) from SchArticle 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;
            }
 
            article.setOrderNum(iMax);
 
            this.saveArticle(article);
            article.setOriginArticleId(article.getArticleId());
            this.saveArticle(article);
 
            articleId = article.getArticleId();
        } else {// 更新
 
            SchArticle articleNew = this.read(SchArticle.class, articleId);
 
            // 原上传的文件url
            String originUrl = articleNew.getUrl();
 
            articleNew.setCoverPageUrl(article.getCoverPageUrl());
            // articleNew.setSubjectId(article.getSubjectId());
            // articleNew.setSubjectName(article.getSubjectName());
            articleNew.setName(article.getName());
            articleNew.setUrl(article.getUrl());
            TraceUtils.setUpdateTrace(articleNew);
 
            this.saveArticle(articleNew);
 
            // // 删除原上传的文件
            // if(StringUtils.isNotBlank(originUrl)){
            // ossService.deleteOSSFile(originUrl.substring(1));
            // }
        }
 
        // 保存OrgText —— 文章内容
        orgTextService.doSaveOrgText(articleId, OrgText.TABLE_NAME_ARTICLE, article.getContent());
 
        // 删除该班级所有缓存
        // new CacheUtils().deleteWBProjectCacheData();
 
        return new Result(true, "操作成功", articleId);
    }
 
    /**
     * 新增或更新列表数据 机构
     * 
     * @return
     */
    @SuppressWarnings("unused")
    @Override
    public Result updateOrgArticle(SchArticle article, String collegeCourseId) {
 
        String articleId = article.getArticleId();
        // jgw 直接将文章修改为已发布
        article.setStatus(SchArticle.STATUS_PUBLISHED);
 
        if (StringUtils.isBlank(articleId)) {// 新增
            article.setArticleId(null);
            article.setOrgId(ClientUtils.getOrgId());
            article.setDeleteFlag(false);
            TraceUtils.setCreateTrace(article);
 
            this.saveArticle(article);
            article.setOriginArticleId(article.getArticleId());
            this.saveArticle(article);
 
            articleId = article.getArticleId();
            // 保存文章课程关联关系
            saveSchArticleReCourse(collegeCourseId, article.getArticleId());
        } else {// 更新
 
            SchArticle articleNew = this.read(SchArticle.class, articleId);
            // 原上传的文件url
            String originUrl = articleNew.getUrl();
 
            articleNew.setCoverPageUrl(article.getCoverPageUrl());
            // articleNew.setSubjectId(article.getSubjectId());
            // articleNew.setSubjectName(article.getSubjectName());
            articleNew.setName(article.getName());
            articleNew.setUrl(article.getUrl());
            TraceUtils.setUpdateTrace(articleNew);
 
            this.saveArticle(articleNew);
 
            // // 删除原上传的文件
            // if(StringUtils.isNotBlank(originUrl)){
            // ossService.deleteOSSFile(originUrl.substring(1));
            // }
        }
 
        // 保存OrgText —— 文章内容
        orgTextService.doSaveOrgText(articleId, OrgText.TABLE_NAME_ARTICLE, article.getContent());
 
        // 删除该班级所有缓存
        // new CacheUtils().deleteWBProjectCacheData();
 
        return new Result(true, "操作成功", articleId);
    }
 
    /**
     * 保存文章课程关联关系
     * 
     * @param collegeCourseId
     * @param articleId
     */
    private void saveSchArticleReCourse(String collegeCourseId, String articleId) {
        SchArticleReCourse course = new SchArticleReCourse();
        course.setCollegeCourseId(collegeCourseId);
        course.setDeleteFlag(false);
        course.setArticleId(articleId);
        course.setOrgId(ClientUtils.getOrgId());
        TraceUtils.setCreateTrace(course);
 
        // 序号
        String hql = "select MAX(c.orderNum) from SchArticleReCourse c where c.deleteFlag is false and c.collegeCourseId = ? and orgId = ? ";
        Integer iMax = this.findUnique(hql, CollectionUtils.newList(collegeCourseId, ClientUtils.getOrgId()),
                Integer.class);
 
        if (iMax == null || iMax == 0) {
            iMax = 1;
        } else {
            iMax = iMax + 1;
        }
 
        course.setOrderNum(iMax);
 
        save(course);
    }
 
    /**
     * 删除文章
     * 
     * @param articleIds
     * @return
     */
    @Override
    public Result deleteArticle(String articleIds) {
 
        if (StringUtils.isBlank(articleIds)) {
            return new Result(false, "提交数据异常");
        }
 
        String[] aIds = articleIds.split(",");
 
        this.bulkUpdateInLoop("update SchArticle set deleteFlag=1 where articleId=?", aIds);
        this.bulkUpdateInLoop("update SchCourseware set deleteFlag=1 where id=?", aIds);
 
        // 删除微商项目所有缓存
        // new CacheUtils().deleteWBProjectCacheData();
 
        return new Result(true, "操作功能");
    }
 
    /**
     * 
     * 查询基本信息
     * 
     * @param articleId
     * @return
     */
    @Override
    public Result queryInfo(String articleId) {
 
        SchArticle article = this.read(SchArticle.class, articleId);
 
        String content = this.findUnique(
                "select content from OrgText where key=? and tableName=? and deleteFlag is false",
                CollectionUtils.newList(articleId, OrgText.TABLE_NAME_ARTICLE), String.class);
 
        if (StringUtils.isBlank(content)) {
            content = "";
        }
        article.setCommentCount(this.findCount("from Comment where deleteFlag is false and commentObjectId = ?",
                CollectionUtils.newList(articleId)));
        article.setContent(content);
        return new Result(true, "查询成功", article);
    }
 
    /**
     * 查询科目文章list
     * 
     * @param collegeCourseId
     * @return
     */
    @Override
    public List<Map<String, Object>> queryArticleOrgLst(String collegeCourseId, String subjectId) {
        List<Object[]> handoutLst = null;
 
        List<Object> args = new ArrayList<Object>();
        String hql = "";
        // 其他
        if (("-" + collegeCourseId).equals(subjectId)) {
            hql = "select v.articleId,v.name,'','','',0,'' from SchArticle v,SchArticleReCourse r where v.collegeCourseId = ? and r.deleteFlag is false and r.articleId = v.articleId and v.deleteFlag is false and r.orgId = ?  "
                    + "and not EXISTS (select 1 from SchSubject t where t.deleteFlag is false and t.subjectId = v.subjectId)";
            args.add(collegeCourseId);
            args.add(ClientUtils.getOrgId());
        } else {
            hql = "select h.articleId,h.name,o.courseCategoryName,o.name,h.subjectName,s.orderNum,h.subjectId "
                    + " from SchArticleReCourse s , SchArticle h,OrgCollegeCourse o"
                    + " where s.deleteFlag is false and h.deleteFlag is false "
                    + " and o.collegeCourseId = s.collegeCourseId" + " and s.orgId = ?"
                    + " and s.articleId = h.articleId ";
            args.add(ClientUtils.getOrgId());
            if (StringUtils.isNotBlank(subjectId)) {
                hql = hql.concat(" and h.subjectId = ?");
                args.add(subjectId);
            }
            hql = hql.concat(" and s.collegeCourseId = ? ORDER BY s.orderNum ASC");
            args.add(collegeCourseId);
        }
 
        handoutLst = findwithRawResult(hql, args);
 
        List<Map<String, Object>> lstMap = new ArrayList<Map<String, Object>>(handoutLst.size());
        Map<String, Object> map = null;
 
        for (Object[] obj : handoutLst) {
            map = new HashMap<String, Object>();
            map.put("articleId", obj[0]);
            map.put("name", obj[1]);
            map.put("courseCategoryName", obj[2]);
            map.put("courseName", obj[3]);
            map.put("subjectName", obj[4]);
            map.put("orderNum", obj[5]);
            map.put("subjectId", obj[6]);
 
            lstMap.add(map);
        }
        return lstMap;
    }
 
    /**
     * 排序
     */
    @Override
    public Result doitemOrder(List<String> ids, List<Integer> index) {
        // 循环修改order
        for (int i = 0; i < ids.size(); i++) {
            String id = ids.get(i);
            if (UserInfoWrapper.ROLE_TEACHER.equals(ClientUtils.getCurrentRole())
                    || UserInfoWrapper.ROLE_CHARGER.equals(ClientUtils.getCurrentRole())) {
                this.bulkUpdate("update SchArticle set orderNum = " + index.get(i) + " where articleId = ?",
                        new Object[] { id });
            } else {
                this.bulkUpdate("update SchArticleReCourse set orderNum = " + index.get(i) + " where articleId = ?",
                        new Object[] { id });
            }
            this.bulkUpdate("update SchCourseware set orderNum = " + index.get(i) + " where id = ?",
                    new Object[] { id });
        }
 
        // 删除微商项目所有缓存
        // new CacheUtils().deleteWBProjectCacheData();
 
        return new Result(true, "修改成功");
    }
 
    /**
     * 删除机构文章信息
     * 
     * @param articleIds
     * @param delAll
     * @param orgIds
     * @param classIds
     */
    @Override
    public Result deleteOrgArticleIds(String[] articleIds, Integer delAll, String[] orgIds, String[] classIds) {
        for (String articleId : articleIds) {
 
            if ((orgIds != null && orgIds.length != 0) || (classIds != null && classIds.length != 0)) {
                // 删除需要回撤的讲义
                deleteAppoint(articleId, orgIds, classIds);
            } else {
                // 删除讲义
                String hql = " from SchArticleReCourse where articleId = ? and deleteFlag is false and orgId = ?";
                SchArticleReCourse shr = findUnique(hql, CollectionUtils.newList(articleId, ClientUtils.getOrgId()),
                        SchArticleReCourse.class);
                if (shr != null) {
                    TraceUtils.setUpdateTrace(shr);
                    shr.setDeleteFlag(true);
                    save(shr);
 
                    this.courseWareService.deleteOrgCourseware(shr.getArticleId(), shr.getOrgId());
                }
                /*
                 * SchArticle article = this.read(SchArticle.class, articleId); if(article !=
                 * null){ TraceUtils.setUpdateTrace(article); article.setDeleteFlag(true);
                 * this.saveArticle(article); }
                 */
                // 用户选择同时删除下级讲义
                if (delAll == 1) {
                    deleteSub(articleId, ClientUtils.getOrgId());
                }
            }
 
        }
 
        return new Result(true, "删除成功");
    }
 
    private Result deleteAppoint(String articleId, String orgIds[], String classIds[]) {
        Map<String, Object> args = new HashMap<String, Object>();
        // 删除需要回撤的机构讲义
        if (orgIds.length != 0) {
 
            String hql = " from SchArticleReCourse where articleId = :articleId and deleteFlag is false and orgId in (:orgIds) and orgId != :currOrgId";
            args = new HashMap<String, Object>();
            args.put("articleId", articleId);
            args.put("orgIds", orgIds);
            args.put("currOrgId", ClientUtils.getOrgId());
            List<SchArticleReCourse> courses = findByComplexHql(hql, args, SchArticleReCourse.class);
            for (SchArticleReCourse course : courses) {
                TraceUtils.setUpdateTrace(course);
                course.setDeleteFlag(true);
                save(course);
 
                this.courseWareService.deleteOrgCourseware(course.getArticleId(), course.getOrgId());
            }
        }
 
        // 删除需要回撤的班主任讲义
        if (classIds.length != 0) {
            args = new HashMap<String, Object>();
            args.put("articleId", articleId);
            args.put("classIds", classIds);
            String hql = " from SchArticle where originArticleId = :articleId and deleteFlag is false and classId in (:classIds)";
            List<SchArticle> articles = findByComplexHql(hql, args, SchArticle.class);
            for (SchArticle article : articles) {
                TraceUtils.setUpdateTrace(article);
                article.setDeleteFlag(true);
                this.saveArticle(article);
            }
 
        }
 
        return new Result(true);
    }
 
    @SuppressWarnings("unchecked")
    private void deleteSub(String articleId, 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 SchArticleReCourse where articleId = :articleId and deleteFlag is false and orgId in (:orgIds)";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("articleId", articleId);
        map.put("orgIds", orgIds.toArray());
        List<SchArticleReCourse> artCourses = findByComplexHql(hql, map, SchArticleReCourse.class);
        for (SchArticleReCourse artCourse : artCourses) {
            artCourse.setDeleteFlag(true);
            TraceUtils.setUpdateTrace(artCourse);
            save(artCourse);
        }
 
        // 班主任层面视频指定过后都是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 SchArticle where originArticleId = :articleId and deleteFlag is false and articleId!=originArticleId and classId in (:classIds)";
            map = new HashMap<String, Object>();
            map.put("articleId", articleId);
            map.put("classIds", clsIds.toArray());
            List<SchArticle> lstArticle = findByComplexHql(hql, map, SchArticle.class);
            for (SchArticle article : lstArticle) {
                article.setDeleteFlag(true);
                TraceUtils.setUpdateTrace(article);
                this.saveArticle(article);
            }
        }
    }
 
    /**
     * 下发机构文件
     * 
     * @param articleId
     * @param orgId
     * @param classIds
     * @param collegeCourseId
     * @return
     */
    @Override
    public Result insertAppointArticle(String[] articleIds, String[] orgIds, String[] classIds,
            String collegeCourseId) {
        if (null == articleIds || articleIds.length == 0) {
            return new Result(false, "参数错误");
        }
        // 一次性查询classId对应的orgId
        Map<String, Object> argsMap = new HashMap<String, Object>();
        Map<String, String> classMap = new HashMap<String, String>();
        if (classIds.length > 0) {
            String hql = " from ClsClass where classId in (:classIds) and deleteFlag is false";
            argsMap = new HashMap<String, Object>();
            argsMap.put("classIds", classIds);
            List<ClsClass> classList = findByComplexHql(hql, argsMap, ClsClass.class);
 
            // 放入map中 KEY:classId VALUE:orgId
 
            for (ClsClass orgClass : classList) {
                classMap.put(orgClass.getClassId(), orgClass.getOrgId());
            }
        }
 
        // 一次性查出handoutId对应的handoutOrder
        Map<String, Integer> orderMap = new HashMap<String, Integer>();
        String hql = " from SchArticleReCourse where articleId in (:articleIds) and deleteFlag is false and orgId = :currOrgId";
        argsMap = new HashMap<String, Object>();
        argsMap.put("articleIds", articleIds);
        argsMap.put("currOrgId", ClientUtils.getOrgId());
        List<SchArticleReCourse> articleCourse = findByComplexHql(hql, argsMap, SchArticleReCourse.class);
        for (SchArticleReCourse schArticleReCourse : articleCourse) {
            orderMap.put(schArticleReCourse.getArticleId(), schArticleReCourse.getOrderNum());
        }
 
        for (String articleId : articleIds) {
            if (orgIds.length > 0) { // 指定给机构
                insertAppointOrg(orgIds, articleId, collegeCourseId, orderMap);
            }
            if (classIds.length > 0) { // 指定给班级
                insertAppointClass(classIds, articleId, collegeCourseId, orderMap);
            }
        }
 
        return new Result(true);
    }
 
    /**
     * 指定给机构
     * 
     * @param orgIds
     * @param handoutId
     * @param collegeCourseId
     */
    private void insertAppointOrg(String orgIds[], String articleId, String collegeCourseId,
            Map<String, Integer> orderMap) {
        String hql = "select orgId from SchArticleReCourse where articleId = :articleId and deleteFlag is false and orgId in (:orgIds)";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("articleId", articleId);
        args.put("orgIds", orgIds);
        List<String> strings = findByComplexHql(hql, args, String.class);
        List<String> orgList = new ArrayList<String>();
        for (String orgId : orgIds) {
            orgList.add(orgId);
        }
        // 剔除掉不符合条件的orgId
        orgList.removeAll(strings);
        if (orgList.isEmpty()) {
            return;
        }
        for (String orgId : orgList) {
            SchArticleReCourse shrc = new SchArticleReCourse();
            shrc.setCollegeCourseId(collegeCourseId);
            shrc.setDeleteFlag(false);
            shrc.setArticleId(articleId);
            shrc.setOrderNum(orderMap.get(articleId));
            shrc.setOrgId(orgId);
            TraceUtils.setCreateTrace(shrc);
            save(shrc);
 
            this.courseWareService.insertOrgCourseware(articleId, ClientUtils.getOrgId(), orgId, collegeCourseId);
        }
    }
 
    /**
     * 指定给班级
     * 
     * @param classIds
     * @param handoutId
     * @param classMap
     */
    private void insertAppointClass(String classIds[], String articleId, String collegeCourseId,
            Map<String, Integer> orderMap) {
        // 章节id
        Map<String, SubjectChapter> origChapterMap = new HashMap<String, SubjectChapter>();
        SchArticle article = read(SchArticle.class, articleId);
 
        String hql = "select classId from SchArticle where deleteFlag is false and originArticleId = :articleId and classId in (:classIds)";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("classIds", classIds);
        args.put("articleId", articleId);
        List<String> strings = findByComplexHql(hql, args, String.class);
        List<String> classList = new ArrayList<String>();
        for (String classId : classIds) {
            classList.add(classId);
        }
        classList.removeAll(strings);
        if (classList.isEmpty()) {
            return;
        }
        // 一次性查询班级ID对应的classSubjectId
        hql = " from SchClassSubject where classId in(:classIds) and origSubjectId = :subjectId and deleteFlag is false";
        args = new HashMap<String, Object>();
        args.put("classIds", classList.toArray());
        args.put("subjectId", article.getSubjectId());
        List<SchClassSubject> lstClassSubject = findByComplexHql(hql, args, SchClassSubject.class);
        Map<String, String> subjectMap = new HashMap<String, String>();
        for (SchClassSubject schClassSubject : lstClassSubject) {
            subjectMap.put(schClassSubject.getClassId(), schClassSubject.getClassSubjectId());
        }
        OrgText oldOrgText = orgTextService.getOrgText(articleId, OrgText.TABLE_NAME_ARTICLE);
        SchArticle sh;
        for (String classId : classList) {
 
            sh = new SchArticle();
            sh.setDeleteFlag(false);
            sh.setName(article.getName());
            sh.setClassId(classId);
            sh.setOrgId(article.getOrgId());
            sh.setOriginArticleId(articleId);
            sh.setSubjectId(subjectMap.get(classId));
            sh.setSubjectName(article.getSubjectName());
            sh.setCoverPageUrl(article.getCoverPageUrl());
            sh.setCollegeCourseId(collegeCourseId);
            sh.setUrl(article.getUrl());
            sh.setOrderNum(1000);
            if (null != origChapterMap.get(classId)) {
                sh.setChapterId(origChapterMap.get(classId).getChapterId());
            } else {
                sh.setChapterId(null);
            }
            TraceUtils.setCreateTrace(sh);
            this.saveArticle(sh);
 
            // 插入OrgText
            orgTextService.doInsertOrgText(sh.getArticleId(), OrgText.TABLE_NAME_ARTICLE,
                    oldOrgText == null ? "" : oldOrgText.getContent());
        }
    }
 
    /**
     * 获取朋友圈
     * 
     * @param pager
     * @return
     */
    @SuppressWarnings("unchecked")
    public Result articleList(Pager pager, String sortType, String sortField, String collegeCourseId,
            String subjectId) {
        List<Object> args = new ArrayList<Object>();
        StringBuffer sb = new StringBuffer(512);
        sb.append(" SELECT ");
        sb.append("     aa.ARTICLE_ID, ");
        sb.append("     aa.NAME, ");
        sb.append("     aa.COLLEGE_COURSE_ID, ");
        sb.append("     aa.SUBJECT_ID, ");
        sb.append("     aa.COVER_PAGE_URL, ");
        sb.append("     aa.VIEW_COUNT, ");
        sb.append("     aa.ORDER_NUM, ");
        sb.append("     aa.CREATE_TIME, ");
        sb.append("     ss.COMP_DEGREE ");
        sb.append(" FROM ");
        sb.append("     ( ");
        sb.append("         SELECT * FROM sch_article a ");
        sb.append("         WHERE ");
        sb.append("             a.CLASS_ID = ? ");
        sb.append("         AND a.DELETE_FLAG IS FALSE ");
        sb.append("     ) aa ");
        sb.append(" LEFT JOIN ( ");
        sb.append("     SELECT * FROM ");
        sb.append("         sch_article_statistic s ");
        sb.append("     WHERE ");
        sb.append("         s.delete_flag IS FALSE ");
        sb.append("     AND s.user_id = ? ");
        sb.append(" ) ss ON ss.article_ID = aa.ARTICLE_ID ");
 
        args.add(ClientUtils.getClassId());
        args.add(ClientUtils.getUserId());
        if (StringUtils.isNotBlank(collegeCourseId)) {
            sb.append(" and college_CourseId = ?");
            args.add(collegeCourseId);
        }
        if (StringUtils.isNotBlank(subjectId)) {
            sb.append(" and SUBJECT_ID = ?");
            args.add(subjectId);
        }
 
        if (StringUtils.isNotBlank(sortType) && StringUtils.isNotBlank(sortField)) {
            sb.append(" order by " + sortField + " " + sortType);
        } else {
            sb.append(" order by order_Num ");
        }
 
        List<Object[]> lstSchArticle = this.findByPageSql(sb.toString(), args, pager);
        List<Map<String, Object>> lstResult = new ArrayList<Map<String, Object>>();
        if (lstSchArticle != null && !lstSchArticle.isEmpty()) {
            for (Object[] obj : lstSchArticle) {
                lstResult.add(CollectionUtils.newObjectMap("articleId", obj[0], "name", obj[1], "coverPageUrl", obj[3],
                        "viewCount", obj[4], "compDegree", obj[7], "createTime", obj[6]));
            }
        }
 
        return new Result(true, "", lstResult);
    }
 
    /**
     * 获取消息的评论图片和文件
     * 
     * @param objSchClassCircleMsg
     * @return
     */
    public SchClassCircleMsg packageMsg(SchClassCircleMsg objSchClassCircleMsg) {
        String msgId = objSchClassCircleMsg.getMsgId();
        // List<SchImg> imgs = cache.get(CACHE_MSG_IMGS + msgId, List.class);
        //
        // List<SchFile> files = cache.get(CACHE_MSG_FILES + msgId, List.class);
        List<SchImg> imgs = null;
        List<SchFile> files = null;
 
        @SuppressWarnings("unused")
        List<Comment> comments = this.updateComment(msgId, false);
 
        if (imgs == null || imgs.isEmpty()) {
            imgs = this.find("from SchImg where deleteFlag is false and objectId = ? order by createTime asc",
                    CollectionUtils.newList(msgId), SchImg.class);
 
            // cache.set(CACHE_MSG_IMGS + msgId, ONE_DAY, imgs);
        }
 
        if (files == null || files.isEmpty()) {
            files = this.find("from SchFile where deleteFlag is false and objectId = ? order by createTime asc",
                    CollectionUtils.newList(msgId), SchFile.class);
 
            // cache.set(CACHE_MSG_FILES + msgId, ONE_DAY, files);
        }
 
        objSchClassCircleMsg.setImgs(imgs);
        objSchClassCircleMsg.setFiles(files);
        //objSchClassCircleMsg.setComments(comments);
 
        return objSchClassCircleMsg;
    }
 
    /**
     * 点赞
     * 
     * @param commentId
     * @return
     */
    public Result doLike(String commentId) {
 
        CommentPraise praiseObj = new CommentPraise();
        praiseObj.setCommentId(commentId);
        praiseObj.setPraise(true);
        praiseObj.setCommentterId(ClientUtils.getUserId());
        praiseObj.setCommentter(ClientUtils.getUserName());
        praiseObj.setPraiseTime(new Date(System.currentTimeMillis()));
        praiseObj.setDeleteFlag(false);
 
        TraceUtils.setCreateTrace(praiseObj);
 
        this.save(praiseObj);
 
        this.bulkUpdate("update Comment set commentPraiseCount = commentPraiseCount +1 where commentId = ?",
                new Object[] { commentId });
 
        return new Result(true);
    }
 
    /**
     * 评论
     * 
     * @param msgId
     * @param content
     * @param parentCommentId
     * @return
     */
    public Result addComment(String articleId, String content, String parentCommentId) {
 
        // 评论表
        Comment comment = new Comment();
        comment.setCommentterId(ClientUtils.getUserId());
        comment.setCommentter(ClientUtils.getUserName());
        comment.setCommentterHeadimg(ClientUtils.getUserImg());
        comment.setContent(content);
        comment.setCommentCount(BigInteger.ZERO);
        comment.setCommentPraiseCount(BigInteger.ZERO);
        comment.setCommentObjectId(articleId);
        comment.setCommentTime(new Date(System.currentTimeMillis()));
        comment.setCommentParentId(parentCommentId);
        comment.setClassId(ClientUtils.getClassId());
        TraceUtils.setCreateTrace(comment);
 
        if (StringUtils.isNotEmpty(parentCommentId)) {
            Comment parentComment = this.read(Comment.class, parentCommentId);
 
            comment.setCommentedName(parentComment.getCommentter());
            comment.setCommentedId(parentComment.getCommentterId());
        }
 
        this.save(comment);
 
        this.bulkUpdate("update SchArticle set commentCount = commentCount +1 where articleId = ?",
                new Object[] { articleId });
 
        this.updateComment(articleId, true);
 
        return new Result(true, "", comment);
    }
 
    /**
     * 删除评论
     * 
     * @param msgId
     * @param commentId
     * @return
     */
    public Result deleteComment(String articleId, String commentId) {
 
        this.bulkUpdate("update Comment set deleteFlag = 1 where commentId = ? ", new Object[] { commentId });
        this.bulkUpdate("update SchArticle set commentCount = commentCount -1 where articleId = ?",
                new Object[] { articleId });
 
        this.updateComment(articleId, true);
 
        return new Result(true);
    }
 
    /**
     * 缓存获取评论
     * 
     * @param articleId
     * @param isUpdate
     * @return
     */
    private List<Comment> updateComment(String articleId, boolean isUpdate) {
        // List<Comment> comments = cache.get(CACHE_ARTICLE_COMMENTS + articleId,
        // List.class);
        List<Comment> comments = null;
 
        if (comments == null || comments.isEmpty() || isUpdate) {
            comments = this.find(
                    "from Comment where deleteFlag is false and commentObjectId = ? order by createTime asc",
                    CollectionUtils.newList(articleId), Comment.class);
 
            // cache.set(CACHE_ARTICLE_COMMENTS + articleId, ONE_DAY, comments);
        }
 
        return comments;
    }
 
    /**
     * 评论列表
     * 
     * @param articleId
     * @return
     */
    public Result commentList(String articleId) {
        return new Result(true, "", this.updateComment(articleId, false));
    }
 
    /**
     * 观看进度
     * 
     * @param articleId
     * @param compDegree
     * @return
     */
    public Result doSubmitSchedule(String articleId, double compDegree) {
        SchArticleStatistic objSchArticleStatistic = this.findUnique(
                "from SchArticleStatistic where deleteFlag is false and userId = ? and articleId = ?",
                CollectionUtils.newList(ClientUtils.getUserId(), articleId), SchArticleStatistic.class);
 
        if (objSchArticleStatistic == null) {
            objSchArticleStatistic = new SchArticleStatistic();
            objSchArticleStatistic.setClassId(ClientUtils.getClassId());
            objSchArticleStatistic.setDeleteFlag(false);
            objSchArticleStatistic.setArticleId(articleId);
            objSchArticleStatistic.setCompDegree(compDegree);
            objSchArticleStatistic.setOrgId(ClientUtils.getOrgId());
            objSchArticleStatistic.setUserId(ClientUtils.getUserId());
 
            TraceUtils.setCreateTrace(objSchArticleStatistic);
 
            this.save(objSchArticleStatistic);
        } else {
            this.bulkUpdate(
                    "update SchArticleStatistic set compDegree = ? where deleteFlag is false and userId = ? and articleId = ?",
                    new Object[] { compDegree, ClientUtils.getUserId(), articleId });
        }
 
        return new Result(true);
 
    }
 
    /**
     * 保存文章接口
     * 
     * @param article
     * @return
     */
    public Result saveArticle(SchArticle article) {
        Result result = new Result(false);
        result = this.save(article);
        String parentChapterId = null;
        if (StringUtils.isNotEmpty(article.getChapterId())) {
            SubjectChapter chapter = this.read(SubjectChapter.class, article.getChapterId());
            parentChapterId = chapter == null ? null : chapter.getParentChapterId();
        }
        boolean isClass = StringUtils.isNotEmpty(ClientUtils.getClassId())
                || StringUtils.isNotEmpty(article.getClassId());
        SchCourseware courseware = null;
        if (isClass) {
            courseware = this.findUnique("from SchCourseware where id = ? and classId = ? and deleteFlag is false",
                    CollectionUtils.newList(article.getArticleId(), article.getClassId()), SchCourseware.class);
        } else {
            courseware = this.findUnique(
                    "from SchCourseware where id = ? and orgId = ? and classId is null and deleteFlag is false",
                    CollectionUtils.newList(article.getArticleId(), ClientUtils.getOrgId()), SchCourseware.class);
 
            this.bulkUpdate("update SchCourseware set name = ? where id = ?",
                    new Object[] { article.getName(), article.getArticleId() });
        }
        // 同步课件表
        if (courseware == null) {
            courseware = new SchCourseware();
            courseware.setChapterId(article.getChapterId());
            courseware.setId(article.getArticleId());
            courseware.setCollegeCourseId(article.getCollegeCourseId());
            courseware.setcType(-1);
            courseware.setType(SchCourseware.COURSEWARE_TYPE_ARTICLE);
            courseware.setName(article.getName());
            courseware.setDeleteFlag(article.getDeleteFlag());
            courseware.setOrderNum(1000);
            courseware.setSubjectId(article.getSubjectId());
            courseware.setStatus(article.getStatus());
            courseware.setOrgId(ClientUtils.getOrgId());
            courseware.setClassId(article.getClassId());
            courseware.setParentChapterId(parentChapterId);
 
            TraceUtils.setCreateTrace(courseware);
        } else {
            courseware.setChapterId(article.getChapterId());
            // courseware.setCollegeCourseId(article.getCollegeCourseId());
            courseware.setcType(-1);
            courseware.setName(article.getName());
            courseware.setDeleteFlag(article.getDeleteFlag());
            courseware.setOrderNum(article.getOrderNum() == null ? 1000 : article.getOrderNum());
            courseware.setSubjectId(article.getSubjectId());
            courseware.setStatus(article.getStatus());
            courseware.setParentChapterId(parentChapterId);
 
            TraceUtils.setUpdateTrace(courseware);
        }
 
        this.save(courseware);
        return result;
    }
}