TCPaperStructController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. boolean lock = redisUtil.lock(lockKey, SystemConstant.REDIS_PAPER_STRUCT_TIME_OUT);
  76. if (!lock) {
  77. throw ExceptionResultEnum.ERROR.exception("正在导入数据,请稍候再试!");
  78. }
  79. Map<String, String> map = null;
  80. try {
  81. map = tcPaperStructService.paperStructExcelImport(file, examId, courseCode, paperNumber);
  82. } catch (Exception e) {
  83. log.error(SystemConstant.LOG_ERROR, e);
  84. if (e instanceof ApiException) {
  85. ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
  86. } else {
  87. ResultUtil.error(e.getMessage());
  88. }
  89. } finally {
  90. redisUtil.releaseLock(lockKey);
  91. }
  92. return ResultUtil.ok(map);
  93. }
  94. @ApiOperation(value = "同步试卷蓝图结构")
  95. @RequestMapping(value = "/final_score/paper_struct_dimension/sync", method = RequestMethod.POST)
  96. @OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.PUSH)
  97. @ApiResponses({@ApiResponse(code = 200, message = "同步成功", response = EditResult.class)})
  98. public Result finalScorePaperStructDimensionSync(@ApiParam(value = "考试id", required = true) @RequestParam Long examId,
  99. @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
  100. @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber) throws IOException {
  101. printCommonService.getLock(examId, courseCode, paperNumber);
  102. String lockKey = SystemConstant.REDIS_PAPER_STRUCT_FLOW_PREFIX + SystemConstant.SYNC + examId + "_" + courseCode + "_" + paperNumber;
  103. boolean lock = redisUtil.lock(lockKey, SystemConstant.REDIS_PAPER_STRUCT_TIME_OUT);
  104. if (!lock) {
  105. throw ExceptionResultEnum.ERROR.exception("正在同步数据,请稍候再试!");
  106. }
  107. Map<String, String> map = null;
  108. try {
  109. map = tcPaperStructService.paperStructSync(examId, courseCode, paperNumber);
  110. } catch (Exception e) {
  111. log.error(SystemConstant.LOG_ERROR, e);
  112. if (e instanceof ApiException) {
  113. ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
  114. } else {
  115. ResultUtil.error(e.getMessage());
  116. }
  117. } finally {
  118. redisUtil.releaseLock(lockKey);
  119. }
  120. return ResultUtil.ok(map);
  121. }
  122. @ApiOperation(value = "期末成绩试卷蓝图保存")
  123. @RequestMapping(value = "/final_score/paper_struct/save", method = RequestMethod.POST)
  124. @OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.UPDATE)
  125. @ApiResponses({@ApiResponse(code = 200, message = "试卷蓝图保存", response = Object.class)})
  126. @Transactional
  127. public Result finalScorePaperStructSave(@ApiParam(value = "试卷蓝图结构", required = true) @Valid @RequestBody PaperStructParams paperStructParams, BindingResult bindingResult) throws IOException {
  128. if (bindingResult.hasErrors()) {
  129. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  130. }
  131. printCommonService.getLock(paperStructParams.getExamId(), paperStructParams.getCourseCode(), paperStructParams.getPaperNumber());
  132. CourseWeightResult courseWeightResult = trBasicInfoService.findCourseWeightResultRmi(paperStructParams.getExamId(), paperStructParams.getCourseCode());
  133. for (CourseWeightDto c : courseWeightResult.getSubmitForm()) {
  134. for (PaperStructDimensionResult paperStructDimensionResult : paperStructParams.getPaperStruct()) {
  135. Objects.requireNonNull(paperStructDimensionResult.getMainNumber(), "大题号为空");
  136. Objects.requireNonNull(paperStructDimensionResult.getSubNumber(), "小题号为空");
  137. if (!CollectionUtils.isEmpty(paperStructDimensionResult.getTargetList()) && paperStructDimensionResult.getTargetList().size() > 1) {
  138. throw ExceptionResultEnum.ERROR.exception("一个题只能属于一个目标");
  139. }
  140. }
  141. List<PaperStructDimensionResult> paperStructDimensionResultList = paperStructParams.getPaperStruct();
  142. Double score = paperStructDimensionResultList.stream().filter(s -> Objects.equals(s.getCourseTargetName(), c.getCourseTargetName())).mapToDouble(PaperStructDimensionResult::getScore).sum();
  143. if (new BigDecimal(score).compareTo(c.getTotalWeight()) == 1) {
  144. throw ExceptionResultEnum.ERROR.exception("[" + c.getCourseTargetName() + "]知识点小题总分大于该课程目标分,请重新设置");
  145. }
  146. paperStructDimensionResultList.stream().filter(s -> {
  147. if (Objects.equals(s.getCourseTargetName(), c.getCourseTargetName())) {
  148. s.getTargetList().stream().peek(e -> e.setFinalScoreQuestionScoreSum(score)).collect(Collectors.toList());
  149. return true;
  150. }
  151. return false;
  152. }).collect(Collectors.toList());
  153. }
  154. SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
  155. TCPaperStruct tcPaperStructDb = tcPaperStructService.queryByExamIdAndCourseCodeAndPaperNumber(paperStructParams.getExamId(), paperStructParams.getCourseCode(), paperStructParams.getPaperNumber());
  156. if (Objects.isNull(tcPaperStructDb)) {
  157. MarkPaper markPaper = printCommonService.getMarkPaper(paperStructParams.getExamId(), paperStructParams.getPaperNumber());
  158. tcPaperStructDb = new TCPaperStruct(paperStructParams.getExamId(), paperStructParams.getCourseCode(), markPaper.getCourseName(), paperStructParams.getPaperNumber(), JacksonUtil.parseJson(paperStructParams.getPaperStruct()), sysUser.getId(), courseWeightResult.getDimensionSign());
  159. tcPaperStructService.save(tcPaperStructDb);
  160. } else {
  161. TCPaperStruct tcPaperStructSource = new TCPaperStruct();
  162. BeanUtils.copyProperties(tcPaperStructDb, tcPaperStructSource);
  163. tcPaperStructDb.updateInfo(JacksonUtil.parseJson(paperStructParams.getPaperStruct()), sysUser.getId());
  164. if (!tcPaperStructDb.equals(tcPaperStructSource)) {
  165. trBasicInfoService.clearReportData(tcPaperStructDb.getExamId(), tcPaperStructDb.getCourseCode(), tcPaperStructDb.getPaperNumber(), false);
  166. tcPaperStructDb.setDimensionSign(courseWeightResult.getDimensionSign());
  167. tcPaperStructService.updateById(tcPaperStructDb);
  168. }
  169. }
  170. return ResultUtil.ok(true);
  171. }
  172. @ApiOperation(value = "期末成绩试卷蓝图查询")
  173. @RequestMapping(value = "/final_score/paper_struct/query", method = RequestMethod.POST)
  174. @OperationLogDetail(customizedOperationType = CustomizedOperationTypeEnum.SEARCH)
  175. @ApiResponses({@ApiResponse(code = 200, message = "试卷蓝图保存", response = PaperStructDimensionResult.class)})
  176. public Result finalScorePaperStructQuery(@ApiParam(value = "考试id", required = true) @RequestParam Long examId,
  177. @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
  178. @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber) throws IOException {
  179. printCommonService.getLock(examId, courseCode, paperNumber);
  180. List<PaperStructDimensionResult> paperStructDimensionResultList = null;
  181. TCPaperStruct tcPaperStruct = tcPaperStructService.queryByExamIdAndCourseCodeAndPaperNumber(examId, courseCode, paperNumber);
  182. if (Objects.isNull(tcPaperStruct) || Objects.isNull(tcPaperStruct.getPaperStruct())) {
  183. List<MarkQuestion> markQuestionList = markQuestionService.listQuestionByExamIdAndPaperNumberAndPaperType(examId, paperNumber, null);
  184. if (CollectionUtils.isEmpty(markQuestionList)) {
  185. throw ExceptionResultEnum.ERROR.exception("未找到试卷结构");
  186. }
  187. paperStructDimensionResultList = new ArrayList<>(markQuestionList.size());
  188. for (MarkQuestion markQuestion : markQuestionList) {
  189. paperStructDimensionResultList.add(new PaperStructDimensionResult(markQuestion.getMainNumber(), markQuestion.getSubNumber(), markQuestion.getTotalScore()));
  190. }
  191. } else {
  192. CourseWeightResult courseWeightResult = trBasicInfoService.findCourseWeightResultRmi(examId, courseCode);
  193. if (Objects.nonNull(tcPaperStruct.getDimensionSign()) && tcPaperStruct.getDimensionSign().longValue() != courseWeightResult.getDimensionSign().longValue()) {
  194. trBasicInfoService.clearReportData(examId, courseCode, paperNumber, false);
  195. paperStructDimensionResultList = this.getPaperStructDimensionResult(tcPaperStruct, paperStructDimensionResultList);
  196. } else {
  197. paperStructDimensionResultList = this.getPaperStructDimensionResult(tcPaperStruct, paperStructDimensionResultList);
  198. }
  199. }
  200. return ResultUtil.ok(paperStructDimensionResultList);
  201. }
  202. /**
  203. * 获取试卷结构蓝图数据
  204. *
  205. * @param tcPaperStruct
  206. * @param paperStructDimensionResultList
  207. * @return
  208. */
  209. protected List<PaperStructDimensionResult> getPaperStructDimensionResult(TCPaperStruct tcPaperStruct,
  210. List<PaperStructDimensionResult> paperStructDimensionResultList) {
  211. if (Objects.nonNull(tcPaperStruct.getPaperStructDimension())) {
  212. paperStructDimensionResultList = GsonUtil.fromJson(tcPaperStruct.getPaperStructDimension(), new TypeToken<List<PaperStructDimensionResult>>() {
  213. }.getType());
  214. } else if (Objects.nonNull(tcPaperStruct.getPaperStruct())) {
  215. List<PaperStructDto> paperStructDtoList = GsonUtil.fromJson(tcPaperStruct.getPaperStruct(), new TypeToken<List<PaperStructDto>>() {
  216. }.getType());
  217. paperStructDimensionResultList = new ArrayList<>(paperStructDtoList.size());
  218. for (PaperStructDto paperStructDto : paperStructDtoList) {
  219. paperStructDimensionResultList.add(new PaperStructDimensionResult(paperStructDto.getMainNumber(), paperStructDto.getSubNumber(), paperStructDto.getScore()));
  220. }
  221. }
  222. return paperStructDimensionResultList;
  223. }
  224. }