CommentService.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.qxgmat.service.inline;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.AbstractService;
  4. import com.nuliji.tools.Tools;
  5. import com.nuliji.tools.Transform;
  6. import com.nuliji.tools.exception.ParameterException;
  7. import com.nuliji.tools.exception.SystemException;
  8. import com.nuliji.tools.mybatis.Example;
  9. import com.qxgmat.data.constants.enums.module.ChannelModule;
  10. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  11. import com.qxgmat.data.constants.enums.user.MoneyRange;
  12. import com.qxgmat.data.dao.CommentMapper;
  13. import com.qxgmat.data.dao.entity.Comment;
  14. import com.qxgmat.data.dao.entity.User;
  15. import com.qxgmat.data.relation.CommentRelationMapper;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import javax.annotation.Resource;
  21. import java.util.*;
  22. @Service
  23. public class CommentService extends AbstractService {
  24. private static final Logger logger = LoggerFactory.getLogger(CommentService.class);
  25. @Resource
  26. private CommentMapper commentMapper;
  27. @Resource
  28. private CommentRelationMapper commentRelationMapper;
  29. private Map<String, String> adminMap = new HashMap<String, String>(){{
  30. put("", "c");
  31. }};
  32. public Page<Comment> listAdmin(int page, int size, Boolean user, String channel, String position, Integer userId, Boolean isSpecial, Boolean isShow, MoneyRange moneyRange, String order, DirectionStatus direction){
  33. Integer max = moneyRange != null ? moneyRange.max == Integer.MAX_VALUE ? null : moneyRange.max : null;
  34. Integer min = moneyRange != null ? moneyRange.min : null;
  35. if(order == null || order.isEmpty()){
  36. order = "id";
  37. }
  38. if(adminMap.containsKey(order)){
  39. order = adminMap.get(order)+".`"+Tools.underscoreName(order)+"`";
  40. }else{
  41. order = adminMap.get("")+".`"+Tools.underscoreName(order)+"`";
  42. }
  43. if (direction == null){
  44. direction = DirectionStatus.DESC;
  45. }
  46. String finalOrder = order;
  47. DirectionStatus finalDirection = direction;
  48. Page<Comment> p = page(
  49. ()-> commentRelationMapper.listWithUser(user, channel, position, userId, isSpecial != null ? (isSpecial ? 1 :0) :null,isShow != null ? (isShow ? 1 :0) :null, min, max, finalOrder, finalDirection.key)
  50. , page, size);
  51. Collection ids = Transform.getIds(p, Comment.class, "id");
  52. Transform.replace(p, select(ids), Comment.class, "id");
  53. return p;
  54. }
  55. public Page<Comment> list(int page, int size, String channel, String position){
  56. Example example = new Example(Comment.class);
  57. if (channel != null){
  58. example.and(
  59. example.createCriteria().andEqualTo("channel", channel)
  60. );
  61. if (position != null){
  62. example.and(
  63. example.createCriteria().andEqualTo("position", position)
  64. );
  65. }
  66. }
  67. example.and(
  68. example.createCriteria()
  69. .orEqualTo("isShow", 1)
  70. );
  71. example.orderBy("sort").desc();
  72. return select(commentMapper, example, page, size);
  73. }
  74. /**
  75. * 替换comment的用户信息为真实用户信息
  76. * @param comments
  77. * @param users
  78. */
  79. public void replaceUser(List<Comment> comments, List<User> users){
  80. Map userMap = Transform.getMap(users, User.class, "id");
  81. for(Comment comment : comments){
  82. if (comment.getUserId() == 0) {
  83. continue;
  84. }
  85. User user = (User)userMap.get(comment.getUserId());
  86. if (user == null){
  87. continue;
  88. }
  89. comment.setAvatar(user.getAvatar());
  90. comment.setNickname(user.getNickname());
  91. }
  92. }
  93. public Map<Object, Collection<Comment>> groupByPosition(String channel, Collection positions, Integer top){
  94. Map<Object, Collection<Comment>> result = new HashMap<>();
  95. if (positions == null || positions.size() == 0) return result;
  96. List<Comment> commentList = commentRelationMapper.groupByPosition(channel, positions, top);
  97. Collection<Comment> tmp;
  98. for(Comment comment : commentList){
  99. if (!result.containsKey(comment.getPosition())){
  100. tmp = new ArrayList<>();
  101. result.put(comment.getPosition(), tmp);
  102. }else{
  103. tmp = result.get(comment.getPosition());
  104. }
  105. tmp.add(comment);
  106. }
  107. return result;
  108. }
  109. /**
  110. * 根据列表顺序全部更新排序:从大到小
  111. * @param ids
  112. */
  113. @Transactional
  114. public void updateOrder(Integer[] ids){
  115. int sort = ids.length;
  116. List<Comment> commentList = select(commentMapper, ids);
  117. Map commentMap = Transform.getMap(commentList, Comment.class, "id");
  118. for (Integer id : ids){
  119. sort -= 1;
  120. Comment comment = (Comment)commentMap.get(id);
  121. if (comment == null) continue;
  122. if (comment.getSort() == sort) continue;
  123. update(commentMapper, Comment.builder()
  124. .id(id)
  125. .sort(sort)
  126. .build()
  127. );
  128. }
  129. }
  130. public Comment add(Comment comment){
  131. int result = insert(commentMapper, comment);
  132. comment = one(commentMapper, comment.getId());
  133. if(comment == null){
  134. throw new SystemException("评价添加失败");
  135. }
  136. return comment;
  137. }
  138. public Comment edit(Comment comment){
  139. Comment in = one(commentMapper, comment.getId());
  140. if(in == null){
  141. throw new ParameterException("评价不存在");
  142. }
  143. int result = update(commentMapper, comment);
  144. return comment;
  145. }
  146. public boolean delete(Number id){
  147. Comment in = one(commentMapper, id);
  148. if(in == null){
  149. throw new ParameterException("评价不存在");
  150. }
  151. int result = delete(commentMapper, id);
  152. return result > 0;
  153. }
  154. public Comment get(Number id){
  155. Comment in = one(commentMapper, id);
  156. if(in == null){
  157. throw new ParameterException("评价不存在");
  158. }
  159. return in;
  160. }
  161. public Page<Comment> select(int page, int pageSize){
  162. return select(commentMapper, page, pageSize);
  163. }
  164. public Page<Comment> select(Integer[] ids){
  165. return page(()->select(commentMapper, ids), 1, ids.length);
  166. }
  167. public List<Comment> select(Collection ids){
  168. return select(commentMapper, ids);
  169. }
  170. }