UserPaperService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package com.qxgmat.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.github.pagehelper.Page;
  4. import com.nuliji.tools.AbstractService;
  5. import com.nuliji.tools.PageResult;
  6. import com.nuliji.tools.Transform;
  7. import com.nuliji.tools.exception.ParameterException;
  8. import com.nuliji.tools.exception.SystemException;
  9. import com.nuliji.tools.mybatis.Example;
  10. import com.qxgmat.data.constants.enums.QuestionType;
  11. import com.qxgmat.data.constants.enums.module.PaperModule;
  12. import com.qxgmat.data.constants.enums.module.PaperOrigin;
  13. import com.qxgmat.data.constants.enums.module.QuestionOrigin;
  14. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  15. import com.qxgmat.data.dao.UserPaperMapper;
  16. import com.qxgmat.data.dao.entity.UserPaper;
  17. import com.qxgmat.data.dao.entity.UserReport;
  18. import com.qxgmat.data.relation.UserPaperRelationMapper;
  19. import com.qxgmat.service.annotation.InitPaper;
  20. import com.qxgmat.service.extend.ToolsService;
  21. import com.qxgmat.service.inline.UserPaperQuestionService;
  22. import com.qxgmat.service.inline.UserReportService;
  23. import com.qxgmat.util.annotation.Callback;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import javax.annotation.Resource;
  29. import java.util.*;
  30. @Service
  31. public class UserPaperService extends AbstractService {
  32. private static final Logger logger = LoggerFactory.getLogger(UserPaperService.class);
  33. protected boolean SOFT_FLAG = true;
  34. @Resource
  35. private UserPaperMapper userPaperMapper;
  36. @Resource
  37. private UserPaperRelationMapper userPaperRelationMapper;
  38. @Resource
  39. private UserReportService userReportService;
  40. @Resource
  41. private UserPaperQuestionService userPaperQuestionService;
  42. @Resource
  43. private ToolsService toolsService;
  44. /**
  45. * 获取报告列表:以paper进行分组
  46. * @param page
  47. * @param size
  48. * @param userId
  49. * @param origin
  50. * @param startTime
  51. * @param endTime
  52. * @param order
  53. * @return
  54. */
  55. public Page<UserPaper> list(int page, int size, Integer userId, String keyword, PaperOrigin origin, String startTime, String endTime, String order){
  56. Page<UserPaper> p = page(()->{
  57. userPaperRelationMapper.list(userId, keyword, origin != null ? origin.key : null, startTime, endTime, order);
  58. }, page, size);
  59. Collection ids = Transform.getIds(p, UserPaper.class, "id");
  60. // 获取详细数据
  61. List<UserPaper> list = select(ids);
  62. Transform.replace(p, list, UserPaper.class, "id");
  63. return p;
  64. }
  65. /**
  66. * 获取报告列表:以paper进行分组
  67. * @param page
  68. * @param size
  69. * @param userId
  70. * @param startTime
  71. * @param endTime
  72. * @param order
  73. * @return
  74. */
  75. public Page<UserPaper> listExercise(int page, int size, Integer userId, String keyword, String[] questionTypes, Integer[] structIds, String[] courseModules, String startTime, String endTime, String order){
  76. Page<UserPaper> p = page(()->{
  77. userPaperRelationMapper.listExercise(userId, keyword, questionTypes, structIds, courseModules, startTime, endTime, order);
  78. }, page, size);
  79. Collection ids = Transform.getIds(p, UserPaper.class, "id");
  80. // 获取详细数据
  81. List<UserPaper> list = select(ids);
  82. Transform.replace(p, list, UserPaper.class, "id");
  83. return p;
  84. }
  85. /**
  86. * 获取报告列表:以paper进行分组
  87. * @param page
  88. * @param size
  89. * @param userId
  90. * @param startTime
  91. * @param endTime
  92. * @param order
  93. * @return
  94. */
  95. public Page<UserPaper> listExamination(int page, int size, Integer userId, String keyword, Integer[] structIds, Integer libraryId, String year, String startTime, String endTime, String order){
  96. Page<UserPaper> p = page(()->{
  97. userPaperRelationMapper.listExamination(userId, keyword, structIds, libraryId, year, startTime, endTime, order);
  98. }, page, size);
  99. Collection ids = Transform.getIds(p, UserPaper.class, "id");
  100. // 获取详细数据
  101. List<UserPaper> list = select(ids);
  102. Transform.replace(p, list, UserPaper.class, "id");
  103. return p;
  104. }
  105. /**
  106. * 获取用户做题记录
  107. * @param userId
  108. * @param origin
  109. * @param ids
  110. * @param recordId
  111. * @return
  112. */
  113. public List<UserPaper> listWithOrigin(Integer userId, PaperOrigin origin, Collection ids, Integer recordId){
  114. if (ids == null || ids.size() == 0) return new ArrayList<>();
  115. Example example = new Example(UserPaper.class);
  116. example.and(
  117. example.createCriteria()
  118. .andEqualTo("userId", userId)
  119. .andEqualTo("paperOrigin", origin.key)
  120. .andIn("originId", ids)
  121. );
  122. if (recordId != null){
  123. example.and(
  124. example.createCriteria()
  125. .andEqualTo("recordId", recordId)
  126. );
  127. }
  128. return select(userPaperMapper, example);
  129. }
  130. /**
  131. * 获取用户预约预习作业记录
  132. * @param ids
  133. * @return
  134. */
  135. public List<UserPaper> listWithAppointment(Collection ids){
  136. if(ids == null || ids.size() == 0) return new ArrayList<>();
  137. Example example = new Example(UserPaper.class);
  138. example.and(
  139. example.createCriteria()
  140. .andEqualTo("paperOrigin", PaperOrigin.PREVIEW.key)
  141. .andIn("originId", ids)
  142. );
  143. return select(userPaperMapper, example);
  144. }
  145. /**
  146. * 获取用户预约预习作业记录
  147. * @param ids
  148. * @return
  149. */
  150. public List<UserPaper> listWithCourse(Integer userId, Collection ids, Integer recordId){
  151. if (ids == null || ids.size() == 0) return new ArrayList<>();
  152. Example example = new Example(UserPaper.class);
  153. example.and(
  154. example.createCriteria()
  155. .andEqualTo("userId", userId)
  156. .andEqualTo("recordId", recordId)
  157. .andEqualTo("paperOrigin", PaperOrigin.PREVIEW.key)
  158. .andIn("originId", ids)
  159. );
  160. return select(userPaperMapper, example);
  161. }
  162. /**
  163. * 获取用户cat做题记录
  164. * @param userId
  165. * @param ids
  166. * @param no
  167. * @return
  168. */
  169. public List<UserPaper> listWithCat(Integer userId, Collection ids, Integer no){
  170. if (ids == null || ids.size() == 0) return new ArrayList<>();
  171. Example example = new Example(UserPaper.class);
  172. example.and(
  173. example.createCriteria()
  174. .andEqualTo("userId", userId)
  175. .andEqualTo("paperOrigin", PaperOrigin.EXAMINATION.key)
  176. .andIn("originId", ids)
  177. .andEqualTo("qxCatNo", no)
  178. );
  179. return select(userPaperMapper, example);
  180. }
  181. /**
  182. * 获取用户做题组卷
  183. * @param userId
  184. * @param origin
  185. * @param originId
  186. * @return
  187. */
  188. public UserPaper getByPaper(Integer userId, PaperOrigin origin, Integer originId){
  189. // 查找对应的paper是否有,没有则添加
  190. Example example = new Example(UserPaper.class);
  191. UserPaper paper;
  192. if (origin.equals(PaperOrigin.ERROR) || origin.equals(PaperOrigin.COLLECT)){
  193. // 错题和收藏组卷,originId即为userPaper的id
  194. example.and(
  195. example.createCriteria()
  196. .andEqualTo("userId", userId)
  197. .andEqualTo("paperOrigin", origin.key)
  198. .andEqualTo("id", originId)
  199. );
  200. paper = one(userPaperMapper, example);
  201. if (paper == null){
  202. throw new ParameterException("试卷不存在");
  203. }
  204. }else{
  205. example.and(
  206. example.createCriteria()
  207. .andEqualTo("userId", userId)
  208. .andEqualTo("paperOrigin", origin.key)
  209. .andEqualTo("originId", originId)
  210. );
  211. paper = one(userPaperMapper, example);
  212. }
  213. return paper;
  214. }
  215. /**
  216. * 生成用户做题组卷
  217. * @param userId
  218. * @param origin
  219. * @param module
  220. * @param originId
  221. * @return
  222. */
  223. public UserPaper newByPaper(Integer userId, PaperOrigin origin, PaperModule module, Integer originId){
  224. return UserPaper.builder()
  225. .userId(userId)
  226. .paperOrigin(origin.key)
  227. .paperModule(module.key)
  228. .originId(originId)
  229. .build();
  230. }
  231. /**
  232. * 重置做题信息:不自动关联最后次做题记录
  233. * @param id
  234. * @return
  235. */
  236. public Boolean reset(Integer id, Integer userId){
  237. Example example = new Example(UserPaper.class);
  238. example.and(
  239. example.createCriteria()
  240. .andEqualTo("id", id)
  241. .andEqualTo("userId", userId)
  242. );
  243. return update(userPaperMapper, example, UserPaper.builder().isReset(1).build()) > 0;
  244. }
  245. /**
  246. * 重置做题信息:不自动关联最后次做题记录
  247. * @param ids
  248. * @return
  249. */
  250. public Boolean reset(Collection ids, Integer userId){
  251. if (ids == null || ids.size() == 0) return true;
  252. Example example = new Example(UserPaper.class);
  253. example.and(
  254. example.createCriteria()
  255. .andIn("id", ids)
  256. .andEqualTo("userId", userId)
  257. );
  258. return update(userPaperMapper, example, UserPaper.builder().isReset(1).build()) > 0;
  259. }
  260. /**
  261. * 累加做题记录到paper
  262. * @param report
  263. */
  264. public void accumulation(UserReport report){
  265. userPaperRelationMapper.accumulation(report.getPaperId(), report.getUserNumber(), report.getUserTime(), report.getUserCorrect(), 1, report.getFinishTime(), report.getId());
  266. }
  267. /**
  268. * 批量删除用户记录:preview切换可用学生
  269. * @param userIds
  270. * @param origin
  271. * @param originId
  272. * @return
  273. */
  274. public Boolean removeByUsersAndPaper(Collection<Integer> userIds, PaperOrigin origin, Integer originId){
  275. if(userIds == null || userIds.size() == 0) return false;
  276. Example example = new Example(UserPaper.class);
  277. example.and(
  278. example.createCriteria()
  279. .andIn("userId", userIds)
  280. .andEqualTo("paperOrigin", origin.key)
  281. .andEqualTo("originId", originId)
  282. );
  283. return delete(userPaperMapper, example, SOFT_FLAG) > 0;
  284. }
  285. /**
  286. * 批量修改用户记录:preview更新内容
  287. * @param userIds
  288. * @param origin
  289. * @param originId
  290. * @return
  291. */
  292. public Boolean editByUsersAndPaper(UserPaper userPaper, Collection<Integer> userIds, PaperOrigin origin, Integer originId){
  293. if(userIds == null || userIds.size() == 0) return false;
  294. Example example = new Example(UserPaper.class);
  295. example.and(
  296. example.createCriteria()
  297. .andIn("userId", userIds)
  298. .andEqualTo("paperOrigin", origin.key)
  299. .andEqualTo("originId", originId)
  300. );
  301. return update(userPaperMapper, example, userPaper) > 0;
  302. }
  303. public UserPaper add(UserPaper paper){
  304. int result = insert(userPaperMapper, paper);
  305. paper = one(userPaperMapper, paper.getId());
  306. if(paper == null){
  307. throw new SystemException("组卷添加失败");
  308. }
  309. return paper;
  310. }
  311. public UserPaper edit(UserPaper paper){
  312. UserPaper in = one(userPaperMapper, paper.getId());
  313. if(in == null){
  314. throw new ParameterException("组卷不存在");
  315. }
  316. int result = update(userPaperMapper, paper);
  317. return paper;
  318. }
  319. public boolean delete(Number id){
  320. UserPaper in = one(userPaperMapper, id);
  321. if(in == null){
  322. throw new ParameterException("组卷不存在");
  323. }
  324. int result = delete(userPaperMapper, id);
  325. return result > 0;
  326. }
  327. public UserPaper get(Number id){
  328. UserPaper in = one(userPaperMapper, id);
  329. if(in == null){
  330. throw new ParameterException("组卷不存在");
  331. }
  332. return in;
  333. }
  334. public Page<UserPaper> select(int page, int pageSize){
  335. return select(userPaperMapper, page, pageSize);
  336. }
  337. public Page<UserPaper> select(Integer[] ids){
  338. return page(()->select(userPaperMapper, ids), 1, ids.length);
  339. }
  340. public List<UserPaper> select(Collection ids){
  341. return select(userPaperMapper, ids);
  342. }
  343. }