TCPaperStructController.java 15 KB

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