From db0aaa2033a43c016ca165513faaee993117ecf8 Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期五, 22 八月 2025 12:14:14 +0800
Subject: [PATCH] 修改bug

---
 user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/service/impl/OrderServiceImpl.java |   89 ++++++++++++++++++++++----------------------
 1 files changed, 44 insertions(+), 45 deletions(-)

diff --git a/user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/service/impl/OrderServiceImpl.java b/user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/service/impl/OrderServiceImpl.java
index d8b358b..d658f5c 100644
--- a/user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/service/impl/OrderServiceImpl.java
+++ b/user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/service/impl/OrderServiceImpl.java
@@ -17,6 +17,8 @@
 import com.supersavedriving.user.modular.system.util.MallBook.util.TrhRequest;
 import com.supersavedriving.user.modular.system.util.mongodb.model.Location;
 import com.supersavedriving.user.modular.system.warpper.*;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
@@ -37,6 +39,7 @@
 import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 
@@ -74,8 +77,8 @@
     @Autowired
     private RedisUtil redisUtil;
 
-//    @Autowired
-//    private RedissonClient redissonClient;
+    @Autowired
+    private RedissonClient redissonClient;
 
     @Autowired
     private MongoTemplate mongoTemplate;
@@ -432,11 +435,11 @@
      */
     @Override
     public ResultUtil travelOrder(Integer uid, TravelOrder travelOrder) throws Exception {
-//        RLock lock1 = redissonClient.getLock("travelOrder:" + uid);
-//        boolean lock2 = lock1.tryLock(5, 10, TimeUnit.SECONDS);
-//        if(!lock2){
-//            return ResultUtil.error("系统繁忙,请稍后重试");
-//        }
+        RLock lock1 = redissonClient.getLock("travelOrder:" + uid);
+        boolean lock2 = lock1.tryLock(5, 10, TimeUnit.SECONDS);
+        if(!lock2){
+            return ResultUtil.error("系统繁忙,请稍后重试");
+        }
         try {
             List<Integer> state = Arrays.asList(101, 102, 103, 104, 105, 106, 107, 201, 401);
             Order order = this.selectOne(new EntityWrapper<Order>().eq("userId", uid).eq("status", 1).in("state", state));
@@ -523,33 +526,40 @@
                 appUserService.updateById(appUser);
             }
 
+            List<Order> orderList = new ArrayList<>();
             for (Integer i = 0; i < travelOrder.getDriverNum(); i++) {
                 order.setId(null);
                 order.setCode(UUIDUtil.getTimeStr() + UUIDUtil.getNumberRandom(5));
                 boolean insert = this.insert(order);
                 if(insert){
-                    //推送状态
-                    pushUtil.pushOrderStatus(uid, 1, order.getId(), order.getState());
-                    if(null != order.getDriverId()){
-                        PushOrderInfoWarpper pushOrderInfoWarpper = new PushOrderInfoWarpper();
-                        pushOrderInfoWarpper.setId(order.getId());
-                        pushOrderInfoWarpper.setState(order.getState());
-                        pushUtil.pushOrderInfo(order.getDriverId(), 2, pushOrderInfoWarpper);
-                    }else{
-                        //推单
-                        Order finalOrder = order;
-                        new Thread(()->{
-                            pushOrder(finalOrder.getId());
-                        }).start();
-                    }
+                    orderList.add(order);
                 }
             }
+            new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    orderList.forEach(order -> {
+                        //推送状态
+                        pushUtil.pushOrderStatus(uid, 1, order.getId(), order.getState());
+                        if(null != order.getDriverId()){
+                            PushOrderInfoWarpper pushOrderInfoWarpper = new PushOrderInfoWarpper();
+                            pushOrderInfoWarpper.setId(order.getId());
+                            pushOrderInfoWarpper.setState(order.getState());
+                            pushUtil.pushOrderInfo(order.getDriverId(), 2, pushOrderInfoWarpper);
+                        }else{
+                            //推单
+                            pushOrder(order.getId(), uid);
+                        }
+                    });
+                }
+            }).start();
+
             redisUtil.setStrValue("newOrder", "true");
             return ResultUtil.success(order.getId());
         }catch (Exception e){
             e.printStackTrace();
         }finally {
-//            lock1.unlock();
+            lock1.unlock();
         }
         return ResultUtil.error("系统异常");
     }
@@ -560,7 +570,9 @@
     /**
      * 订单推送逻辑
      */
-    public void pushOrder(Long orderId){
+    public void pushOrder(Long orderId, Integer uid){
+        System.err.println("开始推单:" + orderId);
+//        RLock lock1 = redissonClient.getLock("pushOrder:" + uid);
         try {
             Order order = this.selectById(orderId);
             /**
@@ -570,28 +582,14 @@
              *        合适司:积分 > 评分 > 距离
              *      3.司机没有接单直接将订单置入大厅
              */
-            boolean lock = redisUtil.lock(5);
-            if(!lock){
-                int num1 = 1;
-                while (num1 <= 10){
-                    Thread.sleep(3000);//等待3秒
-                    lock = redisUtil.lock(5);
-                    if(lock){
-                        break;
-                    }else{
-                        num1++;
-                    }
-                }
-            }
-            if(!lock){
-                order.setHallOrder(1);
-                this.updateById(order);
-                ExtraPushOrder(order);
-                redisUtil.unlock();
-            }
+//            boolean lock2 = lock1.tryLock(5, 10, TimeUnit.SECONDS);
+//            if(!lock2){
+//                order.setHallOrder(1);
+//                this.updateById(order);
+//                ExtraPushOrder(order);
+//            }
             SystemConfig systemConfig = systemConfigService.selectOne(new EntityWrapper<SystemConfig>().eq("type", 1));
             if(null == systemConfig){
-                redisUtil.unlock();
                 return;
             }
             JSONObject jsonObject = JSON.parseObject(systemConfig.getContent());
@@ -738,6 +736,7 @@
             }
 
             if(null != driver){
+                System.err.println("完成司机查找:" + driver);
                 //防止多笔订单推给一个司机
                 Driver driver1 = driverService.selectById(driver);
                 driver1.setServerStatus(2);
@@ -779,10 +778,10 @@
                 ExtraPushOrder(order);
                 redisUtil.setStrValue("lobbyOrder", "true");
             }
-            redisUtil.unlock();
         }catch (Exception e){
             e.printStackTrace();
-            redisUtil.unlock();
+        }finally {
+//            lock1.unlock();
         }
 
     }

--
Gitblit v1.7.1