liujie
2026-09-11 4f6f15b818704079ff2e4e4719b798f48d68a335
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
112
113
114
115
116
117
package com.tourguide.modules.finance.service;
 
import com.tourguide.modules.finance.entity.GuideWithdrawal;
import com.tourguide.modules.finance.mapper.GuideWithdrawalMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
 
import java.math.BigDecimal;
 
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
class GuideWithdrawalStateServiceTest {
    private GuideWithdrawalMapper withdrawalMapper;
    private GuideWithdrawableBalanceService balanceService;
    private GuideWithdrawalStateService service;
 
    @BeforeEach
    void setUp() {
        withdrawalMapper = mock(GuideWithdrawalMapper.class);
        balanceService = mock(GuideWithdrawableBalanceService.class);
        service = new GuideWithdrawalStateService(withdrawalMapper, balanceService);
    }
 
    @Test
    void rejectShouldRefundReservedLocalBalance() {
        GuideWithdrawal withdrawal = pendingWithdrawal();
        when(withdrawalMapper.selectForUpdate(20L)).thenReturn(withdrawal);
        when(withdrawalMapper.updateById(withdrawal)).thenReturn(1);
 
        service.reject(20L, "资料不完整", "admin");
 
        verify(balanceService).credit(8L, new BigDecimal("200.00"));
        assertThat(withdrawal.getStatus()).isEqualTo("REJECTED");
    }
 
    @Test
    void prepareTransferShouldPersistIdempotentRequestIdentity() {
        GuideWithdrawal withdrawal = pendingWithdrawal();
        when(withdrawalMapper.selectForUpdate(20L)).thenReturn(withdrawal);
        when(withdrawalMapper.updateById(withdrawal)).thenReturn(1);
 
        service.prepareTransfer(20L, null, "admin");
 
        assertThat(withdrawal.getStatus()).isEqualTo("TRANSFERRING");
        assertThat(withdrawal.getTransferReqSeqId()).startsWith("TF");
        assertThat(withdrawal.getTransferReqDate()).hasSize(8);
        assertThat(withdrawal.getUpdatedAt()).isNotNull();
    }
 
    @Test
    void transferFailureShouldRemainDistinctFromPendingReview() {
        GuideWithdrawal withdrawal = pendingWithdrawal();
        withdrawal.setStatus("TRANSFERRING");
        when(withdrawalMapper.selectForUpdate(20L)).thenReturn(withdrawal);
        when(withdrawalMapper.updateById(withdrawal)).thenReturn(1);
 
        service.markTransferFailed(20L, "主账户余额不足");
 
        assertThat(withdrawal.getStatus()).isEqualTo("TRANSFER_FAILED");
        assertThat(withdrawal.getPayoutMessage()).isEqualTo("主账户余额不足");
    }
 
    @Test
    void failedTransferShouldAllowManualRetry() {
        GuideWithdrawal withdrawal = pendingWithdrawal();
        withdrawal.setStatus("TRANSFER_FAILED");
        when(withdrawalMapper.selectForUpdate(20L)).thenReturn(withdrawal);
        when(withdrawalMapper.updateById(withdrawal)).thenReturn(1);
 
        service.prepareTransfer(20L, null, "admin");
 
        assertThat(withdrawal.getStatus()).isEqualTo("TRANSFERRING");
        assertThat(withdrawal.getTransferReqSeqId()).startsWith("TF");
    }
 
    @Test
    void rejectShouldRefundReservedTeamBalance() {
        GuideWithdrawal withdrawal = pendingWithdrawal();
        withdrawal.setTeamId(6L);
        when(withdrawalMapper.selectForUpdate(20L)).thenReturn(withdrawal);
        when(withdrawalMapper.updateById(withdrawal)).thenReturn(1);
 
        service.reject(20L, "资料不完整", "admin");
 
        verify(balanceService).creditTeam(6L, new BigDecimal("200.00"));
    }
 
    @Test
    void cashFailureShouldClearPersistedRequestIdentity() {
        GuideWithdrawal withdrawal = pendingWithdrawal();
        withdrawal.setStatus("PROCESSING");
        withdrawal.setCashReqSeqId("CW001");
        withdrawal.setCashReqDate("20260817");
        withdrawal.setCashHfSeqId("HF001");
        when(withdrawalMapper.selectForUpdate(20L)).thenReturn(withdrawal);
        when(withdrawalMapper.resetCashForRetry(any(), any(), any(), any())).thenReturn(1);
 
        service.markCashRetry(20L, "取现失败");
 
        verify(withdrawalMapper).resetCashForRetry(any(), any(), any(), any());
        assertThat(withdrawal.getStatus()).isEqualTo("TRANSFERRED");
        assertThat(withdrawal.getCashReqSeqId()).isNull();
    }
 
    private GuideWithdrawal pendingWithdrawal() {
        GuideWithdrawal withdrawal = new GuideWithdrawal();
        withdrawal.setId(20L);
        withdrawal.setGuideId(8L);
        withdrawal.setAmount(new BigDecimal("200.00"));
        withdrawal.setStatus("PENDING");
        return withdrawal;
    }
}