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 checkUser(CheckUserVo vo) { // HzxResponse 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() .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 citise = cityService.list(); Map> collectByCityId = citise.stream().collect(Collectors.groupingBy(City::getCityId)); Map> 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.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().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 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> collectByCityId, Map> 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); } }