liujie
2026-08-21 a7c8a8f6055e1a03414f2227d12ff2e87ec52d41
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.tourguide.modules.finance.service;
 
import com.alibaba.fastjson.JSONObject;
import com.tourguide.modules.finance.dto.GuideWithdrawalCreateRequest;
import com.tourguide.modules.finance.dto.WithdrawalResponse;
import com.tourguide.modules.finance.entity.GuideWithdrawal;
import com.tourguide.modules.finance.entity.GuideWithdrawalAccount;
import com.tourguide.modules.finance.mapper.GuideWithdrawalMapper;
import com.tourguide.modules.user.entity.GuideUser;
import com.tourguide.modules.user.service.GuideUserService;
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.Mockito.verify;
import static org.mockito.Mockito.never;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
class GuideWithdrawalServiceTest {
    private GuideWithdrawalMapper withdrawalMapper;
    private GuideSettlementService settlementService;
    private GuideUserService guideUserService;
    private GuideWithdrawableBalanceService balanceService;
    private GuideWithdrawalStateService stateService;
    private GuideWithdrawalPayoutService payoutService;
    private GuideWithdrawalService service;
 
    @BeforeEach
    void setUp() {
        withdrawalMapper = mock(GuideWithdrawalMapper.class);
        settlementService = mock(GuideSettlementService.class);
        guideUserService = mock(GuideUserService.class);
        balanceService = mock(GuideWithdrawableBalanceService.class);
        stateService = mock(GuideWithdrawalStateService.class);
        payoutService = mock(GuideWithdrawalPayoutService.class);
        service = new GuideWithdrawalService(withdrawalMapper, settlementService,
                guideUserService, balanceService, stateService, payoutService);
    }
 
    @Test
    void createShouldReserveAvailableBalanceAndPersistCardSnapshot() throws Exception {
        GuideUser guide = new GuideUser();
        guide.setRealName("张三");
        guide.setMobile("13800138000");
        GuideWithdrawalAccount account = new GuideWithdrawalAccount();
        account.setId(10);
        account.setHuifuId("HF100");
        account.setBankName("招商银行");
        account.setBankCardCode("6225888888885678");
        when(guideUserService.findEnabledById(8L)).thenReturn(guide);
        when(settlementService.requireApprovedCardForUpdate(8L, 10)).thenReturn(account);
        when(balanceService.debit(8L, new BigDecimal("200.00"))).thenReturn(true);
        doAnswer(invocation -> {
            GuideWithdrawal withdrawal = invocation.getArgument(0);
            withdrawal.setId(20L);
            return 1;
        }).when(withdrawalMapper).insert(any(GuideWithdrawal.class));
        GuideWithdrawalCreateRequest request = new GuideWithdrawalCreateRequest();
        request.setAmount(new BigDecimal("200.00"));
        request.setBankCardId(10);
 
        WithdrawalResponse response = service.create(8L, request);
 
        assertThat(response.getAmount()).isEqualByComparingTo("200.00");
        assertThat(response.getBankName()).isEqualTo("招商银行");
        assertThat(response.getCardMasked()).isEqualTo("****5678");
        assertThat(response.getStatus()).isEqualTo("PENDING");
        verify(balanceService).debit(8L, new BigDecimal("200.00"));
    }
 
    @Test
    void rejectShouldReturnReservedBalance() {
        GuideWithdrawal withdrawal = new GuideWithdrawal();
        withdrawal.setId(20L);
        withdrawal.setGuideId(8L);
        withdrawal.setAmount(new BigDecimal("200.00"));
        withdrawal.setStatus("REJECTED");
        when(stateService.reject(20L, "资料不完整", "admin"))
                .thenReturn(new WithdrawalResponse(withdrawal));
 
        WithdrawalResponse response = service.process(20L, "REJECTED", "资料不完整", "admin");
 
        assertThat(response.getStatus()).isEqualTo("REJECTED");
        verify(stateService).reject(20L, "资料不完整", "admin");
    }
 
    @Test
    void failedCashCallbackShouldReturnToTransferredWithoutRefundingLocalBalance() {
        GuideWithdrawal withdrawal = new GuideWithdrawal();
        withdrawal.setId(20L);
        withdrawal.setGuideId(8L);
        withdrawal.setAmount(new BigDecimal("200.00"));
        withdrawal.setCashReqSeqId("CW001");
        withdrawal.setCashReqDate("20260817");
        withdrawal.setStatus("PROCESSING");
        GuideWithdrawalAccount account = new GuideWithdrawalAccount();
        account.setHuifuId("HF100");
        when(withdrawalMapper.selectByPayoutSequenceForUpdate("CW001")).thenReturn(withdrawal);
        when(settlementService.findCurrent(8L)).thenReturn(account);
        when(withdrawalMapper.resetCashForRetry(any(), any(), any(), any())).thenReturn(1);
        JSONObject body = new JSONObject();
        body.put("req_seq_id", "CW001");
        body.put("huifu_id", "HF100");
        body.put("cash_amt", "200.00");
        body.put("trans_stat", "F");
        body.put("resp_desc", "银行处理中断");
 
        assertThat(service.handleCallback(body)).isTrue();
 
        assertThat(withdrawal.getStatus()).isEqualTo("TRANSFERRED");
        assertThat(withdrawal.getCashReqSeqId()).isNull();
        verify(balanceService, org.mockito.Mockito.never()).credit(8L, new BigDecimal("200.00"));
    }
 
    @Test
    void nonTerminalCashCallbackShouldRemainProcessing() {
        GuideWithdrawal withdrawal = processingWithdrawal();
        GuideWithdrawalAccount account = new GuideWithdrawalAccount();
        account.setHuifuId("HF100");
        when(withdrawalMapper.selectByPayoutSequenceForUpdate("CW001")).thenReturn(withdrawal);
        when(settlementService.findCurrent(8L)).thenReturn(account);
        when(withdrawalMapper.updateById(withdrawal)).thenReturn(1);
        JSONObject body = new JSONObject();
        body.put("req_seq_id", "CW001");
        body.put("huifu_id", "HF100");
        body.put("cash_amt", "200.00");
        body.put("hf_seq_id", "HF-CASH-1");
 
        assertThat(service.handleCallback(body)).isTrue();
 
        assertThat(withdrawal.getStatus()).isEqualTo("PROCESSING");
        assertThat(withdrawal.getCashReqSeqId()).isEqualTo("CW001");
        assertThat(withdrawal.getCashHfSeqId()).isEqualTo("HF-CASH-1");
        verify(withdrawalMapper, never()).resetCashForRetry(any(), any(), any(), any());
    }
 
    @Test
    void successCallbackShouldRecoverTransferredRecordWithSameCashSequence() {
        GuideWithdrawal withdrawal = processingWithdrawal();
        withdrawal.setStatus("TRANSFERRED");
        GuideWithdrawalAccount account = new GuideWithdrawalAccount();
        account.setHuifuId("HF100");
        when(withdrawalMapper.selectByPayoutSequenceForUpdate("CW001")).thenReturn(withdrawal);
        when(settlementService.findCurrent(8L)).thenReturn(account);
        when(withdrawalMapper.updateById(withdrawal)).thenReturn(1);
        JSONObject body = new JSONObject();
        body.put("req_seq_id", "CW001");
        body.put("huifu_id", "HF100");
        body.put("cash_amt", "200.00");
        body.put("trans_stat", "S");
 
        assertThat(service.handleCallback(body)).isTrue();
 
        assertThat(withdrawal.getStatus()).isEqualTo("APPROVED");
        assertThat(withdrawal.getPayoutMessage()).isEqualTo("取现到账成功");
    }
 
    private GuideWithdrawal processingWithdrawal() {
        GuideWithdrawal withdrawal = new GuideWithdrawal();
        withdrawal.setId(20L);
        withdrawal.setGuideId(8L);
        withdrawal.setAmount(new BigDecimal("200.00"));
        withdrawal.setCashReqSeqId("CW001");
        withdrawal.setCashReqDate("20260817");
        withdrawal.setStatus("PROCESSING");
        return withdrawal;
    }
}