123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- package com.qxgmat.service.inline;
- import com.github.pagehelper.Page;
- import com.nuliji.tools.AbstractService;
- import com.nuliji.tools.Tools;
- import com.nuliji.tools.Transform;
- import com.nuliji.tools.exception.ParameterException;
- import com.nuliji.tools.exception.SystemException;
- import com.nuliji.tools.mybatis.Example;
- import com.qxgmat.data.constants.enums.module.ChannelModule;
- import com.qxgmat.data.constants.enums.status.DirectionStatus;
- import com.qxgmat.data.constants.enums.user.MoneyRange;
- import com.qxgmat.data.dao.CommentMapper;
- import com.qxgmat.data.dao.entity.Comment;
- import com.qxgmat.data.dao.entity.User;
- import com.qxgmat.data.relation.CommentRelationMapper;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.util.*;
- @Service
- public class CommentService extends AbstractService {
- private static final Logger logger = LoggerFactory.getLogger(CommentService.class);
- @Resource
- private CommentMapper commentMapper;
- @Resource
- private CommentRelationMapper commentRelationMapper;
- private Map<String, String> adminMap = new HashMap<String, String>(){{
- put("", "c");
- }};
- 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){
- Integer max = moneyRange != null ? moneyRange.max == Integer.MAX_VALUE ? null : moneyRange.max : null;
- Integer min = moneyRange != null ? moneyRange.min : null;
- if(order == null || order.isEmpty()){
- order = "id";
- }
- if(adminMap.containsKey(order)){
- order = adminMap.get(order)+".`"+Tools.underscoreName(order)+"`";
- }else{
- order = adminMap.get("")+".`"+Tools.underscoreName(order)+"`";
- }
- if (direction == null){
- direction = DirectionStatus.DESC;
- }
- String finalOrder = order;
- DirectionStatus finalDirection = direction;
- Page<Comment> p = page(
- ()-> commentRelationMapper.listWithUser(user, channel, position, userId, isSpecial != null ? (isSpecial ? 1 :0) :null,isShow != null ? (isShow ? 1 :0) :null, min, max, finalOrder, finalDirection.key)
- , page, size);
- Collection ids = Transform.getIds(p, Comment.class, "id");
- Transform.replace(p, select(ids), Comment.class, "id");
- return p;
- }
- public Page<Comment> list(int page, int size, String channel, String position){
- Example example = new Example(Comment.class);
- if (channel != null){
- example.and(
- example.createCriteria().andEqualTo("channel", channel)
- );
- if (position != null){
- example.and(
- example.createCriteria().andEqualTo("position", position)
- );
- }
- }
- example.and(
- example.createCriteria()
- .orEqualTo("isShow", 1)
- );
- example.orderBy("sort").desc();
- return select(commentMapper, example, page, size);
- }
- /**
- * 替换comment的用户信息为真实用户信息
- * @param comments
- * @param users
- */
- public void replaceUser(List<Comment> comments, List<User> users){
- Map userMap = Transform.getMap(users, User.class, "id");
- for(Comment comment : comments){
- if (comment.getUserId() == 0) {
- continue;
- }
- User user = (User)userMap.get(comment.getUserId());
- if (user == null){
- continue;
- }
- comment.setAvatar(user.getAvatar());
- comment.setNickname(user.getNickname());
- }
- }
- public Map<Object, Collection<Comment>> groupByPosition(String channel, Collection positions, Integer top){
- Map<Object, Collection<Comment>> result = new HashMap<>();
- if (positions == null || positions.size() == 0) return result;
- List<Comment> commentList = commentRelationMapper.groupByPosition(channel, positions, top);
- Collection<Comment> tmp;
- for(Comment comment : commentList){
- if (!result.containsKey(comment.getPosition())){
- tmp = new ArrayList<>();
- result.put(comment.getPosition(), tmp);
- }else{
- tmp = result.get(comment.getPosition());
- }
- tmp.add(comment);
- }
- return result;
- }
- /**
- * 根据列表顺序全部更新排序:从大到小
- * @param ids
- */
- @Transactional
- public void updateOrder(Integer[] ids){
- int sort = ids.length;
- List<Comment> commentList = select(commentMapper, ids);
- Map commentMap = Transform.getMap(commentList, Comment.class, "id");
- for (Integer id : ids){
- sort -= 1;
- Comment comment = (Comment)commentMap.get(id);
- if (comment == null) continue;
- if (comment.getSort() == sort) continue;
- update(commentMapper, Comment.builder()
- .id(id)
- .sort(sort)
- .build()
- );
- }
- }
- public Comment add(Comment comment){
- int result = insert(commentMapper, comment);
- comment = one(commentMapper, comment.getId());
- if(comment == null){
- throw new SystemException("评价添加失败");
- }
- return comment;
- }
- public Comment edit(Comment comment){
- Comment in = one(commentMapper, comment.getId());
- if(in == null){
- throw new ParameterException("评价不存在");
- }
- int result = update(commentMapper, comment);
- return comment;
- }
- public boolean delete(Number id){
- Comment in = one(commentMapper, id);
- if(in == null){
- throw new ParameterException("评价不存在");
- }
- int result = delete(commentMapper, id);
- return result > 0;
- }
- public Comment get(Number id){
- Comment in = one(commentMapper, id);
- if(in == null){
- throw new ParameterException("评价不存在");
- }
- return in;
- }
- public Page<Comment> select(int page, int pageSize){
- return select(commentMapper, page, pageSize);
- }
- public Page<Comment> select(Integer[] ids){
- return page(()->select(commentMapper, ids), 1, ids.length);
- }
- public List<Comment> select(Collection ids){
- return select(commentMapper, ids);
- }
- }
|