package com.ydtech.utils.sendCollectUtils; import com.alibaba.fastjson.JSON; import com.ydtech.modules.admin.model.vo.SysAmountAuditingVo; import com.ydtech.modules.order.entity.vo.sendCollect.SendCollect; import com.ydtech.modules.sendCollect.dao.SendCollectMapper; import com.ydtech.modules.sendCollect.entity.SendCollectEntity; import com.ydtech.utils.StringUtils; import com.ydtech.utils.scheduledFutureUtil.ScheduledFutureUtil; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; @Slf4j @Service public class SendCollectUtils implements Runnable { @Autowired private SendCollectMapper sendCollectMapper; @Autowired private ScheduledFutureUtil scheduledFutureUtil; @Value("${sendMessage-zg-yggl-IP}") private String sendMessageIP; private Integer num = 0; private static final String CORN = "0 */1 * * * ?"; private static final String STRING_0 = "0"; private static final String STRING_1 = "1"; private static final String STRING_2 = "2"; private static final Integer INTEGER_0 = 0; private static final String RESPONSE_SUCCESS = "200"; @Async public void send(SendCollectEntity sendCollectEntity, SendCollect sendCollect) { // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 创建Post请求 HttpPost httpPost = new HttpPost(sendMessageIP + "withdrawal/record/acceptPushWithdrawalData"); // 我这里利用阿里的fastjson,将Object转换为json字符串; // (需要导入com.alibaba.fastjson.JSON包) String jsonString = JSON.toJSONString(sendCollect); StringEntity entity = new StringEntity(jsonString, "UTF-8"); // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中 httpPost.setEntity(entity); httpPost.setHeader("Content-Type", "application/json;charset=utf8"); sendCollectEntity.setHttpPost(httpPost.toString()); // 响应模型 CloseableHttpResponse response = null; try { // 由客户端执行(发送)Post请求 response = httpClient.execute(httpPost); // 从响应模型中获取响应实体 HttpEntity responseEntity = response.getEntity(); //从响应实体中截取状态码 String string = EntityUtils.toString(responseEntity); sendCollectEntity.setHttpResponse(string); String responseString = string.substring(string.lastIndexOf(":") + 1); String responseCode = responseString.substring(0, responseString.length() - 1); log.info("响应状态为:{}", response.getStatusLine()); if (RESPONSE_SUCCESS.equals(responseCode)) { sendCollectEntity.setFailNum(null); sendCollectEntity.setSendState(STRING_1); log.info("修改send_collect_history数据库入参:{}", JSON.toJSONString(sendCollectEntity)); sendCollectMapper.updateSendCollectInfo(sendCollectEntity); } else { sendCollectEntity.setSendState(STRING_0); log.info("修改send_collect_history数据库入参:{}", JSON.toJSONString(sendCollectEntity)); sendCollectMapper.updateSendCollectInfo(sendCollectEntity); } } catch (Exception e) { SendCollectUtils sendCollectUtils = new SendCollectUtils(); scheduledFutureUtil.startJob(sendCollectUtils, CORN); e.printStackTrace(); } finally { try { // 释放资源 if (httpClient != null) { httpClient.close(); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void sendCollect(SysAmountAuditingVo sysAmountAuditingVo) { StringBuilder sendUuid = new StringBuilder(); sendUuid.append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())) .append(UUID.randomUUID()); StringBuilder userInfo = new StringBuilder(); userInfo.append(sysAmountAuditingVo.getUserName()) .append("-") .append(sysAmountAuditingVo.getUserId()); SendCollect sendCollect = new SendCollect(); sendCollect.setZgUuid(sysAmountAuditingVo.getId()); sendCollect.setApplicationTime(sysAmountAuditingVo.getCreateTime()); sendCollect.setExtractMoney(sysAmountAuditingVo.getAmount()); sendCollect.setReceivingAccount(sysAmountAuditingVo.getBankNumber()); sendCollect.setTranType(STRING_0); sendCollect.setPayee(userInfo.toString()); SendCollectEntity sendCollectEntity = new SendCollectEntity(); sendCollectEntity.setCollectId(sysAmountAuditingVo.getId()); sendCollectEntity.setSendState(STRING_2); sendCollectEntity.setCollectTime(sysAmountAuditingVo.getCreateTime()); sendCollectEntity.setAmount(sysAmountAuditingVo.getAmount()); sendCollectEntity.setBankNumber(sysAmountAuditingVo.getBankNumber()); sendCollectEntity.setUserInfo(userInfo.toString()); sendCollectEntity.setFailNum(INTEGER_0); sendCollectEntity.setSendUuid(sendUuid.toString()); log.info("新增推送send_collect_history数据库入参:{}", JSON.toJSONString(sendCollectEntity)); sendCollectMapper.addSendCollectInfo(sendCollectEntity); send(sendCollectEntity,sendCollect); } public void sendCollectRetry() { log.info("推送提现历史补偿启动,启动时间:{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); List sendCollectFailInfoList = new ArrayList(); try { sendCollectFailInfoList = sendCollectMapper.getSendCollectFailInfo(); log.info("查询send_collect_history数据库出参:{}", JSON.toJSON(sendCollectFailInfoList)); } catch (Exception e) { e.printStackTrace(); log.error("查询send_collect_history数据库报错:{}", e.getMessage()); } if (sendCollectFailInfoList != null && !sendCollectFailInfoList.isEmpty()) { for (SendCollectEntity sendCollectEntity : sendCollectFailInfoList) { if (sendCollectEntity == null && StringUtils.isNullOrEmpty(sendCollectEntity)) { continue; } if (sendCollectEntity.getFailNum() != null && sendCollectEntity.getFailNum() < 3) { Integer failNum = sendCollectEntity.getFailNum(); failNum = ++failNum; sendCollectEntity.setFailNum(failNum); SendCollect sendCollect = new SendCollect(); sendCollect.setZgUuid(sendCollectEntity.getCollectId()); sendCollect.setApplicationTime(sendCollectEntity.getCollectTime()); sendCollect.setExtractMoney(sendCollectEntity.getAmount()); sendCollect.setReceivingAccount(sendCollectEntity.getBankNumber()); sendCollect.setTranType(sendCollectEntity.getTranType()); sendCollect.setPayee(sendCollectEntity.getUserInfo()); send(sendCollectEntity, sendCollect); } } } } @Override public void run() { num++; log.info("第{}次重新推送提现记录消息", num); this.sendCollectRetry(); if (num == 3) { //停止定时任务 scheduledFutureUtil.endJob("com.ydtech.utils.scheduledFutureUtil"); } } }