From ada9e70f4d1b352113f533e34a856acc9f860c67 Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期五, 27 六月 2025 16:50:14 +0800
Subject: [PATCH] 修改轨迹存储问题

---
 DriverQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushUtil.java |  242 ++++++++++++++++++++++++++---------------------
 1 files changed, 133 insertions(+), 109 deletions(-)

diff --git a/DriverQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushUtil.java b/DriverQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushUtil.java
index 86ac869..a7e58bf 100644
--- a/DriverQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushUtil.java
+++ b/DriverQYTTravel/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;
@@ -12,18 +15,18 @@
 import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
 import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
+import org.springframework.data.redis.core.RedisTemplate;
 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 java.math.BigDecimal;
 import java.math.MathContext;
 import java.math.RoundingMode;
-import java.util.*;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
 
 /**
  * socket推单处理类
@@ -42,29 +45,31 @@
 
     @Autowired
     private IOrderPrivateCarService orderPrivateCarService;
-
+    
     @Autowired
     private IOrderCrossCityService orderCrossCityService;
-
+    
     @Autowired
-    private RedisUtil redisUtil;
-
+    private RedisTemplate redisTemplate;
+    
     @Autowired
     private IOrderLogisticsService orderLogisticsService;
-
+    
     private Map<String, Timer> taskMap = new HashMap<>();//存储定时推送的定时器
-
-
-
+    
+    private final String socket_uri = "http://172.21.35.142:6000";
+    
+    
     /**
      * 推送订单状态
-     * @param type          1=用户,2=司机
-     * @param uid           对象id
-     * @param orderId       订单id
-     * @param orderType     订单类型(1=专车,2=出租车,3=城际,4=小件物流-同城,5=小件物流-跨城,6=包车)
-     * @param state         订单状态(1=待接单,2=待出发,3=待到达预约地点,4=待乘客上车,5=服务中,6=完成服务,7=待支付,8=待评价,9=已完成,10=已取消,11=改派中)
+     *
+     * @param type      1=用户,2=司机
+     * @param uid       对象id
+     * @param orderId   订单id
+     * @param orderType 订单类型(1=专车,2=出租车,3=城际,4=小件物流-同城,5=小件物流-跨城,6=包车)
+     * @param state     订单状态(1=待接单,2=待出发,3=待到达预约地点,4=待乘客上车,5=服务中,6=完成服务,7=待支付,8=待评价,9=已完成,10=已取消,11=改派中)
      */
-    public void pushOrderState(Integer type, Integer uid, Integer orderId, Integer orderType, Integer state){
+    public void pushOrderState(Integer type, Integer uid, Integer orderId, Integer orderType, Integer state) {
         JSONObject jsonObject = new JSONObject();
         jsonObject.put("code", 200);
         jsonObject.put("msg", "SUCCESS");
@@ -74,22 +79,26 @@
         map.put("orderType", orderType);
         map.put("state", state);
         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);
-        if(jsonObject1.getIntValue("code") != 200){
-            System.err.println(jsonObject1.getString("msg"));
+        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"));
+            }
         }
+        
     }
 
 
@@ -109,22 +118,24 @@
         map.put("orderId", orderId);
         map.put("orderType", orderType);
         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", type.toString());
-        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
-        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
-        System.out.println("result:"+s);
-        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
-        if(jsonObject1.getIntValue("code") != 200){
-            System.err.println(jsonObject1.getString("msg"));
+        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"));
+            }
         }
     }
 
@@ -159,9 +170,9 @@
         JSONObject data = new JSONObject();
         data.put("id", userId);
         data.put("type", 1);
-        String value = redisUtil.getValue(orderId + "_" + orderType);
+        String value = (String) redisTemplate.opsForValue().get(orderId + "_" + orderType);
         if(ToolUtil.isEmpty(value)){
-            redisUtil.setStrValue(orderId + "_" + orderType, data.toJSONString());
+            redisTemplate.opsForValue().set(orderId + "_" + orderType, data.toJSONString());
             createTask(orderId, orderType);
         }
     }
@@ -276,7 +287,7 @@
             return;
         }
         //计算预计距离和剩余时间
-        String value = redisUtil.getValue("DRIVER" + String.valueOf(driverId));
+        String value = (String) redisTemplate.opsForValue().get("DRIVER" + String.valueOf(driverId));
         if(null == value || "".equals(value)){
             return;
         }
@@ -332,31 +343,35 @@
                 map.put("laveMileage", d);//距离终点剩余未服务的里程数
                 map.put("laveTime", t);//距离终端剩余未服务的预计时间
             }
-
+    
         }
-
+    
         msg.put("data", map);
-
-        //调用推送
-        HttpHeaders headers = new HttpHeaders();
-        // 以表单的方式提交
-        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
-        //将请求头部和参数合成一个请求
-        String value1 = redisUtil.getValue(orderId + "_" + orderType);
-        if(ToolUtil.isEmpty(value1)){
+    
+        String value1 = (String) redisTemplate.opsForValue().get(orderId + "_" + orderType);
+        if (ToolUtil.isEmpty(value1)) {
             this.removeTask(orderId, orderType);
             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);
-        if(jsonObject1.getIntValue("code") != 200){
-            System.err.println(jsonObject1.getString("msg"));
+    
+        //调用推送
+        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"));
+            }
         }
     }
 
@@ -367,27 +382,30 @@
      * @param id
      * @param type
      */
-    public void pushOffline(Integer id, Integer type){
+    public void pushOffline(Integer id, Integer type) {
         JSONObject msg = new JSONObject();
         msg.put("code", 200);
         msg.put("msg", "SUCCESS");
         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);
-        if(jsonObject1.getIntValue("code") != 200){
-            System.err.println(jsonObject1.getString("msg"));
+        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"));
+            }
         }
     }
 
@@ -412,21 +430,24 @@
         map.put("orderType", orderType);
         map.put("state", state);
         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);
-        if(jsonObject1.getIntValue("code") != 200){
-            System.err.println(jsonObject1.getString("msg"));
+        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"));
+            }
         }
     }
 
@@ -449,21 +470,24 @@
         map.put("orderType", orderType);
         map.put("money", money);
         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);
-        if(jsonObject1.getIntValue("code") != 200){
-            System.err.println(jsonObject1.getString("msg"));
+        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"));
+            }
         }
     }
 
@@ -477,7 +501,7 @@
         if (null != timer){
             timer.cancel();
             taskMap.remove(orderId + "_" + orderType);
-            redisUtil.remove(orderId + "_" + orderType);
+            redisTemplate.delete(orderId + "_" + orderType);
         }
     }
 }

--
Gitblit v1.7.1