TEExamPaperController.java 9.4 KB

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