From 779d8bafe77aef5c02e4618e7d83e39fd5f34a7b Mon Sep 17 00:00:00 2001 From: weizhongxi <2412380450@qq.com> Date: Wed, 21 Apr 2021 18:29:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=9F=E6=8B=A8=E9=BC=A0=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yuyou/openapi/openapi/api/ABClient.java | 18 ++ .../openapi/service/ABClientService.java | 5 + .../service/impl/ABClientServiceImpl.java | 21 ++ .../service/impl/ABClientServiceProxy.java | 5 + .../service/impl/ABMessageServiceImpl.java | 1 + .../openapi/openapi/task/ABDownTask.java | 220 +++++++++++++++++- src/main/resources/application.yml | 31 ++- 7 files changed, 295 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/yuyou/openapi/openapi/api/ABClient.java b/src/main/java/com/yuyou/openapi/openapi/api/ABClient.java index d321f6c..0d3c9f5 100644 --- a/src/main/java/com/yuyou/openapi/openapi/api/ABClient.java +++ b/src/main/java/com/yuyou/openapi/openapi/api/ABClient.java @@ -83,6 +83,24 @@ public class ABClient { CommonResponse.createBySuccess() : CommonResponse.createByErrorMessage("调用失败请重试"); } + /** + * 获取上游公司的AB数据推送接口 - 推送到土拨鼠 + * + * @return 返回调用信息 + */ + @PostMapping("/api/req/taginfo/tuboshu") + @ResponseBody + public CommonResponse getDBTagInfoTuboshu(@RequestBody ABClientInterMessageVO vo) { + // 记录日志 + log.info("====== [ one request comming, request content is {} ] ======", vo.toString()); + // 转换实体类映射 + List dtos = ABMessageConverter.convertABMessageDTOFromVO(vo); + + // 调用业务处理接口 返回校验成功的结果 + return abClientService.recordAndSendABClientMsgTuboshu(dtos) ? + CommonResponse.createBySuccess() : CommonResponse.createByErrorMessage("调用失败请重试"); + } + /** * 获取上游公司的AB数据推送接口 - 推送到时机 * diff --git a/src/main/java/com/yuyou/openapi/openapi/service/ABClientService.java b/src/main/java/com/yuyou/openapi/openapi/service/ABClientService.java index 5cae7f9..cc6f5c7 100644 --- a/src/main/java/com/yuyou/openapi/openapi/service/ABClientService.java +++ b/src/main/java/com/yuyou/openapi/openapi/service/ABClientService.java @@ -24,6 +24,11 @@ public interface ABClientService { */ boolean recordAndSendABClientMsg(List dto); + /** + * 记录接口推送信息对数据进行入库,并异步进行发送给下游 + */ + boolean recordAndSendABClientMsgTuboshu(List dtos); + /** * 记录接口推送信息对数据进行入库,并异步进行发送给下游 */ diff --git a/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceImpl.java b/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceImpl.java index b501f66..dd7392b 100644 --- a/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceImpl.java +++ b/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceImpl.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; + + /** * Copyright (C), 2012 - 2018, qyx * FileName: ABClientServiceImpl @@ -42,6 +44,7 @@ public class ABClientServiceImpl implements ABClientService { @Autowired private ABMessageRepository abMessageRepository; + @Autowired private ShijiMessageRepository shijiMessageRepository; @@ -134,6 +137,24 @@ public class ABClientServiceImpl implements ABClientService { return Boolean.TRUE; } + @Override + public boolean recordAndSendABClientMsgTuboshu(List dtos) { + if (CollectionUtils.isEmpty(dtos)) { + log.error("Param dtos is empty"); + return Boolean.FALSE; + } +// 调用接口进行入库 + List abMessageDOS = ABMessageConverter.convertABMessageDOFromDTO(dtos); + // TODO: 2020/8/10 0010 加密存储 + List dos = abMessageRepository.saveAll(abMessageDOS); + if (CollectionUtils.isEmpty(dos)){ + log.error("========== [insert data error , please check .] =========="); + return Boolean.FALSE; + } + abDownTask.doRunTaskTuboShu(dtos); + return Boolean.TRUE; + } + @Override public boolean recordAndSendABClientMsgShiJi(List dtos) { if (CollectionUtils.isEmpty(dtos)) { diff --git a/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceProxy.java b/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceProxy.java index b95ca0a..bcb90eb 100644 --- a/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceProxy.java +++ b/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceProxy.java @@ -38,6 +38,11 @@ public class ABClientServiceProxy implements ABClientService { return abClientService.recordAndSendABClientMsg(list); } + @Override + public boolean recordAndSendABClientMsgTuboshu(List dtos) { + return abClientService.recordAndSendABClientMsgTuboshu(dtos); + } + /** * fixme 代理类 暂时不用 留在后续使用 diff --git a/src/main/java/com/yuyou/openapi/openapi/service/impl/ABMessageServiceImpl.java b/src/main/java/com/yuyou/openapi/openapi/service/impl/ABMessageServiceImpl.java index 89e79a1..f126337 100644 --- a/src/main/java/com/yuyou/openapi/openapi/service/impl/ABMessageServiceImpl.java +++ b/src/main/java/com/yuyou/openapi/openapi/service/impl/ABMessageServiceImpl.java @@ -24,6 +24,7 @@ public class ABMessageServiceImpl implements ABMessageService { @Autowired private ABMessageRepository abMessageRepository; + @Autowired private LieheMessageRepository lieheMessageRepository; diff --git a/src/main/java/com/yuyou/openapi/openapi/task/ABDownTask.java b/src/main/java/com/yuyou/openapi/openapi/task/ABDownTask.java index f0450a1..3897955 100644 --- a/src/main/java/com/yuyou/openapi/openapi/task/ABDownTask.java +++ b/src/main/java/com/yuyou/openapi/openapi/task/ABDownTask.java @@ -33,12 +33,15 @@ import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.HttpURLConnection; +import java.net.URL; import java.time.LocalDateTime; import java.time.ZoneOffset; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; /** @@ -173,6 +176,33 @@ public class ABDownTask { private String shuangyingSecretKey; + /** + * 配置文件中加载配置信息 - 土拨鼠 + */ + @Value("${ab.tbs.coopname}") + private String coopname; + + @Value("${ab.tbs.url}") + private String tuBoShuUrl; + + @Value("${ab.tbs.tokenurl}") + private String tuBoShuTokenUrl; + + @Value("${ab.tbs.chcode}") + private String chcode; + + @Value("${ab.tbs.password}") + private String passWord; + + @Value("${ab.tbs.coopOrderid}") + private String coopOrderid; + + @Value("${ab.tbs.channelT}") + private String channelT; + + @Value("${ab.tbs.realOrder}") + private String realOrder; + @Value("${ab.xmj.appId}") private String xmjAppid; @@ -211,6 +241,188 @@ public class ABDownTask { log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "ABDownTask", (endMilliSecond-satrtMilliSecond)); } + /** + * 任务处理入口,主要用于时间记录 + * + * @return + */ + @Async(value = "abTaskExecutor") + public void doRunTaskTuboShu(List messageDTOList){ + Long satrtMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); + log.info("====== [ task start running, task name is {} ] ======", "ABDownTask"); + runTaskTuboShu(messageDTOList); + Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); + log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "ABDownTask", (endMilliSecond-satrtMilliSecond)); + } + /** + * 执行土拨鼠任务 + * + * @return + */ + private boolean runTaskTuboShu(List messageDTOList){ + + Map formMap = new HashMap<>(); + Map formMapT = new HashMap<>(); + formMap.put("password", passWord); + formMap.put("coopname", coopname); + + int countToken = 0; + String tokenT = ""; + //获取token + while (countToken < 3) { + // 调用HTTP请求发送数据 + String result = sendPostReqTuBoShu(formMap, tuBoShuTokenUrl); + JSONObject jsonObject = JSON.parseObject(result); + tokenT = jsonObject.get("msg").toString(); + if ("0".equals(jsonObject.getString("status"))) { + log.info("========== [ABDownTask|runTaskTuboShu request success, response is {} ] ==========", jsonObject.toJSONString()); + tokenT = jsonObject.get("msg").toString(); + break; + } else { + countToken++; + try { + // 重新发送前休眠3秒 + Thread.sleep(3_0000); + } catch (InterruptedException e) { + log.error("ABDownTask|runTaskTuboShu sleep ERROR. message is", e.getMessage(), e); + } + log.error("========== [SaveToFileTask|batchSendToEncrypt request fail, response is {} ] ==========", jsonObject.toJSONString()); + } + } + if (countToken >= 3 || "".equals(tokenT)) { + log.error("========== [ABDownTask|runTaskTuboShu update send status fail, url is {} ] =========="); + return Boolean.FALSE; + } + + String finalTokenT = tokenT; + //调下游接口 + messageDTOList.forEach( + list -> { + + // 匹配城市 + if (list.getMobile().length() == 11) { + String substring = StringUtils.substring(list.getMobile(), 0, list.getMobile().length() - 4); + String matchCity = phonesRepository.findByNumber(substring); + if (StringUtils.isNotBlank(matchCity)) { + formMapT.put("city", matchCity.trim()); + } else { + // 查不到随机给一个城市 + formMapT.put("city", SpecialCityConst.SPECIAL_RANDOM_CITYS[RandomUtil.randomInt(0, 42)].trim()); + } + } + formMapT.put("coopname", coopname); + formMapT.put("auth_token", finalTokenT); + formMapT.put("cellphone", list.getMobile()); +// formMapT.put("city", "宁波"); + formMapT.put("chcode", chcode); + formMapT.put("channel", channelT); + formMapT.put("real_order", Integer.parseInt(realOrder)); + formMapT.put("coop_orderid", coopOrderid); + int count = 0; + while (count < 3) { + // 调用HTTP请求发送数据 + String result1 = sendPostReqTuBoShu(formMapT, tuBoShuUrl); + JSONObject jsonObject = JSON.parseObject(result1); + if ("0".equals(jsonObject.getString("status"))) { + log.info("========== [ABDownTask|runTaskTuboShu request success, response is {} ] ==========", jsonObject.toJSONString()); + break; + } else { + count++; + try { + // 重新发送前休眠3秒 + Thread.sleep(3_0000); + } catch (InterruptedException e) { + log.error("ABDownTask|runTaskTuboShu sleep ERROR. message is", e.getMessage(), e); + } + log.error("========== [ABDownTask|runTaskTuboShu request fail, response is {} ] ==========", jsonObject.toJSONString()); + } + } + if (count >= 3) { + log.error("========== [ABDownTask|runTaskTuboShu update send status fail, url is {} ] =========="); + }else { + abMessageService.updateSendStatus(Long.valueOf(list.getRecId()), Boolean.TRUE); + } + + + } + ); + + return Boolean.TRUE; + } + /** + * 调用HTTP请求发送Post请求 + * + * @param json 请求的body内容 + * @return 返回请求结果 + */ + private HttpResponse sendPostReq(String json,String url) { + + HttpResponse httpResponse = HttpRequest + .post(url) + .header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") + .body(json) + .execute(); + + return httpResponse; + } + // + private String sendPostReqTuBoShu(Map formMap ,String urlT){ + String result = ""; + HttpURLConnection connection = null; + String boundary = "--------------------------132183525382215881770481"; + + + + try { + URL url = new URL(urlT); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); + connection.setDoOutput(true); + connection.setDoInput(true); + connection.setRequestProperty("Connection", "Keep-Alive"); + // 不使用缓存 + connection.setUseCaches(false); + + StringBuffer formSB = new StringBuffer(); + if (formMap != null) { + if (formMap.size() > 0) { + for (Map.Entry entry : formMap.entrySet()) { + String inputName = entry.getKey(); + formSB.append("\r\n").append("--").append(boundary).append("\r\n"); + formSB.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n"); + formSB.append(entry.getValue()); + } + formSB.append("\r\n").append("--").append(boundary).append("--"); + } + } + connection.connect(); + //OutputStream out = new DataOutputStream(connection.getOutputStream()); + PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8")); + out.print(formSB.toString()); + out.flush(); + //获得响应状态 + int resultCode = connection.getResponseCode(); + if (HttpURLConnection.HTTP_OK == resultCode) { + formSB = new StringBuffer(); + String readLine; + BufferedReader responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); + while ((readLine = responseReader.readLine()) != null) { + formSB.append(readLine).append("\n"); + } + responseReader.close(); + result = formSB.toString(); + } + out.close(); + } catch (Exception e) { + + + } finally { + connection.disconnect(); + } + return result; + } + /** * 真实的任务执行入口 * diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b51e26c..53db86c 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,6 +1,6 @@ spring: profiles: - active: prod + active: dev # 序列化忽略null的k-v配置 jackson: default-property-inclusion: non_null @@ -22,13 +22,22 @@ spring: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/push?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true username: root - password: root + password: 123456 # 下游请求配置信息 ab: xmj: appId: R1JaQKdh url: http://tt.api.jikefx.cn/xmj/api/record_import secretKey: 9I7Mlbqcy8JXPKExDH3A6zZCjps0rGWf + tbs: + coopname: 326295949@qq.com + url: http://www.tobosu.com/tapi/order/pushchannelorder + chcode: grhz_cpa_qxtoo + password: 326295949 + tokenurl: http://www.tobosu.com/tapi/order/getchannelauthtoken + channelT: pinpai + coopOrderid: 261 + realOrder: 0 customer: url: http://sandbox.openapi.ppke.com.cn/openapi/tmk/valid/receive app_id: YY9X8XCZ @@ -106,6 +115,15 @@ ab: maxPoolSize: 32 queueCapacity: 100 ThreadNamePrefix: "AB-Req-" + tbs: + coopname: 326295949@qq.com + url: http://www.tobosu.com/tapi/order/pushchannelorder + chcode: grhz_cpa_qxtoo + password: 326295949 + tokenurl: http://www.tobosu.com/tapi/order/getchannelauthtoken + channelT: pinpai + coopOrderid: 261 + realOrder: 0 liehe: appID: 29cb2b84cf2bb0 url: http://qkapi.liehe.com/v1.0/create_phone_tip_yy @@ -181,6 +199,15 @@ ab: maxPoolSize: 32 queueCapacity: 100 ThreadNamePrefix: "AB-Req-" + tbs: + coopname: 326295949@qq.com + url: http://www.tobosu.com/tapi/order/pushchannelorder + chcode: grhz_cpa_qxtoo + password: 326295949 + tokenurl: http://www.tobosu.com/tapi/order/getchannelauthtoken + channelT: pinpai + coopOrderid: 261 + realOrder: 0 liehe: appID: 29cb2b84cf2bb0 url: http://qkapi.liehe.com/v1.0/create_phone_tip_yy