派生自 projectDept/qhighschool

Administrator
2022-12-14 1c38c12ef967b754dcbcfed69c8d7739003a69ca
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
package com.qxueyou.scc.operation.topic.service.impl;
 
import java.math.BigInteger;
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.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
 
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQuery;
import com.qxueyou.scc.admin.classes.model.ClsClass;
import com.qxueyou.scc.admin.classes.model.QClsClass;
import com.qxueyou.scc.admin.teacher.service.ITeacherService;
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.TraceUtils;
import com.qxueyou.scc.operation.comment.mode.Comment;
import com.qxueyou.scc.operation.comment.mode.CommentObject;
import com.qxueyou.scc.operation.comment.mode.CommentPraise;
import com.qxueyou.scc.operation.comment.mode.QComment;
import com.qxueyou.scc.operation.comment.mode.QCommentObject;
import com.qxueyou.scc.operation.comment.mode.QCommentPraise;
import com.qxueyou.scc.operation.comment.mode.QSnsCommentToMe;
import com.qxueyou.scc.operation.comment.mode.QSnsMyComment;
import com.qxueyou.scc.operation.comment.mode.QSnsMyPraise;
import com.qxueyou.scc.operation.comment.mode.QSnsPraiseToMe;
import com.qxueyou.scc.operation.comment.mode.SnsCommentToMe;
import com.qxueyou.scc.operation.comment.mode.SnsMyComment;
import com.qxueyou.scc.operation.comment.mode.SnsMyPraise;
import com.qxueyou.scc.operation.comment.mode.SnsPraiseToMe;
import com.qxueyou.scc.operation.topic.model.QTopicClassRe;
import com.qxueyou.scc.operation.topic.model.QTopicInfo;
import com.qxueyou.scc.operation.topic.model.TopicClassRe;
import com.qxueyou.scc.operation.topic.model.TopicInfo;
import com.qxueyou.scc.operation.topic.service.ITopicService;
import com.qxueyou.scc.user.model.QUser;
import com.qxueyou.scc.user.model.User;
import com.qxueyou.scc.user.service.IUserService;
 
/**
 * 话题服务层
 *
 * @author chenjunliang
 */
@Service
public class TopicService extends CommonAppService implements ITopicService {
 
    @Autowired
    IUserService userService;
 
    @Autowired
    ITeacherService teacherService;
 
    @Override
    public Result getTopicLst(String classId, String keyword, Integer limit, Integer pageNum) {
        String keyword_ = StringUtils.isBlank(keyword) ? "" : keyword;
        String userId = ClientUtils.getUserId();
 
        int topicCount = this.getTopicCount(keyword_, classId, userId);
        String hql = "select distinct t.topicId as topicId ,t.topicName as topicName, t.topicStatus as  status, t.creator as issuer, t.createId as issuerId ,"
                + "t.startTime as releaseTime,t.endTime as endTime , t.classId as classId, t.latestCommentName as latestCommentName ,"
                + "t.latestCommentTime as latestCommentTime,o.viewCount as viewCount ,o.commentCount as commentCount from TopicInfo t,TopicClassRe r ,CommentObject o ,StuStudent s "
                + "where t.deleteFlag is false and r.deleteFlag is false and o.deleteFlag is false  and t.topicId "
                + "=o.commentObjectUid and t.topicName like:keyword_ and o.commentObjectType = 5 and r.topicId = t.topicId and r.classId = s.classId and s.userId =:userId ";
        Map<String, Object> hqlMap = new HashMap<String, Object>(2);
        hqlMap.put("keyword_", "%" + keyword_ + "%");
        hqlMap.put("userId", userId);
        if (StringUtils.isNotBlank(classId)) {
            hql = hql.concat("and r.classId =:classId order by t.createTime desc");
            hqlMap.put("classId", classId);
        } else {
            hql = hql.concat(" order by t.createTime desc");
        }
 
        List<Map<String, Object>> topicLst = findListWithMapByHql(hql, hqlMap, new Pager(limit, pageNum));
 
        for (Map<String, Object> map : topicLst) {
            String topicId = (String) map.get("topicId");
            String findPeopleCountByHql = "select distinct c.commentterId as commentterId from CommentObject o,Comment c where o.deleteFlag is false and o.commentObjectUid =:topicId and"
                    + " c.commentObjectId =o.commentObjectId and o.commentObjectType = 5";
            List<Map<String, Object>> commentIdLst = findListWithMapByHql(findPeopleCountByHql,
                    CollectionUtils.newObjectMap("topicId", topicId));
            int peopleCount = commentIdLst.size();// 参与人数
            map.put("peopleCount", peopleCount);
        }
        return new Result(true, "success",
                CollectionUtils.newObjectMap("topicLst", topicLst, "topicCount", topicCount));
    }
 
    public int getTopicCount(String keyword, String classId, String userId) {
        Map<String, Object> map = CollectionUtils.newObjectMap("keyword", "%" + keyword + "%");
        String hql = "";
 
        if (userService.isAdmin()) {
            hql = "select distinct t.topicId from  TopicInfo t,TopicClassRe r ,CommentObject o ,ClsClass c where t.deleteFlag is false and r.deleteFlag is false and o.deleteFlag is false and r.topicId = t.topicId and t.topicId "
                    + "=o.commentObjectUid and t.topicName like:keyword and o.commentObjectType = 5 and r.classId =c.classId and c.deleteFlag is false and t.topicId = t.topicId";
        } else if (userService.isTeacher()) {
            hql = "select distinct t.topicId as topicId from"
                     + " TopicInfo t,TopicClassRe r ,CommentObject o ,Subject s, ClsClassReSubject re "
                     + "where t.deleteFlag is false and r.deleteFlag is false and o.deleteFlag is false and r.topicId = t.topicId and t.topicId "
                     + "=o.commentObjectUid and t.topicName like:keyword and o.commentObjectType = 5 and"
                     + " re.classId =r.classId and s.deleteFlag is false and re.deleteFlag is false and re.subjectId = s.subjectId and t.topicId = t.topicId and s.teacherId =:teacherId ";
            map.put("teacherId", userId);
        } else {
            hql = "select distinct t.topicId  from TopicInfo t,TopicClassRe r , StuStudent s where t.deleteFlag is false and t.topicName like:keyword "
                    + "and r.deleteFlag is false and r.classId = s.classId and  t.topicId =r.topicId and s.userId =:userId  ";
            map.put("userId", userId);
        }
        if (StringUtils.isNotBlank(classId)) {
            hql = hql.concat(" and r.classId =:classId");
            map.put("classId", classId);
        }
 
        List<Map<String, Object>> result = findListWithMapByHql(hql, map);
        int topicCount = result.size();
        return topicCount;
    }
 
    @Override
    public Result doTopicDetails(String topicId) {
        if (StringUtils.isBlank(topicId)) {
            return new Result(false, "error,topicId is null");
        }
        String hql = "from CommentObject where deleteFlag is false and commentObjectUid = ? and commentObjectType = 5";
        CommentObject commentObject = findUnique(hql, CollectionUtils.newList(topicId), CommentObject.class);
        BigInteger viewCount = commentObject.getViewCount();
        commentObject.setViewCount(viewCount.add(new BigInteger("1")));// 查看条数+1
        BigInteger viewCount2Json = commentObject.getViewCount();
        BigInteger commentPraiseCount = commentObject.getCommentPraiseCount();
        TraceUtils.setUpdateTrace(commentObject);
        save(commentObject);
        TopicInfo topic = this.read(TopicInfo.class, topicId);
        String[] classIds = topic.getClassId().split(",");
 
        Map<String, Object> argsMap = new HashMap<String, Object>(1);
        argsMap.put("classIds", classIds);
        List<Map<String, Object>> classMapLst = findListWithMapByHql(
                "select name as className, classId as classId from ClsClass where classId in (:classIds) and deleteFlag is false", argsMap);
 
        String findPeopleCountByHql = "select distinct c.commentterId as commentterId from CommentObject o,Comment c"
                + " where o.deleteFlag is false and o.commentObjectUid =:topicId and"
                + " c.commentObjectId =o.commentObjectId and o.commentObjectType = 5";
 
        List<Map<String, Object>> commentIdLst = findListWithMapByHql(findPeopleCountByHql,CollectionUtils.newObjectMap("topicId", topicId));
        int peopleCount = commentIdLst.size();// 参与人数
 
        return new Result(true, "success",
                CollectionUtils.newObjectMap("topicId", topicId,"topicType", topic.getTopicType(), "createTime", topic.getCreateTime(), "topicName", topic.getTopicName(), "issuer",
                        topic.getCreator(), "issuerId", topic.getCreateId(), "deadline", topic.getEndTime(),
                        "topicDesc", topic.getTopicDesc(), "classLst", classMapLst, "viewCount", viewCount2Json,
                        "commentCount", commentObject.getCommentCount(), "praiseCount", commentPraiseCount, "startTime", topic.getStartTime(), "endTime",
                        topic.getEndTime(), "peopleCount", peopleCount));
    }
 
    @Override
    public Result addTopicInfo(TopicInfo topicInfo) {
//        if(topicInfo.getTopicType()==TopicInfo.TOPIC_TYPE_CLS && StringUtils.isEmpty(topicInfo.getClassId())) {
        if(StringUtils.isEmpty(topicInfo.getClassId())) {
            return new Result(false, "请选择关联班级!");
        }
        String classId = topicInfo.getClassId();
        topicInfo.setTopicId(null);
        topicInfo.setDeleteFlag(false);
        topicInfo.setOrgId(ClientUtils.getOrgId());
        TraceUtils.setCreateTrace(topicInfo);
        if(topicInfo.getStartTime() == null){
            topicInfo.setStartTime(new Date());
        }
        save(topicInfo);
 
        if(StringUtils.isNotEmpty(classId)){
            String[] classIds = classId.split(",");
            TopicClassRe topicClassRe = null;
            for (int i = 0; i < classIds.length; i++) {
                topicClassRe = new TopicClassRe();
                topicClassRe.setClassId(classIds[i]);
                topicClassRe.setTopicId(topicInfo.getTopicId());
                TraceUtils.setCreateTrace(topicClassRe);
                save(topicClassRe);// 保存到中间表
            }
        }
 
        CommentObject commentObject = new CommentObject();
        commentObject.setCommentObjectUid(topicInfo.getTopicId());
        commentObject.setCommentObjectName(topicInfo.getTopicName());
        commentObject.setCommentObjectType(CommentObject.COMMENT_TYPE_INTERACT);
        commentObject.setCreateTime(new Date());
        TraceUtils.setCreateTrace(commentObject);
        BigInteger initParam =BigInteger.ZERO;
        commentObject.setCommentCount(initParam);
        commentObject.setCommentPraiseCount(initParam);
        commentObject.setViewCount(initParam);
        save(commentObject);// 创建评论对象
        return new Result(true, "add success");
    }
 
    @Override
    public Result lstCommonTopic(String keyword, Integer limit, Integer pageNum, String classId) {
        String keyword_ = StringUtils.isBlank(keyword) ? "" : keyword;
        String userId = ClientUtils.getUserId();
        int topicCount = this.getTopicCount(keyword_, classId, userId);
 
        String hql = "select distinct t.topicId as topicId ,t.topicName as topicName, t.topicStatus as  status, t.creator as issuer,"
                + "t.createTime as releaseTime,t.endTime as endTime,t.classId as classId, t.latestCommentName as latestCommentName ,"
                + "t.latestCommentTime as latestCommentTime,o.viewCount as viewCount ,o.commentCount as commentCount from TopicInfo t,TopicClassRe r ,CommentObject o ,ClsClass c "
                + "where t.deleteFlag is false and r.deleteFlag is false and o.deleteFlag is false and r.topicId = t.topicId and t.topicId "
                + "=o.commentObjectUid and t.topicName like:keyword_ and o.commentObjectType = 5 and r.classId =c.classId and c.deleteFlag is false and t.topicId = t.topicId  ";
        Map<String, Object> hqlMap = new HashMap<String, Object>(2);
        hqlMap.put("keyword_", "%" + keyword_ + "%");
        if (StringUtils.isNotBlank(classId)) {
            hql = hql.concat("and r.classId =:classId  order by t.createTime desc ");
            hqlMap.put("classId", classId);
        } else {
            hql = hql.concat("order by  t.createTime desc ");
        }
 
        List<Map<String, Object>> topicLst = findListWithMapByHql(hql, hqlMap, new Pager(limit, pageNum));
 
        return new Result(true, "success",
                CollectionUtils.newObjectMap("commonTopicLst", topicLst, "commonTopicCount", topicCount));
    }
 
    public Result getClassForum() {
 
        String hql = "select distinct c.classId as classId ,c.name as className ,c.imgPath as coverUrl from ClsClass c ,TopicClassRe r "
                + "where c.classId = r.classId  and c.deleteFlag is false and r.deleteFlag is false  ";
        Map<String, Object> args = CollectionUtils.newObjectMap();
        if (!userService.isAdmin()) {
            hql = "select distinct c.classId as classId ,c.name as className ,c.imgPath as coverUrl from ClsClass c ,TopicClassRe r ,StuStudent s "
                    + "where c.classId = r.classId  and c.deleteFlag is false and r.deleteFlag is false and s.classId = c.classId and s.userId =:userId";
            args.put("userId", ClientUtils.getUserId());
        }
        List<Map<String, Object>> classLst = findListWithMapByHql(hql, args);
        for (Map<String, Object> map : classLst) {
            String classId = (String) map.get("classId");
            int topicCount = findCountByComplexHql(
                    "select count(1) from TopicClassRe  where deleteFlag is false and classId =:classId",
                    CollectionUtils.newObjectMap("classId", classId));
            map.put("topicCount", topicCount);
            String CommentCountByHql = "select count(1)  from TopicClassRe r,CommentObject o , Comment c where "
                    + "r.classId =:classId and r.deleteFlag is false and r.topicId = o.commentObjectUid  and "
                    + "c.commentObjectId = o.commentObjectId";
 
            int commentCount = findCountByComplexHql(CommentCountByHql,
                    CollectionUtils.newObjectMap("classId", classId));
            map.put("commentCount", commentCount);
 
        }
        return new Result(true, "success", classLst);
    }
 
    @Override
    public Result getClassForumDetail(String classId) {
        if (StringUtils.isBlank(classId)) {
            return new Result(false, "classId not null");
        }
        ClsClass clsClass = read(ClsClass.class, classId);
        Map<String, Object> mapJson = new HashMap<String, Object>();
        String name = clsClass.getName();
        String imgPath = clsClass.getImgPath();
        int topicCount = findCountByComplexHql(// 获取话题条数
                "select count(1) from TopicClassRe r  where deleteFlag is false and classId =:classId",
                CollectionUtils.newObjectMap("classId", classId));
        String findTopicIdLst = "select topicId as topicId from TopicClassRe where deleteFlag is false and classId=:classId";
        List<Map<String, Object>> topicIdLst = findListWithMapByHql(findTopicIdLst, // 获取班级所有话题的id
                CollectionUtils.newObjectMap("classId", classId));
        int commentCount = 0;
        int studentCommentCount = 0;
        for (Map<String, Object> map : topicIdLst) {
            String topicId = (String) map.get("topicId");
            CommentObject commentObject = findUnique(// 获取话题的评论条数
                    "from CommentObject where deleteFlag is false and commentObjectUid = ? and commentObjectType = 5",
                    CollectionUtils.newList(topicId), CommentObject.class);
            BigInteger BigcommentCount = commentObject.getCommentCount();
            int count = BigcommentCount.intValue();
            commentCount += count;
            List<Map<String, Object>> lstCommentId = findListWithMapByHql(// 获取回复话题的评论人数
                    "select distinct c.commentterId as  commentterId from CommentObject o,Comment c where o.deleteFlag is false and o.commentObjectUid =:topicId and"
                            + " c.commentObjectId =o.commentObjectId and o.commentObjectType = 5",
                    CollectionUtils.newObjectMap("topicId", topicId));
            studentCommentCount += lstCommentId.size();
        }
        mapJson.put("commentCount", commentCount);
        mapJson.put("imgPath", imgPath);
        mapJson.put("className", name);
        mapJson.put("topicCount", topicCount);
        mapJson.put("peopleCount", studentCommentCount);
        return new Result(true, "success", mapJson);
    }
 
    @Override
    public Result getCommentLst(String topicId, Integer pageSize, Integer pageNum) {
        String hql = "select c.commentId as commentId ,c.commentter as userName ,c.content as commentContent"
                + " ,c.createTime as commentTime ,o.commentCount as commentCount ,c.commentParentId as commentParentId,c.commentterId as commentterId from Comment c "
                + ",CommentObject o where c.deleteFlag is false and o.deleteFlag is false "
                + " and c.commentObjectId=o.commentObjectId and o.commentObjectUid =:topicId order by c.createTime desc";
        List<Map<String, Object>> commentLst = findListWithMapByHql(hql,
                CollectionUtils.newObjectMap("topicId", topicId), new Pager(pageSize, pageNum));
        int commentCount = findCountByComplexHql(// 获取总条数
                "select count(1) from Comment c ,CommentObject o where c.deleteFlag is false and o.deleteFlag is false"
                + "  and c.commentObjectId=o.commentObjectId and o.commentObjectUid =:topicId and o.commentObjectType = 5",
                CollectionUtils.newObjectMap("topicId", topicId));
 
        for (Map<String, Object> map : commentLst) {
            String userId = (String) map.get("commentterId");
            String commentId = (String) map.get("commentId");
            String commentParentId = (String) map.get("commentParentId");
            User user = read(User.class, userId);
            String imgPath = user.getImgPath();
            map.put("imgPath", imgPath);
            // 获取每条评论的点赞数 ;
            String findPraiseCountByHql = "select count(1) from CommentPraise where deleteFlag is false and commentId =:commentId";
            int praiseCount = findCountByComplexHql(findPraiseCountByHql,
                    CollectionUtils.newObjectMap("commentId", commentId));
            map.put("praiseCount", praiseCount);
            map.put("commentCount", findCountByComplexHql("select count(1) from Comment where deleteFlag is false and commentParentId =:commentId",
                    CollectionUtils.newObjectMap("commentId", commentId)));
            // 获取回复这个评论的自评论内容
            String findSubCommentDetailByhql = "select commentter as commentter ,content as commentContent ,createTime as commentTime from  Comment "
                    + " where deleteFlag is false and  commentId =:commentParentId ";
            List<Map<String, Object>> subCommentDetail = findListWithMapByHql(findSubCommentDetailByhql,
                    CollectionUtils.newObjectMap("commentParentId", commentParentId));
 
            if (!subCommentDetail.isEmpty()) {
                map.put("subCommentDetail", subCommentDetail);
            }
        }
        return new Result(true, "success",
                CollectionUtils.newObjectMap("commentLst", commentLst, "commentCount", commentCount));
    }
 
    @Override
    public Result myTopic(Integer pageSize, Integer pageNum) {
        String userId = ClientUtils.getUserId();
        String findTopicLstByHql = "select  c.commentObjectId as commentObjectId, t.topicId as topicId ,t.topicName as topicName ,t.createTime as releaseTime, "
                + "t.endTime as endTime ,c.commentCount as commentCount ,c.viewCount as viewCount ,t.latestCommentName as latestCommentName ,t.latestCommentTime as"
                + " latestCommentTime from TopicInfo t ,CommentObject c where t.deleteFlag is false and c.commentObjectType = 5 and "
                + "c.deleteFlag is false and t.createId =:userId and c.commentObjectUid = t.topicId";
        int myTopicCount = findCountByComplexHql(
                "select count(1) from TopicInfo t ,CommentObject c where t.deleteFlag is false and c.commentObjectType = 5 and "
                        + "c.deleteFlag is false and t.createId =:userId and c.commentObjectUid = t.topicId ",
                CollectionUtils.newObjectMap("userId", userId));
        List<Map<String, Object>> myTopicLst = findListWithMapByHql(findTopicLstByHql,
                CollectionUtils.newObjectMap("userId", userId), new Pager(pageSize, pageNum));
        return new Result(true, "success",
                CollectionUtils.newObjectMap("myTopicCount", myTopicCount, "myTopicLst", myTopicLst));
    }
 
    @Override
    public Result myComment(Integer pageSize, Integer pageNum) {
        String userId = ClientUtils.getUserId();
        String findMyCommentLstByHql = "select t.topicId as topicId ,t.topicName as topicName , c.commentId as commentId ,c.content as commentContent ,"
                + " c.commentedName as commentName ,c.createTime as "
                + "commentTime from Comment c ,CommentObject o,TopicInfo t where c.deleteFlag is false and t.deleteFlag is false and "
                + "c.commentterId =:userId and c.commentObjectId =o.commentObjectId and o.commentObjectUid =t.topicId and o.commentObjectType = 5";
        int commentCount = findCountByComplexHql(
                "select count(1) from Comment c ,CommentObject o,TopicInfo t where c.deleteFlag is false and t.deleteFlag is false and "
                        + "c.commentterId =:userId and c.commentObjectId =o.commentObjectId and o.commentObjectUid =t.topicId and o.commentObjectType = 5",
                CollectionUtils.newObjectMap("userId", userId));
        List<Map<String, Object>> commentLst = findListWithMapByHql(findMyCommentLstByHql,
                CollectionUtils.newObjectMap("userId", userId), new Pager(pageSize, pageNum));
        for (Map<String, Object> map : commentLst) {
            String commentId = (String) map.get("commentId");
            int praiseCount = findCountByComplexHql(
                    "select count(1) from CommentPraise  where deleteFlag is false and commentId =:commentId",
                    CollectionUtils.newObjectMap("commentId", commentId));
            map.put("praiseCount", praiseCount);
        }
        return new Result(true, "success",
                CollectionUtils.newObjectMap("commentLst", commentLst, "commentCount", commentCount));
    }
 
    @Override
    public Result appMyComment(String userId, Pager pager) {
        QSnsMyComment qSnsMyComment = QSnsMyComment.snsMyComment;
 
        JPAQuery<SnsMyComment> query = this.getQueryFactory().
                selectFrom(qSnsMyComment).where(qSnsMyComment.createId.eq(userId));
 
        List<SnsMyComment> lstSnsMyComment = query.orderBy(qSnsMyComment.commentTime.desc())
                .offset(pager.getOffset()).limit(pager.getPageSize()).fetch();
 
        long commentCount = query.fetchCount();
 
        return new Result(true, "success",
                CollectionUtils.newObjectMap("dataList", lstSnsMyComment, "count", commentCount));
    }
 
    @Override
    public Result commentToMe(String userId, Pager pager) {
        QSnsCommentToMe qSnsCommentToMe = QSnsCommentToMe.snsCommentToMe;
 
        JPAQuery<SnsCommentToMe> query = this.getQueryFactory().
                selectFrom(qSnsCommentToMe).where(qSnsCommentToMe.createId.eq(userId));
 
        List<SnsCommentToMe> lstSnsMyComment = query.orderBy(qSnsCommentToMe.commentTime.desc())
                .offset(pager.getOffset()).limit(pager.getPageSize()).fetch();
 
        long commentCount = query.fetchCount();
 
        return new Result(true, "success",
                CollectionUtils.newObjectMap("dataList", lstSnsMyComment, "count", commentCount));
    }
 
    @Override
    public Result praiseToMe(String userId, Pager pager) {
        QSnsPraiseToMe qSnsPraiseToMe = QSnsPraiseToMe.snsPraiseToMe;
 
        JPAQuery<SnsPraiseToMe> query = this.getQueryFactory().
                selectFrom(qSnsPraiseToMe).where(qSnsPraiseToMe.createId.eq(userId));
 
        List<SnsPraiseToMe> lstSnsMyComment = query.orderBy(qSnsPraiseToMe.praiseTime.desc())
                .offset(pager.getOffset()).limit(pager.getPageSize()).fetch();
 
        long commentCount = query.fetchCount();
 
        return new Result(true, "success",
                CollectionUtils.newObjectMap("dataList", lstSnsMyComment, "count", commentCount));
    }
 
 
    @Override
    public Result appMyPraise(String userId, Pager pager) {
        QSnsMyPraise qSnsMyPraise = QSnsMyPraise.snsMyPraise;
 
        JPAQuery<SnsMyPraise> query = this.getQueryFactory().
                selectFrom(qSnsMyPraise).where(qSnsMyPraise.createId.eq(userId));
 
        List<SnsMyPraise> lstSnsMyComment = query.orderBy(qSnsMyPraise.praiseTime.desc())
                .offset(pager.getOffset()).limit(pager.getPageSize()).fetch();
 
        long commentCount = query.fetchCount();
 
        return new Result(true, "success",
                CollectionUtils.newObjectMap("dataList", lstSnsMyComment, "count", commentCount));
    }
 
    @Override
    public Result deleteMycomment(String commentId) {
        Comment comment = read(Comment.class, commentId);
        if(!comment.getCreateId().equals(ClientUtils.getUserId())) {
            return new Result(false, "只能删除自己发布的信息");
        }
        comment.setDeleteFlag(true);
        TraceUtils.setCreateTrace(comment);
        save(comment);// 删除评论表的
 
        String[] commentIds = commentId.split(",");
        this.bulkUpdateInLoop("update CommentPraise set deleteFlag = true where commentId = ?", commentIds);// 删除对应的点赞
 
        return new Result(true, "success");
    }
 
    @Override
    public Result getCommentLstToMe(Integer pageSize, Integer pageNum) {
        String hql = "select t.topicId as topicId , t.topicName as topicName ,t.startTime as startTime ,t.endTime as endTime , c.commentId as commentId , c.commentter as userName , "
                + "c.commentterId as userId ,u.imgPath as imgPath , o.commentCount as commentCount , "
                + "c.createTime as commentTime , c.content as commentContent from Comment c , CommentObject o ,"
                + "TopicInfo t ,User u where c.commentedId =:userId and c.deleteFlag is false and o.deleteFlag is false "
                + " and t.deleteFlag is false  and  c.commentObjectId = o.commentObjectId and o.commentObjectUid = t.topicId "
                + "and o.commentObjectType = 5 and u.userId = c.commentterId and u.deleteFlag is false ";
        List<Map<String, Object>> commentLst = findListWithMapByHql(hql,
                CollectionUtils.newObjectMap("userId", ClientUtils.getUserId()), new Pager(pageSize, pageNum));
        for (Map<String, Object> map : commentLst) {
            String commentId = (String) map.get("commentId");
            int praiseCount = findCountByComplexHql(
                    "select count(1) from CommentPraise  where deleteFlag is false and commentId =:commentId",
                    CollectionUtils.newObjectMap("commentId", commentId));
            map.put("praiseCount", praiseCount);
        }
 
        int commentCount = findCountByComplexHql("select count(1)  from  Comment c , CommentObject o ,"
                        + "TopicInfo t where c.commentedId =:userId and c.deleteFlag is false and o.deleteFlag is false "
                        + " and t.deleteFlag is false  and c.commentObjectId = o.commentObjectId and o.commentObjectUid = t.topicId and o.commentObjectType = 5",
                CollectionUtils.newObjectMap("userId", ClientUtils.getUserId()));
        return new Result(true, "success",
                CollectionUtils.newObjectMap("commentLst", commentLst, "commentCount", commentCount));
    }
 
    @Override
    public Result getPraiseLst(Integer pageSize, Integer pageNum) {
        String hql = "select  u.imgPath as imgPath ,c.commentedName as commentName , c.content as commentContent ,p.createTime as reTime , "
                + "  t.topicId as topicId , t.topicName as topicName ,p.commentter as praiseName from Comment c , CommentPraise p ,"
                + " CommentObject o , TopicInfo t , User u where c.commentId = p.commentId and  c.commentterId =:userId and"
                + "  c.commentObjectId = o.commentObjectId and o.commentObjectUid = t.topicId and u.userId = p.commentterId and o.commentObjectType = 5";
 
        int praiseCount = findCountByComplexHql("select count(1) from Comment c , CommentPraise p ,"
                        + " CommentObject o , TopicInfo t , User u where c.commentId = p.commentId and  c.commentterId =:userId and"
                        + "  c.commentObjectId = o.commentObjectId and o.commentObjectUid = t.topicId and u.userId =p.commentterId and o.commentObjectType = 5",
                CollectionUtils.newObjectMap("userId", ClientUtils.getUserId()));
        List<Map<String, Object>> praiseLst = findListWithMapByHql(hql,
                CollectionUtils.newObjectMap("userId", ClientUtils.getUserId()), new Pager(pageSize, pageNum));
        return new Result(true, "success",
                CollectionUtils.newObjectMap("praiseLst", praiseLst, "praiseCount", praiseCount));
    }
 
    @Override
    public Result addComment(String content, String commentObjectId, String commentParentId, String commentedId,
                             String commentedName) {
        if (StringUtils.isBlank(commentObjectId)) {
            return new Result(false, " commentObjectId is null");
        }
        String hql = "from CommentObject where commentObjectUid=? and deleteFlag is false and commentObjectType = 5";
        CommentObject commentObject = findUnique(hql, CollectionUtils.newList(commentObjectId), CommentObject.class);
        BigInteger commentCount = commentObject.getCommentCount();
        commentObject.setCommentCount(commentCount.add(new BigInteger("1")));// 评论+1;
        TraceUtils.setUpdateTrace(commentObject);
        save(commentObject);
        Comment comment = new Comment();
        comment.setCommentedId(commentedId);
        comment.setCommentedName(commentedName);
        comment.setContent(content);
        comment.setCommentObjectId(commentObject.getCommentObjectId());
        comment.setCommentter(ClientUtils.getUserName());
        comment.setCommentterId(ClientUtils.getUserId());
        comment.setDeleteFlag(false);
        comment.setCommentCount(BigInteger.ZERO);
        comment.setParPraiseCount(BigInteger.ZERO);
        TraceUtils.setCreateTrace(comment);
        if (StringUtils.isBlank(commentParentId)) {
            TopicInfo topicInfo = read(TopicInfo.class, commentObjectId);
            topicInfo.setLatestCommentTime(new Date());
            topicInfo.setLatestCommentName(ClientUtils.getUserName());
            TraceUtils.setUpdateTrace(topicInfo);
            save(topicInfo);
        } else {
            comment.setCommentParentId(commentParentId);
 
            this.bulkUpdate("update Comment set commentCount = commentCount + 1 where commentId = ?", new Object[] {commentParentId});
        }
        save(comment);
        return new Result(true, "success");
    }
 
    @Override
    public Result addPraise(String commentId, String commentObjectId) {
        if (StringUtils.isBlank(commentId)) {// 点赞话题
            String hql = "from CommentObject where deleteFlag is false and commentObjectUid = ? and commentObjectType = 5";
            CommentObject commentObject = findUnique(hql, CollectionUtils.newList(commentObjectId),
                    CommentObject.class);
            BigInteger commentPraiseCount = commentObject.getCommentPraiseCount();
            BigInteger one = new BigInteger("1");
            commentObject.setCommentPraiseCount(commentPraiseCount.add(one));// 点赞数+1
            TraceUtils.setCreateTrace(commentObject);
            save(commentObject);
 
        }else {
            this.bulkUpdate("update Comment set commentPraiseCount = commentPraiseCount + 1 where commentId = ?", new Object[] {commentId});
        }
 
        CommentPraise commentPraise = new CommentPraise();
        commentPraise.setCommentId(commentId);
        commentPraise.setCommentObjectId(commentObjectId);
        commentPraise.setDeleteFlag(false);
        commentPraise.setCommentterId(ClientUtils.getUserId());
        commentPraise.setCommentter(ClientUtils.getUserName());
        TraceUtils.setCreateTrace(commentPraise);
        save(commentPraise);// 保存到点赞表
 
        return new Result(true, "success");
    }
 
    public Result commentDetail(String userId) {
        QTopicInfo qTopicInfo = QTopicInfo.topicInfo;
        QCommentObject qCommentObject = QCommentObject.commentObject;
        QComment qComment = QComment.comment;
        QCommentPraise qCommentPraise = QCommentPraise.commentPraise;
        //我发的讨论数量
        long topicCount = this.getQueryFactory().selectFrom(qTopicInfo).where(qTopicInfo.deleteFlag.isFalse().and(qTopicInfo.createId.eq(userId))).fetchCount();
 
        //我发的讨论的评论数
        long topicCommentToMeCount = this.getQueryFactory().select(qCommentObject.commentCount.sum()).from(qCommentObject)
                .where(qCommentObject.deleteFlag.isFalse().and(qCommentObject.createId.eq(userId))
                        .and(qCommentObject.commentObjectType.eq(CommentObject.COMMENT_TYPE_INTERACT)))
                .fetchCount();
        //我发的评论的评论数
        long commentToMeCount = this.getQueryFactory().selectDistinct(qComment.commentId).from(qCommentObject, qComment)
                .where(qComment.deleteFlag.isFalse().and(qCommentObject.deleteFlag.isFalse())
                        .and(qCommentObject.createId.eq(userId)).and(qCommentObject.commentObjectUid.eq(qComment.commentObjectId))
                        .and(qCommentObject.commentObjectType.eq(CommentObject.COMMENT_TYPE_INTERACT)).and(qComment.commentParentId.isNotNull()))
                .fetchCount();
 
        //我评论的讨论的评论数
        long topicCommentCount = this.getQueryFactory().selectDistinct(qComment.commentId).from(qCommentObject, qComment)
                .where(qComment.deleteFlag.isFalse().and(qCommentObject.deleteFlag.isFalse())
                        .and(qComment.createId.eq(userId)).and(qCommentObject.commentObjectUid.eq(qComment.commentObjectId))
                        .and(qCommentObject.commentObjectType.eq(CommentObject.COMMENT_TYPE_INTERACT)).and(qComment.commentParentId.isNotNull()))
                .fetchCount();
        //我评论的数
        long commentCount = this.getQueryFactory().selectDistinct(qComment.commentId).from(qCommentObject, qComment)
                .where(qComment.deleteFlag.isFalse().and(qCommentObject.deleteFlag.isFalse())
                        .and(qComment.createId.eq(userId)).and(qCommentObject.commentObjectUid.eq(qComment.commentObjectId))
                        .and(qCommentObject.commentObjectType.eq(CommentObject.COMMENT_TYPE_INTERACT)).and(qComment.commentParentId.isNotNull()))
                .fetchCount();
        //我对讨论的点赞数
        long topicPraise = this.getQueryFactory().selectDistinct(qCommentPraise.commentPraiseId).from(qCommentObject, qCommentPraise)
                .where(qCommentObject.deleteFlag.isFalse().and(qCommentPraise.createId.eq(userId))
                        .and(qCommentPraise.commentObjectId.eq(qCommentObject.commentObjectId)).and(qCommentPraise.deleteFlag.isFalse())
                        .and(qCommentObject.commentObjectType.eq(CommentObject.COMMENT_TYPE_INTERACT)))
                .fetchCount();
        //我对评论的点赞数
        long commentPraise = this.getQueryFactory().selectDistinct(qCommentPraise.commentPraiseId).from(qCommentObject, qComment, qCommentPraise)
                .where(qComment.deleteFlag.isFalse().and(qCommentObject.deleteFlag.isFalse()).and(qCommentPraise.commentId.eq(qComment.commentId))
                        .and(qCommentPraise.createId.eq(userId)).and(qCommentObject.commentObjectUid.eq(qComment.commentObjectId)).and(qCommentPraise.deleteFlag.isFalse())
                        .and(qCommentObject.commentObjectType.eq(CommentObject.COMMENT_TYPE_INTERACT)))
                .fetchCount();
        //我的讨论的点赞数
        long topicPraiseToMe = this.getQueryFactory().select(qCommentObject.commentPraiseCount.sum()).from(qCommentObject)
                .where(qCommentObject.deleteFlag.isFalse().and(qCommentObject.createId.eq(userId))
                        .and(qCommentObject.commentObjectType.eq(CommentObject.COMMENT_TYPE_INTERACT)))
                .fetchCount();
        //我的讨论的点赞数
        long commentPraiseToMe = this.getQueryFactory().selectDistinct(qCommentPraise.commentPraiseId).from(qCommentObject, qComment, qCommentPraise)
                .where(qComment.deleteFlag.isFalse().and(qCommentObject.deleteFlag.isFalse()).and(qCommentPraise.commentId.eq(qComment.commentId))
                        .and(qComment.createId.eq(userId)).and(qCommentObject.commentObjectUid.eq(qComment.commentObjectId)).and(qCommentPraise.deleteFlag.isFalse())
                        .and(qCommentObject.commentObjectType.eq(CommentObject.COMMENT_TYPE_INTERACT)))
                .fetchCount();
 
        return new Result(true, "success",
                CollectionUtils.newObjectMap("topicCount", topicCount, "topicCommentToMeCount", topicCommentToMeCount,
                        "commentToMeCount", commentToMeCount, "topicCommentCount", topicCommentCount, "commentCount",
                        commentCount, "topicPraise", topicPraise, "commentPraise", commentPraise,
                        "topicPraiseToMe", topicPraiseToMe, "commentPraiseToMe", commentPraiseToMe));
    }
 
    @Override
    public Result deleteTopic(String topicId) {
        String[] topicIds = topicId.split(",");
 
        List<TopicInfo> topicInfoLst = findByComplexHql(
                "from TopicInfo where deleteFlag is false and topicId in(:topicIds)",
                CollectionUtils.newObjectMap("topicIds", topicIds), TopicInfo.class);
        for (TopicInfo topicInfo : topicInfoLst) {
            topicInfo.setDeleteFlag(true);
            TraceUtils.setUpdateTrace(topicInfo);
            save(topicInfo);
        }
 
        bulkUpdateInLoop("update Comment set deleteFlag = true where commentId = ?", topicIds);// 删除话题对应的评论;
 
        bulkUpdateInLoop("update TopicClassRe set deleteFlag = true where topicId = ?", topicIds);// 删除话题对应的班级;
 
        return new Result(true, "success");
    }
 
    @Override
    public Result getTopicLstOfAdmin(String classId, Integer pageSize, Integer pageNum, String keyword) {
 
        String keyword_ = StringUtils.isBlank(keyword) ? "" : keyword;
 
        int topicCount = this.getTopicCount(keyword_, classId, null);
        String hql = "select distinct t.topicId as topicId ,t.topicName as topicName, t.topicStatus as  status, t.creator as issuer,t.createId as issuerId ,"
                + "t.startTime as releaseTime,t.endTime as endTime,t.classId as classId, t.latestCommentName as latestCommentName ,"
                + "t.latestCommentTime as latestCommentTime,o.viewCount as viewCount ,o.commentCount as commentCount from TopicInfo t,TopicClassRe r ,CommentObject o ,ClsClass c "
                + "where t.deleteFlag is false and r.deleteFlag is false and o.deleteFlag is false and r.topicId = t.topicId and t.topicId "
                + "=o.commentObjectUid and t.topicName like:keyword_ and o.commentObjectType = 5 and r.classId =c.classId and c.deleteFlag is false and t.topicId = t.topicId  ";
        Map<String, Object> hqlMap = new HashMap<String, Object>(2);
        hqlMap.put("keyword_", "%" + keyword_ + "%");
        if (StringUtils.isNotBlank(classId)) {
            hql = hql.concat("and r.classId =:classId order by t.createTime desc");
            hqlMap.put("classId", classId);
        } else {
            hql = hql.concat(" order by t.createTime desc");
        }
 
        List<Map<String, Object>> topicLst = findListWithMapByHql(hql, hqlMap, new Pager(pageSize, pageNum));
 
        for (Map<String, Object> map : topicLst) {
            String topicId = (String) map.get("topicId");
            String findPeopleCountByHql = "select distinct c.commentterId as commentterId from CommentObject o,Comment c where o.deleteFlag is false and o.commentObjectUid =:topicId and"
                    + " c.commentObjectId =o.commentObjectId and o.commentObjectType = 5";
            List<Map<String, Object>> commentIdLst = findListWithMapByHql(findPeopleCountByHql,
                    CollectionUtils.newObjectMap("topicId", topicId));
            int peopleCount = commentIdLst.size();// 参与人数
            map.put("peopleCount", peopleCount);
        }
        return new Result(true, "success",
                CollectionUtils.newObjectMap("topicLst", topicLst, "topicCount", topicCount));
    }
 
    @Override
    public Result updateTopicInfo(TopicInfo editTopicInfo) {
 
//        if(editTopicInfo.getTopicType()==TopicInfo.TOPIC_TYPE_CLS && StringUtils.isEmpty(editTopicInfo.getClassId())) {
        if(StringUtils.isEmpty(editTopicInfo.getClassId())) {
            return new Result(false, "请选择关联班级!");
        }
 
        TopicInfo topicInfo = read(TopicInfo.class, editTopicInfo.getTopicId());
        this.bulkUpdate("update TopicClassRe set deleteFlag = true where topicId = ?", new Object[] {topicInfo.getTopicId()});// 删除对应的中间表数据
        topicInfo.setTopicName(editTopicInfo.getTopicName());
        topicInfo.setTopicType(editTopicInfo.getTopicType());
        topicInfo.setTopicDesc(editTopicInfo.getTopicDesc());
        topicInfo.setStartTime(editTopicInfo.getStartTime());
        topicInfo.setEndTime(editTopicInfo.getEndTime());
        topicInfo.setClassId(editTopicInfo.getClassId());
        topicInfo.setDeleteFlag(false);
        TraceUtils.setUpdateTrace(topicInfo);
        save(topicInfo);
 
        if(StringUtils.isNotEmpty(topicInfo.getClassId())){
            String[] classIds = topicInfo.getClassId().split(",");
            TopicClassRe topicClassRe = null;
            for (int i = 0; i < classIds.length; i++) {
                topicClassRe = new TopicClassRe();
                topicClassRe.setClassId(classIds[i]);
                topicClassRe.setTopicId(editTopicInfo.getTopicId());
                TraceUtils.setCreateTrace(topicClassRe);
                save(topicClassRe);// 保存到中间表
            }
        }
        return new Result(true, "success");
    }
 
    @Override
    public Result getTopicLstOfTeacher(String classId, Integer pageSize, Integer pageNum, String keyword) {
        String keyword_ = StringUtils.isBlank(keyword) ? "" : keyword;
        String teacherId = teacherService.getTeacherIdByUserId(ClientUtils.getUserId());
        int topicCount = this.getTopicCount(keyword_, classId, teacherId);
        String hql = "select distinct t.topicId as topicId ,t.topicName as topicName, t.topicStatus as  status, t.creator as issuer,t.createId as issuerId ,"
                + "t.startTime as releaseTime,t.endTime as endTime,t.classId as classId, t.latestCommentName as latestCommentName ,"
                + "t.latestCommentTime as latestCommentTime,o.viewCount as viewCount ,o.commentCount as commentCount from"
                + " TopicInfo t,TopicClassRe r ,CommentObject o ,Subject s, ClsClassReSubject re "
                + "where t.deleteFlag is false and r.deleteFlag is false and o.deleteFlag is false and r.topicId = t.topicId and t.topicId "
                + "=o.commentObjectUid and t.topicName like:keyword_ and o.commentObjectType = 5 and"
                + " re.classId =r.classId and s.deleteFlag is false and re.deleteFlag is false and re.subjectId = s.subjectId and t.topicId = t.topicId and s.teacherId =:teacherId ";
        Map<String, Object> hqlMap = new HashMap<String, Object>(2);
        hqlMap.put("keyword_", "%" + keyword_ + "%");
        hqlMap.put("teacherId", teacherId);
        if (StringUtils.isNotBlank(classId)) {
            hql = hql.concat("and r.classId =:classId order by t.createTime desc");
            hqlMap.put("classId", classId);
        } else {
            hql = hql.concat(" order by t.createTime desc");
        }
 
        List<Map<String, Object>> topicLst = findListWithMapByHql(hql, hqlMap, new Pager(pageSize, pageNum));
 
        for (Map<String, Object> map : topicLst) {
            String topicId = (String) map.get("topicId");
            String findPeopleCountByHql = "select distinct c.commentterId as commentterId from CommentObject o,Comment c where o.deleteFlag is false and o.commentObjectUid =:topicId and"
                    + " c.commentObjectId =o.commentObjectId and o.commentObjectType = 5";
            List<Map<String, Object>> commentIdLst = findListWithMapByHql(findPeopleCountByHql,
                    CollectionUtils.newObjectMap("topicId", topicId));
            int peopleCount = commentIdLst.size();// 参与人数
            map.put("peopleCount", peopleCount);
        }
        return new Result(true, "success",
                CollectionUtils.newObjectMap("topicLst", topicLst, "topicCount", topicCount));
 
    }
 
    /**
     * 获取班级讨论数量
     *
     * @param classId
     * @return
     */
    public long getClassTopicCount(String classId) {
        if(StringUtils.isEmpty(classId)){
            return 0L;
        }
 
        QTopicInfo qTopicInfo = QTopicInfo.topicInfo;
        QTopicClassRe qTopicClassRe = QTopicClassRe.topicClassRe;
        QCommentObject qCommentObject = QCommentObject.commentObject;
        QClsClass qClsClass = QClsClass.clsClass;
        QUser qUser = QUser.user;
 
        JPAQuery<Long> query = this.getQueryFactory().select(qTopicInfo.topicId.countDistinct()).from(qTopicInfo, qTopicClassRe, qCommentObject, qClsClass, qUser )
                .where(qTopicInfo.deleteFlag.isFalse().and(qTopicClassRe.deleteFlag.isFalse()).and(qUser.userId.eq(qTopicInfo.createId))
                        .and(qCommentObject.deleteFlag.isFalse()).and(qTopicClassRe.topicId.eq(qTopicInfo.topicId))
                        .and(qTopicClassRe.classId.eq(qClsClass.classId)).and(qClsClass.classId.eq(classId))
                        .and(qTopicInfo.startTime.isNull().or(qTopicInfo.startTime.before(new Date()).and(qTopicInfo.endTime.after(new Date()))))
                        .and(qCommentObject.commentObjectUid.eq(qTopicInfo.topicId)).and(
                                qTopicInfo.createTime.after(new Date(new Date().getTime() - 1000L * 60 * 60 * 24 * 30 * 12)))
                        .and(qCommentObject.commentObjectType.eq(CommentObject.COMMENT_TYPE_INTERACT))).groupBy(qTopicInfo.topicId);
 
        return query.fetchCount();
    }
 
    /**
     * 获取热门话题列表
     *
     * type 1热门,2最近
     * scope 1班级,2机构
     *
     */
    @CachePut(value = "topic_list", key = "#classId+#keyword+#limit+#pageNum+#type+#scope")
    public Map<String, Object> getHotTopicLst(String classId, String keyword, Integer limit, Integer pageNum, int type, int scope){
        QTopicInfo qTopicInfo = QTopicInfo.topicInfo;
        QTopicClassRe qTopicClassRe = QTopicClassRe.topicClassRe;
        QCommentObject qCommentObject = QCommentObject.commentObject;
        QClsClass qClsClass = QClsClass.clsClass;
        QUser qUser = QUser.user;
 
        //热门取最近30天内,否则取1年内
        long time = type == 1 ? 1000L * 60 * 60 * 24 * 30 : 1000L * 60 * 60 * 24 * 30 * 12;
 
        //讨论的范围
        Predicate scopeRight = scope == 1?qClsClass.classId.eq(classId):qClsClass.orgId.eq(ClientUtils.getOrgId());
 
        @SuppressWarnings("rawtypes")
        //排序方式
        OrderSpecifier order = type == 1?new OrderSpecifier<BigInteger>(Order.DESC, qCommentObject.commentCount.add(qCommentObject.commentPraiseCount)):
            new OrderSpecifier<Date>(Order.DESC, qTopicInfo.createTime);
 
        JPAQuery<Tuple> query = this.getQueryFactory().select(qTopicInfo, qCommentObject, qClsClass, qUser).from(qTopicInfo, qTopicClassRe, qCommentObject, qClsClass, qUser )
                .where(qTopicInfo.deleteFlag.isFalse().and(qTopicClassRe.deleteFlag.isFalse()).and(qUser.userId.eq(qTopicInfo.createId))
                        .and(qCommentObject.deleteFlag.isFalse()).and(qTopicClassRe.topicId.eq(qTopicInfo.topicId)).and(qClsClass.deleteFlag.isFalse())
                        .and(qTopicInfo.startTime.isNull().or(qTopicInfo.startTime.before(new Date()).and(qTopicInfo.endTime.after(new Date()))))
                        .and(qTopicClassRe.classId.eq(qClsClass.classId).and(qTopicInfo.topicName.like("%" + keyword + "%"))).and(scopeRight)
                        .and(qCommentObject.commentObjectUid.eq(qTopicInfo.topicId).and(
                                qTopicInfo.createTime.after(new Date(new Date().getTime() - time))))
                        .and(qCommentObject.commentObjectType.eq(CommentObject.COMMENT_TYPE_INTERACT))).groupBy(qTopicInfo.topicId);
 
        long count = query.fetchCount();
 
        return CollectionUtils.newObjectMap("count", count, "listData", query.offset(pageNum).limit(limit)
                .orderBy(order, qTopicInfo.createTime.desc()).fetch().stream()
                .map(tuple -> {
                    Map<String, Object> map = new HashMap<String, Object>(14);
                    map.put("topicName", tuple.get(qTopicInfo).getTopicName());
                    map.put("topicId", tuple.get(qTopicInfo).getTopicId());
                    map.put("topicStatus", tuple.get(qTopicInfo).getTopicStatus());
                    map.put("creator", tuple.get(qTopicInfo).getCreator());
                    map.put("imgPath", tuple.get(qUser).getImgPath());
                    map.put("createId", tuple.get(qTopicInfo).getCreateId());
                    map.put("createTime", tuple.get(qTopicInfo).getCreateTime());
                    map.put("startTime", tuple.get(qTopicInfo).getStartTime());
                    map.put("endTime", tuple.get(qTopicInfo).getEndTime());
                    map.put("topicDesc", tuple.get(qTopicInfo).getTopicDesc());
                    map.put("latestCommentName", tuple.get(qTopicInfo).getLatestCommentName());
                    map.put("latestCommentTime", tuple.get(qTopicInfo).getLatestCommentTime());
                    map.put("viewCount", tuple.get(qCommentObject).getViewCount());
                    map.put("commentCount", tuple.get(qCommentObject).getCommentCount());
                    map.put("commentPraiseCount", tuple.get(qCommentObject).getCommentPraiseCount());
                    map.put("className", tuple.get(qClsClass).getName());
                    return map;
                }).collect(Collectors.toList()));
 
    }
 
}