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 adminMap = new HashMap(){{ put("", "c"); }}; public Page 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 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 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 comments, List 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> groupByPosition(String channel, Collection positions, Integer top){ Map> result = new HashMap<>(); if (positions == null || positions.size() == 0) return result; List commentList = commentRelationMapper.groupByPosition(channel, positions, top); Collection 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 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 select(int page, int pageSize){ return select(commentMapper, page, pageSize); } public Page select(Integer[] ids){ return page(()->select(commentMapper, ids), 1, ids.length); } public List select(Collection ids){ return select(commentMapper, ids); } }