|
|
@@ -1,203 +0,0 @@
|
|
|
-package com.ydtech.utils.sendProjectUtils;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.ydtech.modules.order.entity.vo.sendProject.SendProjectInfo;
|
|
|
-import com.ydtech.modules.sendProject.dao.SendProjectMapper;
|
|
|
-import com.ydtech.modules.sendProject.entity.SendProjectEntity;
|
|
|
-import com.ydtech.utils.StringUtils;
|
|
|
-import com.ydtech.utils.scheduledFutureUtil.ScheduledFutureUtil;
|
|
|
-import com.ydtech.utils.sendCollectUtils.SendCollectUtils;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.http.HttpEntity;
|
|
|
-import org.apache.http.ParseException;
|
|
|
-import org.apache.http.client.ClientProtocolException;
|
|
|
-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.stereotype.Component;
|
|
|
-
|
|
|
-import java.io.IOException;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-@Component
|
|
|
-public class SendProjrctUtils{
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private SendProjectMapper sendProjectMapper;
|
|
|
-
|
|
|
- @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_9 = "9";
|
|
|
- private static final String RESPONSE_SUCCESS = "200";
|
|
|
-
|
|
|
- public Map<String, String> sendProject(SendProjectInfo sendProjectInfo) {
|
|
|
-
|
|
|
- // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
|
|
|
- CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
-
|
|
|
- // 创建Post请求
|
|
|
- HttpPost httpPost = new HttpPost(sendMessageIP + "system/record/receiveDistributeServices");
|
|
|
- // 我这里利用阿里的fastjson,将Object转换为json字符串;
|
|
|
- // (需要导入com.alibaba.fastjson.JSON包)
|
|
|
- String jsonString = JSON.toJSONString(sendProjectInfo);
|
|
|
-
|
|
|
- StringEntity entity = new StringEntity(jsonString, "UTF-8");
|
|
|
-
|
|
|
- // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
|
|
|
- httpPost.setEntity(entity);
|
|
|
- httpPost.setHeader("Content-Type", "application/json;charset=utf8");
|
|
|
- sendProjectInfo.setHttpPost(httpPost.toString());
|
|
|
-
|
|
|
- // 响应模型
|
|
|
- CloseableHttpResponse response = null;
|
|
|
- String responseCode = new String();
|
|
|
-
|
|
|
- try {
|
|
|
-
|
|
|
- // 由客户端执行(发送)Post请求
|
|
|
- response = httpClient.execute(httpPost);
|
|
|
- // 从响应模型中获取响应实体
|
|
|
- HttpEntity responseEntity = response.getEntity();
|
|
|
-
|
|
|
- //从响应实体中截取状态码
|
|
|
- String string = EntityUtils.toString(responseEntity);
|
|
|
- sendProjectInfo.setHttpResponse(string);
|
|
|
- String responseString = string.substring(string.lastIndexOf(":") + 1);
|
|
|
- responseCode = responseString.substring(0, responseString.length() - 1);
|
|
|
-
|
|
|
-
|
|
|
- log.info("响应状态为:{}", response.getStatusLine());
|
|
|
- } catch (Exception e) {
|
|
|
- sendProjectInfo.setHttpResponse(e.getMessage());
|
|
|
- sendProjectInfo.setSendState(STRING_9);
|
|
|
- updateSendProjectInfo(sendProjectInfo);
|
|
|
-// SendProjrctUtils sendProjrctUtils = new SendProjrctUtils();
|
|
|
-// scheduledFutureUtil.startJob(sendProjrctUtils, CORN);
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- // 释放资源
|
|
|
- if (httpClient != null) {
|
|
|
- httpClient.close();
|
|
|
- }
|
|
|
- if (response != null) {
|
|
|
- response.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- final String code = responseCode;
|
|
|
- return new HashMap<String, String>() {{
|
|
|
- put("responseCode", code);
|
|
|
- }};
|
|
|
- }
|
|
|
-
|
|
|
- public void firstSendProject(SendProjectInfo sendProjectInfo) {
|
|
|
- log.info("首次推送服务接口入参:{}", sendProjectInfo);
|
|
|
- Map<String, String> responseMap = new HashMap<>();
|
|
|
- responseMap = sendProject(sendProjectInfo);
|
|
|
- if (RESPONSE_SUCCESS.equals(responseMap.get("responseCode"))) {
|
|
|
- sendProjectInfo.setSendState(STRING_1);
|
|
|
- updateSendProjectInfo(sendProjectInfo);
|
|
|
- } else {
|
|
|
- sendProjectInfo.setSendState(STRING_0);
|
|
|
- updateSendProjectInfo(sendProjectInfo);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void sendProjectRetry(SendProjectInfo sendProjectInfo) {
|
|
|
- Map<String, String> responseMap = new HashMap<>();
|
|
|
- responseMap = sendProject(sendProjectInfo);
|
|
|
- if (RESPONSE_SUCCESS.equals(responseMap.get("responseCode"))) {
|
|
|
- sendProjectInfo.setSendState(STRING_1);
|
|
|
- updateSendProjectInfo(sendProjectInfo);
|
|
|
- } else {
|
|
|
- Integer failNum = sendProjectInfo.getFailNum();
|
|
|
- failNum = ++failNum;
|
|
|
- sendProjectInfo.setFailNum(failNum);
|
|
|
- sendProjectInfo.setSendState(STRING_0);
|
|
|
- updateSendProjectInfo(sendProjectInfo);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private void updateSendProjectInfo(SendProjectInfo sendProjectInfo) {
|
|
|
- SendProjectEntity updateSendProject = new SendProjectEntity();
|
|
|
- if (sendProjectInfo.getFailNum() != null) {
|
|
|
- Integer failNum = sendProjectInfo.getFailNum();
|
|
|
- failNum = ++failNum;
|
|
|
- updateSendProject.setFailNum(failNum);
|
|
|
- }
|
|
|
- updateSendProject.setSendUuid(sendProjectInfo.getUuid());
|
|
|
- updateSendProject.setSendState(sendProjectInfo.getSendState());
|
|
|
- updateSendProject.setHttpPost(sendProjectInfo.getHttpPost());
|
|
|
- updateSendProject.setHttpResponse(sendProjectInfo.getHttpResponse());
|
|
|
- updateSendProject.setUpdateTime(new Date());
|
|
|
- try {
|
|
|
- log.info("修改推送服务数据库入参:{}", JSON.toJSON(updateSendProject));
|
|
|
- sendProjectMapper.updateSendProjectInfo(updateSendProject);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.error("修改推送服务数据库报错:{}", e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void sendProjectRetry() {
|
|
|
- log.info("推送服务补偿启动,启动时间:{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
- List<SendProjectEntity> sendFailInfo = new ArrayList<SendProjectEntity>();
|
|
|
- try {
|
|
|
- sendFailInfo = sendProjectMapper.getSendFailInfo();
|
|
|
- log.info("查询send_project_history数据库出参:{}", JSON.toJSON(sendFailInfo));
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.error("查询send_project_history数据库报错:{}", e.getMessage());
|
|
|
- }
|
|
|
- if (sendFailInfo != null && !sendFailInfo.isEmpty()) {
|
|
|
- for (SendProjectEntity sendProjectEntity : sendFailInfo) {
|
|
|
- if (sendProjectEntity == null && StringUtils.isNullOrEmpty(sendProjectEntity)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (sendProjectEntity.getFailNum() != null && sendProjectEntity.getFailNum() < 3) {
|
|
|
- SendProjectInfo sendProjectInfo = new SendProjectInfo();
|
|
|
- sendProjectInfo.setGiveBudget(sendProjectEntity.getTotalAmount());
|
|
|
- sendProjectInfo.setUserName(sendProjectEntity.getUserName());
|
|
|
- sendProjectInfo.setUserPhone(sendProjectEntity.getUserPhone());
|
|
|
- sendProjectInfo.setUserCarNum(sendProjectEntity.getUserCarNum());
|
|
|
- sendProjectInfo.setUuid(sendProjectEntity.getSendUuid());
|
|
|
- sendProjectInfo.setFailNum(sendProjectEntity.getFailNum());
|
|
|
- this.sendProjectRetry(sendProjectInfo);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-// @Override
|
|
|
-// public void run() {
|
|
|
-// num++;
|
|
|
-// log.info("第{}次重新推送服务消息", num);
|
|
|
-// this.sendProjectRetry();
|
|
|
-// if (num == 3) {
|
|
|
-// //停止定时任务
|
|
|
-// scheduledFutureUtil.endJob("com.ydtech.utils.sendProjectUtils");
|
|
|
-// }
|
|
|
-// }
|
|
|
-}
|