123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- 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<XdOrderDto> xdOrderDtos) {
- Map<String, List<String>> groupedByBusiId = xdOrderDtos.stream()
- .collect(Collectors.groupingBy(
- XdOrderDto::getProductBizId,
- Collectors.mapping(XdOrderDto::getMobileMd5, Collectors.toList())
- ));
- Map<String, List<String>> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<ThirdStarDto> thirdStarDtos) {
- if (CollectionUtils.isEmpty(thirdStarDtos)) {
- log.info("thirdStarDtos 解析回传数据为null");
- return;
- }
- Map<String, List<String>> groupedByBusiId = thirdStarDtos.stream()
- .collect(Collectors.groupingBy(
- ThirdStarDto::getProductBizId,
- Collectors.mapping(ThirdStarDto::getPhoneMd5, Collectors.toList())
- ));
- Map<String, List<String>> 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<CustomerFollowup> 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<String> md5ListFS){
- List<Map<String, String>> jsonList = new ArrayList<>();
- for (String md5 : md5ListFS) {
- Map<String, String> 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<CustomerFollowup> 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<CustomerFollowup> 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<ThirdStarDto> thirdStarDtos){
- if (CollectionUtils.isEmpty(thirdStarDtos)) {
- return;
- }
- List<CustomerFollowup> list = new ArrayList<>();
- Map<String, String> 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<String, ThirdStarDto> 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<String, String> 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<String, String> args, String token) {
- if (args == null || token == null) {
- return "";
- }
- Map<String, String> sortedArgs = new TreeMap<>(args);
- StringBuilder signStr = new StringBuilder();
- for (Map.Entry<String, String> 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<String> md5s){
- List<CustomerFollowup> 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<CustomerFollowup> data) {
- log.info("busiId:{} 【CasllBackDataProcessor】成功处理 {} 条映射数据 明细:{}",busiId, data.size(), data);
- String post = HttpClientThreeUtil.post(url, JSON.toJSONString(data));
- log.info("busiId:{}【CasllBackDataProcessor】post结果:{}",busiId, post);
- }
- }
|