From 96ecd5028dbf46e48cbb18bc61ae8ef40ba720c3 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 16 Feb 2023 23:54:23 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BC=98=E5=8C=96=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=9B=9E=E8=B0=83=E4=BB=BB=E5=8A=A1=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E9=87=8D=E5=A4=8D=E6=89=93=20SQL=20=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=97=A5=E5=BF=97=202.=20=E4=BF=AE=E5=A4=8D=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1=E7=9A=84=E5=9B=9E=E8=B0=83=EF=BC=8C?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E6=88=90=E5=8A=9F=E6=9C=AA=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/demo/PayDemoOrderController.java | 6 ++-- ...oreMapper.java => PayNotifyLogMapper.java} | 2 +- ...reMapper.java => PayNotifyTaskMapper.java} | 2 +- .../service/notify/PayNotifyServiceImpl.java | 29 ++++++++++--------- .../src/main/resources/application-local.yaml | 1 + 5 files changed, 21 insertions(+), 19 deletions(-) rename yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/{PayNotifyLogCoreMapper.java => PayNotifyLogMapper.java} (76%) rename yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/{PayNotifyTaskCoreMapper.java => PayNotifyTaskMapper.java} (92%) diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/demo/PayDemoOrderController.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/demo/PayDemoOrderController.java index 41a7ffd41..1e3a61eec 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/demo/PayDemoOrderController.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/demo/PayDemoOrderController.java @@ -48,7 +48,7 @@ public class PayDemoOrderController { } @PostMapping("/update-paid") - @Operation(description = "更新示例订单为已支付") // 由 pay-module 支付服务,进行回调,可见 PayNotifyJob + @Operation(summary = "更新示例订单为已支付") // 由 pay-module 支付服务,进行回调,可见 PayNotifyJob @PermitAll // 无需登录,安全由 PayDemoOrderService 内部校验实现 @OperateLog(enable = false) // 禁用操作日志,因为没有操作人 public CommonResult updateDemoOrderPaid(@RequestBody PayOrderNotifyReqDTO notifyReqDTO) { @@ -58,7 +58,7 @@ public class PayDemoOrderController { } @PutMapping("/refund") - @Operation(description = "发起示例订单的退款") + @Operation(summary = "发起示例订单的退款") @Parameter(name = "id", description = "编号", required = true, example = "1024") public CommonResult refundDemoOrder(@RequestParam("id") Long id) { payDemoOrderService.refundDemoOrder(id, getClientIP()); @@ -66,7 +66,7 @@ public class PayDemoOrderController { } @PostMapping("/update-refunded") - @Operation(description = "更新示例订单为已退款") // 由 pay-module 支付服务,进行回调,可见 PayNotifyJob + @Operation(summary = "更新示例订单为已退款") // 由 pay-module 支付服务,进行回调,可见 PayNotifyJob @PermitAll // 无需登录,安全由 PayDemoOrderService 内部校验实现 @OperateLog(enable = false) // 禁用操作日志,因为没有操作人 public CommonResult updateDemoOrderRefunded(@RequestBody PayRefundNotifyReqDTO notifyReqDTO) { diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyLogCoreMapper.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyLogMapper.java similarity index 76% rename from yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyLogCoreMapper.java rename to yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyLogMapper.java index c95621933..34a7b6249 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyLogCoreMapper.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyLogMapper.java @@ -5,5 +5,5 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import org.apache.ibatis.annotations.Mapper; @Mapper -public interface PayNotifyLogCoreMapper extends BaseMapperX { +public interface PayNotifyLogMapper extends BaseMapperX { } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyTaskCoreMapper.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyTaskMapper.java similarity index 92% rename from yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyTaskCoreMapper.java rename to yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyTaskMapper.java index ca876a3ab..37ec65a3a 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyTaskCoreMapper.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/notify/PayNotifyTaskMapper.java @@ -10,7 +10,7 @@ import java.time.LocalDateTime; import java.util.List; @Mapper -public interface PayNotifyTaskCoreMapper extends BaseMapperX { +public interface PayNotifyTaskMapper extends BaseMapperX { /** * 获得需要通知的 PayNotifyTaskDO 记录。需要满足如下条件: diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/notify/PayNotifyServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/notify/PayNotifyServiceImpl.java index 1f8131230..d0c525b90 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/notify/PayNotifyServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/notify/PayNotifyServiceImpl.java @@ -13,8 +13,8 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.notify.PayNotifyLogDO; import cn.iocoder.yudao.module.pay.dal.dataobject.notify.PayNotifyTaskDO; import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO; import cn.iocoder.yudao.module.pay.dal.dataobject.refund.PayRefundDO; -import cn.iocoder.yudao.module.pay.dal.mysql.notify.PayNotifyLogCoreMapper; -import cn.iocoder.yudao.module.pay.dal.mysql.notify.PayNotifyTaskCoreMapper; +import cn.iocoder.yudao.module.pay.dal.mysql.notify.PayNotifyLogMapper; +import cn.iocoder.yudao.module.pay.dal.mysql.notify.PayNotifyTaskMapper; import cn.iocoder.yudao.module.pay.dal.redis.notify.PayNotifyLockRedisDAO; import cn.iocoder.yudao.module.pay.enums.notify.PayNotifyStatusEnum; import cn.iocoder.yudao.module.pay.enums.notify.PayNotifyTypeEnum; @@ -32,6 +32,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import javax.validation.Valid; +import java.time.Duration; import java.time.LocalDateTime; import java.util.HashMap; import java.util.List; @@ -40,6 +41,7 @@ import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*; import static cn.iocoder.yudao.module.pay.framework.job.config.PayJobConfiguration.NOTIFY_THREAD_POOL_TASK_EXECUTOR; /** @@ -69,9 +71,9 @@ public class PayNotifyServiceImpl implements PayNotifyService { private PayRefundService refundService; @Resource - private PayNotifyTaskCoreMapper payNotifyTaskCoreMapper; + private PayNotifyTaskMapper payNotifyTaskMapper; @Resource - private PayNotifyLogCoreMapper payNotifyLogCoreMapper; + private PayNotifyLogMapper payNotifyLogMapper; @Resource(name = NOTIFY_THREAD_POOL_TASK_EXECUTOR) private ThreadPoolTaskExecutor threadPoolTaskExecutor; @@ -101,7 +103,7 @@ public class PayNotifyServiceImpl implements PayNotifyService { } // 执行插入 - payNotifyTaskCoreMapper.insert(task); + payNotifyTaskMapper.insert(task); // 异步直接发起任务。虽然会有定时任务扫描,但是会导致延迟 self.executeNotifyAsync(task); @@ -110,7 +112,7 @@ public class PayNotifyServiceImpl implements PayNotifyService { @Override public int executeNotify() throws InterruptedException { // 获得需要通知的任务 - List tasks = payNotifyTaskCoreMapper.selectListByNotify(); + List tasks = payNotifyTaskMapper.selectListByNotify(); if (CollUtil.isEmpty(tasks)) { return 0; } @@ -168,8 +170,8 @@ public class PayNotifyServiceImpl implements PayNotifyService { payNotifyLockCoreRedisDAO.lock(task.getId(), NOTIFY_TIMEOUT_MILLIS, () -> { // 校验,当前任务是否已经被通知过 // 虽然已经通过分布式加锁,但是可能同时满足通知的条件,然后都去获得锁。此时,第一个执行完后,第二个还是能拿到锁,然后会再执行一次。 - PayNotifyTaskDO dbTask = payNotifyTaskCoreMapper.selectById(task.getId()); - if (LocalDateTimeUtils.afterNow(dbTask.getNextNotifyTime())) { + PayNotifyTaskDO dbTask = payNotifyTaskMapper.selectById(task.getId()); + if (afterNow(dbTask.getNextNotifyTime())) { log.info("[executeNotifySync][dbTask({}) 任务被忽略,原因是未到达下次通知时间,可能是因为并发执行了]", JsonUtils.toJsonString(dbTask)); return; @@ -197,7 +199,7 @@ public class PayNotifyServiceImpl implements PayNotifyService { // 记录 PayNotifyLog 日志 String response = invokeException != null ? ExceptionUtil.getRootCauseMessage(invokeException) : JsonUtils.toJsonString(invokeResult); - payNotifyLogCoreMapper.insert(PayNotifyLogDO.builder().taskId(task.getId()) + payNotifyLogMapper.insert(PayNotifyLogDO.builder().taskId(task.getId()) .notifyTimes(task.getNotifyTimes() + 1).status(newStatus).response(response).build()); } @@ -250,23 +252,22 @@ public class PayNotifyServiceImpl implements PayNotifyService { // 情况一:调用成功 if (invokeResult != null && invokeResult.isSuccess()) { updateTask.setStatus(PayNotifyStatusEnum.SUCCESS.getStatus()); + payNotifyTaskMapper.updateById(updateTask); return updateTask.getStatus(); } // 情况二:调用失败、调用异常 // 2.1 超过最大回调次数 if (updateTask.getNotifyTimes() >= PayNotifyTaskDO.NOTIFY_FREQUENCY.length) { updateTask.setStatus(PayNotifyStatusEnum.FAILURE.getStatus()); + payNotifyTaskMapper.updateById(updateTask); return updateTask.getStatus(); } // 2.2 未超过最大回调次数 - updateTask.setNextNotifyTime(LocalDateTime.now().plusSeconds(PayNotifyTaskDO.NOTIFY_FREQUENCY[updateTask.getNotifyTimes()])); + updateTask.setNextNotifyTime(addTime(Duration.ofSeconds(PayNotifyTaskDO.NOTIFY_FREQUENCY[updateTask.getNotifyTimes()]))); updateTask.setStatus(invokeException != null ? PayNotifyStatusEnum.REQUEST_FAILURE.getStatus() : PayNotifyStatusEnum.REQUEST_SUCCESS.getStatus()); + payNotifyTaskMapper.updateById(updateTask); return updateTask.getStatus(); } - private void processNotifySuccess(PayNotifyTaskDO task, PayNotifyTaskDO updateTask) { - payNotifyTaskCoreMapper.updateById(updateTask); - } - } diff --git a/yudao-server/src/main/resources/application-local.yaml b/yudao-server/src/main/resources/application-local.yaml index 2c8204b0a..6c804b0f7 100644 --- a/yudao-server/src/main/resources/application-local.yaml +++ b/yudao-server/src/main/resources/application-local.yaml @@ -153,6 +153,7 @@ logging: cn.iocoder.yudao.module.infra.dal.mysql: debug cn.iocoder.yudao.module.infra.dal.mysql.job.JobLogMapper: INFO # 配置 JobLogMapper 的日志级别为 info cn.iocoder.yudao.module.pay.dal.mysql: debug + cn.iocoder.yudao.module.pay.dal.mysql.notify.PayNotifyTaskMapper: INFO # 配置 JobLogMapper 的日志级别为 info cn.iocoder.yudao.module.system.dal.mysql: debug cn.iocoder.yudao.module.tool.dal.mysql: debug cn.iocoder.yudao.module.member.dal.mysql: debug