package com.nova.sankuai.domain.api.xinyeyoupin; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpStatus; import cn.hutool.json.JSONUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.nova.sankuai.domain.entity.Customer; import com.nova.sankuai.domain.enums.ProfessionInfoEnum; import com.nova.sankuai.domain.enums.customerinfo.*; import com.nova.sankuai.infra.constants.Constants; import com.nova.sankuai.infra.utils.AesSecretUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; /** * @Author Lilinhong * @Date 2022/5/31 15:39 */ @Component public class XinYeYouPinUtil { @Value("${xyyp.channelcode}") private String channelCode; @Value("${xyyp.aes_key}") private String aesKey; @Value("${xyyp.url}") public String url; private static final Logger logger = LoggerFactory.getLogger("capitalLogger"); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); public String parseParam(Customer customer, String tradeno) { // 请求体 Map body = new HashMap<>(16); // 原始参数 Map param = new HashMap<>(16); param.put("phone", customer.getPhone()); param.put("applyname", customer.getName()); param.put("idcard", customer.getIdCard()); param.put("age", customer.getAge()); //居住城市 param.put("city", customer.getCityName()); loanParse(param, customer); professionalParse(param, customer); param.put("huabei", 1); creditParse(param, customer); //芝麻信用分 zhimaParse(param, customer); //社保 socialParse(param, customer); //公积金 fundParse(param, customer); //微粒贷 param.put("microloan", 2); insuranceParse(param, customer); //房产信息 houseParse(param, customer); //车辆信息 carParse(param, customer); //京东白条 param.put("baitiao", 1); String paramString = null; logger.info("手机号为:" + customer.getPhone() + ",信业优品原始date入参序列化前数据->:{}", param); try { paramString = OBJECT_MAPPER.writeValueAsString(param); logger.info("手机号为:" + customer.getPhone() + ",信业优品原始date入参序列化后数据->:{}", paramString); } catch (JsonProcessingException e) { logger.info("手机号为:" + customer.getPhone() + ",信业优品原始date入参序列化后数据错误"); e.printStackTrace(); return null; } String data = null; try { data = AesSecretUtils.encrypt(paramString, aesKey); logger.info("手机号为:" + customer.getPhone() + ",信业优品原始date入参加密数据->:{}", data); } catch (Exception e) { logger.info("手机号为:" + customer.getPhone() + ",信业优品原始date入参加密错误"); e.printStackTrace(); return null; } body.put("channelcode", channelCode); body.put("tradeno", tradeno); body.put("data", data); String bodyParam = null; logger.info("手机号为:" + customer.getPhone() + ",信业优品原始入参序列化前数据->:{}", body); try { bodyParam = OBJECT_MAPPER.writeValueAsString(body); logger.info("手机号为:" + customer.getPhone() + ",信业优品原始入参序列化前后数据->:{}", body); } catch (JsonProcessingException e) { logger.info("手机号为:" + customer.getPhone() + ",信业优品原始入参序列化后数据错误"); e.printStackTrace(); return null; } return bodyParam; } public XyypResponse push(Customer customer, String tradeno) { String bodyParam = parseParam(customer, tradeno); HttpResponse execute = HttpRequest.post(url) .timeout(Constants.TIME_OUT) .contentType("application/x-www-form-urlencoded") .body(bodyParam).execute(); if (execute == null) { logger.info("手机号为:" + customer.getPhone() + ",信业优品请求返回为空->:{}", execute); return null; } if (execute.getStatus() != HttpStatus.HTTP_OK) { logger.info("手机号为:" + customer.getPhone() + ",信业优品请求返回数据失败->:{}", execute.toString()); return null; } logger.info("手机号为:" + customer.getPhone() + ",信业优品返回数据->:{}", execute.body()); XyypResponse xyypResponse = null; try { xyypResponse = JSONUtil.toBean(execute.body(), XyypResponse.class); } catch (Exception e) { logger.info("手机号为:" + customer.getPhone() + ",信业优品请求返回数据转换失败->:{}", xyypResponse); e.printStackTrace(); return null; } return xyypResponse; } public void loanParse(Map param, Customer customer) { Double first = 100000.00; Double second = 200000.00; Double third = 500000.00; Double fourth = 1000000.00; if (customer.getLoanAmount() == null) { param.put("loan", 10); } else { if (customer.getLoanAmount() <= first) { param.put("loan", 10); } else if (customer.getLoanAmount() > first && customer.getLoanAmount() <= second) { param.put("loan", 20); } else if (customer.getLoanAmount() > second && customer.getLoanAmount() <= third) { param.put("loan", 50); } else if (customer.getLoanAmount() > third && customer.getLoanAmount() <= fourth) { param.put("loan", 100); } else { param.put("loan", 200); } } } public void professionalParse(Map param, Customer customer) { if (customer.getProfessionInfo() == null) { param.put("professional", 1); } else { if (ProfessionInfoEnum.BUSINESS_OWNERS.getCode().equals(customer.getProfessionInfo())) { param.put("professional", 2); } else { param.put("professional", 1); } } } public void creditParse(Map param, Customer customer) { if (customer.getCreditCard() == null) { param.put("credit", 2); } else { if (CreditCardStatusEnum.CreditCard_1.getCode().equals(customer.getCreditCard())) { param.put("credit", 2); } else { param.put("credit", 1); } } } public void zhimaParse(Map param, Customer customer) { if (customer.getSesame() == null) { param.put("zhima", 5); } else { if (SesameStatusEnum.Sesame_2.getCode().equals(customer.getSesame())) { param.put("zhima", 1); } else if (SesameStatusEnum.Sesame_3.getCode().equals(customer.getSesame())) { param.put("zhima", 2); } else if (SesameStatusEnum.Sesame_4.getCode().equals(customer.getSesame())) { param.put("zhima", 3); } else { param.put("zhima", 4); } } } public void socialParse(Map param, Customer customer) { if (customer.getSocialSecurity() == null) { param.put("social", 2); } else { if (SocialSecurityStatusEnum.SocialSecurity_1.getCode().equals(customer.getSocialSecurity())) { param.put("social", 2); } else { param.put("social", 1); } } } public void fundParse(Map param, Customer customer) { if (customer.getProvidentFund() == null) { param.put("fund", 2); } else { if (ProvidentFundStatusEnum.ProvidentFund_1.getCode().equals(customer.getProvidentFund())) { param.put("fund", 2); } else { param.put("fund", 1); } } } public void insuranceParse(Map param, Customer customer) { if (customer.getInsurancePolicy() == null) { param.put("insurance", 2); } else { if (InsuranceStatusEnum.BusinessInsurance_1.getCode().equals(customer.getInsurancePolicy())) { param.put("insurance", 2); } else { param.put("insurance", 1); } } } public void houseParse(Map param, Customer customer) { if (customer.getHouseStatus() == null) { param.put("house", 4); } else { if (HouseStatusEnum.House_1.getCode().equals(customer.getHouseStatus())) { param.put("house", 4); } else if (HouseStatusEnum.House_2.getCode().equals(customer.getHouseStatus())) { param.put("house", 2); } else if (HouseStatusEnum.House_3.getCode().equals(customer.getHouseStatus())) { param.put("house", 1); } else { param.put("house", 5); } } } public void carParse(Map param, Customer customer) { if (customer.getCarStatus() == null) { param.put("car", 3); } else { if (CarStatusEnum.Car_1.getCode().equals(customer.getCarStatus())) { param.put("car", 3); } else if (CarStatusEnum.Car_2.getCode().equals(customer.getCarStatus())) { param.put("car", 2); } else if (CarStatusEnum.Car_3.getCode().equals(customer.getCarStatus())) { param.put("car", 1); } else { param.put("car", 2); } } } }