liujie
2026-07-30 dc7835c8a9258cbcbe0dbecd4e1c24039ee09e58
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
package com.tourguide.modules.finance.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tourguide.common.exception.BusinessException;
import com.tourguide.common.huifu.biz.DouGongBasicDataBiz;
import com.tourguide.modules.finance.dto.GuideElectronicLedgerRequest;
import com.tourguide.modules.finance.dto.GuideElectronicLedgerResponse;
import com.tourguide.modules.finance.dto.GuideElectronicLedgerStatusResponse;
import com.tourguide.modules.finance.entity.GuideWithdrawalAccount;
import com.tourguide.modules.finance.mapper.GuideWithdrawalAccountMapper;
import com.tourguide.modules.user.entity.GuideUser;
import com.tourguide.modules.user.service.GuideUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;
 
@Service
public class GuideElectronicLedgerService {
    private static final int GUIDE_USER_TYPE = 2;
    private static final int HUIFU_TYPE = 1;
    private static final int ACTIVE_STATUS = 1;
    private static final int PENDING_AUDIT = 1;
    private static final String SUCCESS_CODE = "00000000";
 
    private final GuideWithdrawalAccountMapper accountMapper;
    private final GuideUserService guideUserService;
    private final DouGongBasicDataBiz douGongBasicDataBiz;
 
    public GuideElectronicLedgerService(GuideWithdrawalAccountMapper accountMapper,
                                        GuideUserService guideUserService,
                                        DouGongBasicDataBiz douGongBasicDataBiz) {
        this.accountMapper = accountMapper;
        this.guideUserService = guideUserService;
        this.douGongBasicDataBiz = douGongBasicDataBiz;
    }
 
    public GuideElectronicLedgerResponse create(Long guideId, GuideElectronicLedgerRequest request) {
        GuideUser guide = guideUserService.findEnabledById(guideId);
        GuideWithdrawalAccount existing = findCurrent(guideId);
        if (existing != null && StringUtils.hasText(existing.getHuifuId())) {
            throw new BusinessException(409, "当前导游已完成斗拱进件,请使用修改接口");
        }
 
        GuideWithdrawalAccount account = existing == null ? new GuideWithdrawalAccount() : existing;
        applyRequest(account, request);
        account.setUserId(toTableUserId(guideId));
        account.setUserType(GUIDE_USER_TYPE);
        account.setType(HUIFU_TYPE);
        account.setStatus(ACTIVE_STATUS);
        account.setIsAudit(PENDING_AUDIT);
        account.setApplicationStatus("processing");
        account.setFailureMsgsReason(null);
        account.setOutRequestNo(randomRequestNo());
        account.setCreateTime(account.getCreateTime() == null ? LocalDateTime.now() : account.getCreateTime());
        if (!StringUtils.hasText(account.getPhone())) account.setPhone(guide.getMobile());
 
        save(account);
        try {
            Map<String, Object> basicResult = douGongBasicDataBiz.userBasicDataIndv(account);
            requireSuccess(basicResult, "斗拱个人基本信息进件失败");
            account.setHuifuId(stringValue(basicResult.get("huifu_id")));
            account.setApplicationStatus("basic_succeeded");
            save(account);
 
            Map<String, Object> businessResult = douGongBasicDataBiz.userBusinessOpen(account);
            requireSuccess(businessResult, "斗拱业务进件失败");
            account.setTokenNo(stringValue(businessResult.get("token_no")));
            account.setApplicationStatus("submitted");
            account.setFailureMsgsReason(null);
            save(account);
            return GuideElectronicLedgerResponse.from(account);
        } catch (BusinessException exception) {
            markFailed(account, exception.getMessage());
            throw exception;
        } catch (Exception exception) {
            markFailed(account, exception.getMessage());
            throw new BusinessException(502, "斗拱进件服务暂不可用,请稍后重试");
        }
    }
 
    public GuideElectronicLedgerResponse update(Long guideId, GuideElectronicLedgerRequest request) {
        GuideWithdrawalAccount account = requireCurrent(guideId);
        if (!StringUtils.hasText(account.getHuifuId())) {
            throw new BusinessException(409, "斗拱基础进件尚未完成,请重新提交进件");
        }
        applyRequest(account, request);
        account.setApplicationStatus("processing");
        account.setFailureMsgsReason(null);
        save(account);
        try {
            requireSuccess(douGongBasicDataBiz.userBasicDataIndvModify(account), "斗拱个人信息修改失败");
            Map<String, Object> businessResult = douGongBasicDataBiz.userBusinessOpenModify(account);
            requireSuccess(businessResult, "斗拱业务信息修改失败");
            String tokenNo = stringValue(businessResult.get("token_no"));
            if (StringUtils.hasText(tokenNo)) account.setTokenNo(tokenNo);
            account.setApplicationStatus("submitted");
            account.setFailureMsgsReason(null);
            save(account);
            return GuideElectronicLedgerResponse.from(account);
        } catch (BusinessException exception) {
            markFailed(account, exception.getMessage());
            throw exception;
        } catch (Exception exception) {
            markFailed(account, exception.getMessage());
            throw new BusinessException(502, "斗拱进件修改服务暂不可用,请稍后重试");
        }
    }
 
    public GuideElectronicLedgerResponse detail(Long guideId) {
        return GuideElectronicLedgerResponse.from(requireCurrent(guideId));
    }
 
    public GuideElectronicLedgerStatusResponse status(Long guideId) {
        guideUserService.findEnabledById(guideId);
        return new GuideElectronicLedgerStatusResponse(findCurrent(guideId));
    }
 
    public BigDecimal balance(Long guideId) {
        GuideWithdrawalAccount account = requireCurrent(guideId);
        if (!StringUtils.hasText(account.getHuifuId())) {
            throw new BusinessException(409, "请先完成斗拱进件");
        }
        try {
            return douGongBasicDataBiz.balanceQuery(account.getHuifuId());
        } catch (Exception exception) {
            throw new BusinessException(502, "斗拱账户余额查询失败");
        }
    }
 
    private GuideWithdrawalAccount findCurrent(Long guideId) {
        return accountMapper.selectOne(new LambdaQueryWrapper<GuideWithdrawalAccount>()
                .eq(GuideWithdrawalAccount::getUserId, toTableUserId(guideId))
                .eq(GuideWithdrawalAccount::getUserType, GUIDE_USER_TYPE)
                .eq(GuideWithdrawalAccount::getType, HUIFU_TYPE)
                .eq(GuideWithdrawalAccount::getStatus, ACTIVE_STATUS)
                .orderByDesc(GuideWithdrawalAccount::getId)
                .last("LIMIT 1"));
    }
 
    private GuideWithdrawalAccount requireCurrent(Long guideId) {
        guideUserService.findEnabledById(guideId);
        GuideWithdrawalAccount account = findCurrent(guideId);
        if (account == null) throw new BusinessException(404, "当前导游尚未提交斗拱进件");
        return account;
    }
 
    private void applyRequest(GuideWithdrawalAccount account, GuideElectronicLedgerRequest request) {
        account.setCertificateName(trim(request.getCertificateName()));
        account.setCertificateType(request.getCertificateType() == null ? 0 : request.getCertificateType());
        account.setCertificateCode(trim(request.getCertificateCode()));
        account.setCertificateExpireTime(trim(request.getCertificateExpireTime()));
        account.setLicenceIssuingAuthority(trim(request.getLicenceIssuingAuthority()));
        account.setBankName(normalizeBankName(request.getBankName()));
        account.setBankCardCode(trim(request.getBankCardCode()));
        account.setBankCode(trim(request.getBankCode()));
        account.setBankBranchCode(trim(request.getBankBranchCode()));
        account.setOpenAccountName(trim(request.getOpenAccountName()));
        account.setTransactionAuthorizationCode(trim(request.getTransactionAuthorizationCode()));
        account.setPhone(trim(request.getPhone()));
        account.setIdCardFrontPicture(trim(request.getIdCardFrontPicture()));
        account.setIdCardReversePicture(trim(request.getIdCardReversePicture()));
        account.setBankCardProvinceCode(trim(request.getBankCardProvinceCode()));
        account.setBankCardCityCode(trim(request.getBankCardCityCode()));
        account.setImgUrl(trim(request.getImgUrl()));
        account.setImgUrlOne(trim(request.getImgUrlOne()));
    }
 
    private void requireSuccess(Map<String, Object> result, String fallbackMessage) {
        if (result != null && SUCCESS_CODE.equals(stringValue(result.get("resp_code")))) return;
        String message = result == null ? null : stringValue(result.get("resp_desc"));
        throw new BusinessException(502, StringUtils.hasText(message) ? message : fallbackMessage);
    }
 
    private void markFailed(GuideWithdrawalAccount account, String message) {
        account.setApplicationStatus("failed");
        account.setFailureMsgsReason(StringUtils.hasText(message) ? message : "斗拱进件失败");
        save(account);
    }
 
    private void save(GuideWithdrawalAccount account) {
        if (account.getId() == null) accountMapper.insert(account); else accountMapper.updateById(account);
    }
 
    private int toTableUserId(Long guideId) {
        try {
            return Math.toIntExact(guideId);
        } catch (ArithmeticException exception) {
            throw new BusinessException(400, "导游ID超出斗拱进件表支持范围");
        }
    }
 
    private String randomRequestNo() {
        return UUID.randomUUID().toString().replace("-", "");
    }
 
    private String normalizeBankName(String value) {
        String bankName = trim(value);
        return bankName == null ? null : bankName.replace("& #40;", "(").replace("& #41;", ")");
    }
 
    private String trim(String value) {
        return StringUtils.hasText(value) ? value.trim() : null;
    }
 
    private String stringValue(Object value) {
        return value == null ? null : String.valueOf(value);
    }
}