TCPaperStructController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.distributed.print.business.bean.dto.CourseWeightDto;
  5. import com.qmth.distributed.print.business.bean.excel.PaperStructDto;
  6. import com.qmth.distributed.print.business.bean.params.report.PaperStructParams;
  7. import com.qmth.distributed.print.business.bean.result.CourseWeightResult;
  8. import com.qmth.distributed.print.business.bean.result.EditResult;
  9. import com.qmth.distributed.print.business.bean.result.report.PaperStructDimensionResult;
  10. import com.qmth.distributed.print.business.entity.TCPaperStruct;
  11. import com.qmth.distributed.print.business.service.TCFinalScoreService;
  12. import com.qmth.distributed.print.business.service.TCPaperStructService;
  13. import com.qmth.distributed.print.business.service.TRBasicInfoService;
  14. import com.qmth.teachcloud.common.annotation.OperationLogDetail;
  15. import com.qmth.teachcloud.common.contant.SystemConstant;
  16. import com.qmth.teachcloud.common.entity.MarkQuestion;
  17. import com.qmth.teachcloud.common.entity.SysUser;
  18. import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
  19. import com.qmth.teachcloud.common.enums.log.CustomizedOperationTypeEnum;
  20. import com.qmth.teachcloud.common.util.*;
  21. import com.qmth.teachcloud.mark.entity.MarkPaper;
  22. import com.qmth.teachcloud.mark.service.MarkPaperService;
  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.Objects;
  40. /**
  41. * <p>
  42. * 试卷结构 前端控制器
  43. * </p>
  44. *
  45. * @author wangliang
  46. * @since 2024-02-18
  47. */
  48. @Api(tags = "课程目标达成度-成绩管理-试卷结构Controller")
  49. @RestController
  50. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_COURSE_DEGREE)
  51. public class TCPaperStructController {
  52. private final static Logger log = LoggerFactory.getLogger(TCPaperStructController.class);
  53. @Resource
  54. TCFinalScoreService tcFinalScoreService;
  55. @Resource
  56. MarkQuestionService markQuestionService;
  57. @Resource
  58. TCPaperStructService tcPaperStructService;
  59. @Resource
  60. MarkPaperService markPaperService;
  61. @Resource
  62. RedisUtil redisUtil;
  63. @Resource
  64. TRBasicInfoService trBasicInfoService;
  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. String lockKey = SystemConstant.REDIS_FINAL_SCORE_DATA_FLOW_PREFIX + SystemConstant.SYNC + examId + "_" + courseCode + "_" + paperNumber;
  74. Object o = redisUtil.get(lockKey);
  75. if (Objects.nonNull(o)) {
  76. throw ExceptionResultEnum.ERROR.exception("正在同步数据,请稍候再试!");
  77. }
  78. return ResultUtil.ok(tcPaperStructService.paperStructExcelImport(file, examId, courseCode, paperNumber));
  79. }
  80. @ApiOperation(value = "期末成绩试卷蓝图保存")
  81. @RequestMapping(value = "/final_score/paper_struct/save", method = RequestMethod.POST)
  82. @OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.UPDATE)
  83. @ApiResponses({@ApiResponse(code = 200, message = "试卷蓝图保存", response = Object.class)})
  84. @Transactional
  85. public Result finalScorePaperStructSave(@ApiParam(value = "试卷蓝图结构", required = true) @Valid @RequestBody PaperStructParams paperStructParams, BindingResult bindingResult) throws IOException {
  86. if (bindingResult.hasErrors()) {
  87. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  88. }
  89. String lockKey = SystemConstant.REDIS_FINAL_SCORE_DATA_FLOW_PREFIX + SystemConstant.SYNC + paperStructParams.getExamId() + "_" + paperStructParams.getCourseCode() + "_" + paperStructParams.getPaperNumber();
  90. Object o = redisUtil.get(lockKey);
  91. if (Objects.nonNull(o)) {
  92. throw ExceptionResultEnum.ERROR.exception("正在同步数据,请稍候再试!");
  93. }
  94. CourseWeightResult courseWeightResult = trBasicInfoService.findCourseWeightResultRmi(paperStructParams.getExamId(), paperStructParams.getCourseCode());
  95. for (CourseWeightDto c : courseWeightResult.getSubmitForm()) {
  96. for (PaperStructDimensionResult paperStructDimensionResult : paperStructParams.getPaperStruct()) {
  97. Objects.requireNonNull(paperStructDimensionResult.getMainNumber(), "大题号为空");
  98. Objects.requireNonNull(paperStructDimensionResult.getSubNumber(), "小题号为空");
  99. if (!CollectionUtils.isEmpty(paperStructDimensionResult.getTargetList()) && paperStructDimensionResult.getTargetList().size() > 1) {
  100. throw ExceptionResultEnum.ERROR.exception("一个题只能属于一个目标");
  101. }
  102. }
  103. Double score = paperStructParams.getPaperStruct().stream().filter(s -> Objects.equals(s.getCourseTargetName(), c.getCourseTargetName())).mapToDouble(PaperStructDimensionResult::getScore).sum();
  104. if (new BigDecimal(score).compareTo(c.getTotalWeight()) == 1) {
  105. throw ExceptionResultEnum.ERROR.exception("[" + c.getCourseTargetName() + "]知识点小题总分大于该课程目标分,请重新设置");
  106. }
  107. }
  108. SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
  109. TCPaperStruct tcPaperStructDb = tcPaperStructService.queryByExamIdAndCourseCodeAndPaperNumber(paperStructParams.getExamId(), paperStructParams.getCourseCode(), paperStructParams.getPaperNumber());
  110. if (Objects.isNull(tcPaperStructDb)) {
  111. MarkPaper markPaper = markPaperService.getByExamIdAndPaperNumber(paperStructParams.getExamId(), paperStructParams.getPaperNumber());
  112. Objects.requireNonNull(markPaper, "未找到科目信息");
  113. tcPaperStructDb = new TCPaperStruct(paperStructParams.getExamId(), paperStructParams.getCourseCode(), markPaper.getCourseName(), paperStructParams.getPaperNumber(), JacksonUtil.parseJson(paperStructParams.getPaperStruct()), sysUser.getId(), courseWeightResult.getDimensionSign());
  114. tcPaperStructService.save(tcPaperStructDb);
  115. } else {
  116. TCPaperStruct tcPaperStructSource = new TCPaperStruct();
  117. BeanUtils.copyProperties(tcPaperStructDb, tcPaperStructSource);
  118. tcPaperStructDb.updateInfo(JacksonUtil.parseJson(paperStructParams.getPaperStruct()), sysUser.getId());
  119. if (!tcPaperStructDb.equals(tcPaperStructSource)) {
  120. if (Objects.nonNull(tcPaperStructDb.getDimensionSign()) && tcPaperStructDb.getDimensionSign().longValue() != courseWeightResult.getDimensionSign().longValue()) {
  121. trBasicInfoService.clearReportData(tcPaperStructDb.getExamId(), tcPaperStructDb.getCourseCode(), tcPaperStructDb.getPaperNumber(), false);
  122. }
  123. tcPaperStructDb.setDimensionSign(courseWeightResult.getDimensionSign());
  124. tcPaperStructService.updateById(tcPaperStructDb);
  125. }
  126. }
  127. return ResultUtil.ok(true);
  128. }
  129. @ApiOperation(value = "期末成绩试卷蓝图查询")
  130. @RequestMapping(value = "/final_score/paper_struct/query", method = RequestMethod.POST)
  131. @OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.SEARCH)
  132. @ApiResponses({@ApiResponse(code = 200, message = "试卷蓝图保存", response = PaperStructDimensionResult.class)})
  133. public Result finalScorePaperStructQuery(@ApiParam(value = "考试id", required = true) @RequestParam Long examId,
  134. @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
  135. @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber) throws IOException {
  136. String lockKey = SystemConstant.REDIS_FINAL_SCORE_DATA_FLOW_PREFIX + SystemConstant.SYNC + examId + "_" + courseCode + "_" + paperNumber;
  137. Object o = redisUtil.get(lockKey);
  138. if (Objects.nonNull(o)) {
  139. throw ExceptionResultEnum.ERROR.exception("正在同步数据,请稍候再试!");
  140. }
  141. List<PaperStructDimensionResult> paperStructDimensionResultList = null;
  142. TCPaperStruct tcPaperStruct = tcPaperStructService.queryByExamIdAndCourseCodeAndPaperNumber(examId, courseCode, paperNumber);
  143. if (Objects.isNull(tcPaperStruct)) {
  144. List<MarkQuestion> markQuestionList = markQuestionService.listQuestionByExamIdAndPaperNumberAndPaperType(examId, paperNumber, null);
  145. if (CollectionUtils.isEmpty(markQuestionList)) {
  146. throw ExceptionResultEnum.ERROR.exception("未找到试卷结构");
  147. }
  148. paperStructDimensionResultList = new ArrayList<>(markQuestionList.size());
  149. for (MarkQuestion markQuestion : markQuestionList) {
  150. paperStructDimensionResultList.add(new PaperStructDimensionResult(markQuestion.getMainNumber(), markQuestion.getSubNumber(), markQuestion.getTotalScore()));
  151. }
  152. } else {
  153. CourseWeightResult courseWeightResult = trBasicInfoService.findCourseWeightResultRmi(examId, courseCode);
  154. if (Objects.nonNull(tcPaperStruct.getDimensionSign()) && tcPaperStruct.getDimensionSign().longValue() != courseWeightResult.getDimensionSign().longValue()) {
  155. trBasicInfoService.clearReportData(examId, courseCode, paperNumber, false);
  156. paperStructDimensionResultList = new ArrayList<>();
  157. if (Objects.nonNull(tcPaperStruct.getPaperStruct())) {
  158. List<PaperStructDto> paperStructDtoList = GsonUtil.fromJson(tcPaperStruct.getPaperStruct(), new TypeToken<List<PaperStructDto>>() {
  159. }.getType());
  160. paperStructDimensionResultList = new ArrayList<>(paperStructDtoList.size());
  161. for (PaperStructDto paperStructDto : paperStructDtoList) {
  162. paperStructDimensionResultList.add(new PaperStructDimensionResult(paperStructDto.getMainNumber(), paperStructDto.getSubNumber(), paperStructDto.getScore()));
  163. }
  164. } else {
  165. return this.finalScorePaperStructQuery(examId, courseCode, paperNumber);
  166. }
  167. } else {
  168. if (Objects.nonNull(tcPaperStruct.getPaperStructDimension())) {
  169. paperStructDimensionResultList = GsonUtil.fromJson(tcPaperStruct.getPaperStructDimension(), new TypeToken<List<PaperStructDimensionResult>>() {
  170. }.getType());
  171. } else if (Objects.nonNull(tcPaperStruct.getPaperStruct())) {
  172. List<PaperStructDto> paperStructDtoList = GsonUtil.fromJson(tcPaperStruct.getPaperStruct(), new TypeToken<List<PaperStructDto>>() {
  173. }.getType());
  174. paperStructDimensionResultList = new ArrayList<>(paperStructDtoList.size());
  175. for (PaperStructDto paperStructDto : paperStructDtoList) {
  176. paperStructDimensionResultList.add(new PaperStructDimensionResult(paperStructDto.getMainNumber(), paperStructDto.getSubNumber(), paperStructDto.getScore()));
  177. }
  178. } else {
  179. return this.finalScorePaperStructQuery(examId, courseCode, paperNumber);
  180. }
  181. }
  182. }
  183. return ResultUtil.ok(paperStructDimensionResultList);
  184. }
  185. }