TEExamPaperController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.qmth.themis.admin.api;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.qmth.themis.business.annotation.Logs;
  4. import com.qmth.themis.business.cache.bean.ExamCacheBean;
  5. import com.qmth.themis.business.cache.bean.ExamCourseCacheBean;
  6. import com.qmth.themis.business.cache.bean.ExamPaperCacheBean;
  7. import com.qmth.themis.business.constant.SystemConstant;
  8. import com.qmth.themis.business.dto.MqDto;
  9. import com.qmth.themis.business.entity.*;
  10. import com.qmth.themis.business.enums.*;
  11. import com.qmth.themis.business.service.*;
  12. import com.qmth.themis.business.util.MqUtil;
  13. import com.qmth.themis.business.util.OssUtil;
  14. import com.qmth.themis.business.util.ServletUtil;
  15. import com.qmth.themis.business.util.UidUtil;
  16. import com.qmth.themis.common.enums.ExceptionResultEnum;
  17. import com.qmth.themis.common.exception.BusinessException;
  18. import com.qmth.themis.common.util.GsonUtil;
  19. import com.qmth.themis.common.util.Result;
  20. import com.qmth.themis.common.util.ResultUtil;
  21. import io.swagger.annotations.*;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.dao.DuplicateKeyException;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import org.springframework.web.bind.annotation.*;
  27. import org.springframework.web.multipart.MultipartFile;
  28. import javax.annotation.Resource;
  29. import java.util.*;
  30. /**
  31. * @Description: 考试试卷 前端控制器
  32. * @Param:
  33. * @return:
  34. * @Author: wangliang
  35. * @Date: 2020/6/25
  36. */
  37. @Api(tags = "考试试卷Controller")
  38. @RestController
  39. @RequestMapping(SystemConstant.PREFIX_URL_ADMIN + "/exam/paper")
  40. public class TEExamPaperController {
  41. private final static Logger log = LoggerFactory.getLogger(TEExamPaperController.class);
  42. @Resource
  43. TEExamPaperService teExamPaperService;
  44. @Resource
  45. TBTaskHistoryService taskHistoryService;
  46. @Resource
  47. TEExamService teExamService;
  48. @Resource
  49. TBAttachmentService tbAttachmentService;
  50. @Resource
  51. MqDtoService mqDtoService;
  52. @Resource
  53. OssUtil ossUtil;
  54. @Resource
  55. MqUtil mqUtil;
  56. @Resource
  57. TEExamCourseService examCourseService;
  58. @Resource
  59. TOeExamRecordService tOeExamRecordService;
  60. @ApiOperation(value = "考试试卷查询接口")
  61. @RequestMapping(value = "/query", method = RequestMethod.POST)
  62. @ApiResponses({@ApiResponse(code = 200, message = "考试科目信息", response = TEExamPaper.class)})
  63. public Result query(@ApiParam(value = "考试批次id", required = true) @RequestParam Long examId,
  64. @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode) {
  65. if (Objects.isNull(examId) || Objects.equals(examId, "")) {
  66. throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
  67. }
  68. if (Objects.isNull(courseCode) || Objects.equals(courseCode, "")) {
  69. throw new BusinessException(ExceptionResultEnum.COURSE_CODE_IS_NULL);
  70. }
  71. QueryWrapper<TEExamPaper> teExamPaperQueryWrapper = new QueryWrapper<>();
  72. teExamPaperQueryWrapper.lambda().eq(TEExamPaper::getExamId, examId).eq(TEExamPaper::getCourseCode, courseCode);
  73. return ResultUtil.ok(teExamPaperService.list(teExamPaperQueryWrapper));
  74. }
  75. @ApiOperation(value = "考试试卷参数修改接口")
  76. @RequestMapping(value = "/save", method = RequestMethod.POST)
  77. @Transactional
  78. @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
  79. @Logs(value = LogEnum.EXAM_PAPER)
  80. public Result save(@ApiParam(value = "考试试卷信息", required = true) @RequestBody List<TEExamPaper> teExamPaperList) {
  81. if (Objects.isNull(teExamPaperList) || teExamPaperList.size() == 0) {
  82. throw new BusinessException(ExceptionResultEnum.PAPER_INFO_IS_NULL);
  83. }
  84. try {
  85. TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
  86. teExamPaperList.forEach(s -> {
  87. if (Objects.isNull(s.getId())) {
  88. s.setId(UidUtil.nextId());
  89. s.setCreateTime(System.currentTimeMillis());
  90. }
  91. s.setUpdateId(tbUser.getId());
  92. s.setUpdateTime(System.currentTimeMillis());
  93. teExamPaperService.saveOrUpdate(s);
  94. });
  95. } catch (Exception e) {
  96. log.error(SystemConstant.LOG_ERROR, e);
  97. if (e instanceof DuplicateKeyException) {
  98. String errorColumn = e.getCause().toString();
  99. String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3, errorColumn.length())
  100. .replaceAll("'", "");
  101. throw new BusinessException(
  102. "机构id[" + teExamPaperList.get(0).getExamId() + "]下的" + FieldUniqueEnum.convertToCode(columnStr)
  103. + "数据不允许重复插入");
  104. } else if (e instanceof BusinessException) {
  105. throw new BusinessException(e.getMessage());
  106. } else {
  107. throw new RuntimeException(e);
  108. }
  109. }
  110. teExamPaperList.forEach(s -> {
  111. teExamPaperService.updateExamPaperCacheBean(s.getId());
  112. ExamPaperCacheBean paper = teExamPaperService.getExamPaperCacheBean(s.getId());
  113. examCourseService.updateExamCourseCacheBean(paper.getExamId(), paper.getCourseCode());
  114. });
  115. return ResultUtil.ok(true);
  116. }
  117. @ApiOperation(value = "考试试卷数据包上传接口")
  118. @RequestMapping(value = "/import", method = RequestMethod.POST)
  119. @Transactional
  120. @ApiResponses({@ApiResponse(code = 200, message = "{\"taskId\":0}", response = Result.class)})
  121. public Result importData(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
  122. @ApiParam(value = "批次ID", required = true) @RequestParam Long examId,
  123. @ApiParam(value = "客观题乱序", required = true) @RequestParam Boolean objectiveShuffle,
  124. @ApiParam(value = "选项乱序", required = true) @RequestParam Boolean optionShuffle,
  125. @ApiParam(value = "音频播放次数", required = true) @RequestParam Integer audioPlayCount,
  126. @ApiParam(value = "是否允许使用移动端拍照答题", required = true) @RequestParam Integer mobilePhotoUpload,
  127. @ApiParam(value = "解析试卷", required = true) @RequestParam Boolean processPaper,
  128. @ApiParam(value = "解析标答", required = true) @RequestParam Boolean processAnswer) {
  129. if (file == null) {
  130. throw new BusinessException(ExceptionResultEnum.ATTACHMENT_IS_NULL);
  131. }
  132. if (examId == null) {
  133. throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
  134. }
  135. Map<String, Object> mapParameter = ossUtil.getAliYunOssPrivateDomain().getMap();
  136. TBAttachment tbAttachment = null;
  137. TBTaskHistory tbTaskHistory = null;
  138. Map<String, Object> transMap = new HashMap<String, Object>();
  139. try {
  140. TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
  141. tbAttachment = tbAttachmentService
  142. .saveAttachment(file, ServletUtil.getRequestMd5(), ServletUtil.getRequestPath(), mapParameter,
  143. UploadFileEnum.file, tbUser.getOrgId(), tbUser.getId());
  144. if (Objects.isNull(tbAttachment)) {
  145. throw new BusinessException(ExceptionResultEnum.ATTACHMENT_ERROR);
  146. } else {
  147. //往任务表里插一条数据
  148. tbTaskHistory = new TBTaskHistory(TaskTypeEnum.IMPORT_EXAM_PAPER, examId, TaskStatusEnum.INIT,
  149. SystemConstant.IMPORT_INIT, 0d, tbAttachment.getName(), tbAttachment.getRemark(),
  150. tbUser.getId(), tbUser.getOrgId());
  151. tbTaskHistory.setExamId(examId);
  152. taskHistoryService.save(tbTaskHistory);
  153. transMap.put("tbTaskHistory", tbTaskHistory);
  154. }
  155. transMap.put(SystemConstant.EXAM_ID, examId);
  156. transMap.put(SystemConstant.CREATE_ID, tbUser.getId());
  157. transMap.put(SystemConstant.ORG_ID, tbUser.getOrgId());
  158. //先查询考试相关信息
  159. ExamCacheBean examCacheBean = teExamService.getExamCacheBean(examId);
  160. if (Objects.isNull(examCacheBean)) {
  161. throw new BusinessException(ExceptionResultEnum.EXAM_NO);
  162. }
  163. // TEExam teExam = teExamService.cacheConvert(examCacheBean);
  164. transMap.put("objectiveShuffle", objectiveShuffle);
  165. transMap.put("optionShuffle", optionShuffle);
  166. transMap.put("audioPlayCount", audioPlayCount);
  167. transMap.put("processPaper", processPaper);
  168. transMap.put("processAnswer", processAnswer);
  169. transMap.put("mobilePhotoUpload", mobilePhotoUpload);
  170. transMap.put(SystemConstant.REMARK, tbAttachment.getRemark());
  171. //mq发送消息start
  172. MqDto mqDto = new MqDto(mqUtil.getTopic(), MqTagEnum.EXAM_PAPER_IMPORT.name(), transMap,
  173. MqTagEnum.EXAM_PAPER_IMPORT, String.valueOf(tbTaskHistory.getId()), tbUser.getName());
  174. mqDtoService.assembleSendOneWayMsg(mqDto);
  175. //mq发送消息end
  176. } catch (Exception e) {
  177. log.error(SystemConstant.LOG_ERROR, e);
  178. if (Objects.nonNull(tbAttachment)) {
  179. tbAttachmentService.deleteAttachment(mapParameter, UploadFileEnum.file, tbAttachment);
  180. }
  181. if (e instanceof BusinessException) {
  182. throw new BusinessException(e.getMessage());
  183. } else {
  184. throw new RuntimeException(e);
  185. }
  186. }
  187. return ResultUtil.ok(Collections.singletonMap(SystemConstant.TASK_ID, tbTaskHistory.getId()));
  188. }
  189. @ApiOperation(value = "试卷删除接口")
  190. @RequestMapping(value = "/delete", method = RequestMethod.POST)
  191. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = Result.class)})
  192. @Transactional
  193. public Result delete(@ApiParam(value = "试卷id", required = true) @RequestParam Long id) {
  194. int count = tOeExamRecordService.count(new QueryWrapper<TOeExamRecord>().lambda().eq(TOeExamRecord::getPaperId, id));
  195. if (count > 0) {
  196. throw new BusinessException("该试卷已存在考试记录,无法删除");
  197. }
  198. ExamPaperCacheBean examPaperCacheBean = teExamPaperService.getExamPaperCacheBean(id);
  199. Optional.ofNullable(examPaperCacheBean).orElseThrow(() -> new BusinessException("试卷数据为空"));
  200. ExamCourseCacheBean examCourseCacheBean = examCourseService.getExamCourseCacheBean(examPaperCacheBean.getExamId(), examPaperCacheBean.getCourseCode());
  201. Optional.ofNullable(examCourseCacheBean).orElseThrow(() -> new BusinessException("科目数据为空"));
  202. Integer paperCount = Objects.nonNull(examCourseCacheBean.getPaperCount()) ? examCourseCacheBean.getPaperCount() : 0;
  203. paperCount = paperCount.intValue() - 1;
  204. examCourseCacheBean.setPaperCount(Objects.isNull(paperCount) || paperCount.intValue() < 0 ? 0 : paperCount);
  205. TEExamCourse teExamCourse = GsonUtil.fromJson(GsonUtil.toJson(examCourseCacheBean), TEExamCourse.class);
  206. teExamCourse.setUpdateTime(System.currentTimeMillis());
  207. examCourseService.saveOrUpdate(teExamCourse);
  208. examCourseService.updateExamCourseCacheBean(examPaperCacheBean.getExamId(), examPaperCacheBean.getCourseCode());
  209. teExamPaperService.removeById(id);
  210. teExamPaperService.deleteExamPaperCacheBean(id);
  211. return ResultUtil.ok(true);
  212. }
  213. }