|
|
@@ -7,6 +7,7 @@ import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
import com.kym.common.config.WxPayConfig;
|
|
|
import com.kym.common.constant.ResponseEnum;
|
|
|
import com.kym.common.exception.BusinessException;
|
|
|
@@ -301,6 +302,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
* 申请退款
|
|
|
*/
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public void applyWxRefund() {
|
|
|
var userId = StpUtil.getLoginIdAsLong();
|
|
|
var chargeOrder = chargeOrderService.getChargingOrderByUserId(userId);
|
|
|
@@ -313,12 +315,20 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
}
|
|
|
|
|
|
// 将余额转移至冻结余额
|
|
|
- accountService.lambdaUpdate().setSql(" frozen_amount = balance ,balance = 0").eq(Account::getUserId, userId);
|
|
|
+ accountService.lambdaUpdate().setSql(" frozen_amount = balance ,balance = 0").eq(Account::getUserId, userId).update();
|
|
|
|
|
|
// 余额作为退款金额
|
|
|
AtomicInteger refundAmount = new AtomicInteger(account.getBalance());
|
|
|
// 充值记录
|
|
|
- var payLogs = payLogService.lambdaQuery().eq(PayLog::getUserId, userId).eq(PayLog::getTradeState, "SUCCESS").orderByDesc(PayLog::getSuccessTime).list();
|
|
|
+ var payLogs = payLogService.lambdaQuery().eq(PayLog::getUserId, userId).eq(PayLog::getTradeState, PayLog.STATUS_退款成功).orderByDesc(PayLog::getSuccessTime).list();
|
|
|
+ // 充值金额综合不足以支付余额退款
|
|
|
+ if (!CommUtil.isEmptyOrNull(payLogs)) {
|
|
|
+ var total = payLogs.stream().mapToInt(PayLog::getTotal).sum();
|
|
|
+ if (total < account.getBalance()) {
|
|
|
+ LOGGER.error("用户:{},充值总金额:{},不足以支付余额退款金额:{}", userId, total, account.getBalance());
|
|
|
+ throw new BusinessException("充值金额不足以支付余额退款");
|
|
|
+ }
|
|
|
+ }
|
|
|
// 最后一次的充值金额可以覆盖退款金额
|
|
|
if (!CommUtil.isEmptyOrNull(payLogs) && payLogs.get(0).getTotal() >= refundAmount.get()) {
|
|
|
// 退款日志
|
|
|
@@ -375,6 +385,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
+ @DS("db-miniapp")
|
|
|
@Override
|
|
|
public Refund wxRefund(long refundLogId) {
|
|
|
// 通过退款申请id获取退款申请记录
|
|
|
@@ -402,6 +413,8 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
var amount = new AmountReq();
|
|
|
// 退款金额
|
|
|
amount.setRefund((long) refundLog.getRefund());
|
|
|
+ amount.setTotal((long) refundLog.getTotal());
|
|
|
+ amount.setCurrency(refundLog.getCurrency());
|
|
|
request.setAmount(amount);
|
|
|
return refundService.create(request);
|
|
|
}
|
|
|
@@ -453,9 +466,9 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
refundLog.setCreateTime(LocalDateTimeUtil.of(DateUtil.parse(refundNotification.getCreateTime())));
|
|
|
refundLogService.updateById(refundLog);
|
|
|
|
|
|
- if ("SUCCESS".equals(refundNotification.getRefundStatus().name())) {
|
|
|
+ if (PayLog.STATUS_退款成功.equals(refundNotification.getRefundStatus().name())) {
|
|
|
// 冻结金额扣减此次退款金额
|
|
|
- accountService.lambdaUpdate().setSql(" frozen_amount = (frozen_amount - %d)".formatted(refundNotification.getAmount().getRefund().intValue())).eq(Account::getUserId, refundLog.getUserId());
|
|
|
+ accountService.lambdaUpdate().setSql(" frozen_amount = (frozen_amount - %d)".formatted(refundNotification.getAmount().getRefund().intValue())).eq(Account::getUserId, refundLog.getUserId()).update();
|
|
|
// 更新资金流水
|
|
|
var walletDetail = walletDetailService.getWalletDetailByOrderNo(refundNotification.getOutTradeNo(), WalletDetail.TYPE_提现);
|
|
|
walletDetail.setStatus(WalletDetail.STATUS_已确认) //已确认
|