NotifyApiController.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.qmth.distributed.print.api;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.qmth.boot.api.annotation.Aac;
  5. import com.qmth.boot.api.annotation.BOOL;
  6. import com.qmth.boot.api.constant.ApiConstant;
  7. import com.qmth.boot.api.exception.ApiException;
  8. import com.qmth.distributed.print.business.bean.result.CalculateNotifyResult;
  9. import com.qmth.distributed.print.business.entity.GradeBatch;
  10. import com.qmth.distributed.print.business.entity.GradeBatchPaper;
  11. import com.qmth.distributed.print.business.service.GradeBatchPaperService;
  12. import com.qmth.distributed.print.business.service.GradeBatchService;
  13. import com.qmth.teachcloud.common.config.DictionaryConfig;
  14. import com.qmth.teachcloud.common.contant.SystemConstant;
  15. import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
  16. import com.qmth.teachcloud.common.enums.GradeAnalyzePaperStatusEnum;
  17. import com.qmth.teachcloud.common.enums.TaskResultEnum;
  18. import com.qmth.teachcloud.common.enums.TaskStatusEnum;
  19. import com.qmth.teachcloud.common.util.*;
  20. import io.swagger.annotations.*;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import org.springframework.validation.annotation.Validated;
  25. import org.springframework.web.bind.annotation.RequestBody;
  26. import org.springframework.web.bind.annotation.RequestMapping;
  27. import org.springframework.web.bind.annotation.RequestMethod;
  28. import org.springframework.web.bind.annotation.RestController;
  29. import javax.annotation.Resource;
  30. import java.io.UnsupportedEncodingException;
  31. import java.net.URLDecoder;
  32. import java.net.URLEncoder;
  33. import java.util.*;
  34. /**
  35. * <p>
  36. * 回调接口前端控制器
  37. * </p>
  38. *
  39. * @author wangliang
  40. * @since 2022-04-26
  41. */
  42. @Api(tags = "回调接口Controller")
  43. @RestController
  44. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.notify}")
  45. @Validated
  46. public class NotifyApiController {
  47. private final static Logger log = LoggerFactory.getLogger(NotifyApiController.class);
  48. @Resource
  49. DictionaryConfig dictionaryConfig;
  50. @Resource
  51. GradeBatchService gradeBatchService;
  52. @Resource
  53. GradeBatchPaperService gradeBatchPaperService;
  54. @ApiOperation(value = "教研分析进度回调")
  55. @ApiResponses({@ApiResponse(code = 200, message = "教研分析进度回调", response = CalculateNotifyResult.class)})
  56. @RequestMapping(value = "/analysis/progress", method = RequestMethod.POST)
  57. @Aac(auth = BOOL.FALSE)
  58. @Transactional
  59. public Result analysisProgress(@ApiParam(value = "接收教研分析回调数据", required = true) @RequestBody String result) throws UnsupportedEncodingException {
  60. try {
  61. log.info("analysis_progress,result:{}", result);
  62. String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
  63. log.info("analysis_progress decodeJson:{}", decodeJson);
  64. String callbackPwd = dictionaryConfig.printOpenDomain().getCallbackPwd();
  65. Optional.ofNullable(callbackPwd).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("配置文件回调密码为空"));
  66. String sign = ServletUtil.getRequestAuthorization();
  67. Optional.ofNullable(sign).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("签名为空"));
  68. log.info("sign:{}", sign);
  69. String localSign = URLEncoder.encode(Base64Util.encode(ShaUtils.sha1(callbackPwd + decodeJson)), SystemConstant.CHARSET_NAME);
  70. log.info("localSign:{}", localSign);
  71. if (!Objects.equals(localSign, sign)) {
  72. throw ExceptionResultEnum.ERROR.exception("回调签名不匹配");
  73. }
  74. CalculateNotifyResult calculateNotifyResult = JSONObject.toJavaObject(JSONObject.parseObject(decodeJson), CalculateNotifyResult.class);
  75. List<String> courseCodeList = new ArrayList<>();
  76. courseCodeList.addAll(calculateNotifyResult.getCourseCode().keySet());
  77. Collections.sort(courseCodeList);
  78. String source = Base64Util.encode(ShaUtils.sha1(calculateNotifyResult.getExamId() + courseCodeList.toString()));
  79. log.info("source:{}", source);
  80. QueryWrapper<GradeBatch> gradeBatchQueryWrapper = new QueryWrapper<>();
  81. gradeBatchQueryWrapper.lambda().eq(GradeBatch::getThirdExamId, calculateNotifyResult.getExamId())
  82. .eq(GradeBatch::getSource, source);
  83. GradeBatch gradeBatch = gradeBatchService.getOne(gradeBatchQueryWrapper);
  84. Optional.ofNullable(gradeBatch).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("批次数据为空"));
  85. if (Objects.isNull(gradeBatch.getNotifyTime()) ||
  86. (Objects.nonNull(gradeBatch.getNotifyTime())
  87. && gradeBatch.getNotifyTime().longValue() <= calculateNotifyResult.getTime().longValue())) {
  88. TaskStatusEnum taskStatus = Objects.nonNull(calculateNotifyResult.getStatus()) ? TaskStatusEnum.valueOf(calculateNotifyResult.getStatus()) : null;
  89. if (taskStatus == TaskStatusEnum.DATA_VALID || taskStatus == TaskStatusEnum.RUNNING) {
  90. gradeBatch.setStatus(GradeAnalyzePaperStatusEnum.CALCULATING);
  91. } else if (taskStatus == TaskStatusEnum.FINISH) {
  92. gradeBatch.setStatus(GradeAnalyzePaperStatusEnum.FINISH_CALCULATE);
  93. }
  94. if (Objects.nonNull(calculateNotifyResult.getProgress())) {
  95. gradeBatch.setProgress(calculateNotifyResult.getProgress());
  96. }
  97. gradeBatch.setNotifyTime(System.currentTimeMillis());
  98. if (Objects.nonNull(calculateNotifyResult.getResult())) {
  99. gradeBatch.setResult(TaskResultEnum.valueOf(calculateNotifyResult.getResult()));
  100. }
  101. gradeBatchService.updateById(gradeBatch);
  102. QueryWrapper<GradeBatchPaper> gradeBatchPaperQueryWrapper = new QueryWrapper<>();
  103. gradeBatchPaperQueryWrapper.lambda().eq(GradeBatchPaper::getBatchId, gradeBatch.getId());
  104. List<GradeBatchPaper> gradeBatchPaperList = new ArrayList<>();
  105. calculateNotifyResult.getCourseCode().forEach((k, v) -> {
  106. gradeBatchPaperQueryWrapper.lambda().eq(GradeBatchPaper::getGradeCourseCode, k)
  107. .eq(GradeBatchPaper::getEnable, true);
  108. GradeBatchPaper gradeBatchPaper = gradeBatchPaperService.getOne(gradeBatchPaperQueryWrapper);
  109. Optional.ofNullable(gradeBatchPaper).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("批次科目[" + k + "]数据为空"));
  110. if (Objects.isNull(gradeBatchPaper.getStatus()) || (Objects.nonNull(gradeBatchPaper.getStatus()) && gradeBatchPaper.getStatus() != v)) {
  111. gradeBatchPaper.setStatus(v);
  112. gradeBatchPaper.setUpdateTime(System.currentTimeMillis());
  113. gradeBatchPaperList.add(gradeBatchPaper);
  114. }
  115. });
  116. gradeBatchPaperService.updateBatchById(gradeBatchPaperList);
  117. }
  118. } catch (Exception e) {
  119. log.error(SystemConstant.LOG_ERROR, e);
  120. if (e instanceof ApiException) {
  121. ResultUtil.error((ApiException) e, e.getMessage());
  122. } else {
  123. ResultUtil.error(e.getMessage());
  124. }
  125. }
  126. return ResultUtil.ok(System.currentTimeMillis());
  127. }
  128. }