TEExamPaperController.java 9.2 KB

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