UserPaperService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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, Date startTime, Date 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, Date startTime, Date 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, Date startTime, Date 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. // 如果是千行cat,则获取最后一次
  212. example.orderBy("paperNo").desc();
  213. paper = one(userPaperMapper, example);
  214. }
  215. return paper;
  216. }
  217. /**
  218. * 生成用户做题组卷
  219. * @param userId
  220. * @param origin
  221. * @param module
  222. * @param originId
  223. * @return
  224. */
  225. public UserPaper newByPaper(Integer userId, PaperOrigin origin, PaperModule module, Integer originId){
  226. return UserPaper.builder()
  227. .userId(userId)
  228. .paperOrigin(origin.key)
  229. .paperModule(module.key)
  230. .originId(originId)
  231. .build();
  232. }
  233. /**
  234. * 重置做题信息:不自动关联最后次做题记录
  235. * @param id
  236. * @return
  237. */
  238. public Boolean reset(Integer id, Integer userId){
  239. Example example = new Example(UserPaper.class);
  240. example.and(
  241. example.createCriteria()
  242. .andEqualTo("id", id)
  243. .andEqualTo("userId", userId)
  244. );
  245. return update(userPaperMapper, example, UserPaper.builder().isReset(1).build()) > 0;
  246. }
  247. /**
  248. * 重置做题信息:不自动关联最后次做题记录
  249. * @param ids
  250. * @return
  251. */
  252. public Boolean reset(Collection ids, Integer userId){
  253. if (ids == null || ids.size() == 0) return true;
  254. Example example = new Example(UserPaper.class);
  255. example.and(
  256. example.createCriteria()
  257. .andIn("id", ids)
  258. .andEqualTo("userId", userId)
  259. );
  260. return update(userPaperMapper, example, UserPaper.builder().isReset(1).build()) > 0;
  261. }
  262. /**
  263. * 累加做题记录到paper
  264. * @param report
  265. */
  266. public void accumulation(UserReport report){
  267. userPaperRelationMapper.accumulation(report.getPaperId(), report.getUserNumber(), report.getUserTime(), report.getUserCorrect(), 1, report.getIsFinish(), report.getFinishTime(), report.getId());
  268. }
  269. public int countByUser(Integer userId, PaperOrigin origin){
  270. Example example = new Example(UserPaper.class);
  271. example.and(
  272. example.createCriteria()
  273. .andEqualTo("userId", userId)
  274. .andEqualTo("paperOrigin", origin.key)
  275. );
  276. return count(userPaperMapper, example);
  277. }
  278. /**
  279. * 批量删除用户记录:preview切换可用学生
  280. * @param userIds
  281. * @param origin
  282. * @param originId
  283. * @return
  284. */
  285. public Boolean removeByUsersAndPaper(Collection<Integer> userIds, PaperOrigin origin, Integer originId){
  286. if(userIds == null || userIds.size() == 0) return false;
  287. Example example = new Example(UserPaper.class);
  288. example.and(
  289. example.createCriteria()
  290. .andIn("userId", userIds)
  291. .andEqualTo("paperOrigin", origin.key)
  292. .andEqualTo("originId", originId)
  293. );
  294. return delete(userPaperMapper, example, SOFT_FLAG) > 0;
  295. }
  296. /**
  297. * 批量修改用户记录:preview更新内容
  298. * @param userIds
  299. * @param origin
  300. * @param originId
  301. * @return
  302. */
  303. public Boolean editByUsersAndPaper(UserPaper userPaper, Collection<Integer> userIds, PaperOrigin origin, Integer originId){
  304. if(userIds == null || userIds.size() == 0) return false;
  305. Example example = new Example(UserPaper.class);
  306. example.and(
  307. example.createCriteria()
  308. .andIn("userId", userIds)
  309. .andEqualTo("paperOrigin", origin.key)
  310. .andEqualTo("originId", originId)
  311. );
  312. return update(userPaperMapper, example, userPaper) > 0;
  313. }
  314. public UserPaper add(UserPaper paper){
  315. int result = insert(userPaperMapper, paper);
  316. paper = one(userPaperMapper, paper.getId());
  317. if(paper == null){
  318. throw new SystemException("组卷添加失败");
  319. }
  320. return paper;
  321. }
  322. public UserPaper edit(UserPaper paper){
  323. UserPaper in = one(userPaperMapper, paper.getId());
  324. if(in == null){
  325. throw new ParameterException("组卷不存在");
  326. }
  327. int result = update(userPaperMapper, paper);
  328. return paper;
  329. }
  330. public boolean delete(Number id){
  331. UserPaper in = one(userPaperMapper, id);
  332. if(in == null){
  333. throw new ParameterException("组卷不存在");
  334. }
  335. int result = delete(userPaperMapper, id);
  336. return result > 0;
  337. }
  338. public UserPaper get(Number id){
  339. UserPaper in = one(userPaperMapper, id);
  340. if(in == null){
  341. throw new ParameterException("组卷不存在");
  342. }
  343. return in;
  344. }
  345. public Page<UserPaper> select(int page, int pageSize){
  346. return select(userPaperMapper, page, pageSize);
  347. }
  348. public Page<UserPaper> select(Integer[] ids){
  349. return page(()->select(userPaperMapper, ids), 1, ids.length);
  350. }
  351. public List<UserPaper> select(Collection ids){
  352. return select(userPaperMapper, ids);
  353. }
  354. }