package com.cyksj.service.order; import com.cyksj.common.exception.BusinessRuntimeException; import lombok.RequiredArgsConstructor; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; import java.math.BigDecimal; import java.util.Map; @Service @RequiredArgsConstructor public class RefundIntentService { private final JdbcTemplate jdbc; /** Persist before any external payment call, even if the caller later rolls back. */ @Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor=Throwable.class) public void bind(String refundNo, Long orderId, BigDecimal amount) { if(amount==null || amount.signum()<=0 || amount.stripTrailingZeros().scale()>0) throw BusinessRuntimeException.getInstance("退款金额必须为正整数分"); jdbc.update("INSERT INTO payment_refund_intent(refund_no,order_id,amount) VALUES(?,?,?) ON DUPLICATE KEY UPDATE refund_no=VALUES(refund_no)",refundNo,orderId,amount); Map row=jdbc.queryForMap("SELECT order_id,amount FROM payment_refund_intent WHERE refund_no=? FOR UPDATE",refundNo); if(((Number)row.get("order_id")).longValue()!=orderId || new BigDecimal(row.get("amount").toString()).compareTo(amount)!=0) throw BusinessRuntimeException.getInstance("已有未核实退款请求,金额不可变更,请先核对渠道退款结果"); } }