liujie
2026-09-17 819124be8f16794d409a67f58d5257aef37d9b64
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package com.tourguide.common.huifu.biz;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huifu.bspay.sdk.opps.core.net.BasePayRequest;
import com.tourguide.common.huifu.properties.HuiFuProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
 
 
/**
 * 斗拱进件业务。
 *
 * 备注:该类只负责把本项目导游开户资料提交给斗拱;导游账号、银行卡和审核状态应由上层业务服务落库。
 */
@Component
public class DouGongBasicDataBiz {
    private static final Logger log = LoggerFactory.getLogger(DouGongBasicDataBiz.class);
 
    @Autowired
    private HuiFuProperties huiFuProperties;
 
    /**
     * 个人用户基本信息开户
     *
     * 备注:先创建斗拱个人基础资料,成功后才能继续调用业务入驻和结算卡配置。
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> userBasicDataIndv(DouGongUserWithdrawalProfile userWithdrawal) throws Exception {
        // 1. 数据初始化,填入对应的商户配置
        DouGongRequestSupport.initBasePay(huiFuProperties);
        // 2.组装请求参数
        Map<String, Object> paramsInfo = new HashMap<>();
        // 请求流水号(动态生成,确保唯一性)
        paramsInfo.put("req_seq_id", DouGongRequestSupport.generateForHuiFu());
        // 请求日期
        paramsInfo.put("req_date", DouGongRequestSupport.todayForHuiFu());
        // 个人证件类型
        paramsInfo.put("cert_type", "00");
        String[] certificateExpireDateArr = userWithdrawal.getCertificateExpireTime().split(",");
        String startDateStr = certificateExpireDateArr[0];
        // 个人证件有效期开始日期2023-08-02,2043-08-02
        paramsInfo.put("cert_begin_date", startDateStr.replace("-", ""));
        // 个人证件有效期截止日期
        String endDateStr = certificateExpireDateArr[1];
        if (!"长期".equals(endDateStr)) {
            String formatEndDate = endDateStr.replace("-", "");
            paramsInfo.put("cert_end_date", formatEndDate);
            // 个人证件有效期类型
            paramsInfo.put("cert_validity_type", "0");
        }else {
            // 个人证件有效期类型
            paramsInfo.put("cert_validity_type", "1");
        }
        // 手机号
        paramsInfo.put("mobile_no", userWithdrawal.getPhone());
        // 个人姓名
        paramsInfo.put("name", userWithdrawal.getOpenAccountName());
        // 个人证件号码
        paramsInfo.put("cert_no", userWithdrawal.getCertificateCode());
        // 管理员账号
//        paramsInfo.put("login_name", userWithdrawal.getPhone());
        // 是否发送短信标识
        paramsInfo.put("sms_send_flag", "Y");
        // 3. 发起API调用
        log.info("个人用户基本信息开户请求参数:{}", paramsInfo);
        Map<String, Object> response = BasePayRequest.requestBasePay("v2/user/basicdata/indv", paramsInfo, null, false);
        log.info("个人用户基本信息开户返回参数:{}", response);
        return response;
    }
 
    /** 企业用户基础资料进件。 */
    public Map<String, Object> userBasicDataEnt(DouGongUserWithdrawalProfile userWithdrawal) throws Exception {
        DouGongRequestSupport.initBasePay(huiFuProperties);
        Map<String, Object> paramsInfo = enterpriseParams(userWithdrawal);
        paramsInfo.put("req_seq_id", DouGongRequestSupport.generateForHuiFu());
        paramsInfo.put("req_date", DouGongRequestSupport.todayForHuiFu());
        log.info("企业用户基本信息开户请求参数:{}", paramsInfo);
        Map<String, Object> response = BasePayRequest.requestBasePay(
                "v2/user/basicdata/ent", paramsInfo, null, false);
        log.info("企业用户基本信息开户返回参数:{}", response);
        return response;
    }
    /**
     * 个人用户基本信息修改
     * @return
     * @throws Exception
     */
    public Map<String, Object> userBasicDataIndvModify(DouGongUserWithdrawalProfile userWithdrawal) throws Exception {
        // 1. 数据初始化,填入对应的商户配置
        DouGongRequestSupport.initBasePay(huiFuProperties);
        // 2.组装请求参数
        Map<String, Object> paramsInfo = new HashMap<>();
        // 请求流水号
        paramsInfo.put("req_seq_id", DouGongRequestSupport.generateForHuiFu());
        // 请求日期
        paramsInfo.put("req_date", DouGongRequestSupport.todayForHuiFu());
        // 汇付客户Id
        paramsInfo.put("huifu_id", userWithdrawal.getHuifuId());
        String[] certificateExpireDateArr = userWithdrawal.getCertificateExpireTime().split(",");
        String startDateStr = certificateExpireDateArr[0];
        // 个人证件有效期开始日期
        paramsInfo.put("cert_begin_date", startDateStr.replace("-", ""));
        // 个人证件有效期截止日期
        String endDateStr = certificateExpireDateArr[1];
        if (!"长期".equals(endDateStr)) {
            String formatEndDate = endDateStr.replace("-", "");
            paramsInfo.put("cert_end_date", formatEndDate);
            // 个人证件有效期类型
            paramsInfo.put("cert_validity_type", "0");
        }else {
            // 个人证件有效期类型
            paramsInfo.put("cert_validity_type", "1");
        }
        // 手机号
        paramsInfo.put("mobile_no", userWithdrawal.getPhone());
        log.info("个人用户基本信息修改返回参数:{}", paramsInfo);
        // 3. 发起API调用
        Map<String, Object> response = BasePayRequest.requestBasePay("v2/user/basicdata/indv/modify", paramsInfo, null, false);
        log.info("个人用户基本信息修改返回参数:{}", response);
        return response;
    }
 
    /** 企业用户基础资料修改,用于审核驳回后的重新提交。 */
    public Map<String, Object> userBasicDataEntModify(DouGongUserWithdrawalProfile userWithdrawal) throws Exception {
        DouGongRequestSupport.initBasePay(huiFuProperties);
        Map<String, Object> paramsInfo = enterpriseParams(userWithdrawal);
        paramsInfo.put("req_seq_id", DouGongRequestSupport.generateForHuiFu());
        paramsInfo.put("req_date", DouGongRequestSupport.todayForHuiFu());
        paramsInfo.put("huifu_id", userWithdrawal.getHuifuId());
        log.info("企业用户基本信息修改请求参数:{}", paramsInfo);
        Map<String, Object> response = BasePayRequest.requestBasePay(
                "v2/user/basicdata/ent/modify", paramsInfo, null, false);
        log.info("企业用户基本信息修改返回参数:{}", response);
        return response;
    }
 
    /** 查询个人或企业用户的进件申请状态。 */
    public Map<String, Object> userApplyQuery(DouGongUserWithdrawalProfile userWithdrawal,
                                               String applyNo) throws Exception {
        if (applyNo == null || applyNo.trim().isEmpty() || applyNo.trim().length() > 18) {
            throw new IllegalArgumentException("斗拱进件状态查询申请单号不能为空且长度不能超过18个字符");
        }
        DouGongRequestSupport.initBasePay(huiFuProperties);
        Map<String, Object> paramsInfo = new HashMap<>();
        paramsInfo.put("huifu_id", userWithdrawal.getHuifuId());
        paramsInfo.put("req_seq_id", DouGongRequestSupport.generateForHuiFu());
        paramsInfo.put("req_date", DouGongRequestSupport.todayForHuiFu());
        paramsInfo.put("apply_no", applyNo.trim());
        log.info("用户进件申请状态查询请求参数:{}", paramsInfo);
        Map<String, Object> response = BasePayRequest.requestBasePay(
                "v2/user/apply/query", paramsInfo, null, false);
        log.info("用户进件申请状态查询返回参数:{}", response);
        return response;
    }
    /**
     * 用户业务入驻
     *
     * 备注:业务入驻会绑定结算卡和提现规则,后续提现依赖这里返回的取现卡序列号。
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> userBusinessOpen(DouGongUserWithdrawalProfile userWithdrawal) throws Exception {
        // 1. 数据初始化,填入对应的商户配置
        DouGongRequestSupport.initBasePay(huiFuProperties);
        // 2.组装请求参数
        Map<String, Object> paramsInfo = new HashMap<>();
        // 汇付ID
        paramsInfo.put("huifu_id", userWithdrawal.getHuifuId());
        // 请求流水号
        paramsInfo.put("req_seq_id", DouGongRequestSupport.generateForHuiFu());
        // 请求日期
        paramsInfo.put("req_date", DouGongRequestSupport.todayForHuiFu());
        // 渠道商/商户汇付Id
        paramsInfo.put("upper_huifu_id", huiFuProperties.getUpperHuifuId());
        //// 结算信息配置
        //paramsInfo.put("settle_config", "{\"settle_cycle\":\"D1\",\"min_amt\":\"0.01\",\"remained_amt\":\"10.00\",\"settle_abstract\":\"结算摘要\",\"out_settle_flag\":\"2\",\"settle_batch_no\":\"300\",\"fixed_ratio\":\"2\"}");
        // 结算卡信息
        JSONObject cardInfo = new JSONObject();
        cardInfo.put("card_type", "1");
        cardInfo.put("card_name", userWithdrawal.getOpenAccountName());
        cardInfo.put("card_no", userWithdrawal.getBankCardCode());
        cardInfo.put("prov_id", userWithdrawal.getBankCardProvinceCode());
        cardInfo.put("area_id", userWithdrawal.getBankCardCityCode());
        cardInfo.put("cert_type", "00");
        cardInfo.put("cert_no", userWithdrawal.getCertificateCode());
        String[] certificateExpireDateArr = userWithdrawal.getCertificateExpireTime().split(",");
        String startDateStr = certificateExpireDateArr[0];
        // 个人证件有效期开始日期
        cardInfo.put("cert_begin_date", startDateStr.replace("-", ""));
        // 个人证件有效期截止日期
        String endDateStr = certificateExpireDateArr[1];
        if (!"长期".equals(endDateStr)) {
            String formatEndDate = endDateStr.replace("-", "");
            cardInfo.put("cert_end_date", formatEndDate);
            // 个人证件有效期类型
            cardInfo.put("cert_validity_type", "0");
        }else {
            // 个人证件有效期类型
            cardInfo.put("cert_validity_type", "1");
        }
        paramsInfo.put("card_info", cardInfo.toJSONString());
        // 结算卡信息
        JSONArray cashConfigArr = new JSONArray();
        JSONObject cashConfig = new JSONObject();
        /**
         * T1:下一工作日到银行账户;
         * D1:下一自然日到银行账户;
         * D0:当日到银行账户;默认D0;
         * DM:当日到账;到账资金不包括当天的交易资金;
         * 示例值:T1
         */
        cashConfig.put("cash_type", "D1");
        cashConfig.put("fix_amt", "0.00");
        cashConfig.put("weekday_fix_amt", "0.00");
        cashConfig.put("weekday_fee_rate", "0.00");
        cashConfigArr.add(cashConfig);
        paramsInfo.put("cash_config", cashConfigArr.toJSONString());
        log.info("用户业务入驻返回参数:{}", paramsInfo);
        // 3. 发起API调用
        Map<String, Object> response = BasePayRequest.requestBasePay("v2/user/busi/open", paramsInfo, null, false);;
        log.info("用户业务入驻返回参数:{}", response);
        return response;
    }
    /**
     * 用户业务入驻
     * @return
     * @throws Exception
     */
    public Map<String, Object> userBusinessOpenModify(DouGongUserWithdrawalProfile userWithdrawal) throws Exception {
        // 1. 数据初始化,填入对应的商户配置
        DouGongRequestSupport.initBasePay(huiFuProperties);
        // 2.组装请求参数
        Map<String, Object> paramsInfo = new HashMap<>();
        // 汇付ID
        paramsInfo.put("huifu_id", userWithdrawal.getHuifuId());
        // 请求流水号
        paramsInfo.put("req_seq_id", DouGongRequestSupport.generateForHuiFu());
        // 请求日期
        paramsInfo.put("req_date", DouGongRequestSupport.todayForHuiFu());
        // 结算卡信息
        JSONObject cardInfo = new JSONObject();
        cardInfo.put("card_type", "1");
        cardInfo.put("card_name", userWithdrawal.getOpenAccountName());
        cardInfo.put("card_no", userWithdrawal.getBankCardCode());
        cardInfo.put("prov_id", userWithdrawal.getBankCardProvinceCode());
        cardInfo.put("area_id", userWithdrawal.getBankCardCityCode());
        cardInfo.put("cert_type", "00");
        cardInfo.put("cert_no", userWithdrawal.getCertificateCode());
        String[] certificateExpireDateArr = userWithdrawal.getCertificateExpireTime().split(",");
        String startDateStr = certificateExpireDateArr[0];
        // 个人证件有效期开始日期
        cardInfo.put("cert_begin_date", startDateStr.replace("-", ""));
        // 个人证件有效期截止日期
        String endDateStr = certificateExpireDateArr[1];
        if (!"长期".equals(endDateStr)) {
            String formatEndDate = endDateStr.replace("-", "");
            cardInfo.put("cert_end_date", formatEndDate);
            // 个人证件有效期类型
            cardInfo.put("cert_validity_type", "0");
        }else {
            // 个人证件有效期类型
            cardInfo.put("cert_validity_type", "1");
        }
        paramsInfo.put("card_info", cardInfo.toJSONString());
        // 结算卡信息
        JSONArray cashConfigArr = new JSONArray();
        JSONObject cashConfig = new JSONObject();
        /**
         * T1:下一工作日到银行账户;
         * D1:下一自然日到银行账户;
         * D0:当日到银行账户;默认D0;
         * DM:当日到账;到账资金不包括当天的交易资金;
         * 示例值:T1
         */
        cashConfig.put("switch_state", "1");
        cashConfig.put("cash_type", "D1");
        cashConfig.put("fix_amt", "0.00");
        cashConfig.put("weekday_fix_amt", "0.00");
        cashConfig.put("weekday_fee_rate", "0.00");
        cashConfigArr.add(cashConfig);
        paramsInfo.put("cash_config", cashConfigArr.toJSONString());
        log.info("用户业务入驻修改返回参数:{}", paramsInfo);
 
        // 3. 发起API调用
        Map<String, Object> response = BasePayRequest.requestBasePay("v2/user/busi/modify", paramsInfo, null, false);;
        log.info("用户业务入驻修改返回参数:{}", response);
        return response;
    }
 
    public BigDecimal balanceQuery(String huifuId) throws Exception {
        // 1. 数据初始化,填入对应的商户配置
        DouGongRequestSupport.initBasePay(huiFuProperties);
        // 2.组装请求参数
        Map<String, Object> paramsInfo = new HashMap<>();
        // 汇付ID
        paramsInfo.put("huifu_id", huifuId);
        // 请求流水号
        paramsInfo.put("req_seq_id", DouGongRequestSupport.generateForHuiFu());
        // 请求日期
        paramsInfo.put("req_date", DouGongRequestSupport.todayForHuiFu());
        log.info("账户余额信息查询返回参数:{}", paramsInfo);
        // 3. 发起API调用
        Map<String, Object> response = BasePayRequest.requestBasePay("v2/trade/acctpayment/balance/query", paramsInfo, null, false);
        log.info("账户余额信息查询返回参数:{}", response);
        return availableBalance(response);
    }
 
    static BigDecimal availableBalance(Map<String, Object> response) {
        if (response == null) return BigDecimal.ZERO;
        Object accountData = response.get("acctInfo_list");
        if (accountData == null) accountData = response.get("acct_info_list");
        if (accountData == null) return BigDecimal.ZERO;
        JSONArray accountList = accountData instanceof JSONArray
                ? (JSONArray) accountData
                : JSONArray.parseArray(accountData instanceof String
                ? (String) accountData : JSONObject.toJSONString(accountData));
        if (accountList == null || accountList.isEmpty()) return BigDecimal.ZERO;
        JSONObject acctInfo = accountList.getJSONObject(0);
        if (acctInfo == null || acctInfo.get("avail_bal") == null) return BigDecimal.ZERO;
        BigDecimal balance = acctInfo.getBigDecimal("avail_bal");
        return balance == null ? BigDecimal.ZERO : balance;
    }
 
    private Map<String, Object> enterpriseParams(DouGongUserWithdrawalProfile profile) {
        Map<String, Object> params = new HashMap<>();
        params.put("reg_name", profile.getEnterpriseName());
        params.put("license_code", profile.getCreditCode());
        params.put("license_validity_type", validityType(profile.getLicenseValidityType()));
        params.put("license_begin_date", compactDate(profile.getLicenseBeginDate()));
        putFixedEndDate(params, "license_end_date", profile.getLicenseValidityType(), profile.getLicenseEndDate());
        params.put("reg_prov_id", profile.getRegisteredProvinceCode());
        params.put("reg_area_id", profile.getRegisteredCityCode());
        params.put("reg_district_id", profile.getRegisteredDistrictCode());
        params.put("reg_detail", profile.getRegisteredAddress());
        params.put("legal_name", profile.getCertificateName());
        params.put("legal_cert_type", "00");
        params.put("legal_cert_no", profile.getCertificateCode());
        String[] certificateDates = certificateDates(profile.getCertificateExpireTime());
        params.put("legal_cert_validity_type", "长期".equals(certificateDates[1]) ? "1" : "0");
        params.put("legal_cert_begin_date", compactDate(certificateDates[0]));
        if (!"长期".equals(certificateDates[1])) {
            params.put("legal_cert_end_date", compactDate(certificateDates[1]));
        }
        params.put("legal_cert_nationality", "CHN");
        params.put("contact_name", profile.getContactName());
        params.put("contact_mobile", profile.getPhone());
        return params;
    }
 
    private String validityType(String value) {
        return "long_term".equals(value) ? "1" : "0";
    }
 
    private void putFixedEndDate(Map<String, Object> params, String key, String validityType, String value) {
        if (!"long_term".equals(validityType) && value != null && !value.trim().isEmpty()) {
            params.put(key, compactDate(value));
        }
    }
 
    private String compactDate(String value) {
        return value == null ? null : value.replace("-", "");
    }
 
    private String[] certificateDates(String value) {
        String normalized = value == null ? "" : value.replace(",", ",");
        String[] values = normalized.split(",", -1);
        return values.length >= 2 ? values : new String[]{normalized, "长期"};
    }
}