UserAskCourseService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.AnswerStatus;
  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 com.qxgmat.data.relation.entity.UserAskCourseStatRelation;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import javax.annotation.Resource;
  21. import java.util.*;
  22. @Service
  23. public class UserAskCourseService extends AbstractService {
  24. private static final Logger logger = LoggerFactory.getLogger(UserAskCourseService.class);
  25. @Resource
  26. private UserAskCourseMapper userAskCourseMapper;
  27. @Resource
  28. private UserAskCourseRelationMapper userAskCourseRelationMapper;
  29. private Map<String, String> adminMap = new HashMap<String, String>(){{
  30. put("", "uac");
  31. }};
  32. public Page<UserAskCourse> listAdmin(int page, int size, Integer structId, Number courseId, AnswerStatus status, Integer showStatus, Integer userId, MoneyRange moneyRange, String startTime, String endTime, String order, DirectionStatus direction){
  33. Integer statusIndex = status != null ? status.index : null;
  34. Integer max = moneyRange != null ? moneyRange.max == Integer.MAX_VALUE ? null : moneyRange.max : null;
  35. Integer min = moneyRange != null ? moneyRange.min : null;
  36. if(order == null || order.isEmpty()){
  37. order = "id";
  38. }
  39. if(adminMap.containsKey(order)){
  40. order = adminMap.get(order)+".`"+Tools.underscoreName(order)+"`";
  41. }else{
  42. order = adminMap.get("")+".`"+Tools.underscoreName(order)+"`";
  43. }
  44. if (direction == null){
  45. direction = DirectionStatus.DESC;
  46. }
  47. String finalOrder = order;
  48. DirectionStatus finalDirection = direction;
  49. Page<UserAskCourse> p = page(
  50. ()-> userAskCourseRelationMapper.listAdmin(structId, courseId, statusIndex, showStatus, userId, min, max, startTime, endTime, finalOrder, finalDirection.key)
  51. , page, size);
  52. Collection ids = Transform.getIds(p, UserAskCourse.class, "id");
  53. Transform.replace(p, select(ids), UserAskCourse.class, "id");
  54. return p;
  55. }
  56. public List<UserAskCourse> listByCoursePosition(Number courseNoId, Integer position, Boolean showStatus){
  57. Example example = new Example(UserAskCourse.class);
  58. example.and(
  59. example.createCriteria()
  60. .andEqualTo("courseNoId", courseNoId)
  61. .andEqualTo("position", position)
  62. );
  63. if (showStatus != null)
  64. example.and(
  65. example.createCriteria()
  66. .andEqualTo("showStatus", showStatus?1:0)
  67. .andEqualTo("answerStatus", 1)
  68. );
  69. example.orderBy("sort").desc();
  70. return select(userAskCourseMapper, example);
  71. }
  72. public Page<UserAskCourse> listByCourse(int page, int size, String keyword, Integer courseId, Integer courseNoId, Integer position, Boolean showStatus, String order, DirectionStatus direction){
  73. Example example = new Example(UserAskCourse.class);
  74. example.and(
  75. example.createCriteria()
  76. .andEqualTo("courseId", courseId)
  77. );
  78. if(courseNoId != null)
  79. example.and(
  80. example.createCriteria()
  81. .andEqualTo("courseNoId", courseNoId)
  82. );
  83. if(position != null)
  84. example.and(
  85. example.createCriteria()
  86. .andEqualTo("position", position)
  87. );
  88. if (keyword != null)
  89. example.and(
  90. example.createCriteria()
  91. .orLike("content", "%"+keyword+"%")
  92. .orLike("answer", "%"+keyword+"%")
  93. );
  94. if (showStatus != null)
  95. example.and(
  96. example.createCriteria()
  97. .andEqualTo("showStatus", showStatus?1:0)
  98. .andEqualTo("answerStatus", 1)
  99. );
  100. if(order == null || order.isEmpty()) order = "sort";
  101. switch(direction){
  102. case ASC:
  103. example.orderBy(order).asc();
  104. break;
  105. case DESC:
  106. default:
  107. example.orderBy(order).desc();
  108. }
  109. return select(userAskCourseMapper, example, page, size);
  110. }
  111. public Page<UserAskCourse> listByUser(int page, int size, String keyword, Integer userId, Integer courseId, Integer courseNoId, Integer position, AnswerStatus answerStatus, String order, DirectionStatus direction){
  112. Example example = new Example(UserAskCourse.class);
  113. example.and(
  114. example.createCriteria()
  115. .andEqualTo("userId", userId)
  116. .andEqualTo("courseId", courseId)
  117. );
  118. if(courseNoId != null)
  119. example.and(
  120. example.createCriteria()
  121. .andEqualTo("courseNoId", courseNoId)
  122. );
  123. if (position != null)
  124. example.and(
  125. example.createCriteria()
  126. .andEqualTo("position", position)
  127. );
  128. if (answerStatus != null)
  129. example.and(
  130. example.createCriteria()
  131. .andEqualTo("answerStatus", answerStatus.index)
  132. );
  133. if (keyword != null)
  134. example.and(
  135. example.createCriteria()
  136. .orLike("content", "%"+keyword+"%")
  137. .orLike("answer", "%"+keyword+"%")
  138. );
  139. if(order == null || order.isEmpty()) order = "id";
  140. switch(direction){
  141. case ASC:
  142. example.orderBy(order).asc();
  143. break;
  144. case DESC:
  145. default:
  146. example.orderBy(order).desc();
  147. }
  148. return select(userAskCourseMapper, example, page, size);
  149. }
  150. public List<UserAskCourse> listByRecord(Number recordId){
  151. Example example = new Example(UserAskCourse.class);
  152. example.and(
  153. example.createCriteria()
  154. .andEqualTo("recordId", recordId)
  155. .andNotEqualTo("answerStatus", AnswerStatus.IGNORE.index)
  156. );
  157. example.orderBy("id").asc();
  158. return select(userAskCourseMapper, example);
  159. }
  160. /**
  161. * 根据列表顺序全部更新排序:从大到小
  162. * @param ids
  163. */
  164. @Transactional
  165. public void updateOrder(Integer[] ids){
  166. int sort = ids.length;
  167. List<UserAskCourse> userAskCourseList = select(userAskCourseMapper, ids);
  168. Map userAskCourseMap = Transform.getMap(userAskCourseList, UserAskCourse.class, "id");
  169. for (Integer id : ids){
  170. sort -= 1;
  171. UserAskCourse userAskCourse = (UserAskCourse)userAskCourseMap.get(id);
  172. if (userAskCourse == null) continue;
  173. if (userAskCourse.getSort() == sort) continue;
  174. update(userAskCourseMapper, UserAskCourse.builder()
  175. .id(id)
  176. .sort(sort)
  177. .showStatus(1)
  178. .build()
  179. );
  180. }
  181. }
  182. public List<UserAskCourse> getByRecordId(Integer recordId){
  183. Example example = new Example(UserAskCourse.class);
  184. example.and(
  185. example.createCriteria()
  186. .andEqualTo("recordId", recordId)
  187. );
  188. return select(userAskCourseMapper, example);
  189. }
  190. /**
  191. * 获取课程记录分组列表
  192. * @param recordIds
  193. * @return
  194. */
  195. public Map<Object, Collection<UserAskCourse>> groupByRecordId(Collection recordIds){
  196. Map<Object, Collection<UserAskCourse>> relationMap = new HashMap<>();
  197. if(recordIds == null || recordIds.size() == 0) return relationMap;
  198. Example example = new Example(UserAskCourse.class);
  199. example.and(
  200. example.createCriteria()
  201. .andIn("recordId", recordIds)
  202. );
  203. List<UserAskCourse> nos = select(userAskCourseMapper, example);
  204. Collection<UserAskCourse> list;
  205. for(UserAskCourse no : nos){
  206. if (!relationMap.containsKey(no.getRecordId())){
  207. list = new ArrayList<>();
  208. relationMap.put(no.getRecordId(), list);
  209. }else{
  210. list = relationMap.get(no.getRecordId());
  211. }
  212. list.add(no);
  213. }
  214. return relationMap;
  215. }
  216. /**
  217. * 计算课程的汇总信息
  218. * @param courseIds
  219. * @return
  220. */
  221. public List<UserAskCourseStatRelation> statGroupCourse(Collection courseIds, Integer showStatus){
  222. return userAskCourseRelationMapper.statGroupCourse(courseIds, showStatus);
  223. }
  224. public UserAskCourseStatRelation statCourse(Integer courseId, Integer showStatus){
  225. List<UserAskCourseStatRelation> relation = userAskCourseRelationMapper.statGroupCourse(new ArrayList(){{add(courseId);}}, showStatus);
  226. if (relation != null && relation.size() > 0){
  227. return relation.get(0);
  228. }else{
  229. return null;
  230. }
  231. }
  232. /**
  233. * 累加访问记录
  234. */
  235. public void accumulation(Integer id, int view){
  236. userAskCourseRelationMapper.accumulation(id, view);
  237. }
  238. public UserAskCourse add(UserAskCourse message){
  239. int result = insert(userAskCourseMapper, message);
  240. message = one(userAskCourseMapper, message.getId());
  241. if(message == null){
  242. throw new SystemException("提问添加失败");
  243. }
  244. return message;
  245. }
  246. public UserAskCourse edit(UserAskCourse message){
  247. UserAskCourse in = one(userAskCourseMapper, message.getId());
  248. if(in == null){
  249. throw new ParameterException("提问不存在");
  250. }
  251. int result = update(userAskCourseMapper, message);
  252. return message;
  253. }
  254. public boolean delete(Number id){
  255. UserAskCourse in = one(userAskCourseMapper, id);
  256. if(in == null){
  257. throw new ParameterException("提问不存在");
  258. }
  259. int result = delete(userAskCourseMapper, id);
  260. return result > 0;
  261. }
  262. public UserAskCourse get(Number id){
  263. UserAskCourse in = one(userAskCourseMapper, id);
  264. if(in == null){
  265. throw new ParameterException("提问不存在");
  266. }
  267. return in;
  268. }
  269. public Page<UserAskCourse> select(int page, int pageSize){
  270. return select(userAskCourseMapper, page, pageSize);
  271. }
  272. public Page<UserAskCourse> select(Integer[] ids){
  273. return page(()->select(userAskCourseMapper, ids), 1, ids.length);
  274. }
  275. public List<UserAskCourse> select(Collection ids){
  276. return select(userAskCourseMapper, ids);
  277. }
  278. }