Pu Zhibing
4 天以前 70211b41954df4ac1232f48420b89c13fb5fb451
UserQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushUtil.java
@@ -1,5 +1,8 @@
package com.stylefeng.guns.modular.system.util;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.ToolUtil;
@@ -11,14 +14,10 @@
import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
@@ -30,12 +29,7 @@
@Component
public class PushUtil {
    @Autowired
    private RestTemplate internalRestTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
   private final String socket_uri = "http://172.21.35.142:6000";
    @Autowired
    private IOrderTaxiService orderTaxiService;
@@ -49,12 +43,13 @@
    private IOrderCrossCityService orderCrossCityService;
    private Map<String, Timer> taskMap = new HashMap<>();//存储定时推送的定时器
   @Resource
   private RedisTemplate<String, Object> redisTemplate;
    /**
     * 推送订单状态
    *
     * @param type          1=用户,2=司机
     * @param uid           对象id
     * @param orderId       订单id
@@ -79,25 +74,28 @@
        jsonObject.put("data", map);
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
      HttpRequest post = HttpUtil.createPost(socket_uri + "/netty/sendMsgToClient");
      post.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", jsonObject.toJSONString());
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
      Map<String, Object> params = new HashMap<>();
      params.put("msg", jsonObject.toJSONString());
      params.put("id", uid);
      params.put("type", type);
      post.form(params);
      HttpResponse execute = post.execute();
      if (200 != execute.getStatus()) {
         System.err.println("推送异常");
      } else {
         JSONObject jsonObject1 = JSON.parseObject(execute.body(), JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
   }
    /**
     * 推单完成后,没有司机接单的推送提醒
    *
     * @param type
     * @param uid
     * @param orderId
@@ -114,24 +112,24 @@
        jsonObject.put("data", map);
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
      HttpRequest post = HttpUtil.createPost(socket_uri + "/netty/sendMsgToClient");
      post.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", jsonObject.toJSONString());
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
      Map<String, Object> params = new HashMap<>();
      params.put("msg", jsonObject.toJSONString());
      params.put("id", uid);
      params.put("type", type);
      post.form(params);
      HttpResponse execute = post.execute();
      if (200 != execute.getStatus()) {
         System.err.println("推送异常");
      } else {
         JSONObject jsonObject1 = JSON.parseObject(execute.body(), JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
   }
    /**
     * 推送司机位置给乘客端
@@ -162,9 +160,9 @@
        }
    }
    /**
     * 创建定时任务
    *
     * @param orderId
     */
    public void createTask(Integer orderId, Integer orderType){
@@ -183,9 +181,9 @@
        taskMap.put(orderId + "_" + orderType, timer);
    }
    /**
     * 推送处理方法
    *
     * @param orderId
     * @param orderType
     * @throws Exception
@@ -299,10 +297,6 @@
        msg.put("data", map);
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        String value1 = (String) redisTemplate.opsForValue().get(orderId + "_" + orderType);
        if(ToolUtil.isEmpty(value1)){
@@ -310,21 +304,44 @@
            return;
        }
        JSONObject jsonObject = JSON.parseObject(value1);
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", msg.toJSONString());
        params.add("id", jsonObject.getString("id"));
        params.add("type", jsonObject.getString("type"));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
      //调用推送
      HttpRequest post = HttpUtil.createPost(socket_uri + "/netty/sendMsgToClient");
      post.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
      //将请求头部和参数合成一个请求
      Map<String, Object> params = new HashMap<>();
      params.put("msg", msg.toJSONString());
      params.put("id", jsonObject.getIntValue("id"));
      params.put("type", jsonObject.getIntValue("type"));
      post.form(params);
      HttpResponse execute = post.execute();
      if (200 != execute.getStatus()) {
         System.err.println("推送异常");
      } else {
         JSONObject jsonObject1 = JSON.parseObject(execute.body(), JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
   }
   /**
    * 删除定时任务
    *
    * @param orderId
    */
   public void removeTask(Integer orderId, Integer orderType) {
      Timer timer = taskMap.get(orderId + "_" + orderType);
      if (null != timer) {
         timer.cancel();
         taskMap.remove(orderId + "_" + orderType);
         redisTemplate.delete(orderId + "_" + orderType);
      }
   }
    /**
     * 推送强迫下线
    *
     * @param id
     * @param type
     */
@@ -335,26 +352,30 @@
        msg.put("method", "OFFLINE");
        msg.put("data", new Object());
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
      HttpRequest post = HttpUtil.createPost(socket_uri + "/netty/sendMsgToClient");
      post.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", msg.toJSONString());
        params.add("id", id.toString());
        params.add("type", type.toString());
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
      Map<String, Object> params = new HashMap<>();
      params.put("msg", msg.toJSONString());
      params.put("id", id);
      params.put("type", type);
      post.form(params);
      HttpResponse execute = post.execute();
      if (200 != execute.getStatus()) {
         System.err.println("推送异常");
      } else {
         JSONObject jsonObject1 = JSON.parseObject(execute.body(), JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
   }
    /**
     * 摆渡抢单成功后推单
    *
     * @param type
     * @param uid
     * @param orderId
@@ -373,34 +394,22 @@
        jsonObject.put("data", map);
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
      HttpRequest post = HttpUtil.createPost(socket_uri + "/netty/sendMsgToClient");
      post.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", jsonObject.toJSONString());
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
      Map<String, Object> params = new HashMap<>();
      params.put("msg", jsonObject.toJSONString());
      params.put("id", uid);
      params.put("type", type);
      post.form(params);
      HttpResponse execute = post.execute();
      if (200 != execute.getStatus()) {
         System.err.println("推送异常");
      } else {
         JSONObject jsonObject1 = JSON.parseObject(execute.body(), JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
    /**
     * 删除定时任务
     * @param orderId
     */
    public void removeTask(Integer orderId, Integer orderType){
        Timer timer = taskMap.get(orderId + "_" + orderType);
        if (null != timer){
            timer.cancel();
            taskMap.remove(orderId + "_" + orderType);
            redisTemplate.delete(orderId + "_" + orderType);
        }
    }
}