123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- 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<String, String> adminMap = new HashMap<String, String>(){{
- put("", "f");
- }};
- public Page<Faq> 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<Faq> 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<Faq> 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<Object, Collection<Faq>> groupByPosition(String channel, Collection positions, Integer top){
- Map<Object, Collection<Faq>> result = new HashMap<>();
- if (positions == null || positions.size() == 0) return result;
- List<Faq> faqList = faqRelationMapper.groupByPosition(channel, positions, top);
- Collection<Faq> 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<Faq> 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<Faq> select(int page, int pageSize){
- return select(faqMapper, page, pageSize);
- }
- public Page<Faq> select(Integer[] ids){
- return page(()->select(faqMapper, ids), 1, ids.length);
- }
- public List<Faq> select(Collection ids){
- return select(faqMapper, ids);
- }
- }
|