From ddb0b754685fed58660eb12c81bbd2ea52bfa32b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E5=A4=A7=E5=8F=94?= <494979559@qq.com> Date: Sat, 21 Sep 2024 16:51:18 +0800 Subject: [PATCH] =?UTF-8?q?debug:=E8=AE=BE=E5=A4=87=E5=85=85=E5=80=BC?= =?UTF-8?q?=E6=88=90=E5=8A=9F=EF=BC=8C=E4=BD=86=E8=AE=A2=E5=8D=95=E6=9C=AA?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E6=88=90=E5=8A=9F=E3=80=82=20=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=97=A5=E5=BF=97=E5=90=8E=E5=8F=91=E7=8E=B0=EF=BC=8C?= =?UTF-8?q?=E6=98=AF=E7=94=B1=E4=BA=8E=E6=AD=BB=E9=94=81=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E5=BA=93=E5=BC=82=E5=B8=B8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/redis/enums/RedisLockKey.java | 3 +- .../ruoyi/common/utils/http/HttpUtils.java | 10 ++-- .../com/ruoyi/iot/service/IotServiceImpl.java | 6 +- .../service/impl/PayBillServiceImpl.java | 58 ++++++++++--------- .../impl/TransactionBillServiceImpl.java | 38 +++++++++--- .../app/AppTransactionBillController.java | 2 + 6 files changed, 75 insertions(+), 42 deletions(-) diff --git a/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/core/redis/enums/RedisLockKey.java b/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/core/redis/enums/RedisLockKey.java index c4d8b34d..aa3873f6 100644 --- a/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/core/redis/enums/RedisLockKey.java +++ b/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/core/redis/enums/RedisLockKey.java @@ -22,7 +22,8 @@ public enum RedisLockKey { ADD_TIME_BILL("add_time_bill", "创建时长订单"), PAY_BILL("pay_bill", "支付订单"), PREPAY_DEPOSIT("prepay_deposit", "支付押金"), - ADD_RECHARGE_ORDER("add_recharge_order", "创建充值订单"); + ADD_RECHARGE_ORDER("add_recharge_order", "创建充值订单"), + PAY_BILL_SUCCESS("pay_bill_success", "支付订单成功处理"); private final String key; diff --git a/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java b/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java index 7e0e456f..3194ca84 100644 --- a/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java +++ b/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java @@ -85,7 +85,7 @@ public class HttpUtils { result.append(line); } - log.debug("recv - {}", result); +// log.debug("recv - {}", result); } catch (ConnectException e) { @@ -153,7 +153,7 @@ public class HttpUtils { result.append(line); } - log.debug("recv - {}", result); +// log.debug("recv - {}", result); } catch (ConnectException e) { @@ -224,7 +224,7 @@ public class HttpUtils result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8)); } } - log.debug("recv - {}", result); +// log.debug("recv - {}", result); conn.disconnect(); br.close(); } @@ -329,7 +329,7 @@ public class HttpUtils { result.append(line); } - log.debug("recv - {}", result); +// log.debug("recv - {}", result); } catch (ConnectException e) { @@ -396,7 +396,7 @@ public class HttpUtils { result.append(line); } - log.debug("recv - {}", result); +// log.debug("recv - {}", result); } catch (ConnectException e) { diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotServiceImpl.java index f8e8d0f1..4aa708d0 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotServiceImpl.java @@ -105,10 +105,10 @@ public class IotServiceImpl implements IotService { String sendUrl = iotHost + IotConstants.ADDS_HISTORY_DATAPOINTS + "?"+param; String token = Token.getToken(); - log.info("IOT获取到Authorization:【{}】",token); +// log.info("IOT获取到Authorization:【{}】",token); String result = HttpUtils.sendGetWithToken(sendUrl, null, token); - log.info("IOT返回的结果【{}】",result); +// log.info("IOT返回的结果【{}】",result); if (!StringUtils.hasText(result)) { log.error("与OneNet通信异常"); return null; @@ -159,7 +159,7 @@ public class IotServiceImpl implements IotService { String param = "device_name=" + deviceName + "&product_id=" + productId; String token = Token.getToken(); - log.info("IOT获取到Authorization:【{}】",token); +// log.info("IOT获取到Authorization:【{}】",token); String result = HttpUtils.sendGetWithToken(sendUrl, param, token); if (!StringUtils.hasText(result)) { diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/payBill/service/impl/PayBillServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/payBill/service/impl/PayBillServiceImpl.java index 7051a378..4975f504 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/payBill/service/impl/PayBillServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/payBill/service/impl/PayBillServiceImpl.java @@ -277,35 +277,41 @@ public class PayBillServiceImpl implements PayBillService private void handleSuccess(PayBillVO payBill, LocalDateTime payTime) { ServiceUtil.assertion(payBill == null, "支付订单不存在"); - log.info("获取支付订单:{}", payBill.getPayNo()); - // 若已经支付成功,则跳过 - if (PayBillStatus.payedList().contains(payBill.getStatus())) { - return; - } - ServiceUtil.assertion(!PayBillStatus.PAYING.getStatus().equals(payBill.getStatus()), "该支付订单不是正在支付的支付订单"); + Long lockKey = payBill.getPayId(); - transactionTemplate.execute(status -> { - // 修改支付订单状态 - PayBill data = new PayBill(); - data.setStatus(PayBillStatus.PAY_SUCCESS.getStatus()); - data.setPayTime(payTime); - PayBillQuery query = new PayBillQuery(); - query.setStatus(PayBillStatus.PAYING.getStatus()); - query.setPayId(payBill.getPayId()); - int update = this.updateByQuery(data, query); - ServiceUtil.assertion(update != 1, "支付订单状态已改变,请稍后再试"); - - // 处理业务 - PayBillBstType bstType = PayBillBstType.parse(payBill.getBstType()); - if (bstType != null && bstType.getAfterPay() != null) { - PayBillVO newPayBill = selectPayBillByPayId(payBill.getPayId()); - AfterPay afterPay = SpringUtils.getBean(bstType.getAfterPay()); - int bstResult = afterPay.onPaySuccess(newPayBill); - ServiceUtil.assertion(bstResult == 0, "业务处理失败"); + ServiceUtil.assertion(redisLock.lock(RedisLockKey.PAY_BILL_SUCCESS, lockKey), "支付订单正在处理中:payId=" + payBill.getPayId()); + try { + // 若已经支付成功,则跳过 + if (PayBillStatus.payedList().contains(payBill.getStatus())) { + return; } + ServiceUtil.assertion(!PayBillStatus.PAYING.getStatus().equals(payBill.getStatus()), "该支付订单不是正在支付的支付订单"); - return update; - }); + transactionTemplate.execute(status -> { + // 修改支付订单状态 + PayBill data = new PayBill(); + data.setStatus(PayBillStatus.PAY_SUCCESS.getStatus()); + data.setPayTime(payTime); + PayBillQuery query = new PayBillQuery(); + query.setStatus(PayBillStatus.PAYING.getStatus()); + query.setPayId(payBill.getPayId()); + int update = this.updateByQuery(data, query); + ServiceUtil.assertion(update != 1, "支付订单状态已改变,请稍后再试"); + + // 处理业务 + PayBillBstType bstType = PayBillBstType.parse(payBill.getBstType()); + if (bstType != null && bstType.getAfterPay() != null) { + PayBillVO newPayBill = selectPayBillByPayId(payBill.getPayId()); + AfterPay afterPay = SpringUtils.getBean(bstType.getAfterPay()); + int bstResult = afterPay.onPaySuccess(newPayBill); + ServiceUtil.assertion(bstResult == 0, "业务处理失败"); + } + + return update; + }); + } finally { + redisLock.unlock(RedisLockKey.PAY_BILL_SUCCESS, lockKey); + } } @Override diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java index 335efa04..d97b8268 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java @@ -723,13 +723,18 @@ public class TransactionBillServiceImpl implements TransactionBillService, After TransactionBillVO bill = transactionBillMapper.selectSmTransactionBillByBillId(billId); ServiceUtil.assertion(bill == null || !TransactionBillType.RECHARGE.getType().equals(bill.getType()), "不存在的充值订单"); ServiceUtil.assertion(!TransactionBillStatus.SUCCESS.getStatus().equals(bill.getStatus()), "订单未支付"); - ServiceUtil.assertion(TransactionBillDeviceRechargeStatus.SUCCESS.getStatus().equals(bill.getDeviceRechargeStatus()), "设备已充值成功,不允许再次充值"); + + if (TransactionBillDeviceRechargeStatus.SUCCESS.getStatus().equals(bill.getDeviceRechargeStatus())) { + log.warn( "设备已充值成功,不允许再次充值:billId={}", billId); + return false; + } + ServiceUtil.assertion(TransactionBillDeviceRechargeStatus.BLUETOOTH.getStatus().equals(bill.getDeviceRechargeStatus()), "设备已选择蓝牙充值,请使用蓝牙进行充值"); - // 刷新设备数据 - deviceService.pullDeviceInfo(bill.getDeviceId()); + Boolean result = transactionTemplate.execute(status -> { + // 刷新设备数据 + deviceService.pullDeviceInfo(bill.getDeviceId()); - Boolean result = (Boolean) transactionTemplate.execute(status -> { DeviceVO device = deviceService.selectSmDeviceByDeviceId(bill.getDeviceId()); // 如果设备离线,则直接返回失败 @@ -743,7 +748,7 @@ public class TransactionBillServiceImpl implements TransactionBillService, After // 修改设备充值状态:成功 int updateRecharge = transactionBillMapper.updateDeviceRechargeStatus(bill.getBillId(), TransactionBillDeviceRechargeStatus.SUCCESS.getStatus()); - ServiceUtil.assertion(updateRecharge != 1, "订单状态发生变化,请稍后重试"); + ServiceUtil.assertion(updateRecharge != 1, "设备充值状态发生变化,请稍后重试"); try { if (SuitFeeType.TIME.getType().equals(bill.getSuitFeeType())) { @@ -849,14 +854,33 @@ public class TransactionBillServiceImpl implements TransactionBillService, After return updateCount; }); + // 异步充值设备,尝试3次 if (result != null && result == 1) { - // 设备充值 - rechargeDevice(bill.getBillId()); + this.tryRechargeDevice(bill.getBillId(), 3); } return result == null ? 0 : result; } + /** + * 异步尝试充值设备 + * @param billId + * @param tryCount + */ + private void tryRechargeDevice(Long billId, int tryCount) { + if (tryCount <= 0) { + return; + } + scheduledExecutorService.schedule(()-> { + try { + boolean result = rechargeDevice(billId); + ServiceUtil.assertion(!result, String.format("尝试充值设备失败:billId=%s:剩余次数:%s,", billId, tryCount - 1)); + } catch (Exception e) { + this.tryRechargeDevice(billId, tryCount - 1); + } + }, 0, TimeUnit.SECONDS); + } + @Override public DoPayVO payDeposit(RechargePayDepositBO bo) { // 校验 diff --git a/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppTransactionBillController.java b/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppTransactionBillController.java index 3273783f..067a7422 100644 --- a/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppTransactionBillController.java +++ b/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppTransactionBillController.java @@ -272,6 +272,7 @@ public class AppTransactionBillController extends BaseController @ApiOperation("支付押金") @PutMapping("/payDeposit") public AjaxResult payDeposit(@RequestBody @Validated PayDepositDTO dto) { + dto.setChannelId(1L); return success(transactionBillService.payDeposit(transactionBillConverter.toRechargePayDepositBO(dto))); } @@ -344,6 +345,7 @@ public class AppTransactionBillController extends BaseController @ApiOperation("支付订单") @PutMapping("/pay") public AjaxResult pay(@RequestBody BillPayDTO dto) { + dto.setChannelId(1L); TransactionBillVO bill = transactionBillService.selectSmTransactionBillByBillNo(dto.getBillNo()); if (!transactionBillValidator.isUser(bill, getUserId())) { return error("这不是您的订单,无法支付");