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.InsideModule; import com.qxgmat.data.constants.enums.status.DirectionStatus; import com.qxgmat.data.constants.enums.user.MoneyRange; import com.qxgmat.data.dao.FaqMapper; import com.qxgmat.data.dao.entity.Faq; import com.qxgmat.data.relation.FaqRelationMapper; 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 FaqService extends AbstractService { private static final Logger logger = LoggerFactory.getLogger(FaqService.class); @Resource private FaqMapper faqMapper; @Resource private FaqRelationMapper faqRelationMapper; private Map adminMap = new HashMap(){{ put("", "f"); }}; public Page listAdmin(int page, int size, Boolean user, String channel, String position, Integer userId, Integer answerStatus, 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( ()-> faqRelationMapper.listWithUser(user, channel, position, userId, answerStatus, isSpecial != null ? (isSpecial ? 1 :0) :null,isShow != null ? (isShow ? 1 :0) :null, min, max, finalOrder, finalDirection.key) , page, size); Collection ids = Transform.getIds(p, Faq.class, "id"); Transform.replace(p, select(ids), Faq.class, "id"); return p; } public Page list(int page, int size, String channel, String position){ Example example = new Example(Faq.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(faqMapper, example, page, size); } public Map> groupByPosition(String channel, Collection positions, Integer top){ Map> result = new HashMap<>(); if (positions == null || positions.size() == 0) return result; List faqList = faqRelationMapper.groupByPosition(channel, positions, top); Collection tmp; for(Faq faq : faqList){ if (!result.containsKey(faq.getPosition())){ tmp = new ArrayList<>(); result.put(faq.getPosition(), tmp); }else{ tmp = result.get(faq.getPosition()); } tmp.add(faq); } return result; } /** * 根据列表顺序全部更新排序:从大到小 * @param ids */ @Transactional public void updateOrder(Integer[] ids){ int sort = ids.length; List faqList = select(faqMapper, ids); Map faqMap = Transform.getMap(faqList, Faq.class, "id"); for (Integer id : ids){ sort -= 1; Faq faq = (Faq)faqMap.get(id); if (faq == null) continue; if (faq.getSort() == sort) continue; update(faqMapper, Faq.builder() .id(id) .sort(sort) .build() ); } } public Faq add(Faq faq){ int result = insert(faqMapper, faq); faq = one(faqMapper, faq.getId()); if(faq == null){ throw new SystemException("faq添加失败"); } return faq; } public Faq edit(Faq faq){ Faq in = one(faqMapper, faq.getId()); if(in == null){ throw new ParameterException("faq不存在"); } int result = update(faqMapper, faq); return faq; } public boolean delete(Number id){ Faq in = one(faqMapper, id); if(in == null){ throw new ParameterException("faq不存在"); } int result = delete(faqMapper, id); return result > 0; } public Faq get(Number id){ Faq in = one(faqMapper, id); if(in == null){ throw new ParameterException("faq不存在"); } return in; } public Page select(int page, int pageSize){ return select(faqMapper, page, pageSize); } public Page select(Integer[] ids){ return page(()->select(faqMapper, ids), 1, ids.length); } public List select(Collection ids){ return select(faqMapper, ids); } }