Sunshine
2024-11-05 8f7985d7764a0aad24bd593ac5ea47b7fc290961
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
package com.nova.sankuai.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nova.sankuai.domain.api.huizhixin.Base64Utils;
import com.nova.sankuai.domain.api.huizhixin.EncryptResponse;
import com.nova.sankuai.domain.api.huizhixin.HzxResponse;
import com.nova.sankuai.domain.api.huizhixin.HzxUtil;
import com.nova.sankuai.domain.api.huizhixin.vo.*;
import com.nova.sankuai.domain.entity.*;
import com.nova.sankuai.domain.enums.CallbackSourceEnum;
import com.nova.sankuai.domain.enums.capitalInfo.CapitalApprovalResultEnum;
import com.nova.sankuai.infra.config.CommonException;
import com.nova.sankuai.infra.mapper.CallbackInfoMapper;
import com.nova.sankuai.infra.mapper.CustomerCapitalResultMapper;
import com.nova.sankuai.infra.mapper.CustomerMapper;
import com.nova.sankuai.infra.mapper.CustomerUserMapper;
import com.nova.sankuai.infra.utils.RsaSecretUtils;
import com.nova.sankuai.service.ICityService;
import com.nova.sankuai.service.ICustomerCapitalResultService;
import com.nova.sankuai.service.ICustomerUserService;
import com.nova.sankuai.service.IHzxService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 汇智信ServiceImpl
 */
@Service
public class HzxServiceImpl implements IHzxService {
 
    private final HzxUtil hzxUtil;
 
    private final RsaSecretUtils rsaSecretUtils;
 
    public final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
 
    private static final Logger log = LoggerFactory.getLogger("capitalLogger");
 
    @Resource
    ICustomerCapitalResultService customerCapitalResultService;
 
    @Resource
    ICustomerUserService customerUserService;
 
    @Resource
    CustomerMapper customerMapper;
 
    @Resource
    private CustomerUserMapper customerUserMapper;
 
    @Resource
    private CallbackInfoMapper callbackInfoMapper;
 
    @Resource
    private ICityService cityService;
 
    @Resource
    private CustomerCapitalResultMapper capitalResultMapper;
 
    /**
     * 己方私钥base64
     */
    @Value("${hzx.self.private}")
    private String privateKeyBase64;
 
    /**
     * 对方公钥base64
     */
    @Value("${hzx.other.public}")
    private String publicKeyBase64;
 
    /**
     * 产品号
     */
    @Value("${hzx.product_cid}")
    private String productCid;
 
    public HzxServiceImpl(HzxUtil hzxUtil, RsaSecretUtils rsaSecretUtils) {
        this.hzxUtil = hzxUtil;
        this.rsaSecretUtils = rsaSecretUtils;
    }
 
//    /**
//     * 前置检测接口
//     *
//     * @param vo
//     */
//    @Override
//    public HzxResponse<HzxResponseData> checkUser(CheckUserVo vo) {
//        HzxResponse<HzxResponseData> hzxResponseDataHzxResponse = this.hzxUtil.checkUser(vo.getId(), vo.getPhone(), vo.getName());
//        return hzxResponseDataHzxResponse;
//    }
//
//    @Override
//    public HzxResponse apply() {
//        String orderNo = SerialGenerator.getOrder();
//        log.info("订单号:{}",orderNo);
//        HzxResponse apply = hzxUtil.apply(orderNo, productCid);
//        return apply;
//    }
//
//    @Override
//    public HzxGetResponse getUrl() {
//        String appid= "20220518200025772106363467429863";
//        HzxGetResponse partnerUrl = hzxUtil.getPartnerUrl(appid, null);
//        return partnerUrl;
//    }
 
    /**
     * 获取订单信息
     *
     * @param vo
     * @return
     */
    @Override
    public EncryptResponse orderInfo(RequestVo vo) {
        String content = vo.getContent();
        LoadApplyExtInfoVo loadApplyExtInfoVo = null;
        String data = null;
        try {
            // 解密密文
            data = new String(Objects.requireNonNull(RsaSecretUtils.privateDecrypt(Base64Utils.decode(content), this.privateKeyBase64)));
            loadApplyExtInfoVo = JSON.parseObject(data, LoadApplyExtInfoVo.class);
            log.info("汇智信调用接口信息:入参信息:{}",loadApplyExtInfoVo.toString());
        } catch (Exception e) {
            throw new CommonException("数据解密失败");
        }
        // 验签
        boolean signPass = RsaSecretUtils.rsaSignCheck(Base64Utils.decode(content), Base64Utils.decode(vo.getSign()), publicKeyBase64);
        log.info("获取订单验签:{}",signPass);
        if (!signPass){
            throw new CommonException("验签失败");
        }
        if (StringUtils.isEmpty(loadApplyExtInfoVo.getProductCid()) || !productCid.equals(loadApplyExtInfoVo.getProductCid())) {
            log.error("汇智信调用接口错误:产品号错误:{}", loadApplyExtInfoVo.getProductCid());
            throw new CommonException("产品号有误");
        }
        CustomerCapitalResult capitalResult = customerCapitalResultService.getOne(new LambdaQueryWrapper<CustomerCapitalResult>()
                .eq(CustomerCapitalResult::getCreditNo, loadApplyExtInfoVo.getApplyId()));
        if (capitalResult == null) {
            log.error("汇智信调用接口错误:不存在该产品");
            throw new CommonException("不存在该产品");
        }
        CustomerUser customerUser = customerUserMapper.getCustomerUser(capitalResult.getCustomerUserId());
        if (customerUser == null) {
            log.error("汇智信调用接口错误:用户不存在");
            throw new CommonException("用户不存在");
        }
        Customer latestCustomer = customerMapper.getLatestCustomerRecord(customerUser.getPhone());
        if (latestCustomer == null) {
            log.error("汇智信调用接口错误:用户不存在");
            throw new CommonException("用户不存在");
        }
        OrderInfoVo orderInfo = new OrderInfoVo();
        setOrderInfo(orderInfo, customerUser, latestCustomer, capitalResult);
        setOcrInfo(orderInfo, latestCustomer);
        setImageInfo(orderInfo, latestCustomer);
        List<City> citise = cityService.list();
        Map<String, List<City>> collectByCityId = citise.stream().collect(Collectors.groupingBy(City::getCityId));
        Map<String, List<City>> collectByCityName = citise.stream().collect(Collectors.groupingBy(City::getName));
        setLoanBefore(orderInfo, latestCustomer, collectByCityId, collectByCityName);
        setLoanInfo(orderInfo, latestCustomer);
        EncryptResponse response = new EncryptResponse();
        HzxResponse hzxResponse = new HzxResponse();
        hzxResponse.setCode("0000");
        hzxResponse.setMsg("成功");
        hzxResponse.setData(orderInfo);
        try {
            // 返回值加密
            byte[] encryptByte = RsaSecretUtils.publicEncrypt(OBJECT_MAPPER.writeValueAsBytes(hzxResponse), this.publicKeyBase64);
            // 返回值加签
            String signStr = RsaSecretUtils.rsaSign(encryptByte, privateKeyBase64);
            response.setContent(Base64Utils.encode(encryptByte));
            response.setSign(signStr);
        }catch (Exception e){
            throw new CommonException("数据加密失败");
        }
        log.info("返回信息为:{}",orderInfo.toString());
        customerCapitalResultService.update(Wrappers.<CustomerCapitalResult>lambdaUpdate()
                .set(CustomerCapitalResult::getApprovalResult, CapitalApprovalResultEnum.APPROVAL_SUCCESS.getCode())
                .eq(CustomerCapitalResult::getCreditNo,loadApplyExtInfoVo.getApplyId()));
        return response;
 
    }
 
    /**
     * 回调
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CallbackVo callBack(RequestVo vo) {
        String data;
        Map paramMap;
 
        try {
            // 解密密文
            data = new String(Objects.requireNonNull(RsaSecretUtils.privateDecrypt(Base64Utils.decode(vo.getContent()), this.privateKeyBase64)));
            paramMap = JSON.parseObject(data, Map.class);
            System.out.println("data: "+data);
            System.out.println("map: "+paramMap);
            log.info("汇智信调用回调信息:入参信息:{}",paramMap);
        } catch (Exception e) {
            throw new CommonException("数据解密失败");
        }
        // 验签
        boolean signPass = RsaSecretUtils.rsaSignCheck(Base64Utils.decode(vo.getContent()), Base64Utils.decode(vo.getSign()), publicKeyBase64);
        log.info("获取订单验签:{}",signPass);
        if (!signPass){
            throw new CommonException("验签失败");
        }
        log.info("获取订单验签:{}",signPass);
        CustomerCapitalResult capitalResult = capitalResultMapper.selectOne(new QueryWrapper<CustomerCapitalResult>().eq("credit_no", paramMap.get("applyId")));
        if (Objects.isNull(capitalResult)){
            log.error("汇智信调用回调接口错误:订单不存在");
            throw new CommonException("订单不存在");
        }
        CallbackInfo callbackInfo = new CallbackInfo();
        callbackInfo.setCreditNo(capitalResult.getCreditNo());
        callbackInfo.setSource(CallbackSourceEnum.HZX.getCode());
        callbackInfoMapper.insert(callbackInfo);
        CallbackVo callbackVo = new CallbackVo();
        callbackVo.setCode(200);
        callbackVo.setMsg("成功");
        return callbackVo;
    }
 
    public void setOrderInfo(OrderInfoVo orderInfo, CustomerUser customerUser, Customer latestCustomer, CustomerCapitalResult capitalResult) {
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setUserSid(capitalResult.getCustomerUserId());
        userInfoVo.setApplyId(capitalResult.getCreditNo());
        userInfoVo.setPhone(customerUser.getPhone());
        userInfoVo.setIp(latestCustomer.getClientIp());
        if (StringUtils.isEmpty(latestCustomer.getClientIp())){
            userInfoVo.setIp("127.0.0.1");
        }
        userInfoVo.setRegTime(customerUser.getCreationDate());
        userInfoVo.setName(latestCustomer.getName());
        userInfoVo.setID(latestCustomer.getIdCard());
        orderInfo.setUserInfo(userInfoVo);
    }
 
    public void setOcrInfo(OrderInfoVo orderInfo, Customer latestCustomer) {
        OcrInfoVo ocrInfoVo = new OcrInfoVo();
        ocrInfoVo.setRace(latestCustomer.getNation());
        ocrInfoVo.setName(latestCustomer.getName());
        ocrInfoVo.setGender(latestCustomer.getSex());
        ocrInfoVo.setIdCardNumber(latestCustomer.getIdCard());
        ocrInfoVo.setAddress(latestCustomer.getAddress());
        if (!StringUtils.isEmpty(latestCustomer.getIdValidDateBegin())) {
            StringBuffer dateBegin = new StringBuffer(latestCustomer.getIdValidDateBegin());
            dateBegin.insert(6, ".");
            dateBegin.insert(4, ".");
            latestCustomer.setIdValidDateBegin(dateBegin.toString());
        }
        if (!StringUtils.isEmpty(latestCustomer.getIdValidDateEnd()) && !latestCustomer.getIdValidDateEnd().equals("长期")) {
            StringBuffer dateEnd = new StringBuffer(latestCustomer.getIdValidDateEnd());
            dateEnd.insert(6, ".");
            dateEnd.insert(4, ".");
            latestCustomer.setIdValidDateEnd(dateEnd.toString());
        }else{
            latestCustomer.setIdValidDateEnd("长期");
        }
        if ("1".equals(latestCustomer.getIdLongTerm())) {
            ocrInfoVo.setValidDate(latestCustomer.getIdValidDateBegin() + "-长期");
        } else {
            ocrInfoVo.setValidDate(latestCustomer.getIdValidDateBegin() + "-" + latestCustomer.getIdValidDateEnd());
        }
        ocrInfoVo.setIssuedBy(latestCustomer.getSignOrganization());
        HashMap<String, String> birthday = new HashMap<>();
        String year = "";
        String month = "";
        String day = "";
        if (!StringUtils.isEmpty(latestCustomer.getBirthday())) {
            year = latestCustomer.getBirthday().substring(0, 4);
            month = latestCustomer.getBirthday().substring(4, 6);
            day = latestCustomer.getBirthday().substring(6);
        }
        birthday.put("year", year);
        birthday.put("month", month);
        birthday.put("day", day);
        ocrInfoVo.setBirthday(birthday);
        orderInfo.setOcrInfo(ocrInfoVo);
    }
 
    public void setImageInfo(OrderInfoVo orderInfo, Customer latestCustomer) {
        ImageVo imageVo = new ImageVo();
        imageVo.setFront(latestCustomer.getIdCardFrontPath());
        imageVo.setBack(latestCustomer.getIdCardBackPath());
        imageVo.setImageBest(latestCustomer.getFacePhotoPath());
        orderInfo.setImage(imageVo);
    }
 
    public void setLoanBefore(OrderInfoVo orderInfo, Customer latestCustomer, Map<String, List<City>> collectByCityId, Map<String, List<City>> collectByCityName) {
        LoanBeforeVo loanBeforeVo = new LoanBeforeVo();
        hzxUtil.educationParse(loanBeforeVo,latestCustomer);
        hzxUtil.maritalStatusParse(loanBeforeVo,latestCustomer);
        loanBeforeVo.setProvinceName(latestCustomer.getLiveProvince());
        if (collectByCityId.get(latestCustomer.getCity()) != null) {
            loanBeforeVo.setProvinceCode(collectByCityId.get(latestCustomer.getCity()).get(0).getProvinceId());
            loanBeforeVo.setProvinceCode(collectByCityId.get(latestCustomer.getCity()).get(0).getCityId());
        }
        loanBeforeVo.setCityName(latestCustomer.getCityName());
        loanBeforeVo.setDistrictName(latestCustomer.getLiveArea());
        loanBeforeVo.setDetailAddress(latestCustomer.getAddress());
        loanBeforeVo.setCompany(latestCustomer.getCompanyName());
        loanBeforeVo.setCompanyProvinceName(latestCustomer.getCompanyProvince());
        loanBeforeVo.setCompanyCityName(latestCustomer.getCompanyCity());
        if (collectByCityName.get(latestCustomer.getCompanyCity()) != null) {
            loanBeforeVo.setCompanyProvinceCode(collectByCityName.get(latestCustomer.getCompanyCity()).get(0).getProvinceId());
            loanBeforeVo.setCompanyCityCode(collectByCityName.get(latestCustomer.getCompanyCity()).get(0).getCityId());
        }
        loanBeforeVo.setCompanyDistrictName(latestCustomer.getCompanyArea());
        loanBeforeVo.setCompanyDetailAddress(latestCustomer.getCompanyAddress());
        hzxUtil.industryParse(loanBeforeVo,latestCustomer);
        hzxUtil.positionParse(loanBeforeVo,latestCustomer);
        hzxUtil.monthlySalaryParse(loanBeforeVo,latestCustomer);
        hzxUtil.loanPurposeParse(loanBeforeVo,latestCustomer);
        loanBeforeVo.setEmail(latestCustomer.getEmail());
        loanBeforeVo.setFirstContactName(latestCustomer.getRelationshipName());
        loanBeforeVo.setFirstContactPhone(latestCustomer.getRelationshipPhone());
        //第一联系人关系
        hzxUtil.relationshipParse(loanBeforeVo,latestCustomer);
        loanBeforeVo.setSecondContactName(latestCustomer.getContactName());
        loanBeforeVo.setSecondContactPhone(latestCustomer.getContactPhone());
        //第二联系人关系
        hzxUtil.secondContactParse(loanBeforeVo,latestCustomer);
        orderInfo.setLoanBefore(loanBeforeVo);
    }
 
    public void setLoanInfo(OrderInfoVo orderInfo, Customer latestCustomer) {
        LoanVo loanVo = new LoanVo();
        hzxUtil.houseSituationParse(loanVo,latestCustomer);
        hzxUtil.loanOccupationParse(loanVo,latestCustomer);
        //职称
        loanVo.setPositionTitle(null);
        //职务
        hzxUtil.positionDutyParse(loanVo,latestCustomer);
        //单位性质
        hzxUtil.companyTypeParse(loanVo,latestCustomer);
        //还款来源
        loanVo.setRepaySource(null);
        //贷款用途
        hzxUtil.pullLoanPurposeParse(loanVo,latestCustomer);
        //负债总额
        loanVo.setDebtAmount("0");
        //负债情况
        loanVo.setDebtSituation(null);
        orderInfo.setLoan(loanVo);
    }
 
 
}