TCPaperStructController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.qmth.distributed.print.api;
  2. import com.google.gson.reflect.TypeToken;
  3. import com.qmth.boot.api.constant.ApiConstant;
  4. import com.qmth.boot.api.exception.ApiException;
  5. import com.qmth.distributed.print.business.bean.dto.CourseWeightDto;
  6. import com.qmth.distributed.print.business.bean.excel.PaperStructDto;
  7. import com.qmth.distributed.print.business.bean.params.report.PaperStructParams;
  8. import com.qmth.distributed.print.business.bean.result.CourseWeightResult;
  9. import com.qmth.distributed.print.business.bean.result.EditResult;
  10. import com.qmth.distributed.print.business.bean.result.report.PaperStructDimensionResult;
  11. import com.qmth.distributed.print.business.entity.TCPaperStruct;
  12. import com.qmth.distributed.print.business.service.PrintCommonService;
  13. import com.qmth.distributed.print.business.service.TCPaperStructService;
  14. import com.qmth.distributed.print.business.service.TRBasicInfoService;
  15. import com.qmth.teachcloud.common.annotation.OperationLogDetail;
  16. import com.qmth.teachcloud.common.contant.SystemConstant;
  17. import com.qmth.teachcloud.common.entity.MarkQuestion;
  18. import com.qmth.teachcloud.common.entity.SysUser;
  19. import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
  20. import com.qmth.teachcloud.common.enums.log.CustomizedOperationTypeEnum;
  21. import com.qmth.teachcloud.common.util.*;
  22. import com.qmth.teachcloud.mark.entity.MarkPaper;
  23. import com.qmth.teachcloud.mark.service.MarkQuestionService;
  24. import io.swagger.annotations.*;
  25. import org.apache.commons.collections4.CollectionUtils;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.BeanUtils;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import org.springframework.validation.BindingResult;
  31. import org.springframework.web.bind.annotation.*;
  32. import org.springframework.web.multipart.MultipartFile;
  33. import javax.annotation.Resource;
  34. import javax.validation.Valid;
  35. import java.io.IOException;
  36. import java.math.BigDecimal;
  37. import java.util.ArrayList;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.Objects;
  41. import java.util.stream.Collectors;
  42. /**
  43. * <p>
  44. * 试卷结构 前端控制器
  45. * </p>
  46. *
  47. * @author wangliang
  48. * @since 2024-02-18
  49. */
  50. @Api(tags = "课程目标达成度-成绩管理-试卷结构Controller")
  51. @RestController
  52. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_COURSE_DEGREE)
  53. public class TCPaperStructController {
  54. private final static Logger log = LoggerFactory.getLogger(TCPaperStructController.class);
  55. @Resource
  56. MarkQuestionService markQuestionService;
  57. @Resource
  58. TCPaperStructService tcPaperStructService;
  59. @Resource
  60. TRBasicInfoService trBasicInfoService;
  61. @Resource
  62. PrintCommonService printCommonService;
  63. @Resource
  64. RedisUtil redisUtil;
  65. @ApiOperation(value = "导入试卷结构")
  66. @RequestMapping(value = "/final_score/paper_struct/import", method = RequestMethod.POST)
  67. @OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.IMPORT)
  68. @ApiResponses({@ApiResponse(code = 200, message = "导入成功", response = EditResult.class)})
  69. public Result finalScorePaperStructImport(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
  70. @ApiParam(value = "考试id", required = true) @RequestParam Long examId,
  71. @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
  72. @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber) throws IOException {
  73. printCommonService.getLock(examId, courseCode, paperNumber);
  74. String lockKey = SystemConstant.REDIS_PAPER_STRUCT_FLOW_PREFIX + SystemConstant.IMPORT + examId + "_" + courseCode + "_" + paperNumber;
  75. Map<String, String> map = null;
  76. try {
  77. map = tcPaperStructService.paperStructExcelImport(file, examId, courseCode, paperNumber);
  78. } catch (Exception e) {
  79. log.error(SystemConstant.LOG_ERROR, e);
  80. if (e instanceof ApiException) {
  81. ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
  82. } else {
  83. ResultUtil.error(e.getMessage());
  84. }
  85. } finally {
  86. redisUtil.releaseLock(lockKey);
  87. }
  88. return ResultUtil.ok(map);
  89. }
  90. @ApiOperation(value = "同步试卷蓝图结构")
  91. @RequestMapping(value = "/final_score/paper_struct_dimension/sync", method = RequestMethod.POST)
  92. @OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.PUSH)
  93. @ApiResponses({@ApiResponse(code = 200, message = "同步成功", response = EditResult.class)})
  94. public Result finalScorePaperStructDimensionSync(@ApiParam(value = "考试id", required = true) @RequestParam Long examId,
  95. @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
  96. @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber) throws IOException {
  97. printCommonService.getLock(examId, courseCode, paperNumber);
  98. String lockKey = SystemConstant.REDIS_PAPER_STRUCT_FLOW_PREFIX + SystemConstant.SYNC + examId + "_" + courseCode + "_" + paperNumber;
  99. Map<String, String> map = null;
  100. try {
  101. map = tcPaperStructService.paperStructSync(examId, courseCode, paperNumber);
  102. } catch (Exception e) {
  103. log.error(SystemConstant.LOG_ERROR, e);
  104. if (e instanceof ApiException) {
  105. ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
  106. } else {
  107. ResultUtil.error(e.getMessage());
  108. }
  109. } finally {
  110. redisUtil.releaseLock(lockKey);
  111. }
  112. return ResultUtil.ok(map);
  113. }
  114. @ApiOperation(value = "期末成绩试卷蓝图保存")
  115. @RequestMapping(value = "/final_score/paper_struct/save", method = RequestMethod.POST)
  116. @OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.UPDATE)
  117. @ApiResponses({@ApiResponse(code = 200, message = "试卷蓝图保存", response = Object.class)})
  118. @Transactional
  119. public Result finalScorePaperStructSave(@ApiParam(value = "试卷蓝图结构", required = true) @Valid @RequestBody PaperStructParams paperStructParams, BindingResult bindingResult) throws IOException {
  120. if (bindingResult.hasErrors()) {
  121. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  122. }
  123. printCommonService.getLock(paperStructParams.getExamId(), paperStructParams.getCourseCode(), paperStructParams.getPaperNumber());
  124. CourseWeightResult courseWeightResult = trBasicInfoService.findCourseWeightResultRmi(paperStructParams.getExamId(), paperStructParams.getCourseCode());
  125. for (CourseWeightDto c : courseWeightResult.getSubmitForm()) {
  126. for (PaperStructDimensionResult paperStructDimensionResult : paperStructParams.getPaperStruct()) {
  127. Objects.requireNonNull(paperStructDimensionResult.getMainNumber(), "大题号为空");
  128. Objects.requireNonNull(paperStructDimensionResult.getSubNumber(), "小题号为空");
  129. if (!CollectionUtils.isEmpty(paperStructDimensionResult.getTargetList()) && paperStructDimensionResult.getTargetList().size() > 1) {
  130. throw ExceptionResultEnum.ERROR.exception("一个题只能属于一个目标");
  131. }
  132. }
  133. List<PaperStructDimensionResult> paperStructDimensionResultList = paperStructParams.getPaperStruct();
  134. Double score = paperStructDimensionResultList.stream().filter(s -> Objects.equals(s.getCourseTargetName(), c.getCourseTargetName())).mapToDouble(PaperStructDimensionResult::getScore).sum();
  135. if (new BigDecimal(score).compareTo(c.getTotalWeight()) == 1) {
  136. throw ExceptionResultEnum.ERROR.exception("[" + c.getCourseTargetName() + "]知识点小题总分大于该课程目标分,请重新设置");
  137. }
  138. paperStructDimensionResultList.stream().filter(s -> {
  139. if (Objects.equals(s.getCourseTargetName(), c.getCourseTargetName())) {
  140. s.getTargetList().stream().peek(e -> e.setFinalScoreQuestionScoreSum(score)).collect(Collectors.toList());
  141. return true;
  142. }
  143. return false;
  144. }).collect(Collectors.toList());
  145. }
  146. SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
  147. TCPaperStruct tcPaperStructDb = tcPaperStructService.queryByExamIdAndCourseCodeAndPaperNumber(paperStructParams.getExamId(), paperStructParams.getCourseCode(), paperStructParams.getPaperNumber());
  148. if (Objects.isNull(tcPaperStructDb)) {
  149. MarkPaper markPaper = printCommonService.getMarkPaper(paperStructParams.getExamId(), paperStructParams.getPaperNumber());
  150. tcPaperStructDb = new TCPaperStruct(paperStructParams.getExamId(), paperStructParams.getCourseCode(), markPaper.getCourseName(), paperStructParams.getPaperNumber(), JacksonUtil.parseJson(paperStructParams.getPaperStruct()), sysUser.getId(), courseWeightResult.getDimensionSign());
  151. tcPaperStructService.save(tcPaperStructDb);
  152. } else {
  153. TCPaperStruct tcPaperStructSource = new TCPaperStruct();
  154. BeanUtils.copyProperties(tcPaperStructDb, tcPaperStructSource);
  155. tcPaperStructDb.updateInfo(JacksonUtil.parseJson(paperStructParams.getPaperStruct()), sysUser.getId());
  156. if (!tcPaperStructDb.equals(tcPaperStructSource)) {
  157. trBasicInfoService.clearReportData(tcPaperStructDb.getExamId(), tcPaperStructDb.getCourseCode(), tcPaperStructDb.getPaperNumber(), false);
  158. tcPaperStructDb.setDimensionSign(courseWeightResult.getDimensionSign());
  159. tcPaperStructService.updateById(tcPaperStructDb);
  160. }
  161. }
  162. return ResultUtil.ok(true);
  163. }
  164. @ApiOperation(value = "期末成绩试卷蓝图查询")
  165. @RequestMapping(value = "/final_score/paper_struct/query", method = RequestMethod.POST)
  166. @OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.SEARCH)
  167. @ApiResponses({@ApiResponse(code = 200, message = "试卷蓝图保存", response = PaperStructDimensionResult.class)})
  168. public Result finalScorePaperStructQuery(@ApiParam(value = "考试id", required = true) @RequestParam Long examId,
  169. @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
  170. @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber) throws IOException {
  171. printCommonService.getLock(examId, courseCode, paperNumber);
  172. List<PaperStructDimensionResult> paperStructDimensionResultList = null;
  173. TCPaperStruct tcPaperStruct = tcPaperStructService.queryByExamIdAndCourseCodeAndPaperNumber(examId, courseCode, paperNumber);
  174. if (Objects.isNull(tcPaperStruct) || Objects.isNull(tcPaperStruct.getPaperStruct())) {
  175. List<MarkQuestion> markQuestionList = markQuestionService.listQuestionByExamIdAndPaperNumberAndPaperType(examId, paperNumber, null);
  176. if (CollectionUtils.isEmpty(markQuestionList)) {
  177. throw ExceptionResultEnum.ERROR.exception("未找到试卷结构");
  178. }
  179. paperStructDimensionResultList = new ArrayList<>(markQuestionList.size());
  180. for (MarkQuestion markQuestion : markQuestionList) {
  181. paperStructDimensionResultList.add(new PaperStructDimensionResult(markQuestion.getMainNumber(), markQuestion.getSubNumber(), markQuestion.getTotalScore()));
  182. }
  183. } else {
  184. CourseWeightResult courseWeightResult = trBasicInfoService.findCourseWeightResultRmi(examId, courseCode);
  185. if (Objects.nonNull(tcPaperStruct.getDimensionSign()) && tcPaperStruct.getDimensionSign().longValue() != courseWeightResult.getDimensionSign().longValue()) {
  186. trBasicInfoService.clearReportData(examId, courseCode, paperNumber, false);
  187. paperStructDimensionResultList = this.getPaperStructDimensionResult(tcPaperStruct, paperStructDimensionResultList);
  188. } else {
  189. paperStructDimensionResultList = this.getPaperStructDimensionResult(tcPaperStruct, paperStructDimensionResultList);
  190. }
  191. }
  192. return ResultUtil.ok(paperStructDimensionResultList);
  193. }
  194. /**
  195. * 获取试卷结构蓝图数据
  196. *
  197. * @param tcPaperStruct
  198. * @param paperStructDimensionResultList
  199. * @return
  200. */
  201. protected List<PaperStructDimensionResult> getPaperStructDimensionResult(TCPaperStruct tcPaperStruct,
  202. List<PaperStructDimensionResult> paperStructDimensionResultList) {
  203. if (Objects.nonNull(tcPaperStruct.getPaperStructDimension())) {
  204. paperStructDimensionResultList = GsonUtil.fromJson(tcPaperStruct.getPaperStructDimension(), new TypeToken<List<PaperStructDimensionResult>>() {
  205. }.getType());
  206. } else if (Objects.nonNull(tcPaperStruct.getPaperStruct())) {
  207. List<PaperStructDto> paperStructDtoList = GsonUtil.fromJson(tcPaperStruct.getPaperStruct(), new TypeToken<List<PaperStructDto>>() {
  208. }.getType());
  209. paperStructDimensionResultList = new ArrayList<>(paperStructDtoList.size());
  210. for (PaperStructDto paperStructDto : paperStructDtoList) {
  211. paperStructDimensionResultList.add(new PaperStructDimensionResult(paperStructDto.getMainNumber(), paperStructDto.getSubNumber(), paperStructDto.getScore()));
  212. }
  213. }
  214. return paperStructDimensionResultList;
  215. }
  216. }