liujie
2026-08-04 e9e42b621afdf6ac94f5327ee843b32b10a401b4
backend/src/main/java/com/tourguide/modules/order/service/TourOrderService.java
@@ -15,6 +15,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@@ -72,8 +73,8 @@
    }
    @Transactional
    public TourOrderResponse refund(Long id) {
        TourOrder order = requireOrder(id);
    public TourOrderResponse refund(Long id, BigDecimal refundAmount) {
        TourOrder order = requireOrderForUpdate(id);
        boolean notifyGuide = OrderStatus.PENDING_MEETING.matches(order.getStatus()) && order.getGuideId() != null;
        if (OrderStatus.REFUNDED.matches(order.getStatus()) || RefundStatus.REFUNDED.matches(order.getRefundStatus())) {
            throw new BusinessException(409, "订单已退款,请勿重复操作");
@@ -84,9 +85,19 @@
        if (!OrderStatus.isRefundable(order.getStatus())) {
            throw new BusinessException(409, "当前订单状态不支持退款");
        }
        BigDecimal refundableAmount = order.getPaymentAmount() == null ? order.getAmount() : order.getPaymentAmount();
        if (refundableAmount == null || refundableAmount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException(409, "订单无可退金额");
        }
        if (refundAmount == null || refundAmount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException(400, "退款金额必须大于0");
        }
        if (refundAmount.compareTo(refundableAmount) > 0) {
            throw new BusinessException(400, "退款金额不能超过当前可退金额");
        }
        order.setRefundStatus(RefundStatus.REFUNDED.getCode());
        order.setStatus(OrderStatus.REFUNDED.getCode());
        order.setRefundAmount(order.getPaymentAmount() == null ? order.getAmount() : order.getPaymentAmount());
        order.setRefundAmount(refundAmount);
        order.setRefundTime(LocalDateTime.now());
        order.setRefundOrderNo(generateRefundOrderNo());
        order.setRefundMethod(order.getPaymentMethod());
@@ -103,6 +114,14 @@
        return order;
    }
    private TourOrder requireOrderForUpdate(Long id) {
        TourOrder order = orderMapper.selectForUpdate(id);
        if (order == null) {
            throw new BusinessException(404, "订单不存在");
        }
        return order;
    }
    private String generateRefundOrderNo() {
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        return "RF" + time + UUID.randomUUID().toString().replace("-", "").substring(0, 6).toUpperCase();