package com.tiangua.star.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.tiangua.star.enmus.CommercialCallBackReadyEnum; import com.tiangua.star.model.CustomerFollowup; import com.tiangua.star.model.ThirdStarDto; import com.tiangua.star.model.XdOrderDto; import com.tiangua.star.service.CallBackService; import com.tiangua.star.util.HttpClientThreeUtil; import com.tiangua.star.util.SignUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.function.Function; import java.util.stream.Collectors; @Service @Slf4j public class CasllBackDataProcessor implements CallBackService { // HTTP请求工具 private static final CloseableHttpClient httpClient = HttpClients.createDefault(); @Value("${md5.callback.url}") private String url; /** * 处理机构数据入口 */ @Override public void processInstitutionData(List xdOrderDtos) { Map> groupedByBusiId = xdOrderDtos.stream() .collect(Collectors.groupingBy( XdOrderDto::getProductBizId, Collectors.mapping(XdOrderDto::getMobileMd5, Collectors.toList()) )); Map> callBackMap = groupedByBusiId.entrySet().stream() .filter(entry -> CommercialCallBackReadyEnum.of(entry.getKey()) != null) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); log.info("已经对接md5星级回传的机构id:{}", callBackMap.keySet()); if (CollectionUtils.isEmpty(callBackMap.keySet())) { log.info("MD5 无需要处理的星级回传:{}", callBackMap.keySet()); return; } for (String productBizId : callBackMap.keySet()) { CommercialCallBackReadyEnum commercialCallBackReadyEnum = CommercialCallBackReadyEnum.of(productBizId); if (commercialCallBackReadyEnum != null) { switch (commercialCallBackReadyEnum) { case Commercial_ONE: List md5List = callBackMap.get(productBizId); log.info("开始处理机构id:{},当日订单数:{},条数:{}", productBizId,md5List,md5List.size()); if (CollectionUtils.isEmpty(md5List)) { return; } sendGetRequest(CommercialCallBackReadyEnum.Commercial_ONE.getCode(), CommercialCallBackReadyEnum.Commercial_ONE.getBusid(),commercialCallBackReadyEnum.getUrl(),md5List); break; case Commercial_HZ: List md5ListHz = callBackMap.get(productBizId); log.info("开始处理机构id:{},杭州聚壹融商务信息咨询有限公司 当日订单数:{},条数:{}", productBizId,md5ListHz,md5ListHz.size()); if (CollectionUtils.isEmpty(md5ListHz)) { return; } sendGetRequestHz(commercialCallBackReadyEnum.getCode(), commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl(),"杭州聚壹融商务信息咨询有限公司"); break; case Commercial_NBHX: List md5ListNBHX = callBackMap.get(productBizId); log.info("开始处理机构id:{},宁波垣信信息科技有限公司 当日订单数:{},条数:{}", productBizId,md5ListNBHX,md5ListNBHX.size()); if (CollectionUtils.isEmpty(md5ListNBHX)) { return; } sendGetRequestHz(commercialCallBackReadyEnum.getCode(), commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl(),"宁波垣信信息科技有限公司"); break; case Commercial_BJQN: List md5ListBJQN = callBackMap.get(productBizId); log.info("开始处理机构id:{},北京签牛企业管理咨询有限公司 当日订单数:{},条数:{}", productBizId,md5ListBJQN,md5ListBJQN.size()); if (CollectionUtils.isEmpty(md5ListBJQN)) { return; } sendGetRequestHz(commercialCallBackReadyEnum.getCode(), commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl(),"北京签牛企业管理咨询有限公司"); break; case Commercial_FOSHAN: List md5ListFS = callBackMap.get(productBizId); log.info("开始处理机构id:{},佛山市创智盈通企业管理有限公司 当日订单数:{},条数:{}", productBizId,md5ListFS,md5ListFS.size()); if (CollectionUtils.isEmpty(md5ListFS)) { return; } sendGetRequestFoShan(commercialCallBackReadyEnum.getCode(), commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl(), md5ListFS); break; case Commercial_SXRKX: List md5ListSXRKX = callBackMap.get(productBizId); log.info("开始处理机构id:{},山西瑞凯鑫 当日订单数:{},条数:{}", productBizId,md5ListSXRKX,md5ListSXRKX.size()); if (CollectionUtils.isEmpty(md5ListSXRKX)) { return; } sendGetRequestSXRKX(commercialCallBackReadyEnum.getCode(), commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl()); break; // case Commercial_XJZZ: // List md5ListXJZZ = callBackMap.get(busiId); // log.info("开始处理机构id:{},当日订单数:{},条数:{}", busiId,md5ListXJZZ,md5ListXJZZ.size()); // if (CollectionUtils.isEmpty(md5ListXJZZ)) { // return; // } // sendGetRequestSXRKX(commercialCallBackReadyEnum.getCode(), // commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl()); // break; case Commercial_CHONGQINGZC: List md5ListCq = callBackMap.get(productBizId); log.info("开始处理机构id:{},重庆众诚 当日订单数:{},条数:{}", productBizId,md5ListCq,md5ListCq.size()); if (CollectionUtils.isEmpty(md5ListCq)) { return; } String url = commercialCallBackReadyEnum.getUrl(); String[] split = url.split(";"); for (String urlTemp : split) { sendGetRequestHz(commercialCallBackReadyEnum.getCode(), commercialCallBackReadyEnum.getBusid(),urlTemp,"重庆众诚"); } break; // case Commercial_Test: // log.info("开始处理机构id:{}", busiId); // List md5List2 = callBackMap.get(busiId); // if (CollectionUtils.isEmpty(md5List2)) { // return; // } // sendGetRequest(CommercialCallBackReadyEnum.Commercial_Test.getCode(), // CommercialCallBackReadyEnum.Commercial_Test.getBusid(),commercialCallBackReadyEnum.getUrl(),md5List2); // break; } } } } @Override public void thirdProcessInstitutionData(List thirdStarDtos) { if (CollectionUtils.isEmpty(thirdStarDtos)) { log.info("thirdStarDtos 解析回传数据为null"); return; } Map> groupedByBusiId = thirdStarDtos.stream() .collect(Collectors.groupingBy( ThirdStarDto::getProductBizId, Collectors.mapping(ThirdStarDto::getPhoneMd5, Collectors.toList()) )); Map> callBackMap = groupedByBusiId.entrySet().stream() .filter(entry -> CommercialCallBackReadyEnum.of(entry.getKey()) != null) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); log.info("已经对接third星级回传的机构id:{}", callBackMap.keySet()); if (CollectionUtils.isEmpty(callBackMap.keySet())) { log.info("third 无需要处理的星级回传:{}", callBackMap.keySet()); return; } for (String productBizId : callBackMap.keySet()) { CommercialCallBackReadyEnum commercialCallBackReadyEnum = CommercialCallBackReadyEnum.of(productBizId); if (commercialCallBackReadyEnum != null) { switch (commercialCallBackReadyEnum) { case Commercial_DAQN: if (CollectionUtils.isEmpty(thirdStarDtos)) { return; } sendDalian(commercialCallBackReadyEnum.getCode(), commercialCallBackReadyEnum.getBusid(), commercialCallBackReadyEnum.getUrl(),thirdStarDtos); break; } } } } private void sendGetRequestSXRKX(String productBizId,String busiId,String url){ JSONObject param = SignUtil.generateSign(81, 43, "730969D3F673BA42432C574378B9A8F1"); log.info("busiId:{},:山西瑞凯鑫 url:{} ,jsonString:{}" ,busiId, url,param); String responseStr = HttpClientThreeUtil.post(url, JSON.toJSONString(param)); JSONObject jsonObject = JSON.parseObject(responseStr); List followupList = new ArrayList<>(); if (jsonObject.getIntValue("code") == 0) { JSONObject data = jsonObject.getJSONObject("data"); JSONArray list = data.getJSONArray("list"); if (CollectionUtils.isEmpty(list)) { log.info("busiId:{} 山西瑞凯鑫 list为空 没有星级数据", busiId); return; } for (int i = 0; i < list.size(); i++) { JSONObject item = list.getJSONObject(i); CustomerFollowup customerFollowup = new CustomerFollowup(); customerFollowup.setBusiId(busiId); customerFollowup.setProductBizId(Integer.parseInt(productBizId)); customerFollowup.setCustomerName(item.getString("name")); customerFollowup.setStarLevel(new BigDecimal(item.getIntValue("stars"))); customerFollowup.setFollowStatus(item.getString("statusStr")); customerFollowup.setFollowRemark(item.getString("remark")); String mobile = item.getString("mobile"); if (StringUtils.isEmpty(mobile)) { log.error("busiId:{} 山西瑞凯鑫 手机号为空",busiId); continue; } customerFollowup.setPhoneMd5(DigestUtils.md5Hex(mobile)); customerFollowup.setFollowTime(new Date()); followupList.add(customerFollowup); } } if (CollectionUtils.isEmpty(followupList)) { log.info("busiId:{} 山西瑞凯鑫 无星级数据", busiId); return; } log.info("busiId:{} 山西瑞凯鑫 处理数据条:{}", busiId, followupList.size()); handleMappedData(busiId,followupList); } private void sendGetRequestFoShan(String productBizId,String busiId,String url,List md5ListFS){ List> jsonList = new ArrayList<>(); for (String md5 : md5ListFS) { Map map = new HashMap<>(); map.put("source", "hr"); map.put("mobile_md5", md5); jsonList.add(map); } String jsonString = JSON.toJSONString(jsonList); log.info("busiId:{},:佛山市创智盈通企业管理有限公司 jsonString:{}" ,busiId, jsonString); String responseStr = HttpClientThreeUtil.post(url, jsonString); JSONObject response = JSON.parseObject(responseStr); String code = response.getString("code"); if (!"200".equals(code)) { log.info("佛山市创智盈通企业管理有限公司 返回数据错误: " + response.getString("msg")); return ; } JSONArray dataArray = response.getJSONArray("data"); List followupList = new ArrayList<>(); if (CollectionUtils.isEmpty(dataArray)) { log.info("busiId:{},:佛山市创智盈通企业管理有限公司 今天没有星级数据",busiId); return; } for (int i = 0; i < dataArray.size(); i++) { JSONObject obj = dataArray.getJSONObject(i); CustomerFollowup followup = new CustomerFollowup(); followup.setPhoneMd5(obj.getString("mobile_md5")); followup.setBusiId(busiId); followup.setProductBizId(Integer.parseInt(productBizId)); followup.setFollowTime(new Date()); followup.setSourceType(3); String star = obj.getString("industry"); if (StringUtils.isEmpty(star)) { log.info("busiId:{} 佛山创智盈通企业管理有限公司 mobile_md5{} 没有填星级:", busiId, followup.getPhoneMd5()); continue; } else { followup.setStarLevel(new BigDecimal(star)); } followupList.add(followup); } if (CollectionUtils.isEmpty(followupList)) { log.info("busiId:{} 佛山创智盈通企业管理有限公司 无星级数据", busiId); return; } log.info("busiId:{} 佛山创智盈通企业管理有限公司 处理数据条:{}", busiId, followupList.size()); handleMappedData(busiId,followupList); } //https://api.zhudaicms.com/ 通用 private void sendGetRequestHz(String productBizId,String busiId,String url,String companyName){ String responseStr = HttpClientThreeUtil.get(url,null); JSONObject response = JSON.parseObject(responseStr); String code = response.getString("code"); if (!"101".equals(code)) { log.info("busiId:{},companyName {},24h返回数据错误: {},{}" ,busiId,companyName, response.getString("msg"),response.getString("code")); return; } JSONArray dataArray = response.getJSONArray("data"); List followupList = new ArrayList<>(); // 遍历 data 数组,创建实体对象并设置对应字段 for (int i = 0; i < dataArray.size(); i++) { JSONObject obj = dataArray.getJSONObject(i); CustomerFollowup followup = new CustomerFollowup(); followup.setBusiId(busiId); followup.setProductBizId(Integer.parseInt(productBizId)); followup.setPhoneMd5(obj.getString("md5mobile")); followup.setStarLevel(new BigDecimal(obj.getString("stars"))); followup.setSourceType(3); followup.setFollowTime(new Date()); followupList.add(followup); } if (CollectionUtils.isEmpty(followupList)) { log.info("busiId:{} companyName:{},没有24h星级数据", busiId,companyName); return; } log.info("busiId:{} companyName:{} 处理数据条:{}", busiId, companyName,followupList.size()); handleMappedData(busiId,followupList); } private void sendDalian(String productBizId,String busiId,String url,List thirdStarDtos){ if (CollectionUtils.isEmpty(thirdStarDtos)) { return; } List list = new ArrayList<>(); Map map = new HashMap(); JSONObject response = null; String joined = thirdStarDtos.stream() .map(ThirdStarDto::getUniqueIdentify) .collect(Collectors.joining(",")); map.put("app_id", "45"); map.put("timestamp", String.valueOf(System.currentTimeMillis()/1000)); map.put("unique_id", joined); String sign = makeSign(map, "THITVXdSPQRQzArVwtssyD4uMdQIUxEY"); map.put("sign", sign); log.info("【大连青宁】请求前参数:{}",map); String responseStr = HttpClientThreeUtil.post(url, JSON.toJSONString(map)); response = JSON.parseObject(responseStr); log.info("【大连青宁】响应数据:{}",response); if (response.getInteger("code") == 200) { JSONArray data = response.getJSONArray("data"); Map dtoMap = thirdStarDtos.stream() .collect(Collectors.toMap(ThirdStarDto::getUniqueIdentify, Function.identity(), (a, b) -> a)); for (int i = 0; i < data.size(); i++) { JSONObject obj = data.getJSONObject(i); String uniqueId = obj.getString("unique_id"); Integer star = obj.getInteger("star"); ThirdStarDto dto = dtoMap.get(uniqueId); if (dto != null) { CustomerFollowup followup = new CustomerFollowup(); followup.setStarLevel(new BigDecimal(star)); followup.setPhoneMd5(dto.getPhoneMd5()); followup.setBusiId(busiId); followup.setProductBizId(Integer.parseInt(productBizId)); followup.setFollowTime(new Date()); followup.setSourceType(3); list.add(followup); } else { log.warn("未匹配到 unique_id={} 对应的 ThirdStarDto", uniqueId); } } } log.info("busiId:{} 处理数据条:{}", busiId, list.size()); handleMappedData(busiId,list); } public static void main(String[] args) { Map map = new HashMap(); map.put("app_id", "45"); map.put("timestamp", String.valueOf(System.currentTimeMillis()/1000)); map.put("unique_id", "hryk1744092978"); String sign = makeSign(map, "THITVXdSPQRQzArVwtssyD4uMdQIUxEY"); map.put("sign", sign); System.out.println(map); } public static String makeSign(Map args, String token) { if (args == null || token == null) { return ""; } Map sortedArgs = new TreeMap<>(args); StringBuilder signStr = new StringBuilder(); for (Map.Entry entry : sortedArgs.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (value != null && !value.isEmpty()) { signStr.append(key).append(value); } } System.out.println(signStr.toString()); return md5(md5(signStr.toString()) + token).toUpperCase(); } private static String md5(String str) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes()); byte[] digest = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b & 0xff)); } return sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return ""; } } private void sendGetRequest(String productBizId,String busiId,String url,List md5s){ List list = new ArrayList<>(); JSONObject map = new JSONObject(); JSONObject response = null; for (String md5 : md5s) { map.put("customer_phone", md5); map.put("customer_source", "LCD-HX"); String responseStr = HttpClientThreeUtil.post(url, map.toJSONString()); response = JSON.parseObject(responseStr); if (response.getInteger("code") == 1) { JSONObject data = response.getJSONObject("data"); CustomerFollowup param = new CustomerFollowup(); JSONArray logList = data.getJSONArray("log_list"); if (CollectionUtils.isNotEmpty(logList)) { StringBuilder result = new StringBuilder(); for (int i = 0; i < logList.size(); i++) { JSONObject logEntry = logList.getJSONObject(i); String logTime = logEntry.getString("log_time"); String content = logEntry.getString("content"); result.append(logTime).append("-").append(content); if (i < logList.size() - 1) { result.append(";"); } } param.setFollowRemark(result.toString()); } param.setPhoneMd5(md5); param.setStarLevel(data.getBigDecimal("customer_starlevel")); param.setProductBizId(Integer.parseInt(productBizId)); param.setBusiId(busiId); param.setSourceType(3); list.add(param); } else { log.warn("url:{} ,productBizId:{},md5:{} 获取结果失败",url,productBizId,md5); } } log.info("busiId:{} 处理数据条:{}", busiId, list.size()); handleMappedData(busiId,list); } private void handleMappedData(String busiId,List data) { log.info("busiId:{} 【CasllBackDataProcessor】成功处理 {} 条映射数据 明细:{}",busiId, data.size(), data); String post = HttpClientThreeUtil.post(url, JSON.toJSONString(data)); log.info("busiId:{}【CasllBackDataProcessor】post结果:{}",busiId, post); } }