From e46b648749b8ba9cde852b7bc965ef602ca57da4 Mon Sep 17 00:00:00 2001
From: zhibing.pu <393733352@qq.com>
Date: 星期三, 11 九月 2024 10:43:13 +0800
Subject: [PATCH] 修改bug

---
 DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java |  183 +++++++++++++++++++++++++++++++++++----------
 1 files changed, 140 insertions(+), 43 deletions(-)

diff --git a/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java b/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java
index d98d1e7..debbc89 100644
--- a/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java
+++ b/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java
@@ -1,6 +1,7 @@
 package com.stylefeng.guns.modular.specialTrain.server.impl;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.service.impl.ServiceImpl;
@@ -15,15 +16,22 @@
 import com.stylefeng.guns.modular.system.util.*;
 import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
 import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
+import org.gavaghan.geodesy.Ellipsoid;
+import org.gavaghan.geodesy.GeodeticCalculator;
+import org.gavaghan.geodesy.GeodeticCurve;
+import org.gavaghan.geodesy.GlobalCoordinates;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
 
 import javax.annotation.Resource;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.PrintWriter;
 import java.math.BigDecimal;
+import java.math.MathContext;
+import java.math.RoundingMode;
 import java.util.*;
 
 
@@ -48,8 +56,8 @@
     @Autowired
     private GDMapGeocodingUtil gdMapGeocodingUtil;
 
-    @Autowired
-    private ChinaMobileUtil chinaMobileUtil;
+//    @Autowired
+//    private ChinaMobileUtil chinaMobileUtil;
 
     @Resource
     private RegionMapper regionMapper;
@@ -177,19 +185,20 @@
         orderPrivateCar.setSnatchOrderTime(new Date());
 
         //调用高德创建轨迹
-        String s = gdFalconUtil.selectTerminal(driver.getPhone());
-        String track = gdFalconUtil.createTrack(s);
-        orderPrivateCar.setTrackId(track);
+//        String s = gdFalconUtil.selectTerminal(driver.getPhone());
+//        String track = gdFalconUtil.createTrack(s);
+//        JSONObject jsonObject = JSONObject.parseObject(track);
+//        orderPrivateCar.setTrackId(String.valueOf(jsonObject.getInteger("trid")));
 
         //调用移动的小号接口
         /*Map<String, String> geocode = gdMapGeocodingUtil.geocode(orderPrivateCar.getStartLon().toString(), orderPrivateCar.getStartLat().toString());
         Region region = regionMapper.query(geocode.get("districtCode"));*/
-        Map<String, String> map = chinaMobileUtil.midAxbBindSend(orderPrivateCar.getPassengersPhone(), driver.getPhone(), null);
-        if(String.valueOf(map.get("code")).equals("200")){
-            orderPrivateCar.setTelX(map.get("telX"));
-            orderPrivateCar.setBindId(map.get("bindId"));
-        }
-        System.out.println(map);
+//        Map<String, String> map = chinaMobileUtil.midAxbBindSend(orderPrivateCar.getPassengersPhone(), driver.getPhone(), null);
+//        if(String.valueOf(map.get("code")).equals("200")){
+//            orderPrivateCar.setTelX(map.get("telX"));
+//            orderPrivateCar.setBindId(map.get("bindId"));
+//        }
+//        System.out.println(map);
         this.updateById(orderPrivateCar);
 
         //如果是预约单,则不修改司机为服务中
@@ -304,29 +313,6 @@
                         }
                     }
                 }).start();
-                OrderPosition orderPosition = new OrderPosition();
-                orderPosition.setOrderId(orderId);
-                orderPosition.setOrderType(1);
-                orderPosition.setDriverId(orderPrivateCar.getDriverId());
-                orderPosition.setLat(lat.toString());
-                orderPosition.setLon(lon.toString());
-                orderPosition.setAltitude("0");
-                orderPosition.setDirectionAngle("0");
-                //先计算里程,在存储最新位置
-                orderPosition.setInsertTime(new Date());
-                //将数据存储到文件中
-                List<OrderPosition> orderPositions = orderPositionService.queryPosition(orderPosition.getOrderId(), orderPosition.getOrderType());
-                File file = new File(filePath + orderPosition.getOrderId() + "_" + orderPosition.getOrderType() + ".txt");
-                if(!file.exists()){
-                    file.getParentFile().mkdirs();
-                    file.createNewFile();
-                }
-                //写入相应的文件
-                PrintWriter out = new PrintWriter(new FileWriter(file));
-                orderPositions.add(orderPosition);
-                out.write(JSON.toJSONString(orderPositions));
-                out.flush();
-                out.close();
                 break;
             case 6://结束服务(专车可以返回继续服务)不修改状态
                 orderPrivateCar.setGetoffLon(lon);
@@ -334,6 +320,102 @@
                 orderPrivateCar.setGetoffAddress(address);
                 orderPrivateCar.setGetoffTime(new Date());
                 orderPrivateCar.setEndServiceTime(new Date());
+
+                // 结束服务时查询轨迹,计算距离
+                Driver driver = driverService.selectById(orderPrivateCar.getDriverId());
+                String tid = gdFalconUtil.selectTerminal(driver.getPhone());
+                String result = gdFalconUtil.selectTrack(tid, orderPrivateCar.getTrackId(),1);
+
+                JSONObject jsonObject = JSONObject.parseObject(result);
+                JSONArray tracks = jsonObject.getJSONArray("tracks");
+                Integer counts = jsonObject.getInteger("counts");
+                JSONArray points = new JSONArray();
+                // 如果一单轨迹点数大于999,需进行分页查询,将所有的点查询出
+                if(counts > 999){
+                    int i = counts%999;
+                    for (int j = 1; j < i + 1; j++) {
+                        String res = gdFalconUtil.selectTrack(tid, orderPrivateCar.getTrackId(),1);
+                        JSONObject jsonObject1 = JSONObject.parseObject(res);
+                        JSONArray tracks1 = jsonObject1.getJSONArray("tracks");
+                        Object ob = tracks1.get(0);
+                        String obs = JSONObject.toJSONString(ob);
+                        JSONObject object = JSONObject.parseObject(obs);
+                        JSONArray point = object.getJSONArray("points");
+                        points.addAll(point);
+                    }
+                }
+                // 轨迹点数小于999,一页查询出即可
+                if(counts > 0 && counts < 999){
+                    Object ob = tracks.get(0);
+                    String obs = JSONObject.toJSONString(ob);
+                    JSONObject object = JSONObject.parseObject(obs);
+                    JSONArray point = object.getJSONArray("points");
+                    points.addAll(point);
+                }
+
+                System.out.println("轨迹坐标点=====:"+points);
+                String toLonLat = "";
+                double sum = 0;
+                List<OrderPosition> orderPositions = new ArrayList<>();
+                for (Object o : points) {
+                    String s = JSONObject.toJSONString(o);
+                    JSONObject obj = JSONObject.parseObject(s);
+                    String fromLonLat = obj.getString("location");
+                    String[] split = fromLonLat.split(",");
+                    Long locatetime = obj.getLong("locatetime");
+                    Date date = new Date(locatetime);
+                    if(date.after(orderPrivateCar.getStartServiceTime()) && date.before(orderPrivateCar.getEndServiceTime())){
+                        // 封装定位文件里的单个定位对象
+                        OrderPosition orderPosition = new OrderPosition();
+                        orderPosition.setOrderId(orderId);
+                        orderPosition.setOrderType(1);
+                        orderPosition.setDriverId(orderPrivateCar.getDriverId());
+                        orderPosition.setLat(split[1]);
+                        orderPosition.setLon(split[0]);
+                        orderPosition.setAltitude("0");
+                        orderPosition.setDirectionAngle("0");
+                        //先计算里程,在存储最新位置
+                        orderPosition.setInsertTime(new Date());
+                        orderPositions.add(orderPosition);
+                        if(StringUtils.hasLength(fromLonLat) && StringUtils.hasLength(toLonLat)){
+                            String[] from = fromLonLat.split(",");
+                            String[] to = toLonLat.split(",");
+                            GlobalCoordinates source = new GlobalCoordinates(Double.valueOf(from[1]), Double.valueOf(from[0]));
+                            GlobalCoordinates target = new GlobalCoordinates(Double.valueOf(to[1]), Double.valueOf(to[0]));
+                            double WGS84 = getDistanceMeter(source, target, Ellipsoid.WGS84);
+                            System.err.println("WGS84坐标系计算结果:"+WGS84 + "米");
+                            sum += WGS84;
+                        }
+                        toLonLat = fromLonLat;
+                    }
+                }
+                
+                //存储轨迹
+                // 将数据存储到文件中
+                File file = new File(filePath + orderId + "_" + 1 + ".txt");
+                if(!file.exists()){
+                    file.getParentFile().mkdirs();
+                    file.createNewFile();
+                }
+                //写入相应的文件
+                PrintWriter out = new PrintWriter(new FileWriter(file));
+                out.write(JSON.toJSONString(orderPositions));
+                out.flush();
+                out.close();
+    
+    
+                //获取预估里程,使用预估里程作为行驶里程计算金额
+                String origins = orderPrivateCar.getStartLon() + "," + orderPrivateCar.getStartLat();
+                String destination = orderPrivateCar.getGetoffLon() + "," + orderPrivateCar.getGetoffLat();
+                Map<String, String> distance = gdMapElectricFenceUtil.getDistance(origins, destination, 1);
+                Double distance1 = Double.valueOf(distance.get("distance"));
+                if(distance1 - sum > 1000){
+                    orderPrivateCar.setMileage(distance1);
+                    orderPrivateCar.setIsAbnormal(1);
+                }else{
+                    orderPrivateCar.setMileage(sum);
+                    orderPrivateCar.setIsAbnormal(0);
+                }
                 break;
         }
         this.updateById(orderPrivateCar);
@@ -350,6 +432,11 @@
         return ResultUtil.success();
     }
 
+    private double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){
+        //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
+        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
+        return geoCurve.getEllipsoidalDistance();
+    }
 
 
     /**
@@ -364,6 +451,9 @@
     @Override
     public ResultUtil confirmFees(Integer orderId, Integer type, Double parkingFee, Double crossingFee) throws Exception {
         OrderPrivateCar orderPrivateCar = this.selectById(orderId);
+        if(orderPrivateCar.getState() != 5){
+            return ResultUtil.error("异常操作,请刷新订单");
+        }
         if(orderPrivateCar.getArriveTime()==null){
             orderPrivateCar.setArriveTime(orderPrivateCar.getStartServiceTime());
         }
@@ -393,7 +483,7 @@
                     }
                     if(company.getIsSpeFixedOrProportional() == 1){//比例
                         Double price = orderPrivateCar.getStartMoney() + orderPrivateCar.getMileageMoney() + orderPrivateCar.getWaitMoney() + orderPrivateCar.getDurationMoney() + orderPrivateCar.getLongDistanceMoney();
-                        d = new BigDecimal(price).multiply(new BigDecimal(speMoney).divide(new BigDecimal(100))).setScale(2, BigDecimal.ROUND_HALF_EVEN);
+                        d = new BigDecimal(price).multiply(new BigDecimal(speMoney).divide(new BigDecimal(100), new MathContext(2, RoundingMode.HALF_EVEN))).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                         c = new BigDecimal(orderPrivateCar.getOrderMoney()).subtract(d).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                     }
                     incomeService.saveData(1, orderPrivateCar.getCompanyId(), 2, orderPrivateCar.getId(), 1, d.doubleValue());
@@ -418,11 +508,11 @@
             orderPrivateCar.setState(7);
         }
 
-        Map<String, String> map = chinaMobileUtil.midAxbUnBindSend(orderPrivateCar.getBindId(),orderPrivateCar.getTelX());
-        if(String.valueOf(map.get("code")).equals("200")){
-            orderPrivateCar.setTelX("");
-            orderPrivateCar.setBindId("");
-        }
+//        Map<String, String> map = chinaMobileUtil.midAxbUnBindSend(orderPrivateCar.getBindId(),orderPrivateCar.getTelX());
+//        if(String.valueOf(map.get("code")).equals("200")){
+//            orderPrivateCar.setTelX("");
+//            orderPrivateCar.setBindId("");
+//        }
         this.updateById(orderPrivateCar);
 
 
@@ -512,10 +602,10 @@
             this.updateById(orderPrivateCar);
             return true;//第一条数据不作处理,直接存储
         }
-        Map<String, String> distance = gdMapElectricFenceUtil.getDistance(now, old, 0);//直线距离
+        Map<String, Double> distance = GeodesyUtil.getDistance(now, old);//直线距离
         if(null != distance){
-            String distance1 = distance.get("distance");
-            if(Double.valueOf(distance1) > 50 && orderPrivateCar.getState()==5/* && orderPosition.getInsertTime().getTime()>=orderPrivateCar.getBoardingTime().getTime()*/){//大于50米表示在移动
+            Double distance1 = distance.get("WGS84");
+            if(distance1 > 50 && orderPrivateCar.getState()==5){//大于50米表示在移动
                 orderPrivateCar.setMileage(new BigDecimal(orderPrivateCar.getMileage()).add(new BigDecimal(distance1)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                 this.updateById(orderPrivateCar);
                 return true;
@@ -756,4 +846,11 @@
     public List<OrderPrivateCar> getSmsOrderList() {
         return this.baseMapper.getSmsOrderList();
     }
+
+    @Override
+    public void getTrackId(Integer orderId, Integer trackId) {
+        OrderPrivateCar orderPrivateCar = this.selectById(orderId);
+        orderPrivateCar.setTrackId(String.valueOf(trackId));
+        this.updateById(orderPrivateCar);
+    }
 }

--
Gitblit v1.7.1