UserAskCourseService.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.status.AskStatus;
  10. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  11. import com.qxgmat.data.constants.enums.user.MoneyRange;
  12. import com.qxgmat.data.dao.UserAskCourseMapper;
  13. import com.qxgmat.data.dao.entity.UserAskCourse;
  14. import com.qxgmat.data.relation.UserAskCourseRelationMapper;
  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.Collection;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. @Service
  25. public class UserAskCourseService extends AbstractService {
  26. private static final Logger logger = LoggerFactory.getLogger(UserAskCourseService.class);
  27. @Resource
  28. private UserAskCourseMapper userAskCourseMapper;
  29. @Resource
  30. private UserAskCourseRelationMapper userAskCourseRelationMapper;
  31. private Map<String, String> adminMap = new HashMap<String, String>(){{
  32. put("", "uac");
  33. }};
  34. public Page<UserAskCourse> listAdmin(int page, int size, Integer structId, Number courseId, AskStatus status, Integer showStatus, Integer userId, MoneyRange moneyRange, String startTime, String endTime, String order, DirectionStatus direction){
  35. Integer statusIndex = status != null ? status.index : null;
  36. Integer max = moneyRange != null ? moneyRange.max == Integer.MAX_VALUE ? null : moneyRange.max : null;
  37. Integer min = moneyRange != null ? moneyRange.min : null;
  38. if(order == null || order.isEmpty()){
  39. order = "id";
  40. }
  41. if(adminMap.containsKey(order)){
  42. order = adminMap.get(order)+".`"+Tools.underscoreName(order)+"`";
  43. }else{
  44. order = adminMap.get("")+".`"+Tools.underscoreName(order)+"`";
  45. }
  46. if (direction == null){
  47. direction = DirectionStatus.DESC;
  48. }
  49. String finalOrder = order;
  50. DirectionStatus finalDirection = direction;
  51. Page<UserAskCourse> p = page(
  52. ()-> userAskCourseRelationMapper.listAdmin(structId, courseId, statusIndex, showStatus, userId, min, max, startTime, endTime, finalOrder, finalDirection.key)
  53. , page, size);
  54. Collection ids = Transform.getIds(p, UserAskCourse.class, "id");
  55. Transform.replace(p, select(ids), UserAskCourse.class, "id");
  56. return p;
  57. }
  58. public List<UserAskCourse> listByCoursePosition(Number courseNoId, String position, Boolean showStatus){
  59. Example example = new Example(UserAskCourse.class);
  60. example.and(
  61. example.createCriteria()
  62. .andEqualTo("courseNoId", courseNoId)
  63. .andEqualTo("position", position)
  64. );
  65. if (showStatus != null)
  66. example.and(
  67. example.createCriteria()
  68. .andEqualTo("showStatus", showStatus?1:0)
  69. .andEqualTo("answerStatus", 1)
  70. );
  71. example.orderBy("sort").desc();
  72. return select(userAskCourseMapper, example);
  73. }
  74. /**
  75. * 根据列表顺序全部更新排序:从大到小
  76. * @param ids
  77. */
  78. @Transactional
  79. public void updateOrder(Integer[] ids){
  80. int sort = ids.length;
  81. List<UserAskCourse> userAskCourseList = select(userAskCourseMapper, ids);
  82. Map userAskCourseMap = Transform.getMap(userAskCourseList, UserAskCourse.class, "id");
  83. for (Integer id : ids){
  84. sort -= 1;
  85. UserAskCourse userAskCourse = (UserAskCourse)userAskCourseMap.get(id);
  86. if (userAskCourse == null) continue;
  87. if (userAskCourse.getSort() == sort) continue;
  88. update(userAskCourseMapper, UserAskCourse.builder()
  89. .id(id)
  90. .sort(sort)
  91. .showStatus(1)
  92. .build()
  93. );
  94. }
  95. }
  96. public UserAskCourse add(UserAskCourse message){
  97. int result = insert(userAskCourseMapper, message);
  98. message = one(userAskCourseMapper, message.getId());
  99. if(message == null){
  100. throw new SystemException("提问添加失败");
  101. }
  102. return message;
  103. }
  104. public UserAskCourse edit(UserAskCourse message){
  105. UserAskCourse in = one(userAskCourseMapper, message.getId());
  106. if(in == null){
  107. throw new ParameterException("提问不存在");
  108. }
  109. int result = update(userAskCourseMapper, message);
  110. return message;
  111. }
  112. public boolean delete(Number id){
  113. UserAskCourse in = one(userAskCourseMapper, id);
  114. if(in == null){
  115. throw new ParameterException("提问不存在");
  116. }
  117. int result = delete(userAskCourseMapper, id);
  118. return result > 0;
  119. }
  120. public UserAskCourse get(Number id){
  121. UserAskCourse in = one(userAskCourseMapper, id);
  122. if(in == null){
  123. throw new ParameterException("提问不存在");
  124. }
  125. return in;
  126. }
  127. public Page<UserAskCourse> select(int page, int pageSize){
  128. return select(userAskCourseMapper, page, pageSize);
  129. }
  130. public Page<UserAskCourse> select(Integer[] ids){
  131. return page(()->select(userAskCourseMapper, ids), 1, ids.length);
  132. }
  133. public List<UserAskCourse> select(Collection ids){
  134. return select(userAskCourseMapper, ids);
  135. }
  136. }