CasllBackDataProcessor.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. package com.tiangua.star.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.tiangua.star.enmus.CommercialCallBackReadyEnum;
  6. import com.tiangua.star.model.CustomerFollowup;
  7. import com.tiangua.star.model.ThirdStarDto;
  8. import com.tiangua.star.model.XdOrderDto;
  9. import com.tiangua.star.service.CallBackService;
  10. import com.tiangua.star.util.HttpClientThreeUtil;
  11. import com.tiangua.star.util.SignUtil;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.codec.digest.DigestUtils;
  14. import org.apache.commons.collections4.CollectionUtils;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.apache.http.impl.client.CloseableHttpClient;
  17. import org.apache.http.impl.client.HttpClients;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.stereotype.Service;
  20. import java.math.BigDecimal;
  21. import java.security.MessageDigest;
  22. import java.security.NoSuchAlgorithmException;
  23. import java.util.ArrayList;
  24. import java.util.Date;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.TreeMap;
  29. import java.util.function.Function;
  30. import java.util.stream.Collectors;
  31. @Service
  32. @Slf4j
  33. public class CasllBackDataProcessor implements CallBackService {
  34. // HTTP请求工具
  35. private static final CloseableHttpClient httpClient = HttpClients.createDefault();
  36. @Value("${md5.callback.url}")
  37. private String url;
  38. /**
  39. * 处理机构数据入口
  40. */
  41. @Override
  42. public void processInstitutionData(List<XdOrderDto> xdOrderDtos) {
  43. Map<String, List<String>> groupedByBusiId = xdOrderDtos.stream()
  44. .collect(Collectors.groupingBy(
  45. XdOrderDto::getProductBizId,
  46. Collectors.mapping(XdOrderDto::getMobileMd5, Collectors.toList())
  47. ));
  48. Map<String, List<String>> callBackMap = groupedByBusiId.entrySet().stream()
  49. .filter(entry -> CommercialCallBackReadyEnum.of(entry.getKey()) != null)
  50. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  51. log.info("已经对接md5星级回传的机构id:{}", callBackMap.keySet());
  52. if (CollectionUtils.isEmpty(callBackMap.keySet())) {
  53. log.info("MD5 无需要处理的星级回传:{}", callBackMap.keySet());
  54. return;
  55. }
  56. for (String productBizId : callBackMap.keySet()) {
  57. CommercialCallBackReadyEnum commercialCallBackReadyEnum = CommercialCallBackReadyEnum.of(productBizId);
  58. if (commercialCallBackReadyEnum != null) {
  59. switch (commercialCallBackReadyEnum) {
  60. case Commercial_ONE:
  61. List<String> md5List = callBackMap.get(productBizId);
  62. log.info("开始处理机构id:{},当日订单数:{},条数:{}", productBizId,md5List,md5List.size());
  63. if (CollectionUtils.isEmpty(md5List)) {
  64. return;
  65. }
  66. sendGetRequest(CommercialCallBackReadyEnum.Commercial_ONE.getCode(),
  67. CommercialCallBackReadyEnum.Commercial_ONE.getBusid(),commercialCallBackReadyEnum.getUrl(),md5List);
  68. break;
  69. case Commercial_HZ:
  70. List<String> md5ListHz = callBackMap.get(productBizId);
  71. log.info("开始处理机构id:{},杭州聚壹融商务信息咨询有限公司 当日订单数:{},条数:{}", productBizId,md5ListHz,md5ListHz.size());
  72. if (CollectionUtils.isEmpty(md5ListHz)) {
  73. return;
  74. }
  75. sendGetRequestHz(commercialCallBackReadyEnum.getCode(),
  76. commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl(),"杭州聚壹融商务信息咨询有限公司");
  77. break;
  78. case Commercial_NBHX:
  79. List<String> md5ListNBHX = callBackMap.get(productBizId);
  80. log.info("开始处理机构id:{},宁波垣信信息科技有限公司 当日订单数:{},条数:{}", productBizId,md5ListNBHX,md5ListNBHX.size());
  81. if (CollectionUtils.isEmpty(md5ListNBHX)) {
  82. return;
  83. }
  84. sendGetRequestHz(commercialCallBackReadyEnum.getCode(),
  85. commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl(),"宁波垣信信息科技有限公司");
  86. break;
  87. case Commercial_BJQN:
  88. List<String> md5ListBJQN = callBackMap.get(productBizId);
  89. log.info("开始处理机构id:{},北京签牛企业管理咨询有限公司 当日订单数:{},条数:{}", productBizId,md5ListBJQN,md5ListBJQN.size());
  90. if (CollectionUtils.isEmpty(md5ListBJQN)) {
  91. return;
  92. }
  93. sendGetRequestHz(commercialCallBackReadyEnum.getCode(),
  94. commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl(),"北京签牛企业管理咨询有限公司");
  95. break;
  96. case Commercial_FOSHAN:
  97. List<String> md5ListFS = callBackMap.get(productBizId);
  98. log.info("开始处理机构id:{},佛山市创智盈通企业管理有限公司 当日订单数:{},条数:{}", productBizId,md5ListFS,md5ListFS.size());
  99. if (CollectionUtils.isEmpty(md5ListFS)) {
  100. return;
  101. }
  102. sendGetRequestFoShan(commercialCallBackReadyEnum.getCode(),
  103. commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl(), md5ListFS);
  104. break;
  105. case Commercial_SXRKX:
  106. List<String> md5ListSXRKX = callBackMap.get(productBizId);
  107. log.info("开始处理机构id:{},山西瑞凯鑫 当日订单数:{},条数:{}", productBizId,md5ListSXRKX,md5ListSXRKX.size());
  108. if (CollectionUtils.isEmpty(md5ListSXRKX)) {
  109. return;
  110. }
  111. sendGetRequestSXRKX(commercialCallBackReadyEnum.getCode(),
  112. commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl());
  113. break;
  114. // case Commercial_XJZZ:
  115. // List<String> md5ListXJZZ = callBackMap.get(busiId);
  116. // log.info("开始处理机构id:{},当日订单数:{},条数:{}", busiId,md5ListXJZZ,md5ListXJZZ.size());
  117. // if (CollectionUtils.isEmpty(md5ListXJZZ)) {
  118. // return;
  119. // }
  120. // sendGetRequestSXRKX(commercialCallBackReadyEnum.getCode(),
  121. // commercialCallBackReadyEnum.getBusid(),commercialCallBackReadyEnum.getUrl());
  122. // break;
  123. case Commercial_CHONGQINGZC:
  124. List<String> md5ListCq = callBackMap.get(productBizId);
  125. log.info("开始处理机构id:{},重庆众诚 当日订单数:{},条数:{}", productBizId,md5ListCq,md5ListCq.size());
  126. if (CollectionUtils.isEmpty(md5ListCq)) {
  127. return;
  128. }
  129. String url = commercialCallBackReadyEnum.getUrl();
  130. String[] split = url.split(";");
  131. for (String urlTemp : split) {
  132. sendGetRequestHz(commercialCallBackReadyEnum.getCode(),
  133. commercialCallBackReadyEnum.getBusid(),urlTemp,"重庆众诚");
  134. }
  135. break;
  136. // case Commercial_Test:
  137. // log.info("开始处理机构id:{}", busiId);
  138. // List<String> md5List2 = callBackMap.get(busiId);
  139. // if (CollectionUtils.isEmpty(md5List2)) {
  140. // return;
  141. // }
  142. // sendGetRequest(CommercialCallBackReadyEnum.Commercial_Test.getCode(),
  143. // CommercialCallBackReadyEnum.Commercial_Test.getBusid(),commercialCallBackReadyEnum.getUrl(),md5List2);
  144. // break;
  145. }
  146. }
  147. }
  148. }
  149. @Override
  150. public void thirdProcessInstitutionData(List<ThirdStarDto> thirdStarDtos) {
  151. if (CollectionUtils.isEmpty(thirdStarDtos)) {
  152. log.info("thirdStarDtos 解析回传数据为null");
  153. return;
  154. }
  155. Map<String, List<String>> groupedByBusiId = thirdStarDtos.stream()
  156. .collect(Collectors.groupingBy(
  157. ThirdStarDto::getProductBizId,
  158. Collectors.mapping(ThirdStarDto::getPhoneMd5, Collectors.toList())
  159. ));
  160. Map<String, List<String>> callBackMap = groupedByBusiId.entrySet().stream()
  161. .filter(entry -> CommercialCallBackReadyEnum.of(entry.getKey()) != null)
  162. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  163. log.info("已经对接third星级回传的机构id:{}", callBackMap.keySet());
  164. if (CollectionUtils.isEmpty(callBackMap.keySet())) {
  165. log.info("third 无需要处理的星级回传:{}", callBackMap.keySet());
  166. return;
  167. }
  168. for (String productBizId : callBackMap.keySet()) {
  169. CommercialCallBackReadyEnum commercialCallBackReadyEnum = CommercialCallBackReadyEnum.of(productBizId);
  170. if (commercialCallBackReadyEnum != null) {
  171. switch (commercialCallBackReadyEnum) {
  172. case Commercial_DAQN:
  173. if (CollectionUtils.isEmpty(thirdStarDtos)) {
  174. return;
  175. }
  176. sendDalian(commercialCallBackReadyEnum.getCode(),
  177. commercialCallBackReadyEnum.getBusid(),
  178. commercialCallBackReadyEnum.getUrl(),thirdStarDtos);
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. private void sendGetRequestSXRKX(String productBizId,String busiId,String url){
  185. JSONObject param = SignUtil.generateSign(81, 43, "730969D3F673BA42432C574378B9A8F1");
  186. log.info("busiId:{},:山西瑞凯鑫 url:{} ,jsonString:{}" ,busiId, url,param);
  187. String responseStr = HttpClientThreeUtil.post(url, JSON.toJSONString(param));
  188. JSONObject jsonObject = JSON.parseObject(responseStr);
  189. List<CustomerFollowup> followupList = new ArrayList<>();
  190. if (jsonObject.getIntValue("code") == 0) {
  191. JSONObject data = jsonObject.getJSONObject("data");
  192. JSONArray list = data.getJSONArray("list");
  193. if (CollectionUtils.isEmpty(list)) {
  194. log.info("busiId:{} 山西瑞凯鑫 list为空 没有星级数据", busiId);
  195. return;
  196. }
  197. for (int i = 0; i < list.size(); i++) {
  198. JSONObject item = list.getJSONObject(i);
  199. CustomerFollowup customerFollowup = new CustomerFollowup();
  200. customerFollowup.setBusiId(busiId);
  201. customerFollowup.setProductBizId(Integer.parseInt(productBizId));
  202. customerFollowup.setCustomerName(item.getString("name"));
  203. customerFollowup.setStarLevel(new BigDecimal(item.getIntValue("stars")));
  204. customerFollowup.setFollowStatus(item.getString("statusStr"));
  205. customerFollowup.setFollowRemark(item.getString("remark"));
  206. String mobile = item.getString("mobile");
  207. if (StringUtils.isEmpty(mobile)) {
  208. log.error("busiId:{} 山西瑞凯鑫 手机号为空",busiId);
  209. continue;
  210. }
  211. customerFollowup.setPhoneMd5(DigestUtils.md5Hex(mobile));
  212. customerFollowup.setFollowTime(new Date());
  213. followupList.add(customerFollowup);
  214. }
  215. }
  216. if (CollectionUtils.isEmpty(followupList)) {
  217. log.info("busiId:{} 山西瑞凯鑫 无星级数据", busiId);
  218. return;
  219. }
  220. log.info("busiId:{} 山西瑞凯鑫 处理数据条:{}", busiId, followupList.size());
  221. handleMappedData(busiId,followupList);
  222. }
  223. private void sendGetRequestFoShan(String productBizId,String busiId,String url,List<String> md5ListFS){
  224. List<Map<String, String>> jsonList = new ArrayList<>();
  225. for (String md5 : md5ListFS) {
  226. Map<String, String> map = new HashMap<>();
  227. map.put("source", "hr");
  228. map.put("mobile_md5", md5);
  229. jsonList.add(map);
  230. }
  231. String jsonString = JSON.toJSONString(jsonList);
  232. log.info("busiId:{},:佛山市创智盈通企业管理有限公司 jsonString:{}" ,busiId, jsonString);
  233. String responseStr = HttpClientThreeUtil.post(url, jsonString);
  234. JSONObject response = JSON.parseObject(responseStr);
  235. String code = response.getString("code");
  236. if (!"200".equals(code)) {
  237. log.info("佛山市创智盈通企业管理有限公司 返回数据错误: " + response.getString("msg"));
  238. return ;
  239. }
  240. JSONArray dataArray = response.getJSONArray("data");
  241. List<CustomerFollowup> followupList = new ArrayList<>();
  242. if (CollectionUtils.isEmpty(dataArray)) {
  243. log.info("busiId:{},:佛山市创智盈通企业管理有限公司 今天没有星级数据",busiId);
  244. return;
  245. }
  246. for (int i = 0; i < dataArray.size(); i++) {
  247. JSONObject obj = dataArray.getJSONObject(i);
  248. CustomerFollowup followup = new CustomerFollowup();
  249. followup.setPhoneMd5(obj.getString("mobile_md5"));
  250. followup.setBusiId(busiId);
  251. followup.setProductBizId(Integer.parseInt(productBizId));
  252. followup.setFollowTime(new Date());
  253. followup.setSourceType(3);
  254. String star = obj.getString("industry");
  255. if (StringUtils.isEmpty(star)) {
  256. log.info("busiId:{} 佛山创智盈通企业管理有限公司 mobile_md5{} 没有填星级:", busiId, followup.getPhoneMd5());
  257. continue;
  258. } else {
  259. followup.setStarLevel(new BigDecimal(star));
  260. }
  261. followupList.add(followup);
  262. }
  263. if (CollectionUtils.isEmpty(followupList)) {
  264. log.info("busiId:{} 佛山创智盈通企业管理有限公司 无星级数据", busiId);
  265. return;
  266. }
  267. log.info("busiId:{} 佛山创智盈通企业管理有限公司 处理数据条:{}", busiId, followupList.size());
  268. handleMappedData(busiId,followupList);
  269. }
  270. //https://api.zhudaicms.com/ 通用
  271. private void sendGetRequestHz(String productBizId,String busiId,String url,String companyName){
  272. String responseStr = HttpClientThreeUtil.get(url,null);
  273. JSONObject response = JSON.parseObject(responseStr);
  274. String code = response.getString("code");
  275. if (!"101".equals(code)) {
  276. log.info("busiId:{},companyName {},24h返回数据错误: {},{}" ,busiId,companyName,
  277. response.getString("msg"),response.getString("code"));
  278. return;
  279. }
  280. JSONArray dataArray = response.getJSONArray("data");
  281. List<CustomerFollowup> followupList = new ArrayList<>();
  282. // 遍历 data 数组,创建实体对象并设置对应字段
  283. for (int i = 0; i < dataArray.size(); i++) {
  284. JSONObject obj = dataArray.getJSONObject(i);
  285. CustomerFollowup followup = new CustomerFollowup();
  286. followup.setBusiId(busiId);
  287. followup.setProductBizId(Integer.parseInt(productBizId));
  288. followup.setPhoneMd5(obj.getString("md5mobile"));
  289. followup.setStarLevel(new BigDecimal(obj.getString("stars")));
  290. followup.setSourceType(3);
  291. followup.setFollowTime(new Date());
  292. followupList.add(followup);
  293. }
  294. if (CollectionUtils.isEmpty(followupList)) {
  295. log.info("busiId:{} companyName:{},没有24h星级数据", busiId,companyName);
  296. return;
  297. }
  298. log.info("busiId:{} companyName:{} 处理数据条:{}", busiId, companyName,followupList.size());
  299. handleMappedData(busiId,followupList);
  300. }
  301. private void sendDalian(String productBizId,String busiId,String url,List<ThirdStarDto> thirdStarDtos){
  302. if (CollectionUtils.isEmpty(thirdStarDtos)) {
  303. return;
  304. }
  305. List<CustomerFollowup> list = new ArrayList<>();
  306. Map<String, String> map = new HashMap();
  307. JSONObject response = null;
  308. String joined = thirdStarDtos.stream()
  309. .map(ThirdStarDto::getUniqueIdentify)
  310. .collect(Collectors.joining(","));
  311. map.put("app_id", "45");
  312. map.put("timestamp", String.valueOf(System.currentTimeMillis()/1000));
  313. map.put("unique_id", joined);
  314. String sign = makeSign(map, "THITVXdSPQRQzArVwtssyD4uMdQIUxEY");
  315. map.put("sign", sign);
  316. log.info("【大连青宁】请求前参数:{}",map);
  317. String responseStr = HttpClientThreeUtil.post(url, JSON.toJSONString(map));
  318. response = JSON.parseObject(responseStr);
  319. log.info("【大连青宁】响应数据:{}",response);
  320. if (response.getInteger("code") == 200) {
  321. JSONArray data = response.getJSONArray("data");
  322. Map<String, ThirdStarDto> dtoMap = thirdStarDtos.stream()
  323. .collect(Collectors.toMap(ThirdStarDto::getUniqueIdentify, Function.identity(), (a, b) -> a));
  324. for (int i = 0; i < data.size(); i++) {
  325. JSONObject obj = data.getJSONObject(i);
  326. String uniqueId = obj.getString("unique_id");
  327. Integer star = obj.getInteger("star");
  328. ThirdStarDto dto = dtoMap.get(uniqueId);
  329. if (dto != null) {
  330. CustomerFollowup followup = new CustomerFollowup();
  331. followup.setStarLevel(new BigDecimal(star));
  332. followup.setPhoneMd5(dto.getPhoneMd5());
  333. followup.setBusiId(busiId);
  334. followup.setProductBizId(Integer.parseInt(productBizId));
  335. followup.setFollowTime(new Date());
  336. followup.setSourceType(3);
  337. list.add(followup);
  338. } else {
  339. log.warn("未匹配到 unique_id={} 对应的 ThirdStarDto", uniqueId);
  340. }
  341. }
  342. }
  343. log.info("busiId:{} 处理数据条:{}", busiId, list.size());
  344. handleMappedData(busiId,list);
  345. }
  346. public static void main(String[] args) {
  347. Map<String, String> map = new HashMap();
  348. map.put("app_id", "45");
  349. map.put("timestamp", String.valueOf(System.currentTimeMillis()/1000));
  350. map.put("unique_id", "hryk1744092978");
  351. String sign = makeSign(map, "THITVXdSPQRQzArVwtssyD4uMdQIUxEY");
  352. map.put("sign", sign);
  353. System.out.println(map);
  354. }
  355. public static String makeSign(Map<String, String> args, String token) {
  356. if (args == null || token == null) {
  357. return "";
  358. }
  359. Map<String, String> sortedArgs = new TreeMap<>(args);
  360. StringBuilder signStr = new StringBuilder();
  361. for (Map.Entry<String, String> entry : sortedArgs.entrySet()) {
  362. String key = entry.getKey();
  363. String value = entry.getValue();
  364. if (value != null && !value.isEmpty()) {
  365. signStr.append(key).append(value);
  366. }
  367. }
  368. System.out.println(signStr.toString());
  369. return md5(md5(signStr.toString()) + token).toUpperCase();
  370. }
  371. private static String md5(String str) {
  372. try {
  373. MessageDigest md = MessageDigest.getInstance("MD5");
  374. md.update(str.getBytes());
  375. byte[] digest = md.digest();
  376. StringBuilder sb = new StringBuilder();
  377. for (byte b : digest) {
  378. sb.append(String.format("%02x", b & 0xff));
  379. }
  380. return sb.toString();
  381. } catch (NoSuchAlgorithmException e) {
  382. e.printStackTrace();
  383. return "";
  384. }
  385. }
  386. private void sendGetRequest(String productBizId,String busiId,String url,List<String> md5s){
  387. List<CustomerFollowup> list = new ArrayList<>();
  388. JSONObject map = new JSONObject();
  389. JSONObject response = null;
  390. for (String md5 : md5s) {
  391. map.put("customer_phone", md5);
  392. map.put("customer_source", "LCD-HX");
  393. String responseStr = HttpClientThreeUtil.post(url, map.toJSONString());
  394. response = JSON.parseObject(responseStr);
  395. if (response.getInteger("code") == 1) {
  396. JSONObject data = response.getJSONObject("data");
  397. CustomerFollowup param = new CustomerFollowup();
  398. JSONArray logList = data.getJSONArray("log_list");
  399. if (CollectionUtils.isNotEmpty(logList)) {
  400. StringBuilder result = new StringBuilder();
  401. for (int i = 0; i < logList.size(); i++) {
  402. JSONObject logEntry = logList.getJSONObject(i);
  403. String logTime = logEntry.getString("log_time");
  404. String content = logEntry.getString("content");
  405. result.append(logTime).append("-").append(content);
  406. if (i < logList.size() - 1) {
  407. result.append(";");
  408. }
  409. }
  410. param.setFollowRemark(result.toString());
  411. }
  412. param.setPhoneMd5(md5);
  413. param.setStarLevel(data.getBigDecimal("customer_starlevel"));
  414. param.setProductBizId(Integer.parseInt(productBizId));
  415. param.setBusiId(busiId);
  416. param.setSourceType(3);
  417. list.add(param);
  418. } else {
  419. log.warn("url:{} ,productBizId:{},md5:{} 获取结果失败",url,productBizId,md5);
  420. }
  421. }
  422. log.info("busiId:{} 处理数据条:{}", busiId, list.size());
  423. handleMappedData(busiId,list);
  424. }
  425. private void handleMappedData(String busiId,List<CustomerFollowup> data) {
  426. log.info("busiId:{} 【CasllBackDataProcessor】成功处理 {} 条映射数据 明细:{}",busiId, data.size(), data);
  427. String post = HttpClientThreeUtil.post(url, JSON.toJSONString(data));
  428. log.info("busiId:{}【CasllBackDataProcessor】post结果:{}",busiId, post);
  429. }
  430. }