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 hqlMap = new HashMap(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> topicLst = findListWithMapByHql(hql, hqlMap, new Pager(limit, pageNum)); for (Map 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> 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 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> 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 argsMap = new HashMap(1); argsMap.put("classIds", classIds); List> 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> 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); 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 hqlMap = new HashMap(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> 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 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> classLst = findListWithMapByHql(hql, args); for (Map 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 mapJson = new HashMap(); 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> topicIdLst = findListWithMapByHql(findTopicIdLst, // »ñÈ¡°à¼¶ËùÓл°ÌâµÄid CollectionUtils.newObjectMap("classId", classId)); int commentCount = 0; int studentCommentCount = 0; for (Map 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> 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> 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 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> 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> 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> commentLst = findListWithMapByHql(findMyCommentLstByHql, CollectionUtils.newObjectMap("userId", userId), new Pager(pageSize, pageNum)); for (Map 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 query = this.getQueryFactory(). selectFrom(qSnsMyComment).where(qSnsMyComment.createId.eq(userId)); List 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 query = this.getQueryFactory(). selectFrom(qSnsCommentToMe).where(qSnsCommentToMe.createId.eq(userId)); List 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 query = this.getQueryFactory(). selectFrom(qSnsPraiseToMe).where(qSnsPraiseToMe.createId.eq(userId)); List 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 query = this.getQueryFactory(). selectFrom(qSnsMyPraise).where(qSnsMyPraise.createId.eq(userId)); List 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> commentLst = findListWithMapByHql(hql, CollectionUtils.newObjectMap("userId", ClientUtils.getUserId()), new Pager(pageSize, pageNum)); for (Map 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> 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 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 hqlMap = new HashMap(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> topicLst = findListWithMapByHql(hql, hqlMap, new Pager(pageSize, pageNum)); for (Map 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> 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 hqlMap = new HashMap(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> topicLst = findListWithMapByHql(hql, hqlMap, new Pager(pageSize, pageNum)); for (Map 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> 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 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 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(Order.DESC, qCommentObject.commentCount.add(qCommentObject.commentPraiseCount)): new OrderSpecifier(Order.DESC, qTopicInfo.createTime); JPAQuery 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 map = new HashMap(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())); } }