UserNoteQuestionService.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package com.qxgmat.service;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.AbstractService;
  4. import com.nuliji.tools.Transform;
  5. import com.nuliji.tools.exception.ParameterException;
  6. import com.nuliji.tools.exception.SystemException;
  7. import com.nuliji.tools.mybatis.Example;
  8. import com.qxgmat.data.dao.UserNoteQuestionMapper;
  9. import com.qxgmat.data.dao.entity.UserNoteQuestion;
  10. import com.qxgmat.data.relation.UserNoteQuestionRelationMapper;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import javax.annotation.Resource;
  16. import java.util.ArrayList;
  17. import java.util.Collection;
  18. import java.util.Date;
  19. import java.util.List;
  20. @Service
  21. public class UserNoteQuestionService extends AbstractService {
  22. private static final Logger logger = LoggerFactory.getLogger(UserNoteQuestionService.class);
  23. @Resource
  24. private UserNoteQuestionMapper userNoteQuestionMapper;
  25. @Resource
  26. private UserNoteQuestionRelationMapper userNoteQuestionRelationMapper;
  27. /**
  28. * 获取用户笔记列表
  29. * @param page
  30. * @param size
  31. * @param userId
  32. * @param questionTypes
  33. * @param structIds
  34. * @param startTime
  35. * @param endTime
  36. * @param order
  37. * @return
  38. */
  39. public Page<UserNoteQuestion> listExercise(int page, int size, Integer userId, String keyword, String[] questionTypes, Integer[] structIds, Date startTime, Date endTime, String order){
  40. Page<UserNoteQuestion> p = page(()->{
  41. userNoteQuestionRelationMapper.listExercise(userId, keyword, questionTypes, structIds, startTime, endTime, order);
  42. }, page, size);
  43. Collection ids = Transform.getIds(p, UserNoteQuestion.class, "id");
  44. // 获取详细数据
  45. List<UserNoteQuestion> list = select(ids);
  46. Transform.replace(p, list, UserNoteQuestion.class, "id");
  47. return p;
  48. }
  49. /**
  50. * 获取用户笔记列表
  51. * @param page
  52. * @param size
  53. * @param userId
  54. * @param questionTypes
  55. * @param structIds
  56. * @param startTime
  57. * @param endTime
  58. * @param order
  59. * @return
  60. */
  61. public Page<UserNoteQuestion> listExamination(int page, int size, Integer userId, String keyword, String[] questionTypes, Integer[] structIds, Integer libraryId, String year, Date startTime, Date endTime, String order){
  62. Page<UserNoteQuestion> p = page(()->{
  63. userNoteQuestionRelationMapper.listExamination(userId, keyword, questionTypes, structIds, libraryId, year, startTime, endTime, order);
  64. }, page, size);
  65. Collection ids = Transform.getIds(p, UserNoteQuestion.class, "id");
  66. // 获取详细数据
  67. List<UserNoteQuestion> list = select(ids);
  68. Transform.replace(p, list, UserNoteQuestion.class, "id");
  69. return p;
  70. }
  71. /**
  72. * 查询用户笔记情况
  73. * @param userId
  74. * @param questionId
  75. * @return
  76. */
  77. public UserNoteQuestion getByUserAndQuestion(Integer userId, Integer questionId){
  78. Example example = new Example(UserNoteQuestion.class);
  79. example.and(
  80. example.createCriteria()
  81. .andEqualTo("userId", userId)
  82. .andEqualTo("questionId", questionId)
  83. );
  84. UserNoteQuestion in = one(userNoteQuestionMapper, example);
  85. return in;
  86. }
  87. /**
  88. * 查询用户笔记情况:批量
  89. * @param userId
  90. * @param ids
  91. * @return
  92. */
  93. public List<UserNoteQuestion> listByUserAndQuestions(Integer userId, Collection ids){
  94. if(ids==null || ids.size() == 0) return new ArrayList<>();
  95. Example example = new Example(UserNoteQuestion.class);
  96. example.and(
  97. example.createCriteria()
  98. .andEqualTo("userId", userId)
  99. .andIn("questionId", ids)
  100. );
  101. return select(userNoteQuestionMapper, example);
  102. }
  103. /**
  104. * 更新用户笔记:没有则添加
  105. * @param note
  106. * @return
  107. */
  108. @Transactional
  109. public UserNoteQuestion updateNote(UserNoteQuestion note){
  110. Example example = new Example(UserNoteQuestion.class);
  111. example.and(
  112. example.createCriteria()
  113. .andEqualTo("userId", note.getUserId())
  114. .andEqualTo("questionModule", note.getQuestionModule())
  115. );
  116. example.and(
  117. example.createCriteria()
  118. .orEqualTo("questionId", note.getQuestionId())
  119. .orEqualTo("questionNoId", note.getQuestionNoId())
  120. );
  121. UserNoteQuestion in = one(userNoteQuestionMapper, example);
  122. Date now = new Date();
  123. if(in == null){
  124. // 按实际更新更改更新时间
  125. if (note.getQuestionContent() != null && !note.getQuestionContent().isEmpty()){
  126. note.setQuestionTime(now);
  127. }
  128. if (note.getOfficialContent() != null && !note.getOfficialContent().isEmpty()){
  129. note.setOfficialTime(now);
  130. }
  131. if (note.getQxContent() != null && !note.getQxContent().isEmpty()){
  132. note.setQxTime(now);
  133. }
  134. if (note.getAssociationContent() != null && !note.getAssociationContent().isEmpty()){
  135. note.setAssociationTime(now);
  136. }
  137. if (note.getQaContent() != null && !note.getQaContent().isEmpty()){
  138. note.setQaTime(now);
  139. }
  140. return add(note);
  141. }else{
  142. note.setId(in.getId());
  143. // 按实际更新更改更新时间
  144. if (note.getQuestionContent() != null && !note.getQuestionContent().equals(in.getQuestionContent())){
  145. note.setQuestionTime(now);
  146. }
  147. if (note.getOfficialContent() != null && !note.getOfficialContent().equals(in.getOfficialContent())){
  148. note.setOfficialTime(now);
  149. }
  150. if (note.getQxContent() != null && !note.getQxContent().equals(in.getQxContent())){
  151. note.setQxTime(now);
  152. }
  153. if (note.getAssociationContent() != null && !note.getAssociationContent().equals(in.getAssociationContent())){
  154. note.setAssociationTime(now);
  155. }
  156. if (note.getQaContent() != null && !note.getQaContent().equals(in.getQaContent())){
  157. note.setQaTime(now);
  158. }
  159. // 如果所有内容为空,则删除
  160. return edit(note);
  161. }
  162. }
  163. /**
  164. * 删除笔记
  165. * @param userId
  166. * @param questionId
  167. * @return
  168. */
  169. @Transactional
  170. public boolean deleteNote(Integer userId, Integer questionId){
  171. Example example = new Example(UserNoteQuestion.class);
  172. example.and(
  173. example.createCriteria()
  174. .andEqualTo("userId", userId)
  175. .andEqualTo("questionId", questionId)
  176. );
  177. UserNoteQuestion in = one(userNoteQuestionMapper, example);
  178. if (in == null){
  179. return true;
  180. }
  181. return delete(in.getId());
  182. }
  183. public UserNoteQuestion add(UserNoteQuestion note){
  184. int result = insert(userNoteQuestionMapper, note);
  185. note = one(userNoteQuestionMapper, note.getId());
  186. if(note == null){
  187. throw new SystemException("笔记添加失败");
  188. }
  189. return note;
  190. }
  191. public UserNoteQuestion edit(UserNoteQuestion note){
  192. UserNoteQuestion in = one(userNoteQuestionMapper, note.getId());
  193. if(in == null){
  194. throw new ParameterException("笔记不存在");
  195. }
  196. int result = update(userNoteQuestionMapper, note);
  197. return note;
  198. }
  199. public boolean delete(Number id){
  200. UserNoteQuestion in = one(userNoteQuestionMapper, id);
  201. if(in == null){
  202. throw new ParameterException("笔记不存在");
  203. }
  204. int result = delete(userNoteQuestionMapper, id);
  205. return result > 0;
  206. }
  207. public UserNoteQuestion get(Number id){
  208. UserNoteQuestion in = one(userNoteQuestionMapper, id);
  209. if(in == null){
  210. throw new ParameterException("笔记不存在");
  211. }
  212. return in;
  213. }
  214. public Page<UserNoteQuestion> select(int page, int pageSize){
  215. return select(userNoteQuestionMapper, page, pageSize);
  216. }
  217. public Page<UserNoteQuestion> select(Integer[] ids){
  218. return page(()->select(userNoteQuestionMapper, ids), 1, ids.length);
  219. }
  220. public List<UserNoteQuestion> select(Collection ids){
  221. return select(userNoteQuestionMapper, ids);
  222. }
  223. }