FaqService.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.InsideModule;
  10. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  11. import com.qxgmat.data.constants.enums.user.MoneyRange;
  12. import com.qxgmat.data.dao.FaqMapper;
  13. import com.qxgmat.data.dao.entity.Faq;
  14. import com.qxgmat.data.relation.FaqRelationMapper;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import javax.annotation.Resource;
  20. import java.util.*;
  21. @Service
  22. public class FaqService extends AbstractService {
  23. private static final Logger logger = LoggerFactory.getLogger(FaqService.class);
  24. @Resource
  25. private FaqMapper faqMapper;
  26. @Resource
  27. private FaqRelationMapper faqRelationMapper;
  28. private Map<String, String> adminMap = new HashMap<String, String>(){{
  29. put("", "f");
  30. }};
  31. 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){
  32. Integer max = moneyRange != null ? moneyRange.max == Integer.MAX_VALUE ? null : moneyRange.max : null;
  33. Integer min = moneyRange != null ? moneyRange.min : null;
  34. if(order == null || order.isEmpty()){
  35. order = "id";
  36. }
  37. if(adminMap.containsKey(order)){
  38. order = adminMap.get(order)+".`"+Tools.underscoreName(order)+"`";
  39. }else{
  40. order = adminMap.get("")+".`"+Tools.underscoreName(order)+"`";
  41. }
  42. if (direction == null){
  43. direction = DirectionStatus.DESC;
  44. }
  45. String finalOrder = order;
  46. DirectionStatus finalDirection = direction;
  47. Page<Faq> p = page(
  48. ()-> faqRelationMapper.listWithUser(user, channel, position, userId, answerStatus, isSpecial != null ? (isSpecial ? 1 :0) :null,isShow != null ? (isShow ? 1 :0) :null, min, max, finalOrder, finalDirection.key)
  49. , page, size);
  50. Collection ids = Transform.getIds(p, Faq.class, "id");
  51. Transform.replace(p, select(ids), Faq.class, "id");
  52. return p;
  53. }
  54. public Page<Faq> list(int page, int size, String channel, String position){
  55. Example example = new Example(Faq.class);
  56. if (channel != null){
  57. example.and(
  58. example.createCriteria().andEqualTo("channel", channel)
  59. );
  60. if (position != null){
  61. example.and(
  62. example.createCriteria().andEqualTo("position", position)
  63. );
  64. }
  65. }
  66. example.and(
  67. example.createCriteria()
  68. .orEqualTo("isShow", 1)
  69. );
  70. example.orderBy("sort").desc();
  71. return select(faqMapper, example, page, size);
  72. }
  73. public Map<Object, Collection<Faq>> groupByPosition(String channel, Collection positions, Integer top){
  74. Map<Object, Collection<Faq>> result = new HashMap<>();
  75. if (positions == null || positions.size() == 0) return result;
  76. List<Faq> faqList = faqRelationMapper.groupByPosition(channel, positions, top);
  77. Collection<Faq> tmp;
  78. for(Faq faq : faqList){
  79. if (!result.containsKey(faq.getPosition())){
  80. tmp = new ArrayList<>();
  81. result.put(faq.getPosition(), tmp);
  82. }else{
  83. tmp = result.get(faq.getPosition());
  84. }
  85. tmp.add(faq);
  86. }
  87. return result;
  88. }
  89. /**
  90. * 根据列表顺序全部更新排序:从大到小
  91. * @param ids
  92. */
  93. @Transactional
  94. public void updateOrder(Integer[] ids){
  95. int sort = ids.length;
  96. List<Faq> faqList = select(faqMapper, ids);
  97. Map faqMap = Transform.getMap(faqList, Faq.class, "id");
  98. for (Integer id : ids){
  99. sort -= 1;
  100. Faq faq = (Faq)faqMap.get(id);
  101. if (faq == null) continue;
  102. if (faq.getSort() == sort) continue;
  103. update(faqMapper, Faq.builder()
  104. .id(id)
  105. .sort(sort)
  106. .build()
  107. );
  108. }
  109. }
  110. public Faq add(Faq faq){
  111. int result = insert(faqMapper, faq);
  112. faq = one(faqMapper, faq.getId());
  113. if(faq == null){
  114. throw new SystemException("faq添加失败");
  115. }
  116. return faq;
  117. }
  118. public Faq edit(Faq faq){
  119. Faq in = one(faqMapper, faq.getId());
  120. if(in == null){
  121. throw new ParameterException("faq不存在");
  122. }
  123. int result = update(faqMapper, faq);
  124. return faq;
  125. }
  126. public boolean delete(Number id){
  127. Faq in = one(faqMapper, id);
  128. if(in == null){
  129. throw new ParameterException("faq不存在");
  130. }
  131. int result = delete(faqMapper, id);
  132. return result > 0;
  133. }
  134. public Faq get(Number id){
  135. Faq in = one(faqMapper, id);
  136. if(in == null){
  137. throw new ParameterException("faq不存在");
  138. }
  139. return in;
  140. }
  141. public Page<Faq> select(int page, int pageSize){
  142. return select(faqMapper, page, pageSize);
  143. }
  144. public Page<Faq> select(Integer[] ids){
  145. return page(()->select(faqMapper, ids), 1, ids.length);
  146. }
  147. public List<Faq> select(Collection ids){
  148. return select(faqMapper, ids);
  149. }
  150. }