xuhy
2025-08-01 8e680f301f31c91391579213661be4af04810e8c
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;
@@ -45,7 +47,11 @@
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
@@ -201,7 +207,7 @@
     */
    @Override
    public synchronized ResultUtil<BaseWarpper> saveOrderPrivateCar(Integer serverCarModelId, Date travelTime, Integer reservation, Integer instead, String placementLon, String placementLat, String startLon, String startLat,
                                          String startAddress, String endLon, String endLat, String endAddress, String name, String phone, Integer orderSource, Integer driverId, Integer type, Integer uid) throws Exception {
                                          String startAddress, String endLon, String endLat, String endAddress, String name, String phone, Integer orderSource, Integer driverId, Integer type, Integer uid,BigDecimal estimatedPrice) throws Exception {
        //如果出行时间大于当前10分钟则默认为预约单
        if(travelTime.getTime() > (System.currentTimeMillis() + 600000)){
@@ -279,6 +285,7 @@
        orderPrivateCar.setTravelTime(travelTime);
        orderPrivateCar.setOrderType(reservation);
        orderPrivateCar.setOrderSource(orderSource);
        orderPrivateCar.setEstimatedPrice(estimatedPrice);
        if(orderSource == 2){//扫码下单
            Driver driver = driverService.selectById(driverId);
            if(null == driver){
@@ -356,67 +363,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(query.getLimitationTime()).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 +477,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");
                }
            }
        }
@@ -625,88 +673,150 @@
     */
    @Override
    public ResultUtil cancleOrderPrivateCar(Integer id, Integer payType, Integer cancleId, Integer type) throws Exception {
        OrderPrivateCar orderPrivateCar = this.selectById(id);
        Integer uid = orderPrivateCar.getUserId();
        UserInfo userInfo = userInfoService.selectById(uid);
        boolean lock = redisUtil.lock(id + "_cancleOrder", 10);
        if(!lock){
            int num1 = 1;
            while (num1 <= 10){
                Thread.sleep(3000);//等待3秒
                lock = redisUtil.lock(id + "_cancleOrder", 10);
                if(lock){
                    break;
                }else{
                    num1++;
                }
            }
        }
        ResultUtil resultUtil = ResultUtil.success("");
        if(null == orderPrivateCar){
            return ResultUtil.error("取消订单失败,订单信息有误", "");
        }
        if(orderPrivateCar.getState() != 12){
            return ResultUtil.error("取消订单失败,不合法的操作", "");
        }
        OrderCancel orderCancel = null;
        if(null == cancleId || cancleId==0){
            orderCancel = orderCancelService.query(id, 1, null, null, 1);
        }else{
            orderCancel = orderCancelService.selectById(cancleId);
        }
        CancleOrder query = cancleOrderService.query(orderPrivateCar.getOrderType(), 1, orderPrivateCar.getCompanyId());
        if(null != query){
            if(payType == 1){//微信支付
                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());
                if(resultUtil.getCode()==200){
                    paymentRecordService.saveData(1, null, null, id, 1, 1, query.getMoney(), null, 1);//添加预支付数据
                    resultUtil = resultUtil;
                }else{
                    resultUtil = ResultUtil.error("支付失败", "");
                }
        try {
            OrderPrivateCar orderPrivateCar = this.selectById(id);
            Integer uid = orderPrivateCar.getUserId();
            UserInfo userInfo = userInfoService.selectById(uid);
            if(null == orderPrivateCar){
                return ResultUtil.error("取消订单失败,订单信息有误", "");
            }
            if(payType == 2){//支付宝支付
                orderCancel.setPayType(2);
                orderCancelService.updateById(orderCancel);
                resultUtil =  payMoneyUtil.alipay("订单取消","订单取消", "",id + "_1_fei",query.getMoney()+"","/base/aliCancelOrderTaxi");
                if(resultUtil.getCode()==200){
                    paymentRecordService.saveData(1, null, null, id, 1, 2, query.getMoney(), null, 1);//添加预支付数据
                    resultUtil = resultUtil;
                }else{
                    resultUtil = ResultUtil.error("支付失败", "");
                }
            if(orderPrivateCar.getState() != 12){
                return ResultUtil.error("取消订单失败,不合法的操作", "");
            }
            if(payType == 3){//余额支付
                if(userInfo.getBalance() != null && userInfo.getBalance() < query.getMoney()){
                    return ResultUtil.error("余额不足,无法完成支付", "");
            OrderCancel orderCancel = null;
            if(null == cancleId || cancleId==0){
                orderCancel = orderCancelService.query(id, 1, null, null, 1);
            }else{
                orderCancel = orderCancelService.selectById(cancleId);
            }
            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 +"",code,query.getMoney()+"","/base/wxCancelOrderTaxi","JSAPI",userInfo.getAppletsOpenId());
                    if(resultUtil.getCode()==200){
                        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("支付失败", "");
                    }
                }
                userInfo.setBalance(new BigDecimal(userInfo.getBalance()).subtract(new BigDecimal(query.getMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                //添加交易明细
                transactionDetailsService.saveData(uid, "取消订单", query.getMoney(), 2, 1, 1, 1, id);
                userInfoService.updateById(userInfo);
                //解除小号绑定
                if(payType == 2){//支付宝支付
                    orderCancel.setPayType(2);
                    orderCancelService.updateById(orderCancel);
                    resultUtil =  payMoneyUtil.alipay("订单取消","订单取消", "",code,query.getMoney()+"","/base/aliCancelOrderTaxi");
                    if(resultUtil.getCode()==200){
                        paymentRecordService.saveData(1, null, null, id, 1, 2, query.getMoney(), null, 1, code);//添加预支付数据
                        resultUtil = resultUtil;
                    }else{
                        resultUtil = ResultUtil.error("支付失败", "");
                    }
                }
                if(payType == 3){//余额支付
                    if(userInfo.getBalance() != null && userInfo.getBalance() < query.getMoney()){
                        return ResultUtil.error("余额不足,无法完成支付", "");
                    }
                    userInfo.setBalance(new BigDecimal(userInfo.getBalance()).subtract(new BigDecimal(query.getMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                    //添加交易明细
                    transactionDetailsService.saveData(uid, "取消订单", query.getMoney(), 2, 1, 1, 1, id);
                    userInfoService.updateById(userInfo);
                    //解除小号绑定
//                if(orderPrivateCar.getBindId() != null){
//                    chinaMobileUtil.midAxbUnBindSend(orderPrivateCar.getBindId(),orderPrivateCar.getTelX());
//                }
                orderPrivateCar.setState(10);
                orderPrivateCar.setTelX("");
                orderPrivateCar.setBindId("");
                this.updateById(orderPrivateCar);
                orderCancel.setState(2);
                orderCancel.setPayType(3);
                orderCancelService.updateById(orderCancel);
                //添加已收入明细
                incomeService.saveData(1, orderPrivateCar.getCompanyId(), 3, orderPrivateCar.getId(), 1, query.getMoney());
                //添加已收入明细
                Company company = companyService.selectById(orderPrivateCar.getCompanyId());
                Double taxi = company.getSpeMoney();
                BigDecimal c =new BigDecimal(query.getMoney());//司机收入
                incomeService.saveData(2, orderPrivateCar.getDriverId(), 3, 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);
                this.deleteTask(id);//删除定时任务
                    orderPrivateCar.setState(10);
                    orderPrivateCar.setTelX("");
                    orderPrivateCar.setBindId("");
                    this.updateById(orderPrivateCar);
                    orderCancel.setState(2);
                    orderCancel.setPayType(3);
                    orderCancelService.updateById(orderCancel);
                    //添加已收入明细
                    incomeService.saveData(1, orderPrivateCar.getCompanyId(), 3, orderPrivateCar.getId(), 1, query.getMoney());
                    //添加已收入明细
                    Company company = companyService.selectById(orderPrivateCar.getCompanyId());
                    Double taxi = company.getSpeMoney();
                    BigDecimal c =new BigDecimal(query.getMoney());//司机收入
                    incomeService.saveData(2, orderPrivateCar.getDriverId(), 3, 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);
                    this.deleteTask(id);//删除定时任务
//                new Thread(new Runnable() {
//                    @Override
@@ -714,10 +824,15 @@
//                        pushUtil.pushOrderState(2, orderPrivateCar.getDriverId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
//                    }
//                }).start();
                //添加消息
                systemNoticeService.addSystemNotice(1, "您已使用余额成功支付取消订单费用,谢谢使用!", orderPrivateCar.getUserId(), 1);
                    //添加消息
                    systemNoticeService.addSystemNotice(1, "您已使用余额成功支付取消订单费用,谢谢使用!", orderPrivateCar.getUserId(), 1);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            redisUtil.unlock(id + "_cancleOrder");
        }
        return resultUtil;
    }
@@ -746,10 +861,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(query.getLimitationTime()).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){
@@ -771,6 +900,14 @@
                        if(i == querys.size() && state == 1){
                            pushUtil.pushEndPush(1, orderPrivateCar.getUserId(), orderPrivateCar.getId(), 1);
                            orderIds.remove(orderPrivateCar.getId());
                            // 第三轮取消订单
                            orderPrivateCar.setState(10);
                            orderPrivateCarMapper.updateById(orderPrivateCar);
                            // 添加站内信
                            systemNoticeService.addSystemNotice(1, "当前区域未能及时为您匹配到合适车辆。为避免耽误您的行程,系统已自动取消订单,建议您稍后重新尝试。", orderPrivateCar.getUserId(), 1);
                            // TODO 0731 添加短信发送
                        }
                    }
                } catch (Exception e) {
@@ -862,172 +999,282 @@
        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())"));
            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;
        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 == 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("支付失败", "");
        try {
            OrderPrivateCar orderPrivateCar = this.selectById(orderId);
            if(orderPrivateCar.getState() == 8 || orderPrivateCar.getState() == 9){
                return ResultUtil.error("订单已完成支付,不允许重复支付", "");
            }
        }
        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("支付失败", "");
            if(orderPrivateCar.getState() != 7){
                return 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);//只有出行金额参与抽成,其余归属司机
            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), 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 这里需要给司机和用户推送订单状态
            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
@@ -1119,6 +1366,9 @@
    @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){
            //添加交易明细
@@ -1172,77 +1422,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), 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);
        }
        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");
        }
    }