From 13b1838cfd7df8c49450e2529943a0f87fbd0354 Mon Sep 17 00:00:00 2001
From: xuhy <3313886187@qq.com>
Date: 星期四, 19 六月 2025 18:01:55 +0800
Subject: [PATCH] 推单距离优化

---
 UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java |  907 +++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 594 insertions(+), 313 deletions(-)

diff --git a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java
index 6df50ec..7cc99cb 100644
--- a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java
+++ b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java
@@ -16,6 +16,7 @@
 import com.stylefeng.guns.modular.system.dao.*;
 import com.stylefeng.guns.modular.system.model.*;
 import com.stylefeng.guns.modular.system.service.*;
+import com.stylefeng.guns.modular.system.service.impl.OrderPositionServiceImpl;
 import com.stylefeng.guns.modular.system.util.*;
 import com.stylefeng.guns.modular.system.warpper.BaseWarpper;
 import com.stylefeng.guns.modular.system.warpper.EndPushWarpper;
@@ -33,6 +34,7 @@
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
@@ -43,7 +45,13 @@
 import java.io.FileInputStream;
 import java.io.InputStreamReader;
 import java.math.BigDecimal;
+import java.math.MathContext;
+import java.math.RoundingMode;
+import java.text.SimpleDateFormat;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
 import java.util.*;
+import java.util.stream.Collectors;
 
 
 @Service
@@ -118,8 +126,8 @@
     @Autowired
     private ITransactionDetailsService transactionDetailsService;
 
-    @Autowired
-    private ChinaMobileUtil chinaMobileUtil;
+//    @Autowired
+//    private ChinaMobileUtil chinaMobileUtil;
 
     @Autowired
     private IIncomeService incomeService;
@@ -160,11 +168,8 @@
     @Autowired
     private IAssignOrderService assignOrderService;
 
-    @Autowired
-    private ALiSendSms aLiSendSms;
-
-    @Autowired
-    private SMSUtil smsUtil;
+//    @Autowired
+//    private ALiSendSms aLiSendSms;
 
 
     @Value("${filePath}")
@@ -315,18 +320,19 @@
             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> geocode1 = gdMapGeocodingUtil.geocode(orderPrivateCar.getStartLon().toString(), orderPrivateCar.getStartLat().toString());
             Region region = regionMapper.query(geocode1.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"));
-            }
+//            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"));
+//            }
 
             driver.setState(3);
             driverService.updateById(driver);
@@ -356,67 +362,113 @@
             }
             AssignOrder assignOrder = assignOrderService.selectOne(new EntityWrapper<AssignOrder>().eq("companyId", query.getId()).eq("type", 1));
             if(null != assignOrder){//配置了指派规则才处理
-                //获取空闲司机
-                List<Driver> drivers = driverService.queryIdleDriver(1, orderPrivateCar.getServerCarModelId(), orderPrivateCar.getStartLon(), orderPrivateCar.getStartLat(), assignOrder.getDistance(), null);//获取范围内空闲司机
-                if(drivers.size() > 0){//有司机,直接指派给司机
-                    Driver dr = null;
-                    if(drivers.size() > 1){
-                        List<Driver> ds = new ArrayList<>();//存储直线距离最近的几个司机
-                        for(int i = 0; i < assignOrder.getPeople(); i++){
-                            Driver driver = null;
-                            int m = 0;
-                            int index = 0;
-                            for(int j = 0; j < drivers.size(); j++){
-                                String value = redisUtil.getValue("DRIVER" + drivers.get(j).getId());
-                                if(null != value){
+                boolean lock = redisUtil.lock("order_lock", 10);
+                if(!lock){
+                    int num1 = 1;
+                    while (num1 <= 10){
+                        Thread.sleep(3000);//等待3秒
+                        lock = redisUtil.lock("order_lock", 10);
+                        if(lock){
+                            break;
+                        }else{
+                            num1++;
+                        }
+                    }
+                }
+                try {
+                    //获取空闲司机
+                    List<Driver> drivers = driverService.queryIdleDriver(1, orderPrivateCar.getServerCarModelId(), orderPrivateCar.getStartLon(), orderPrivateCar.getStartLat(), assignOrder.getDistance(), null);//获取范围内空闲司机
+                    List<Integer> driverIds = drivers.stream().map(Driver::getId).collect(Collectors.toList());
+                    if(drivers.size() > 0) {//有司机,直接指派给司机
+                        Driver dr = null;
+                        if (drivers.size() > 1) {
+                            List<Driver> ds = new ArrayList<>();//存储直线距离最近的几个司机
+                            for (int i = 0; i < assignOrder.getPeople(); i++) {
+                                Driver driver = null;
+                                int m = 0;
+                                int index = 0;
+                                // 查询预约单
+                                List<OrderPrivateCar> orderPrivateCarss = orderPrivateCarMapper.queryOrderByState(driverIds, 2, 1, 2, 3, 4, 5, 6, 11);
+                                List<Integer> orderPrivateCarDriverIds = orderPrivateCars.stream().map(OrderPrivateCar::getDriverId).collect(Collectors.toList());
+                                for (int j = 0; j < drivers.size(); j++) {
+                                    Driver driver1 = drivers.get(j);
+                                    if (orderPrivateCar.getOrderType() == 2 && orderPrivateCarDriverIds.contains(driver.getId())) {
+                                        continue;
+                                    }
+                                    // 判断该司机是否有30分钟内预约单
+                                    long count = orderPrivateCarss.stream().filter(s -> s.getDriverId().equals(driver1.getId()) &&
+                                            DateUtil.dateToLocalDateTime(s.getTravelTime()).minusMinutes(30).isBefore(LocalDateTime.now())).count();
+                                    if (count > 0) {
+                                        continue;
+                                    }
+                                    String value = redisUtil.getValue("DRIVER" + drivers.get(j).getId());
+                                    if (null != value) {
 //                                Map<String, String> distance1 = gdMapElectricFenceUtil.getDistance(orderPrivateCar.getStartLon() + "," + orderPrivateCar.getStartLat(), value, 0);//计算距离
 //                                Integer d = Integer.valueOf(distance1.get("distance"));
-                                    String[] split = value.split(",");
-                                    double distance = GDMapGeocodingUtil.getDistance(orderPrivateCar.getStartLat(), orderPrivateCar.getStartLon(), Double.valueOf(split[1]), Double.valueOf(split[0]));
-                                    int d = Double.valueOf(distance).intValue();
-                                    if((0 == m && null == driver) || (d < m)){
-                                        driver = drivers.get(j);
+                                        String[] split = value.split(",");
+                                        double distance = GDMapGeocodingUtil.getDistance(orderPrivateCar.getStartLat(), orderPrivateCar.getStartLon(), Double.valueOf(split[1]), Double.valueOf(split[0]));
+                                        int d = Double.valueOf(distance).intValue();
+                                        if ((0 == m && null == driver) || (d < m)) {
+                                            driver = drivers.get(j);
+                                            m = d;
+                                            index = j;
+                                        }
+                                    }
+                                }
+                                ds.add(driver);
+                                drivers.remove(index);
+                            }
+    
+                            //再根据直线距离最短的司机中找出行驶距离最短的司机
+                            Integer m = 0;
+                            for (Driver driver : ds) {
+                                String value = redisUtil.getValue("DRIVER" + driver.getId());
+                                if (null != value) {
+                                    Map<String, String> distance1 = gdMapElectricFenceUtil.getDistance(orderPrivateCar.getStartLon() + "," + orderPrivateCar.getStartLat(), value, 1);//计算距离
+                                    Integer d = Integer.valueOf(distance1.get("distance"));
+                                    if ((0 == m && null == dr) || (d.intValue() < m.intValue())) {
+                                        dr = driver;
                                         m = d;
-                                        index = j;
                                     }
                                 }
                             }
-                            ds.add(driver);
-                            drivers.remove(index);
-                        }
-
-                        //再根据直线距离最短的司机中找出行驶距离最短的司机
-                        Integer m = 0;
-                        for(Driver driver : ds){
-                            String value = redisUtil.getValue("DRIVER" + driver.getId());
-                            if(null != value){
-                                Map<String, String> distance1 = gdMapElectricFenceUtil.getDistance(orderPrivateCar.getStartLon() + "," + orderPrivateCar.getStartLat(), value, 1);//计算距离
-                                Integer d = Integer.valueOf(distance1.get("distance"));
-                                if((0 == m && null == dr) || (d.intValue() < m.intValue())){
-                                    dr = driver;
-                                    m = d;
-                                }
+                        } else {
+                            List<OrderPrivateCar> orderPrivateCarss = orderPrivateCarMapper.queryOrderByState(driverIds, 2, 1, 2, 3, 4, 5, 6, 11);
+                            if (orderPrivateCarss.size() == 0) {
+                                dr = drivers.get(0);
                             }
                         }
-                    }else{
-                        dr = drivers.get(0);
-                    }
-
-                    orderPrivateCar.setDriverId(dr.getId());
-                    orderPrivateCar.setCompanyId(dr.getFranchiseeId() != null && dr.getFranchiseeId() != 0 ? dr.getFranchiseeId() : (
-                            dr.getCompanyId() != null && dr.getCompanyId() != 0 ? dr.getCompanyId() : 1));
-                    orderPrivateCar.setState(2);
-                    orderPrivateCar.setCarId(dr.getCarId());
-                    CarService query1 = carServiceMapper.query(1, dr.getCarId());
-                    orderPrivateCar.setServerCarModelId(query1.getServerCarModelId());
-                    orderPrivateCar.setSnatchOrderTime(new Date());
-
-                    //调用高德创建轨迹
+    
+                        if (Objects.nonNull(dr)) {
+                            orderPrivateCar.setDriverId(dr.getId());
+                            orderPrivateCar.setCompanyId(dr.getFranchiseeId() != null && dr.getFranchiseeId() != 0 ? dr.getFranchiseeId() : (
+                                    dr.getCompanyId() != null && dr.getCompanyId() != 0 ? dr.getCompanyId() : 1));
+                            orderPrivateCar.setState(2);
+                            orderPrivateCar.setCarId(dr.getCarId());
+                            CarService query1 = carServiceMapper.query(1, dr.getCarId());
+                            orderPrivateCar.setServerCarModelId(query1.getServerCarModelId());
+                            orderPrivateCar.setSnatchOrderTime(new Date());
+                            if (orderPrivateCar.getOrderType() != 2) {
+                                dr.setState(3);
+                            }
+                            driverService.updateById(dr);
+                            new Thread(new Runnable() {
+                                @Override
+                                public void run() {
+                                    pushUtil.pushOrderState(1, uid, orderPrivateCar.getId(), 1, 2, 0);
+                                    pushUtil.pushOrderState(2, orderPrivateCar.getDriverId(), orderPrivateCar.getId(), 1, 2, 0);
+                                    pushUtil.pushDriverPosition(orderPrivateCar.getId(), 1);
+                                }
+                            }).start();
+                        }
+    
+    
+                        //调用高德创建轨迹
 //                    String s = gdFalconUtil.selectTerminal(dr.getPhone());
 //                    String track = gdFalconUtil.createTrack(s);
 //                    orderPrivateCar.setTrackId(track);
-
-                    //调用移动的小号接口
+    
+                        //调用移动的小号接口
                /* Map<String, String> geocode1 = gdMapGeocodingUtil.geocode(orderPrivateCar.getStartLon().toString(), orderPrivateCar.getStartLat().toString());
                 Region region = regionMapper.query(geocode1.get("districtCode"));*/
 //                    Map<String, String> map = chinaMobileUtil.midAxbBindSend(orderPrivateCar.getPassengersPhone(), dr.getPhone(), null);
@@ -424,21 +476,16 @@
 //                        orderPrivateCar.setTelX(map.get("telX"));
 //                        orderPrivateCar.setBindId(map.get("bindId"));
 //                    }
-
-                    dr.setState(3);
-                    driverService.updateById(dr);
-                    this.updateById(orderPrivateCar);
-
-                    //发送短信给司机
+    
+                        this.updateById(orderPrivateCar);
+    
+                        //发送短信给司机
 //                    aLiSendSms.sendSms(dr.getPhone(), "SMS_216832951", "{\"" + orderPrivateCar.getStartAddress() + "\"}");
-                    new Thread(new Runnable() {
-                        @Override
-                        public void run() {
-                            pushUtil.pushOrderState(1, uid, orderPrivateCar.getId(), 1, 2, 0);
-                            pushUtil.pushOrderState(2, orderPrivateCar.getDriverId(), orderPrivateCar.getId(), 1, 2, 0);
-                            pushUtil.pushDriverPosition(orderPrivateCar.getId(), 1);
-                        }
-                    }).start();
+                    }
+                }catch (Exception e){
+                    e.printStackTrace();
+                }finally {
+                    redisUtil.unlock("order_lock");
                 }
             }
         }
@@ -516,8 +563,8 @@
         }
         double amount = 0;
         CancleOrder query = cancleOrderService.query(orderPrivateCar.getOrderType(), 1, orderPrivateCar.getCompanyId());
-        if(null != orderPrivateCar.getDriverId() && orderPrivateCar.getArriveTime()!=null &&
-                (orderPrivateCar.getArriveTime().getTime() + query.getMinuteNum() * 60 * 1000) < new Date().getTime()){//收费的情况
+        if(null != orderPrivateCar.getDriverId() && orderPrivateCar.getSnatchOrderTime()!=null &&
+                (orderPrivateCar.getSnatchOrderTime().getTime() + query.getMinuteNum() * 60 * 1000) < new Date().getTime()){//收费的情况
             if(null != query){
                 amount += query.getMoney();
             }
@@ -645,14 +692,62 @@
 
         CancleOrder query = cancleOrderService.query(orderPrivateCar.getOrderType(), 1, orderPrivateCar.getCompanyId());
         if(null != query){
+            String code = id + "_1_" + UUIDUtil.getRandomCode(3);
             if(payType == 1){//微信支付
+                if(ToolUtil.isEmpty(userInfo.getAppletsOpenId())){
+                    return ResultUtil.error("未授权微信,无法完成支付");
+                }
                 orderCancel.setPayType(1);
                 orderCancelService.updateById(orderCancel);
-                resultUtil =  payMoneyUtil.weixinpay("订单取消",id +"",id + "_1_fei",query.getMoney()+"","/base/wxCancelOrderTaxi","JSAPI",userInfo.getAppletsOpenId());
-                //Map<String, String> map = icbcPayUtil.placeAnOrder(id + ",1", 9, 5, uid.toString(), "订单取消", query.getMoney(), callbackPath + "/base/wxCancelOrderTaxi", "", type, userInfo.getAppletsOpenId());
+                resultUtil =  payMoneyUtil.weixinpay("订单取消",id +"",code,query.getMoney()+"","/base/wxCancelOrderTaxi","JSAPI",userInfo.getAppletsOpenId());
                 if(resultUtil.getCode()==200){
-                    paymentRecordService.saveData(1, null, null, id, 1, 1, query.getMoney(), null, 1);//添加预支付数据
-                    resultUtil = resultUtil;
+                    paymentRecordService.saveData(1, null, null, id, 1, 1, query.getMoney(), null, 1, code);//添加预支付数据
+                    new Thread(()->{
+                        int num = 1;
+                        int min = 5000;
+                        int w = 0;
+                        while (num <= 10) {
+                            try {
+                                w += min * num;
+                                OrderPrivateCar orderPrivateCar1 = orderPrivateCarMapper.selectById(id);
+                                if (orderPrivateCar1.getState() == 10) {
+                                    break;
+                                }
+                                ResultUtil<Map<String, Object>> resultUtil1 = payMoneyUtil.queryWXOrder("", code, "JSAPI");
+                                if (resultUtil1.getCode() == 200) {
+                                    /**
+                                     * SUCCESS--支付成功
+                                     * REFUND--转入退款
+                                     * NOTPAY--未支付
+                                     * CLOSED--已关闭
+                                     * REVOKED--已撤销(刷卡支付)
+                                     * USERPAYING--用户支付中
+                                     * PAYERROR--支付失败(其他原因,如银行返回失败)
+                                     * ACCEPT--已接收,等待扣款
+                                     */
+                                    String s = resultUtil1.getData().get("trade_state").toString();
+                                    if ("REFUND".equals(s) || "CLOSED".equals(s) || "REVOKED".equals(s) || "PAYERROR".equals(s) || num == 10) {
+                                        break;
+                                    }
+                                    if ("SUCCESS".equals(s) || "ACCEPT".equals(s)) {
+                                        String transaction_id = resultUtil1.getData().get("transaction_id").toString();
+                                        payCancelOrderPrivateCar(id, transaction_id, 1);
+                                        break;
+                                    }
+                                    if ("NOTPAY".equals(s) || "USERPAYING".equals(s)) {
+                                        Thread.sleep(w);
+                                        num++;
+                                    }
+                                } else {
+                                    Thread.sleep(w);
+                                    num++;
+                                }
+                            }catch (Exception e){
+                                e.printStackTrace();
+                            }
+                        }
+                    }).start();
+
                 }else{
                     resultUtil = ResultUtil.error("支付失败", "");
                 }
@@ -661,9 +756,9 @@
             if(payType == 2){//支付宝支付
                 orderCancel.setPayType(2);
                 orderCancelService.updateById(orderCancel);
-                resultUtil =  payMoneyUtil.alipay("订单取消","订单取消",id + "_1_fei",query.getMoney()+"","/base/aliCancelOrderTaxi");
+                resultUtil =  payMoneyUtil.alipay("订单取消","订单取消", "",code,query.getMoney()+"","/base/aliCancelOrderTaxi");
                 if(resultUtil.getCode()==200){
-                    paymentRecordService.saveData(1, null, null, id, 1, 2, query.getMoney(), null, 1);//添加预支付数据
+                    paymentRecordService.saveData(1, null, null, id, 1, 2, query.getMoney(), null, 1, code);//添加预支付数据
                     resultUtil = resultUtil;
                 }else{
                     resultUtil = ResultUtil.error("支付失败", "");
@@ -680,9 +775,9 @@
                 userInfoService.updateById(userInfo);
 
                 //解除小号绑定
-                if(orderPrivateCar.getBindId() != null){
-                    chinaMobileUtil.midAxbUnBindSend(orderPrivateCar.getBindId(),orderPrivateCar.getTelX());
-                }
+//                if(orderPrivateCar.getBindId() != null){
+//                    chinaMobileUtil.midAxbUnBindSend(orderPrivateCar.getBindId(),orderPrivateCar.getTelX());
+//                }
 
                 orderPrivateCar.setState(10);
                 orderPrivateCar.setTelX("");
@@ -746,10 +841,24 @@
                         //获取空闲司机
                         List<Driver> list = driverService.queryIdleDriver(1, orderPrivateCar.getServerCarModelId(), orderPrivateCar.getStartLon(), orderPrivateCar.getStartLat(), pushOrder.getPushDistance(), null);//所有附近空闲司机
                         if(list.size() > 0){
+                            // 查询预约单
+                            List<Integer> driverIds = list.stream().map(Driver::getId).collect(Collectors.toList());
+                            List<OrderPrivateCar> orderPrivateCars = orderPrivateCarMapper.queryOrderByState(driverIds, 2, 1, 2, 3, 4, 5, 6,  11);
+                            List<Integer> orderPrivateCarDriverIds = orderPrivateCars.stream().map(OrderPrivateCar::getDriverId).collect(Collectors.toList());
                             double driverProportion = pushOrder.getDriverProportion() / 100;//推送占比计算成小数
                             int lastIndex = Double.valueOf(list.size() * driverProportion).intValue();//计算占比转成整数(下标截取)
                             list = list.subList(0, lastIndex);//获取空闲司机中占比数据
                             for(Driver driver : list){//开始进行推送
+                                // 判断该司机是否有30分钟内预约单
+                                long count = orderPrivateCars.stream().filter(orderPrivateCar1 -> driver.getId().equals(orderPrivateCar1.getDriverId())
+                                        && DateUtil.dateToLocalDateTime(orderPrivateCar1.getTravelTime()).minusMinutes(31).isBefore(LocalDateTime.now())
+                                        && DateUtil.dateToLocalDateTime(orderPrivateCar1.getTravelTime()).isAfter(LocalDateTime.now())).count();
+                                if(orderPrivateCar.getOrderType() == 2 && orderPrivateCarDriverIds.contains(driver.getId())){
+                                    continue;
+                                }
+                                if(count > 0){
+                                    continue;
+                                }
                                 boolean bo = false;
                                 for(Integer integer : integers){
                                     if(integer.compareTo(driver.getId()) == 0){
@@ -814,70 +923,8 @@
         UserInfo userInfo = userInfoService.selectById(uid);
         map.put("balance", userInfo.getBalance());
         OrderPrivateCar orderPrivateCar = this.selectById(orderId);
-        int i = userCouponRecordService.queryAvailable(uid, orderPrivateCar.getCompanyId(), 1, 1, orderPrivateCar.getOrderMoney());
-        i = i + userCouponRecordService.queryAvailable(uid, orderPrivateCar.getCompanyId(), 1, 0, orderPrivateCar.getOrderMoney());
-        map.put("coupon", i);
-        return map;
-    }
-
-    @Override
-    public List<Map<String, Object>> queryCoupon(Integer orderId, Integer uid, Integer pageNum, Integer size) throws Exception {
-        OrderPrivateCar orderPrivateCar = this.selectById(orderId);
-        List<Map<String, Object>> list = userCouponRecordService.queryCoupon(uid, orderPrivateCar.getCompanyId(), 1, 1, orderPrivateCar.getOrderMoney(), pageNum, size);
-        List<Map<String, Object>> list1 = userCouponRecordService.queryCoupon(uid, orderPrivateCar.getCompanyId(), 1, 0, orderPrivateCar.getOrderMoney(), pageNum, size);
-        list.addAll(list1);
-        return list;
-    }
-
-    @Override
-    public ResultUtil payPrivateCarOrder(Integer payType, Integer orderId, Integer couponId, Integer type) throws Exception {
-        OrderPrivateCar orderPrivateCar = this.selectById(orderId);
-        if(orderPrivateCar.getState() == 8 || orderPrivateCar.getState() == 9){
-            return ResultUtil.error("订单已完成支付,不允许重复支付", "");
-        }
-        if(orderPrivateCar.getState() != 7){
-            return ResultUtil.error("订单不在待支付状态,不允许支付", "");
-        }
-        Integer uid = orderPrivateCar.getUserId();
         Double orderMoney = orderPrivateCar.getOrderMoney();
-        UserInfo userInfo = userInfoService.selectById(uid);
-        ResultUtil resultUtil = ResultUtil.success(new HashMap<>());
-        orderPrivateCar.setCouponMoney(0D);//初始化历史数据
-        orderPrivateCar.setCouponId(null);
-
-        //计算优惠券
-        UserCouponRecord userCouponRecord = null;
-        if(null != couponId){
-            userCouponRecord = userCouponRecordService.selectById(couponId);
-            if(userCouponRecord.getCompanyId() != orderPrivateCar.getCompanyId()){
-                return ResultUtil.error("优惠券不能用于此订单", "");
-            }
-            if(userCouponRecord.getState() == 2){
-                return ResultUtil.error("优惠券已使用", "");
-            }
-            if(userCouponRecord.getState() == 3){
-                return ResultUtil.error("优惠券已过期", "");
-            }
-            if(userCouponRecord.getCouponUseType() != 0 && userCouponRecord.getCouponUseType() != 1){
-                return ResultUtil.error("优惠券不能用于此类型订单", "");
-            }
-            if(userCouponRecord.getCouponType() == 2 && orderMoney.compareTo(userCouponRecord.getFullMoney()) < 0){
-                return ResultUtil.error("优惠券不能用于此订单", "");
-            }
-            orderMoney = orderMoney - userCouponRecord.getMoney();
-            orderPrivateCar.setCouponMoney(userCouponRecord.getMoney());
-            orderPrivateCar.setCouponId(couponId);
-        }
-        orderMoney=new BigDecimal(orderMoney).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
-
-        //计算红包
-        UserRedPacketRecord query = userRedPacketRecordService.query(uid, orderPrivateCar.getCompanyId(), 1, 1, orderMoney);
-        if(null != query && query.getMoney().compareTo(orderMoney) < 0){
-            orderMoney = orderMoney - query.getMoney();
-            orderPrivateCar.setRedPacketMoney(query.getMoney());
-            orderPrivateCar.setRedPacketId(query.getId());
-        }
-       //计算折扣
+        //计算折扣
         UserActivityDiscount1 query2 = userActivityDiscount1Mapper.query(orderPrivateCar.getCompanyId());
         if(null != query2){
             Integer orderNum=this.selectCount(new EntityWrapper<OrderPrivateCar>().eq("userId",orderPrivateCar.getUserId()).eq("activityId",query2.getId()).last(" and to_days(getoffTime) = to_days(now())"));
@@ -893,107 +940,313 @@
             }
 
         }
+        int i = userCouponRecordService.queryAvailable(uid, orderPrivateCar.getCompanyId(), 1, 1, orderMoney);
+        i = i + userCouponRecordService.queryAvailable(uid, orderPrivateCar.getCompanyId(), 1, 0, orderMoney);
+        map.put("coupon", i);
+        return map;
+    }
 
-        if(payType == 1){//微信支付
-            //Map<String, String> map = icbcPayUtil.placeAnOrder(orderId + ",1", 9, 5, uid.toString(), "完成订单", orderMoney, callbackPath + "/base/wxPayOrderTaxi", "", type, userInfo.getAppletsOpenId());
-            resultUtil =  payMoneyUtil.weixinpay("完成订单",orderId +"",orderId + "_1_fei",orderMoney+"","/base/wxPayOrderTaxi","JSAPI",userInfo.getAppletsOpenId());
-
-            if(resultUtil.getCode()==200){
-                paymentRecordService.saveData(1, null, null, orderId, 1, 1, orderMoney, null, 1);//添加预支付数据
-                resultUtil = resultUtil;
-            }else{
-                resultUtil = ResultUtil.error("支付失败", "");
+    @Override
+    public List<Map<String, Object>> queryCoupon(Integer orderId, Integer uid, Integer pageNum, Integer size) throws Exception {
+        OrderPrivateCar orderPrivateCar = this.selectById(orderId);
+        Double orderMoney = orderPrivateCar.getOrderMoney();
+        //计算折扣
+        UserActivityDiscount1 query2 = userActivityDiscount1Mapper.query(orderPrivateCar.getCompanyId());
+        if(null != query2){
+            Integer orderNum=this.selectCount(new EntityWrapper<OrderPrivateCar>().eq("userId",orderPrivateCar.getUserId()).eq("activityId",query2.getId()).last(" and to_days(getoffTime) = to_days(now())"));
+            if(query2.getDistance()*1000>orderPrivateCar.getMileage() && query2.getOrderNum()>orderNum){
+                Double special = query2.getSpecial();
+                orderPrivateCar.setDiscount(special);
+                double v = new BigDecimal(orderMoney).multiply(new BigDecimal(special / 10)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+                if(orderMoney.compareTo(v) > 0){
+                    orderPrivateCar.setDiscountMoney(orderMoney - v);
+                    orderPrivateCar.setActivityId(query2.getId());
+                    orderMoney = v;
+                }
             }
 
         }
-        if(payType == 2) {//支付宝支付
-            resultUtil =  payMoneyUtil.alipay("完成订单","完成订单",orderId + "_1_fei",orderMoney+"","/base/aliPayOrderTaxi");
-            //Map<String, String> map = icbcPayUtil.placeAnOrder(orderId + ",1", 10, 5, uid.toString(), "完成订单", orderMoney, callbackPath + "/base/aliPayOrderTaxi", "", type, null);
-            if (resultUtil.getCode()==200) {
-                paymentRecordService.saveData(1, null, null, orderId, 1, 2, orderMoney, null, 1);//添加预支付数据
-                resultUtil = resultUtil;
-            } else {
-                resultUtil = ResultUtil.error("支付失败", "");
+        List<Map<String, Object>> list = userCouponRecordService.queryCoupon(uid, orderPrivateCar.getCompanyId(), 1, 1, orderMoney, pageNum, size);
+        List<Map<String, Object>> list1 = userCouponRecordService.queryCoupon(uid, orderPrivateCar.getCompanyId(), 1, 0, orderMoney, pageNum, size);
+        list.addAll(list1);
+        return list;
+    }
+    
+    
+    
+
+    @Override
+    public ResultUtil payPrivateCarOrder(Integer payType, Integer orderId, Integer couponId, Integer type) throws Exception {
+        boolean lock = redisUtil.lock(orderId + "_payment", 10);
+        if(!lock){
+            int num1 = 1;
+            while (num1 <= 10){
+                Thread.sleep(3000);//等待3秒
+                lock = redisUtil.lock(orderId + "_payment", 10);
+                if(lock){
+                    break;
+                }else{
+                    num1++;
+                }
             }
         }
-        if(payType == 3){//余额支付
-            if(userInfo.getBalance() == null || userInfo.getBalance() < orderMoney){
-                return ResultUtil.error("余额不足,无法完成支付", "");
+        try {
+            OrderPrivateCar orderPrivateCar = this.selectById(orderId);
+            if(orderPrivateCar.getState() == 8 || orderPrivateCar.getState() == 9){
+                return ResultUtil.error("订单已完成支付,不允许重复支付", "");
             }
-
-            userInfo.setBalance(new BigDecimal(userInfo.getBalance()).subtract(new BigDecimal(orderMoney)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-
-            SysIntegral query1 = sysIntegralMapper.query(orderPrivateCar.getCompanyId());
-            userInfo.setIntegral(userInfo.getIntegral() + (orderMoney.intValue() * query1.getIntegral()));//积分
-
-            //添加交易明细
-            transactionDetailsService.saveData(uid, "完成订单", orderMoney, 2, 1, 1, 1, orderId);
-            userInfoService.updateById(userInfo);
-
-            orderPrivateCar.setState(8);
-            orderPrivateCar.setPayType(3);
-            orderPrivateCar.setPayMoney(orderMoney);
-            orderPrivateCar.setOrderMoney(orderPrivateCar.getOrderMoney()+orderPrivateCar.getFreeMoney().doubleValue());
-
-            //处理优惠券和红包
-            if(null != userCouponRecord){
-                userCouponRecord.setState(2);
-                userCouponRecord.setEndTime(new Date());
-                userCouponRecordService.updateById(userCouponRecord);
+            if(orderPrivateCar.getState() != 7){
+                return ResultUtil.error("订单不在待支付状态,不允许支付", "");
             }
-            if(null != query){
-                query.setState(2);
-                query.setEndTime(new Date());
-                userRedPacketRecordService.updateById(query);
-            }
-            if(orderPrivateCar.getIsplatPay()==1){
-                //添加已收入明细
-                Company company = companyService.selectById(orderPrivateCar.getCompanyId());
-                Double speMoney = company.getSpeMoney();
-                BigDecimal d = null;//企业收入
-                BigDecimal c = null;//司机收入
-                if(company.getIsSpeFixedOrProportional() == 2){//固定
-                    d = new BigDecimal(speMoney);
-                    c = new BigDecimal(orderPrivateCar.getOrderMoney()).subtract(d);//只有出行金额参与抽成,其余归属司机
+            PaymentRecord query3 = paymentRecordService.query(1, null, null, orderId, 1, null, 1);
+            if(null != query3){
+                ResultUtil<Map<String, Object>> resultUtil1 = payMoneyUtil.queryWXOrder("", query3.getSerialNumber(), "JSAPI");
+                if (resultUtil1.getCode() == 200) {
+                    /**
+                     * SUCCESS--支付成功
+                     * REFUND--转入退款
+                     * NOTPAY--未支付
+                     * CLOSED--已关闭
+                     * REVOKED--已撤销(刷卡支付)
+                     * USERPAYING--用户支付中
+                     * PAYERROR--支付失败(其他原因,如银行返回失败)
+                     * ACCEPT--已接收,等待扣款
+                     */
+                    String result_code = resultUtil1.getData().get("result_code").toString();
+                    if("SUCCESS".equals(result_code)){
+                        String s = resultUtil1.getData().get("trade_state").toString();
+                        if ("REFUND".equals(s) || "CLOSED".equals(s) || "REVOKED".equals(s) || "PAYERROR".equals(s)) {
+                            payMoneyUtil.closeWXOrder(query3.getSerialNumber());
+                            paymentRecordService.deleteById(query3.getId());
+                        }
+                        if ("SUCCESS".equals(s) || "ACCEPT".equals(s)) {
+                            return ResultUtil.error("不允许重复支付");
+                        }
+                        if ("NOTPAY".equals(s) || "USERPAYING".equals(s)) {
+                            payMoneyUtil.closeWXOrder(query3.getSerialNumber());
+                            paymentRecordService.deleteById(query3.getId());
+                        }
+                    }
                 }
-                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);
-                    c = new BigDecimal(orderPrivateCar.getOrderMoney()).subtract(d).setScale(2, BigDecimal.ROUND_HALF_EVEN);
-                }
-                incomeService.saveData(1, orderPrivateCar.getCompanyId(), 2, orderPrivateCar.getId(), 1, d.doubleValue());
-                incomeService.saveData(2, orderPrivateCar.getDriverId(), 2, orderPrivateCar.getId(), 1, c.doubleValue());
-                Driver driver = driverService.selectById(orderPrivateCar.getDriverId());
-                driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-                driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-                driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-                driverService.updateById(driver);
             }
-
-
-            // TODO: 2020/5/24 这里需要给司机和用户推送订单状态
+    
+    
+    
+            Integer uid = orderPrivateCar.getUserId();
+            Double orderMoney = orderPrivateCar.getOrderMoney();
+            UserInfo userInfo = userInfoService.selectById(uid);
+            ResultUtil resultUtil = ResultUtil.success(new HashMap<>());
+            orderPrivateCar.setCouponMoney(0D);//初始化历史数据
+            orderPrivateCar.setCouponId(null);
+    
+            //计算优惠券
+            UserCouponRecord userCouponRecord = null;
+            if(null != couponId){
+                userCouponRecord = userCouponRecordService.selectById(couponId);
+                if(userCouponRecord.getCompanyId().compareTo(orderPrivateCar.getCompanyId()) != 0){
+                    return ResultUtil.error("优惠券不能用于此订单", "");
+                }
+                if(userCouponRecord.getState() == 2){
+                    return ResultUtil.error("优惠券已使用", "");
+                }
+                if(userCouponRecord.getState() == 3){
+                    return ResultUtil.error("优惠券已过期", "");
+                }
+                if(userCouponRecord.getCouponUseType() != 0 && userCouponRecord.getCouponUseType() != 1){
+                    return ResultUtil.error("优惠券不能用于此类型订单", "");
+                }
+                if(userCouponRecord.getCouponType() == 2 && orderMoney.compareTo(userCouponRecord.getFullMoney()) < 0){
+                    return ResultUtil.error("优惠券不能用于此订单", "");
+                }
+                orderMoney = orderMoney - userCouponRecord.getMoney();
+                orderPrivateCar.setCouponMoney(userCouponRecord.getMoney());
+                orderPrivateCar.setCouponId(couponId);
+            }
+            orderMoney=new BigDecimal(orderMoney).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+    
+            //计算红包
+            UserRedPacketRecord query = userRedPacketRecordService.query(uid, orderPrivateCar.getCompanyId(), 1, 1, orderMoney);
+            if(null != query && query.getMoney().compareTo(orderMoney) < 0){
+                orderMoney = orderMoney - query.getMoney();
+                orderPrivateCar.setRedPacketMoney(query.getMoney());
+                orderPrivateCar.setRedPacketId(query.getId());
+            }
+            //计算折扣
+            UserActivityDiscount1 query2 = userActivityDiscount1Mapper.query(orderPrivateCar.getCompanyId());
+            if(null != query2){
+                Integer orderNum=this.selectCount(new EntityWrapper<OrderPrivateCar>().eq("userId",orderPrivateCar.getUserId()).eq("activityId",query2.getId()).last(" and to_days(getoffTime) = to_days(now())"));
+                if(query2.getDistance()*1000>orderPrivateCar.getMileage() && query2.getOrderNum()>orderNum){
+                    Double special = query2.getSpecial();
+                    orderPrivateCar.setDiscount(special);
+                    double v = new BigDecimal(orderMoney).multiply(new BigDecimal(special / 10)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+                    if(orderMoney.compareTo(v) > 0){
+                        orderPrivateCar.setDiscountMoney(orderMoney - v);
+                        orderPrivateCar.setActivityId(query2.getId());
+                        orderMoney = v;
+                    }
+                }
+        
+            }
+            if(ToolUtil.isEmpty(userInfo.getAppletsOpenId())){
+                return ResultUtil.error("未授权微信,无法完成支付");
+            }
+            String code = orderId + "_1_" + UUIDUtil.getRandomCode(5);
+            if(payType == 1){//微信支付
+                resultUtil = payMoneyUtil.weixinpay("完成订单",orderId +"", code,orderMoney+"","/base/wxPayOrderTaxi","JSAPI",userInfo.getAppletsOpenId());
+                if(resultUtil.getCode()==200){
+                    paymentRecordService.saveData(1, orderPrivateCar.getUserId(), 1, orderId, 1, 1, orderMoney, null, 1, code);//添加预支付数据
+                    long millis = System.currentTimeMillis();
+                    new Thread(()->{
+                        long time = millis;
+                        try {
+                            Thread.sleep(5000);
+                        } catch (InterruptedException e) {
+                            throw new RuntimeException(e);
+                        }
+                        //订单创建后15分钟
+                        while ((System.currentTimeMillis() - time) <= 900000) {
+                            OrderPrivateCar orderPrivateCar1 = OrderPrivateCarServiceImpl.this.selectById(orderId);
+                            if(orderPrivateCar1.getState() != 7){
+                                break;
+                            }
+                            try {
+                                ResultUtil<Map<String, Object>> resultUtil2 = payMoneyUtil.queryWXOrder("", code, "JSAPI");
+                                if (resultUtil2.getCode() == 200) {
+                                    /**
+                                     * SUCCESS--支付成功
+                                     * REFUND--转入退款
+                                     * NOTPAY--未支付
+                                     * CLOSED--已关闭
+                                     * REVOKED--已撤销(刷卡支付)
+                                     * USERPAYING--用户支付中
+                                     * PAYERROR--支付失败(其他原因,如银行返回失败)
+                                     * ACCEPT--已接收,等待扣款
+                                     */
+                                    String s = resultUtil2.getData().get("trade_state").toString();
+                                    if ("REFUND".equals(s) || "CLOSED".equals(s) || "REVOKED".equals(s) || "PAYERROR".equals(s)) {
+                                        break;
+                                    }
+                                    if ("SUCCESS".equals(s) || "ACCEPT".equals(s)) {
+                                        String transaction_id = resultUtil2.getData().get("transaction_id").toString();
+                                        payOrderPrivateCarCallback(orderId, transaction_id, 1);
+                                        break;
+                                    }
+                                    if ("NOTPAY".equals(s) || "USERPAYING".equals(s)) {
+                                    }
+                                }
+                                Thread.sleep(5000);
+                            }catch (Exception e){
+                                e.printStackTrace();
+                            }
+                        }
+                
+                        OrderPrivateCar orderPrivateCar1 = OrderPrivateCarServiceImpl.this.selectById(orderId);
+                        if(7 == orderPrivateCar1.getState()){
+                            //关闭订单,不允许支付
+                            payMoneyUtil.closeWXOrder(code);
+                            PaymentRecord paymentRecord = paymentRecordService.selectOne(new EntityWrapper<PaymentRecord>().eq("serialNumber", code));
+                            if(null != paymentRecord){
+                                paymentRecordService.deleteById(paymentRecord.getId());
+                            }
+                        }
+                    }).start();
+                }else{
+                    resultUtil = ResultUtil.error("支付失败", "");
+                }
+        
+            }
+            if(payType == 2) {//支付宝支付
+                resultUtil =  payMoneyUtil.alipay("完成订单","完成订单", "",code,orderMoney+"","/base/aliPayOrderTaxi");
+                if (resultUtil.getCode()==200) {
+                    paymentRecordService.saveData(1, orderPrivateCar.getUserId(), 1, orderId, 1, 2, orderMoney, null, 1, code);//添加预支付数据
+                } else {
+                    resultUtil = ResultUtil.error("支付失败", "");
+                }
+            }
+            if(payType == 3){//余额支付
+                if(userInfo.getBalance() == null || userInfo.getBalance() < orderMoney){
+                    return ResultUtil.error("余额不足,无法完成支付", "");
+                }
+        
+                userInfo.setBalance(new BigDecimal(userInfo.getBalance()).subtract(new BigDecimal(orderMoney)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+        
+                SysIntegral query1 = sysIntegralMapper.query(orderPrivateCar.getCompanyId());
+                userInfo.setIntegral(userInfo.getIntegral() + (orderMoney.intValue() * query1.getIntegral()));//积分
+        
+                //添加交易明细
+                transactionDetailsService.saveData(uid, "完成订单", orderMoney, 2, 1, 1, 1, orderId);
+                userInfoService.updateById(userInfo);
+        
+                orderPrivateCar.setState(8);
+                orderPrivateCar.setPayType(3);
+                orderPrivateCar.setPayMoney(orderMoney);
+                orderPrivateCar.setOrderMoney(orderPrivateCar.getOrderMoney()+orderPrivateCar.getFreeMoney().doubleValue());
+        
+                //处理优惠券和红包
+                if(null != userCouponRecord){
+                    userCouponRecord.setState(2);
+                    userCouponRecord.setEndTime(new Date());
+                    userCouponRecordService.updateById(userCouponRecord);
+                }
+                if(null != query){
+                    query.setState(2);
+                    query.setEndTime(new Date());
+                    userRedPacketRecordService.updateById(query);
+                }
+                if(orderPrivateCar.getIsplatPay()==1){
+                    //添加已收入明细
+                    Company company = companyService.selectById(orderPrivateCar.getCompanyId());
+                    Double speMoney = company.getSpeMoney();
+                    BigDecimal d = null;//企业收入
+                    BigDecimal c = null;//司机收入
+                    if(company.getIsSpeFixedOrProportional() == 2){//固定
+                        d = new BigDecimal(speMoney);
+                        c = new BigDecimal(orderPrivateCar.getOrderMoney()).subtract(d);//只有出行金额参与抽成,其余归属司机
+                    }
+                    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), 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());
+                    incomeService.saveData(2, orderPrivateCar.getDriverId(), 2, orderPrivateCar.getId(), 1, c.doubleValue());
+                    Driver driver = driverService.selectById(orderPrivateCar.getDriverId());
+                    driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                    driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                    driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                    driverService.updateById(driver);
+                }
+        
+        
+                // TODO: 2020/5/24 这里需要给司机和用户推送订单状态
+                new Thread(new Runnable() {
+                    @Override
+                    public void run() {
+                        pushUtil.pushOrderState(1, orderPrivateCar.getUserId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
+                        pushUtil.pushOrderState(2, orderPrivateCar.getDriverId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
+                    }
+                }).start();
+        
+                systemNoticeService.addSystemNotice(1, "您已使用余额成功完成出行订单支付,谢谢使用!", uid, 1);
+            }
+    
+            this.updateAllColumnById(orderPrivateCar);
+    
             new Thread(new Runnable() {
                 @Override
                 public void run() {
-                    pushUtil.pushOrderState(1, orderPrivateCar.getUserId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
-                    pushUtil.pushOrderState(2, orderPrivateCar.getDriverId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
+                    if(pushMinistryOfTransport){//上传数据
+                        pushMinistryOfTransportUtil.operatePay(orderId);
+                    }
                 }
             }).start();
-
-            systemNoticeService.addSystemNotice(1, "您已使用余额成功完成出行订单支付,谢谢使用!", uid, 1);
+            return resultUtil;
+        }catch (Exception e){
+            e.printStackTrace();
+        }finally {
+            redisUtil.unlock(orderId + "_payment");
         }
-
-        this.updateAllColumnById(orderPrivateCar);
-
-        new Thread(new Runnable() {
-            @Override
-            public void run() {
-                if(pushMinistryOfTransport){//上传数据
-                    pushMinistryOfTransportUtil.operatePay(orderId);
-                }
-            }
-        }).start();
-        return resultUtil;
+        return ResultUtil.error("支付异常");
     }
 
     @Override
@@ -1085,15 +1338,18 @@
     @Override
     public void payCancelOrderPrivateCar(Integer id, String order_id, Integer type) throws Exception {
         OrderPrivateCar orderPrivateCar = this.selectById(id);
+        if(orderPrivateCar.getState() == 10){
+            return;
+        }
         PaymentRecord query = paymentRecordService.query(1, null, null, Integer.valueOf(id), 1, type, 1);
         if(null != query){
             //添加交易明细
             transactionDetailsService.saveData(orderPrivateCar.getUserId(), "取消订单", query.getAmount(), 2, 1, 1, 1, query.getOrderId());
             orderPrivateCar.setState(10);
             //解除小号绑定
-            if(orderPrivateCar.getBindId() != null){
-                chinaMobileUtil.midAxbUnBindSend(orderPrivateCar.getBindId(),orderPrivateCar.getTelX());
-            }
+//            if(orderPrivateCar.getBindId() != null){
+//                chinaMobileUtil.midAxbUnBindSend(orderPrivateCar.getBindId(),orderPrivateCar.getTelX());
+//            }
             orderPrivateCar.setBindId("");
             orderPrivateCar.setTelX("");
             this.updateById(orderPrivateCar);
@@ -1138,77 +1394,102 @@
     }
 
     @Override
-    public void payOrderPrivateCarCallback(Integer id, String order_id, Integer type) throws Exception {
-        OrderPrivateCar orderPrivateCar = this.selectById(id);
-        PaymentRecord query = paymentRecordService.query(1, null, null, Integer.valueOf(id), 1, type, 1);
-        if(null != query){
-            //添加交易明细
-            transactionDetailsService.saveData(orderPrivateCar.getUserId(), "完成订单", query.getAmount(), 2, 1, 1, 1, query.getOrderId());
-            orderPrivateCar.setState(8);
-            orderPrivateCar.setPayType(type);
-            orderPrivateCar.setOrderMoney(orderPrivateCar.getOrderMoney()+orderPrivateCar.getFreeMoney().doubleValue());
-            orderPrivateCar.setPayMoney(query.getAmount());
-            this.updateById(orderPrivateCar);
-
-            UserInfo userInfo = userInfoService.selectById(orderPrivateCar.getUserId());
-            SysIntegral query1 = sysIntegralMapper.query(orderPrivateCar.getCompanyId());
-            userInfo.setIntegral(userInfo.getIntegral() + (query.getAmount().intValue() * query1.getIntegral()));//积分
-            userInfoService.updateById(userInfo);
-
-            //处理优惠券和红包
-            if(null != orderPrivateCar.getCouponId()){
-                UserCouponRecord userCouponRecord = userCouponRecordService.selectById(orderPrivateCar.getCouponId());
-                userCouponRecord.setState(2);
-                userCouponRecord.setEndTime(new Date());
-                userCouponRecordService.updateById(userCouponRecord);
-            }
-            if(null != orderPrivateCar.getRedPacketId()){
-                UserRedPacketRecord userRedPacketRecord = userRedPacketRecordService.selectById(orderPrivateCar.getRedPacketId());
-                userRedPacketRecord.setState(2);
-                userRedPacketRecord.setEndTime(new Date());
-                userRedPacketRecordService.updateById(userRedPacketRecord);
-            }
-
-
-            query.setState(2);
-            query.setCode(order_id);
-            paymentRecordService.updateById(query);
-            if(orderPrivateCar.getIsplatPay()==1){
-                //添加已收入明细
-                Company company = companyService.selectById(orderPrivateCar.getCompanyId());
-                Double taxi = company.getSpeMoney();
-                BigDecimal d = null;//企业收入
-                BigDecimal c = null;//司机收入
-                if(company.getIsSpeFixedOrProportional() == 2){//固定
-                    d = new BigDecimal(taxi);
-                    c = new BigDecimal(orderPrivateCar.getOrderMoney()).subtract(d).setScale(2, BigDecimal.ROUND_HALF_EVEN);
+    @Transactional(rollbackFor = Exception.class)
+    public synchronized void payOrderPrivateCarCallback(Integer id, String order_id, Integer type) throws Exception {
+        boolean lock = redisUtil.lock(order_id + "_paymentCallback", 10);
+        if(!lock){
+            int num1 = 1;
+            while (num1 <= 10){
+                Thread.sleep(3000);//等待3秒
+                lock = redisUtil.lock(order_id + "_paymentCallback", 10);
+                if(lock){
+                    break;
+                }else{
+                    num1++;
                 }
-                if(company.getIsSpeFixedOrProportional() == 1){//比例
-                    Double price = orderPrivateCar.getStartMoney() + orderPrivateCar.getMileageMoney() + orderPrivateCar.getWaitMoney() + orderPrivateCar.getDurationMoney() + orderPrivateCar.getLongDistanceMoney();
-                    d = new BigDecimal(price).multiply(new BigDecimal(taxi).divide(new BigDecimal(100))).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());
-                incomeService.saveData(2, orderPrivateCar.getDriverId(), 2, orderPrivateCar.getId(), 1, c.doubleValue());
-                Driver driver = driverService.selectById(orderPrivateCar.getDriverId());
-                driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-                driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-                driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-                driverService.updateById(driver);
             }
-            // TODO: 2020/5/24 这里需要给司机和用户推送订单状态
-            new Thread(new Runnable() {
-                @Override
-                public void run() {
-                    pushUtil.pushOrderState(1, orderPrivateCar.getUserId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
-                    pushUtil.pushOrderState(2, orderPrivateCar.getDriverId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
+        }
+        
+        
+        try {
+            PaymentRecord query = paymentRecordService.query(1, null, null, Integer.valueOf(id), 1, type, null);
+            if(null != query){
+                //添加交易明细
+                OrderPrivateCar orderPrivateCar = this.selectById(id);
+                if(7 != orderPrivateCar.getState()){
+                    return;
                 }
-            }).start();
-
-
-            systemNoticeService.addSystemNotice(1, "您已使用" + (type == 1 ? "微信" : "支付宝") + "成功完成出行订单支付,谢谢使用!", orderPrivateCar.getUserId(), 1);
-        }else{
-            System.err.println("预支付数据异常(orderId = "  + id + ")");
+                transactionDetailsService.saveData(orderPrivateCar.getUserId(), "完成订单", query.getAmount(), 2, 1, 1, 1, query.getOrderId());
+                orderPrivateCar.setState(8);
+                orderPrivateCar.setPayType(type);
+                orderPrivateCar.setOrderMoney(orderPrivateCar.getOrderMoney()+orderPrivateCar.getFreeMoney().doubleValue());
+                orderPrivateCar.setPayMoney(query.getAmount());
+                this.updateById(orderPrivateCar);
+        
+                UserInfo userInfo = userInfoService.selectById(orderPrivateCar.getUserId());
+                SysIntegral query1 = sysIntegralMapper.query(orderPrivateCar.getCompanyId());
+                userInfo.setIntegral(userInfo.getIntegral() + (query.getAmount().intValue() * query1.getIntegral()));//积分
+                userInfoService.updateById(userInfo);
+        
+                //处理优惠券和红包
+                if(null != orderPrivateCar.getCouponId()){
+                    UserCouponRecord userCouponRecord = userCouponRecordService.selectById(orderPrivateCar.getCouponId());
+                    userCouponRecord.setState(2);
+                    userCouponRecord.setEndTime(new Date());
+                    userCouponRecordService.updateById(userCouponRecord);
+                }
+                if(null != orderPrivateCar.getRedPacketId()){
+                    UserRedPacketRecord userRedPacketRecord = userRedPacketRecordService.selectById(orderPrivateCar.getRedPacketId());
+                    userRedPacketRecord.setState(2);
+                    userRedPacketRecord.setEndTime(new Date());
+                    userRedPacketRecordService.updateById(userRedPacketRecord);
+                }
+        
+                query.setState(2);
+                query.setCode(order_id);
+                paymentRecordService.updateById(query);
+        
+                if(orderPrivateCar.getIsplatPay()==1){
+                    //添加已收入明细
+                    Company company = companyService.selectById(orderPrivateCar.getCompanyId());
+                    Double taxi = company.getSpeMoney();
+                    BigDecimal d = null;//企业收入
+                    BigDecimal c = null;//司机收入
+                    if(company.getIsSpeFixedOrProportional() == 2){//固定
+                        d = new BigDecimal(taxi);
+                        c = new BigDecimal(orderPrivateCar.getOrderMoney()).subtract(d).setScale(2, BigDecimal.ROUND_HALF_EVEN);
+                    }
+                    if(company.getIsSpeFixedOrProportional() == 1){//比例
+                        Double price = orderPrivateCar.getStartMoney() + orderPrivateCar.getMileageMoney() + orderPrivateCar.getWaitMoney() + orderPrivateCar.getDurationMoney() + orderPrivateCar.getLongDistanceMoney();
+                        d = new BigDecimal(price).multiply(new BigDecimal(taxi).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());
+                    incomeService.saveData(2, orderPrivateCar.getDriverId(), 2, orderPrivateCar.getId(), 1, c.doubleValue());
+                    Driver driver = driverService.selectById(orderPrivateCar.getDriverId());
+                    driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                    driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                    driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                    driverService.updateById(driver);
+                }
+                // TODO: 2020/5/24 这里需要给司机和用户推送订单状态
+                new Thread(new Runnable() {
+                    @Override
+                    public void run() {
+                        pushUtil.pushOrderState(1, orderPrivateCar.getUserId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
+                        pushUtil.pushOrderState(2, orderPrivateCar.getDriverId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
+                    }
+                }).start();
+        
+        
+                systemNoticeService.addSystemNotice(1, "您已使用" + (type == 1 ? "微信" : "支付宝") + "成功完成出行订单支付,谢谢使用!", orderPrivateCar.getUserId(), 1);
+            }else{
+                System.err.println("预支付数据异常(orderId = "  + id + ")");
+            }
+        }catch (Exception e){
+            e.printStackTrace();
+        }finally {
+            redisUtil.unlock(order_id + "_paymentCallback");
         }
     }
 
@@ -1310,8 +1591,8 @@
         if(null == distance){
             System.err.println("查询距离出错了");
         }else{
-            d = new BigDecimal(distance.get("distance")).divide(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString();
-            t = new BigDecimal(distance.get("duration")).divide(new BigDecimal(60)).setScale(2, BigDecimal.ROUND_HALF_EVEN).intValue() + "";
+            d = new BigDecimal(distance.get("distance")).divide(new BigDecimal(1000), new MathContext(2, RoundingMode.HALF_EVEN)).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString();
+            t = new BigDecimal(distance.get("duration")).divide(new BigDecimal(60), new MathContext(2, RoundingMode.HALF_EVEN)).setScale(2, BigDecimal.ROUND_HALF_EVEN).intValue() + "";
         }
         OrderServerWarpper orderServerWarpper = new OrderServerWarpper();
         orderServerWarpper.setOrderId(orderPrivateCar.getId());
@@ -1333,8 +1614,8 @@
             if(null == distance){
                 System.err.println("查询距离出错了");
             }else{
-                d = new BigDecimal(distance.get("distance")).divide(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString();
-                t = new BigDecimal(distance.get("duration")).divide(new BigDecimal(60)).setScale(2, BigDecimal.ROUND_HALF_EVEN).intValue() + "";
+                d = new BigDecimal(distance.get("distance")).divide(new BigDecimal(1000), new MathContext(2, RoundingMode.HALF_EVEN)).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString();
+                t = new BigDecimal(distance.get("duration")).divide(new BigDecimal(60), new MathContext(2, RoundingMode.HALF_EVEN)).setScale(2, BigDecimal.ROUND_HALF_EVEN).intValue() + "";
             }
             orderServerWarpper.setReservationMileage("0");
             orderServerWarpper.setReservationTime("0");

--
Gitblit v1.7.1