From 79fc712ed025069c1d21de230bdc95c74910288c Mon Sep 17 00:00:00 2001
From: zhibing.pu <393733352@qq.com>
Date: 星期四, 25 七月 2024 18:22:40 +0800
Subject: [PATCH] 修改2.0 bug

---
 DriverIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java |  192 ++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 161 insertions(+), 31 deletions(-)

diff --git a/DriverIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java b/DriverIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java
index 16025aa..78ab33c 100644
--- a/DriverIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java
+++ b/DriverIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java
@@ -15,7 +15,9 @@
 import com.stylefeng.guns.modular.system.model.*;
 import com.stylefeng.guns.modular.system.service.*;
 import com.stylefeng.guns.modular.system.util.*;
+import com.stylefeng.guns.modular.system.util.GoogleMap.DistancematrixVo;
 import com.stylefeng.guns.modular.system.util.GoogleMap.FleetEngineUtil;
+import com.stylefeng.guns.modular.system.util.GoogleMap.GoogleMapUtil;
 import com.stylefeng.guns.modular.system.util.itextpdf.HtmlToPdfUtils;
 import com.stylefeng.guns.modular.system.util.quartz.QuartzUtil;
 import com.stylefeng.guns.modular.system.util.quartz.jobs.OrderTimeOutJob;
@@ -26,6 +28,8 @@
 import org.jsoup.nodes.Element;
 import org.jsoup.select.Elements;
 import org.quartz.JobDataMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -44,6 +48,8 @@
 
 @Service
 public class OrderLogisticsServiceImpl extends ServiceImpl<OrderLogisticsMapper, OrderLogistics> implements IOrderLogisticsService {
+    
+    Logger log = LoggerFactory.getLogger(this.getClass());
 
     @Resource
     private OrderLogisticsMapper orderLogisticsMapper;
@@ -123,6 +129,15 @@
     
     @Autowired
     private ICarService carService;
+    
+    @Autowired
+    private ISysOvertimeService sysOvertimeService;
+    
+    @Resource
+    private ICancleOrderService cancleOrderService;
+    
+    @Autowired
+    private IOrderPositionService orderPositionService;
 
 
 
@@ -173,45 +188,88 @@
         if(!StringUtils.hasLength(orderLogistics.getTripId())){
             orderLogistics.setTripId(UUIDUtil.getRandomCode());
         }
+        String value = redisUtil.getValue("DRIVER" + driver.getId());
+        if(ToolUtil.isNotEmpty(value)) {
+            String[] split = value.split(",");
+            DistancematrixVo distancematrix = GoogleMapUtil.getDistancematrix(orderLogistics.getStartLat(), orderLogistics.getStartLon(), Double.valueOf(split[1]), Double.valueOf(split[0]));
+            //超时时间
+            long timeOut = System.currentTimeMillis() + (distancematrix.getDuration() * 1000);
+            orderLogistics.setEstimateArriveTime(new Date(timeOut));
+            orderLogistics.setEstimateArriveMileage(distancematrix.getDistance());
+        }
         this.updateById(orderLogistics);
         //修改司机为服务中
         driver.setState(3);
         driverService.updateById(driver);
     
+        /**
+         * 超时用户取消不收费的提醒
+         *   即时单:预估到达预约点时间 + 配置不收费的时间 > 当前时间 (只弹一次)
+         *
+         * 超时用户取消订单后需要弹给司机提醒弹框,超时时间 = 当前时间 - 行程时间 - 配置不收费的时间
+         *
+         * 定时提醒弹框
+         *   司机只要开始超时且还未到达预约点,则需要定时提醒
+         */
+    
         //添加定时任务(普通任务)
-        ReminderRules reminderRules = reminderRulesService.selectOne(new EntityWrapper<ReminderRules>().eq("companyId", driver.getCompanyId()));
+        SysOvertime reminderRules = sysOvertimeService.selectOne(new EntityWrapper<SysOvertime>().eq("companyId", driver.getCompanyId()));
         if(null != reminderRules){
-            JSONObject jsonObject = JSON.parseObject(reminderRules.getContent());
-            long packageTimeoutReminderInterval = jsonObject.getInteger("packageTimeoutReminderInterval") * 60000L;
-            long m = (System.currentTimeMillis() - orderLogistics.getSnatchOrderTime().getTime()) / 60000;
+            CancleOrder cancleOrder = cancleOrderService.selectOne(new EntityWrapper<CancleOrder>().eq("companyId", driver.getCompanyId()));
+            Integer driverTimeout = JSON.parseObject(cancleOrder.getContent()).getInteger("driverTimeout");
+            //超时时间
+            long timeOut = orderLogistics.getEstimateArriveTime().getTime() + (driverTimeout * 60 * 1000);
+            //乘客取消不收费提醒
             JobDataMap jobDataMap = new JobDataMap();
             jobDataMap.put("driverId", uid);
+            jobDataMap.put("timeOutType", 1);
             jobDataMap.put("orderId", orderLogistics.getId());
             jobDataMap.put("orderType", 4);
-            jobDataMap.put("describe", language == 1 ? "您的包裹订单已超时" + m + "分钟,请抓紧!" : language == 2 ? "Your delivery order is overdue for " + m + " minute(s), please go faster." : "Votre commande de livraison est en retard depuis " + m + " minute(s), veuillez aller plus vite.");
+            jobDataMap.put("language", language);
+            jobDataMap.put("timeOut", timeOut);
+            jobDataMap.put("describe", language == 1 ? "您已超时" + driverTimeout + "分钟,用户可免费取消订单" : language == 2 ? "Reminder You are overdue for " + driverTimeout + " minutes The subscriber could cancel the order for free Confirm" : "Rappel Vous êtes en retard de " + driverTimeout + " minutes L’abonné peut annuler la commande gratuitement Confirmer");
             QuartzUtil.addSimpleQuartzTask(
                     new OrderTimeOutJob().buildQuartzJob(UUIDUtil.getRandomCode(5) + "_" + orderLogistics.getId() + "_4", "ORDER_TIME_OUT", jobDataMap)
-                    , new Date(packageTimeoutReminderInterval), packageTimeoutReminderInterval, -1);
+                    , new Date(timeOut), timeOut, 0);
+    
+            //超时循环提醒
+            jobDataMap = new JobDataMap();
+            jobDataMap.put("driverId", uid);
+            jobDataMap.put("timeOutType", 3);
+            jobDataMap.put("orderId", orderLogistics.getId());
+            jobDataMap.put("orderType", 4);
+            jobDataMap.put("language", language);
+            jobDataMap.put("timeOut", orderLogistics.getEstimateArriveTime().getTime());
+            jobDataMap.put("describe", "");
+            QuartzUtil.addSimpleQuartzTask(
+                    new OrderTimeOutJob().buildQuartzJob(UUIDUtil.getRandomCode(5) + "_" + orderLogistics.getId() + "_4", "ORDER_TIME_OUT", jobDataMap)
+                    , orderLogistics.getEstimateArriveTime(), reminderRules.getCar() * 60000, -1);
         }
     
-        Car car = carService.selectById(orderLogistics.getCarId());
-        if(!StringUtils.hasLength(car.getVehicleId())){
-            car.setVehicleId(UUIDUtil.getRandomCode());
-            carService.updateById(car);
-        }
-        String trip = fleetEngineUtil.getTrip(orderLogistics.getTripId());
-        if(ToolUtil.isEmpty(trip)){
-            String vehicles = fleetEngineUtil.getVehicles(car.getVehicleId());
-            if(ToolUtil.isEmpty(vehicles)){
-                CarModel carModel = carModelMapper.selectById(car.getCarModelId());
-                fleetEngineUtil.createVehicles(carModel.getSeat() - 1, car.getCarLicensePlate(), car.getVehicleId());
+        new Thread(()->{
+            try {
+                Car car = carService.selectById(orderLogistics.getCarId());
+                if(!StringUtils.hasLength(car.getVehicleId())){
+                    car.setVehicleId(UUIDUtil.getRandomCode());
+                    carService.updateById(car);
+                }
+                String trip = fleetEngineUtil.getTrip(orderLogistics.getTripId());
+                if(ToolUtil.isEmpty(trip)){
+                    String vehicles = fleetEngineUtil.getVehicles(car.getVehicleId());
+                    if(ToolUtil.isEmpty(vehicles)){
+                        CarModel carModel = carModelMapper.selectById(car.getCarModelId());
+                        fleetEngineUtil.createVehicles(carModel.getSeat() - 1, car.getCarLicensePlate(), car.getVehicleId());
+                    }
+                    fleetEngineUtil.createTrip(car.getVehicleId(), 1, orderLogistics.getTripId(),
+                            orderLogistics.getStartLat().toString(), orderLogistics.getStartLon().toString(),  orderLogistics.getEndLat().toString(), orderLogistics.getEndLon().toString());
+                }else{
+                    //开始修改行程数据
+                    fleetEngineUtil.updateTrip(null, car.getVehicleId(), null, orderLogistics.getTripId(), null, null, null, null);
+                }
+            }catch (Exception e){
+                e.printStackTrace();
             }
-            fleetEngineUtil.createTrip(car.getVehicleId(), 1, orderLogistics.getTripId(),
-                    orderLogistics.getStartLat().toString(), orderLogistics.getStartLon().toString(),  orderLogistics.getEndLat().toString(), orderLogistics.getEndLon().toString());
-        }else{
-            //开始修改行程数据
-            fleetEngineUtil.updateTrip(null, car.getVehicleId(), null, orderLogistics.getTripId(), null, null, null, null);
-        }
+        }).start();
         
         //推送相关代码------------------start----------------
         new Thread(new Runnable() {
@@ -322,9 +380,16 @@
         this.updateById(orderLogistics);
     
         Driver driver = driverService.selectById(orderLogistics.getDriverId());
-        Car car = carMapper.selectById(driver.getCarId());
-        //修改行程数据
-        fleetEngineUtil.updateTrip(tripStatus, car.getVehicleId(), null, orderLogistics.getTripId(), null, null, null, null);
+        String finalTripStatus = tripStatus;
+        new Thread(()->{
+            Car car = carMapper.selectById(driver.getCarId());
+            //修改行程数据
+            try {
+                fleetEngineUtil.updateTrip(finalTripStatus, car.getVehicleId(), null, orderLogistics.getTripId(), null, null, null, null);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }).start();
         
         // TODO: 2020/6/5 推送状态
         new Thread(new Runnable() {
@@ -377,8 +442,8 @@
     }
 
     @Override
-    public List<Map<String, Object>> queryOrderList(Integer driverId, Integer language) throws Exception {
-        List<Map<String, Object>> list = orderLogisticsMapper.queryOrderList(driverId, language);
+    public List<Map<String, Object>> queryOrderList(Integer state, Integer driverId, Integer language) throws Exception {
+        List<Map<String, Object>> list = orderLogisticsMapper.queryOrderList(state, driverId, language);
         for (Map<String, Object> map : list) {
             if(null != map.get("time")){
                 String time = map.get("time").toString();
@@ -632,6 +697,14 @@
             orderLogistics.setEndLon(Double.valueOf(orderLogistics.getDestinationLon()));
             orderLogistics.setEndLat(Double.valueOf(orderLogistics.getDestinationLat()));
             orderLogistics.setEndAddress(orderLogistics.getDestination());
+            //修改google地图行程终点
+            try {
+                fleetEngineUtil.updateTrip(null, null, null, orderLogistics.getTripId(),
+                        null, null, orderLogistics.getEndLat().toString(), orderLogistics.getEndLon().toString());
+            } catch (Exception e) {
+                e.printStackTrace();
+                throw new RuntimeException(e);
+            }
         }
         orderLogistics.setDestinationLon("");
         orderLogistics.setDestination("");
@@ -650,7 +723,7 @@
     @Override
     public Map<String, Object> queryMoneyInfo(Integer orderId) throws Exception {
         OrderLogistics orderLogistics = this.selectById(orderId);
-        if(orderLogistics.getState() == 5){//服务中的时候获取实时费用数据
+        if(orderLogistics.getState() == 6){//服务中的时候获取实时费用数据
             this.setMoney(orderLogistics, 0D, 0D);
         }
     
@@ -828,6 +901,7 @@
             amount = num25 + (d1 * num26) + (t1 * num27) + (w1 * num7) + yt1 + yt2 + yt3;
             orderLogistics.setStartMileage(num2);
             orderLogistics.setStartMoney(num25);//起步价
+            orderLogistics.setStartDuration((int)(num3.doubleValue()));
             orderLogistics.setMileageKilometers(new BigDecimal(d1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
             orderLogistics.setMileageMoney(new BigDecimal(d1 * num26).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//里程费
             orderLogistics.setDuration(new BigDecimal(t1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
@@ -843,8 +917,6 @@
             orderLogistics.setDiscount(0D);//优惠抵扣
             orderLogistics.setPayMoney(0D);//支付金额
             orderLogistics.setOrderMoney(new BigDecimal(amount + parkingFee + crossingFee).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-            
-            
             return orderLogistics;
         }
         
@@ -867,6 +939,7 @@
         amount = num1 + (d1 * num4) + (t1 * num5) + (w1 * num7) + yt1 + yt2 + yt3;
         orderLogistics.setStartMileage(num2);
         orderLogistics.setStartMoney(num1);//起步价
+        orderLogistics.setStartDuration((int)(num3.doubleValue()));
         orderLogistics.setMileageKilometers(new BigDecimal(d1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
         orderLogistics.setMileageMoney(new BigDecimal(d1 * num4).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//里程费
         orderLogistics.setDuration(new BigDecimal(t1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
@@ -900,6 +973,9 @@
     @Override
     public ResultUtil confirmFees(Integer language, Integer orderId, Integer type, Double parkingFee, Double crossingFee) throws Exception {
         OrderLogistics orderLogistics = this.selectById(orderId);
+        if(6 != orderLogistics.getState()){
+            return ResultUtil.error("操作异常,请刷新订单");
+        }
         if(orderLogistics.getArriveTime()==null){
             orderLogistics.setArriveTime(orderLogistics.getStartServiceTime());
         }
@@ -978,6 +1054,60 @@
                 pushUtil.pushOrderState(2, finalOrderTaxi.getDriverId(), finalOrderTaxi.getId(), 4, finalOrderTaxi.getState());
             }
         }).start();
+    
+        //上报google
+        OrderLogistics finalOrderLogistics = orderLogistics;
+        new Thread(()->{
+            try {
+                fleetEngineUtil.reportBillableEvent(finalOrderLogistics.getTripId());
+                log.warn("上报时间:{},tripid:{},created_at:{},completed_at:{}", System.currentTimeMillis(), finalOrderLogistics.getTripId(),
+                        finalOrderLogistics.getInsertTime().getTime(), finalOrderLogistics.getEndServiceTime().getTime());
+            }catch (Exception e){
+                e.printStackTrace();
+            }
+        }).start();
         return ResultUtil.success();
     }
+    
+    
+    /**
+     * 计算已服务的实时里程
+     * @param orderId
+     * @param lon
+     * @param lat
+     */
+    @Override
+    public boolean calculateMileage(Integer orderId, String lon, String lat) throws Exception {
+        OrderLogistics orderLogistics = this.selectById(orderId);
+        OrderPosition orderPosition = orderPositionService.queryNew(orderId, 4);
+        String now = lon + "," + lat;
+        String old = null;
+        if(null != orderPosition){
+            old = orderPosition.getLon() + "," + orderPosition.getLat();
+        }else{
+            orderLogistics.setToStartPointMileage(0D);
+            orderLogistics.setMileage(0D);
+            this.updateById(orderLogistics);
+            return true;//第一条数据不作处理,直接存储
+        }
+        
+        Map<String, Double> distance = GeodesyUtil.getDistance(now, old);
+        if(null != distance){
+            Double distance1 = distance.get("WGS84");
+            if(distance1 > 50 && orderLogistics.getState() < 5){//大于50米表示在移动
+                orderLogistics.setToStartPointMileage(new BigDecimal(orderLogistics.getToStartPointMileage()).add(new BigDecimal(distance1)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                this.updateById(orderLogistics);
+                return true;
+            }
+            if(distance1 > 50 && orderLogistics.getState()==5){//大于50米表示在移动
+                orderLogistics.setMileage(new BigDecimal(orderLogistics.getMileage()).add(new BigDecimal(distance1)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                this.updateById(orderLogistics);
+                return true;
+            }
+            return false;
+        }else{
+            System.err.println("调用高德计算距离出错");
+        }
+        return false;
+    }
 }

--
Gitblit v1.7.1