SendCollectUtils.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.ydtech.utils.sendCollectUtils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.ydtech.modules.admin.model.vo.SysAmountAuditingVo;
  4. import com.ydtech.modules.order.entity.vo.sendCollect.SendCollect;
  5. import com.ydtech.modules.sendCollect.dao.SendCollectMapper;
  6. import com.ydtech.modules.sendCollect.entity.SendCollectEntity;
  7. import com.ydtech.utils.StringUtils;
  8. import com.ydtech.utils.scheduledFutureUtil.ScheduledFutureUtil;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.apache.http.HttpEntity;
  11. import org.apache.http.client.methods.CloseableHttpResponse;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.entity.StringEntity;
  14. import org.apache.http.impl.client.CloseableHttpClient;
  15. import org.apache.http.impl.client.HttpClientBuilder;
  16. import org.apache.http.util.EntityUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.scheduling.annotation.Async;
  20. import org.springframework.stereotype.Service;
  21. import java.io.IOException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.ArrayList;
  24. import java.util.Date;
  25. import java.util.List;
  26. import java.util.UUID;
  27. @Slf4j
  28. @Service
  29. public class SendCollectUtils implements Runnable {
  30. @Autowired
  31. private SendCollectMapper sendCollectMapper;
  32. @Autowired
  33. private ScheduledFutureUtil scheduledFutureUtil;
  34. @Value("${sendMessage-zg-yggl-IP}")
  35. private String sendMessageIP;
  36. private Integer num = 0;
  37. private static final String CORN = "0 */1 * * * ?";
  38. private static final String STRING_0 = "0";
  39. private static final String STRING_1 = "1";
  40. private static final String STRING_2 = "2";
  41. private static final Integer INTEGER_0 = 0;
  42. private static final String RESPONSE_SUCCESS = "200";
  43. @Async
  44. public void send(SendCollectEntity sendCollectEntity, SendCollect sendCollect) {
  45. // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
  46. CloseableHttpClient httpClient = HttpClientBuilder.create().build();
  47. // 创建Post请求
  48. HttpPost httpPost = new HttpPost(sendMessageIP + "withdrawal/record/acceptPushWithdrawalData");
  49. // 我这里利用阿里的fastjson,将Object转换为json字符串;
  50. // (需要导入com.alibaba.fastjson.JSON包)
  51. String jsonString = JSON.toJSONString(sendCollect);
  52. StringEntity entity = new StringEntity(jsonString, "UTF-8");
  53. // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
  54. httpPost.setEntity(entity);
  55. httpPost.setHeader("Content-Type", "application/json;charset=utf8");
  56. sendCollectEntity.setHttpPost(httpPost.toString());
  57. // 响应模型
  58. CloseableHttpResponse response = null;
  59. try {
  60. // 由客户端执行(发送)Post请求
  61. response = httpClient.execute(httpPost);
  62. // 从响应模型中获取响应实体
  63. HttpEntity responseEntity = response.getEntity();
  64. //从响应实体中截取状态码
  65. String string = EntityUtils.toString(responseEntity);
  66. sendCollectEntity.setHttpResponse(string);
  67. String responseString = string.substring(string.lastIndexOf(":") + 1);
  68. String responseCode = responseString.substring(0, responseString.length() - 1);
  69. log.info("响应状态为:{}", response.getStatusLine());
  70. if (RESPONSE_SUCCESS.equals(responseCode)) {
  71. sendCollectEntity.setFailNum(null);
  72. sendCollectEntity.setSendState(STRING_1);
  73. log.info("修改send_collect_history数据库入参:{}", JSON.toJSONString(sendCollectEntity));
  74. sendCollectMapper.updateSendCollectInfo(sendCollectEntity);
  75. } else {
  76. sendCollectEntity.setSendState(STRING_0);
  77. log.info("修改send_collect_history数据库入参:{}", JSON.toJSONString(sendCollectEntity));
  78. sendCollectMapper.updateSendCollectInfo(sendCollectEntity);
  79. }
  80. } catch (Exception e) {
  81. SendCollectUtils sendCollectUtils = new SendCollectUtils();
  82. scheduledFutureUtil.startJob(sendCollectUtils, CORN);
  83. e.printStackTrace();
  84. } finally {
  85. try {
  86. // 释放资源
  87. if (httpClient != null) {
  88. httpClient.close();
  89. }
  90. if (response != null) {
  91. response.close();
  92. }
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98. public void sendCollect(SysAmountAuditingVo sysAmountAuditingVo) {
  99. StringBuilder sendUuid = new StringBuilder();
  100. sendUuid.append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))
  101. .append(UUID.randomUUID());
  102. StringBuilder userInfo = new StringBuilder();
  103. userInfo.append(sysAmountAuditingVo.getUserName())
  104. .append("-")
  105. .append(sysAmountAuditingVo.getUserId());
  106. SendCollect sendCollect = new SendCollect();
  107. sendCollect.setZgUuid(sysAmountAuditingVo.getId());
  108. sendCollect.setApplicationTime(sysAmountAuditingVo.getCreateTime());
  109. sendCollect.setExtractMoney(sysAmountAuditingVo.getAmount());
  110. sendCollect.setReceivingAccount(sysAmountAuditingVo.getBankNumber());
  111. sendCollect.setTranType(STRING_0);
  112. sendCollect.setPayee(userInfo.toString());
  113. SendCollectEntity sendCollectEntity = new SendCollectEntity();
  114. sendCollectEntity.setCollectId(sysAmountAuditingVo.getId());
  115. sendCollectEntity.setSendState(STRING_2);
  116. sendCollectEntity.setCollectTime(sysAmountAuditingVo.getCreateTime());
  117. sendCollectEntity.setAmount(sysAmountAuditingVo.getAmount());
  118. sendCollectEntity.setBankNumber(sysAmountAuditingVo.getBankNumber());
  119. sendCollectEntity.setUserInfo(userInfo.toString());
  120. sendCollectEntity.setFailNum(INTEGER_0);
  121. sendCollectEntity.setSendUuid(sendUuid.toString());
  122. log.info("新增推送send_collect_history数据库入参:{}", JSON.toJSONString(sendCollectEntity));
  123. sendCollectMapper.addSendCollectInfo(sendCollectEntity);
  124. send(sendCollectEntity,sendCollect);
  125. }
  126. public void sendCollectRetry() {
  127. log.info("推送提现历史补偿启动,启动时间:{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  128. List<SendCollectEntity> sendCollectFailInfoList = new ArrayList<SendCollectEntity>();
  129. try {
  130. sendCollectFailInfoList = sendCollectMapper.getSendCollectFailInfo();
  131. log.info("查询send_collect_history数据库出参:{}", JSON.toJSON(sendCollectFailInfoList));
  132. } catch (Exception e) {
  133. e.printStackTrace();
  134. log.error("查询send_collect_history数据库报错:{}", e.getMessage());
  135. }
  136. if (sendCollectFailInfoList != null && !sendCollectFailInfoList.isEmpty()) {
  137. for (SendCollectEntity sendCollectEntity : sendCollectFailInfoList) {
  138. if (sendCollectEntity == null && StringUtils.isNullOrEmpty(sendCollectEntity)) {
  139. continue;
  140. }
  141. if (sendCollectEntity.getFailNum() != null && sendCollectEntity.getFailNum() < 3) {
  142. Integer failNum = sendCollectEntity.getFailNum();
  143. failNum = ++failNum;
  144. sendCollectEntity.setFailNum(failNum);
  145. SendCollect sendCollect = new SendCollect();
  146. sendCollect.setZgUuid(sendCollectEntity.getCollectId());
  147. sendCollect.setApplicationTime(sendCollectEntity.getCollectTime());
  148. sendCollect.setExtractMoney(sendCollectEntity.getAmount());
  149. sendCollect.setReceivingAccount(sendCollectEntity.getBankNumber());
  150. sendCollect.setTranType(sendCollectEntity.getTranType());
  151. sendCollect.setPayee(sendCollectEntity.getUserInfo());
  152. send(sendCollectEntity, sendCollect);
  153. }
  154. }
  155. }
  156. }
  157. @Override
  158. public void run() {
  159. num++;
  160. log.info("第{}次重新推送提现记录消息", num);
  161. this.sendCollectRetry();
  162. if (num == 3) {
  163. //停止定时任务
  164. scheduledFutureUtil.endJob("com.ydtech.utils.scheduledFutureUtil");
  165. }
  166. }
  167. }