liujie
2026-09-14 15defa991fcd30b9fa60bab17d2230b5929bf2a2
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package com.tourguide.modules.finance.service;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tourguide.common.api.PageResponse;
import com.tourguide.common.exception.BusinessException;
import com.tourguide.modules.finance.dto.GuideWithdrawalCreateRequest;
import com.tourguide.modules.finance.dto.GuideWithdrawalSummaryResponse;
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.entity.GuideTeam;
import com.tourguide.modules.user.mapper.GuideTeamMapper;
import com.tourguide.modules.user.mapper.GuideUserMapper;
import com.tourguide.modules.user.service.GuideUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
 
/** 导游提现申请、审核发起和斗拱到账回调。 */
@Service
public class GuideWithdrawalService {
    private static final DateTimeFormatter NO_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    private static final String STATUS_PENDING = "PENDING";
    private static final String STATUS_APPROVED = "APPROVED";
    private static final String STATUS_REJECTED = "REJECTED";
 
    private final GuideWithdrawalMapper withdrawalMapper;
    private final GuideSettlementService settlementService;
    private final GuideUserService guideUserService;
    private final GuideWithdrawableBalanceService balanceService;
    private final GuideWithdrawalStateService stateService;
    private final GuideWithdrawalPayoutService payoutService;
    private final GuideTeamMapper teamMapper;
    private final GuideUserMapper guideUserMapper;
 
    public GuideWithdrawalService(GuideWithdrawalMapper withdrawalMapper,
                                  GuideSettlementService settlementService,
                                  GuideUserService guideUserService,
                                  GuideWithdrawableBalanceService balanceService,
                                  GuideWithdrawalStateService stateService,
                                  GuideWithdrawalPayoutService payoutService,
                                  GuideTeamMapper teamMapper,
                                  GuideUserMapper guideUserMapper) {
        this.withdrawalMapper = withdrawalMapper;
        this.settlementService = settlementService;
        this.guideUserService = guideUserService;
        this.balanceService = balanceService;
        this.stateService = stateService;
        this.payoutService = payoutService;
        this.teamMapper = teamMapper;
        this.guideUserMapper = guideUserMapper;
    }
 
    public GuideWithdrawalSummaryResponse summary(Long guideId) {
        try {
            settlementService.requireApproved(guideId);
            GuideUser guide = guideUserService.findEnabledById(guideId);
            GuideTeam collectionTeam = findCollectionTeam(guide);
            return new GuideWithdrawalSummaryResponse(collectionTeam == null
                    ? balanceService.getAvailableAmount(guideId)
                    : balanceService.getAvailableTeamAmount(collectionTeam.getId()));
        } catch (BusinessException exception) {
            if (exception.getCode() == 409) {
                return new GuideWithdrawalSummaryResponse(money(BigDecimal.ZERO));
            }
            throw exception;
        }
    }
 
    public PageResponse<WithdrawalResponse> page(Long guideId, long page, long size) {
        guideUserService.findEnabledById(guideId);
        long safePage = Math.max(page, 1);
        long safeSize = Math.min(Math.max(size, 1), 100);
        Page<GuideWithdrawal> result = withdrawalMapper.selectPage(
                new Page<GuideWithdrawal>(safePage, safeSize),
                new LambdaQueryWrapper<GuideWithdrawal>()
                        .eq(GuideWithdrawal::getGuideId, guideId)
                        .orderByDesc(GuideWithdrawal::getAppliedAt)
                        .orderByDesc(GuideWithdrawal::getId));
        return new PageResponse<WithdrawalResponse>(result.getRecords().stream()
                .map(WithdrawalResponse::new).collect(Collectors.toList()),
                result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    @Transactional
    public WithdrawalResponse create(Long guideId, GuideWithdrawalCreateRequest request) {
        GuideUser guide = guideUserMapper.selectForUpdate(guideId);
        if (guide == null || !Integer.valueOf(1).equals(guide.getStatus())) {
            throw new BusinessException(401, "导游用户不存在或已停用");
        }
        GuideWithdrawalAccount account = settlementService.requireApprovedCardForUpdate(
                guideId, request.getBankCardId());
        BigDecimal amount = money(request.getAmount());
        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException(400, "提现金额必须大于0");
        }
        GuideTeam collectionTeam = findCollectionTeam(guide);
        boolean debited = collectionTeam == null
                ? balanceService.debit(guideId, amount)
                : balanceService.debitTeam(collectionTeam.getId(), amount);
        if (!debited) {
            throw new BusinessException(409, "提现金额不能超过当前可提金额");
        }
        GuideWithdrawal withdrawal = new GuideWithdrawal();
        withdrawal.setWithdrawalNo(generateWithdrawalNo());
        withdrawal.setGuideId(guideId);
        withdrawal.setTeamId(collectionTeam == null ? null : collectionTeam.getId());
        withdrawal.setGuideName(StringUtils.hasText(guide.getRealName())
                ? guide.getRealName() : guide.getUsername());
        withdrawal.setGuideMobile(guide.getMobile());
        withdrawal.setAmount(amount);
        withdrawal.setBankCardId(account.getId());
        withdrawal.setBankName(account.getBankName());
        withdrawal.setCardMasked(maskCard(account.getBankCardCode()));
        withdrawal.setStatus(STATUS_PENDING);
        withdrawal.setAppliedAt(LocalDateTime.now());
        withdrawal.setCreatedAt(LocalDateTime.now());
        withdrawal.setUpdatedAt(LocalDateTime.now());
        withdrawalMapper.insert(withdrawal);
        return new WithdrawalResponse(withdrawal);
    }
 
    public WithdrawalResponse process(Long id, String status, String remark, String processor) {
        if (STATUS_REJECTED.equals(status)) {
            return stateService.reject(id, remark, processor);
        }
        if (!STATUS_APPROVED.equals(status)) throw new BusinessException(400, "提现处理状态不正确");
        return payoutService.approve(id, remark, processor);
    }
 
    @Transactional
    public boolean handleCallback(JSONObject body) {
        List<String> requestSequences = callbackSequences(body);
        if (requestSequences.isEmpty()) {
            throw new BusinessException(400, "汇付提现回调缺少请求流水号");
        }
        String requestSequence = null;
        GuideWithdrawal withdrawal = null;
        for (String candidate : requestSequences) {
            GuideWithdrawal matched = withdrawalMapper.selectByPayoutSequenceForUpdate(candidate);
            if (matched != null) {
                requestSequence = candidate;
                withdrawal = matched;
                break;
            }
        }
        if (withdrawal == null) throw new BusinessException(404, "提现申请不存在");
        if (STATUS_APPROVED.equals(withdrawal.getStatus()) || STATUS_REJECTED.equals(withdrawal.getStatus())) {
            return false;
        }
        boolean processing = GuideWithdrawalStateService.PROCESSING.equals(withdrawal.getStatus());
        boolean recoverableTransferred = GuideWithdrawalStateService.TRANSFERRED.equals(withdrawal.getStatus())
                && (requestSequence.equals(withdrawal.getCashReqSeqId())
                || requestSequence.equals(withdrawal.getCashHfSeqId()));
        if (!processing && !recoverableTransferred) {
            return false;
        }
        validateCallback(body, withdrawal);
        String transStatus = first(body, "trans_stat", "cash_stat", "cash_status",
                "trans_status", "status");
        LocalDateTime now = LocalDateTime.now();
        if (isFailure(body, transStatus)) {
            String message = "导游账户取现失败:" + callbackMessage(body);
            if (withdrawalMapper.resetCashForRetry(withdrawal.getId(), message, now, now) != 1) {
                throw new BusinessException(500, "提现回调状态更新失败");
            }
            withdrawal.setStatus(GuideWithdrawalStateService.TRANSFERRED);
            withdrawal.setCashReqSeqId(null);
            withdrawal.setCashReqDate(null);
            withdrawal.setCashHfSeqId(null);
            withdrawal.setPayoutMessage(message);
            withdrawal.setProcessedAt(now);
            withdrawal.setUpdatedAt(now);
            return true;
        }
        String hfSeqId = first(body, "hf_seq_id", "trans_seq_id", "cash_hf_seq_id");
        if (StringUtils.hasText(hfSeqId)) withdrawal.setCashHfSeqId(hfSeqId);
        if (isSuccess(transStatus)) {
            withdrawal.setStatus(STATUS_APPROVED);
            withdrawal.setPayoutMessage("取现到账成功");
            withdrawal.setProcessedAt(now);
        } else {
            withdrawal.setStatus(GuideWithdrawalStateService.PROCESSING);
            withdrawal.setPayoutMessage("导游账户取现结果待确认:" + callbackMessage(body));
        }
        withdrawal.setUpdatedAt(now);
        if (withdrawalMapper.updateById(withdrawal) != 1) {
            throw new BusinessException(500, "提现回调状态更新失败");
        }
        return true;
    }
 
    private void validateCallback(JSONObject body, GuideWithdrawal withdrawal) {
        GuideWithdrawalAccount account = settlementService.findCurrent(withdrawal.getGuideId());
        String callbackHuifuId = first(body, "huifu_id", "out_huifu_id");
        if (account == null || !StringUtils.hasText(callbackHuifuId)
                || !callbackHuifuId.equals(account.getHuifuId())) {
            throw new BusinessException(400, "汇付提现回调账户不匹配");
        }
        String amountText = first(body, "cash_amt", "trans_amt", "ord_amt");
        if (StringUtils.hasText(amountText)) {
            try {
                if (withdrawal.getAmount().compareTo(new BigDecimal(amountText)) != 0) {
                    throw new BusinessException(409, "汇付提现回调金额不匹配");
                }
            } catch (NumberFormatException exception) {
                throw new BusinessException(400, "汇付提现回调金额格式错误");
            }
        }
    }
 
    private String callbackMessage(JSONObject body) {
        String message = first(body, "resp_desc", "fail_reason", "failure_reason");
        return StringUtils.hasText(message) ? message : "斗拱未返回终态说明";
    }
 
    private String generateWithdrawalNo() {
        return "WD" + LocalDateTime.now().format(NO_TIME_FORMAT)
                + String.format("%04d", ThreadLocalRandom.current().nextInt(10000));
    }
 
    private GuideTeam findCollectionTeam(GuideUser guide) {
        if (guide == null || guide.getTeamId() == null) return null;
        GuideTeam team = teamMapper.selectById(guide.getTeamId());
        return team != null && guide.getId().equals(team.getCollectionGuideId()) ? team : null;
    }
 
    private String maskCard(String cardNumber) {
        if (!StringUtils.hasText(cardNumber)) return "";
        String value = cardNumber.trim();
        return "****" + value.substring(Math.max(0, value.length() - 4));
    }
 
    private BigDecimal money(BigDecimal value) {
        return (value == null ? BigDecimal.ZERO : value).setScale(2, RoundingMode.HALF_UP);
    }
 
    private boolean isSuccess(String status) {
        return "S".equalsIgnoreCase(status) || "SUCCESS".equalsIgnoreCase(status)
                || "SUCCEEDED".equalsIgnoreCase(status);
    }
 
    private boolean isFailure(JSONObject body, String status) {
        if ("F".equalsIgnoreCase(status) || "FAIL".equalsIgnoreCase(status)
                || "FAILED".equalsIgnoreCase(status)) {
            return true;
        }
        String responseCode = first(body, "resp_code", "code");
        return StringUtils.hasText(responseCode)
                && !"00000000".equals(responseCode)
                && !"00000100".equals(responseCode);
    }
 
    private String first(JSONObject body, String... keys) {
        if (body == null) return null;
        for (String key : keys) {
            String value = body.getString(key);
            if (StringUtils.hasText(value)) return value;
        }
        JSONObject data = body.getJSONObject("data");
        if (data != null) return first(data, keys);
        String dataText = body.getString("data");
        if (StringUtils.hasText(dataText)) {
            try {
                JSONObject nested = JSONObject.parseObject(dataText);
                return nested == null ? null : first(nested, keys);
            } catch (RuntimeException ignored) {
                // 非 JSON 包装直接按字段缺失处理。
            }
        }
        return null;
    }
 
    private List<String> callbackSequences(JSONObject body) {
        List<String> sequences = new ArrayList<>();
        collectCallbackSequences(body, sequences);
        return sequences;
    }
 
    private void collectCallbackSequences(JSONObject body, List<String> sequences) {
        if (body == null) return;
        String[] keys = {"req_seq_id", "org_req_seq_id", "cash_req_seq_id", "withdrawal_no",
                "hf_seq_id", "trans_seq_id", "cash_hf_seq_id"};
        for (String key : keys) {
            String value = body.getString(key);
            if (StringUtils.hasText(value) && !sequences.contains(value)) sequences.add(value);
        }
        JSONObject data = body.getJSONObject("data");
        if (data != null) {
            collectCallbackSequences(data, sequences);
            return;
        }
        String dataText = body.getString("data");
        if (StringUtils.hasText(dataText)) {
            try {
                JSONObject nested = JSONObject.parseObject(dataText);
                collectCallbackSequences(nested, sequences);
            } catch (RuntimeException ignored) {
                // 非 JSON 包装继续使用顶层字段。
            }
        }
    }
}