liujie
2026-09-16 a3a56121a476cd23d0f29bc391c07374dc5008f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.tourguide.modules.finance.service;
 
import com.tourguide.common.exception.BusinessException;
import com.tourguide.modules.order.domain.OrderStatus;
import com.tourguide.modules.order.domain.RefundAmountCalculator;
import com.tourguide.modules.order.domain.RefundStatus;
import com.tourguide.modules.order.entity.TourOrder;
import com.tourguide.modules.order.mapper.TourOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
 
/** 将到期且未分账的订单收益幂等计入导游余额。 */
@Service
public class GuideBalanceSettlementService {
    private final TourOrderMapper orderMapper;
    private final GuideWithdrawableBalanceService balanceService;
 
    public GuideBalanceSettlementService(TourOrderMapper orderMapper,
                                         GuideWithdrawableBalanceService balanceService) {
        this.orderMapper = orderMapper;
        this.balanceService = balanceService;
    }
 
    @Transactional
    public boolean settle(Long orderId, LocalDateTime settledAt) {
        TourOrder order = orderMapper.selectForUpdate(orderId);
        if (!canSettle(order, settledAt)) return false;
 
        BigDecimal earning = effectiveEarning(order);
        if (earning.signum() > 0) {
            if (order.getCommissionTeamId() != null) {
                balanceService.creditTeam(order.getCommissionTeamId(), earning);
            } else {
                balanceService.credit(order.getGuideId(), earning);
            }
        }
        order.setSettledAt(settledAt);
        if (orderMapper.updateById(order) != 1) {
            throw new BusinessException(500, "订单分账状态更新失败");
        }
        return true;
    }
 
    /** 退款成功后冲正已入账收益;按累计退款差额计算,保证部分退款幂等。 */
    @Transactional
    public void reverseRefund(TourOrder order, BigDecimal previousRefundedAmount,
                              BigDecimal currentRefundedAmount) {
        if (order == null || order.getSettledAt() == null
                || order.getGuideEarningAmount() == null) return;
        BigDecimal paidAmount = RefundAmountCalculator.paidAmount(order);
        if (paidAmount.signum() <= 0) return;
        BigDecimal previous = nonNegative(previousRefundedAmount);
        BigDecimal current = nonNegative(currentRefundedAmount).min(paidAmount);
        BigDecimal previousReversal = proratedEarning(order.getGuideEarningAmount(), previous, paidAmount);
        BigDecimal currentReversal = proratedEarning(order.getGuideEarningAmount(), current, paidAmount);
        BigDecimal reversal = currentReversal.subtract(previousReversal).setScale(2, RoundingMode.HALF_UP);
        if (reversal.signum() <= 0) return;
        boolean debited;
        if (order.getCommissionTeamId() != null) {
            debited = balanceService.debitTeamForRefund(order.getCommissionTeamId(), reversal);
        } else if (order.getGuideId() != null) {
            debited = balanceService.debitForRefund(order.getGuideId(), reversal);
        } else {
            return;
        }
        if (!debited) {
            throw new BusinessException(500, "退款后导游收益冲正失败");
        }
    }
 
    private boolean canSettle(TourOrder order, LocalDateTime now) {
        return order != null
                && OrderStatus.COMPLETED.matches(order.getStatus())
                && order.getSettledAt() == null
                && order.getSettlementDueAt() != null
                && !order.getSettlementDueAt().isAfter(now)
                && order.getGuideId() != null
                && order.getGuideEarningAmount() != null
                && order.getGuideEarningAmount().signum() >= 0
                && canSettleRefund(order.getRefundStatus());
    }
 
    private BigDecimal effectiveEarning(TourOrder order) {
        BigDecimal earning = order.getGuideEarningAmount();
        BigDecimal paidAmount = RefundAmountCalculator.paidAmount(order);
        if (paidAmount.signum() <= 0) return earning;
        BigDecimal refunded = RefundAmountCalculator.refundedAmount(order).min(paidAmount);
        return earning.multiply(paidAmount.subtract(refunded))
                .divide(paidAmount, 2, RoundingMode.HALF_UP);
    }
 
    private BigDecimal proratedEarning(BigDecimal earning, BigDecimal refunded, BigDecimal paidAmount) {
        return earning.multiply(refunded).divide(paidAmount, 2, RoundingMode.HALF_UP);
    }
 
    private BigDecimal nonNegative(BigDecimal value) {
        return value == null ? BigDecimal.ZERO : value.max(BigDecimal.ZERO);
    }
 
    private boolean canSettleRefund(String refundStatus) {
        return refundStatus == null
                || refundStatus.trim().isEmpty()
                || RefundStatus.NONE.matches(refundStatus)
                || RefundStatus.PARTIAL.matches(refundStatus)
                || RefundStatus.REFUND_FAILED.matches(refundStatus);
    }
}