6.5
luodangjia
2024-06-05 06c5eda038f967dc0c0261e16eff0ad1a18e41e7
DriverNTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/TOrderTransferServiceImpl.java
@@ -1,9 +1,12 @@
package com.stylefeng.guns.modular.system.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.system.dao.SystemPriceMapper;
import com.stylefeng.guns.modular.system.dao.TOrderTransferMapper;
import com.stylefeng.guns.modular.system.model.Driver;
import com.stylefeng.guns.modular.system.model.TOrderTransfer;
@@ -20,9 +23,8 @@
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.math.BigDecimal;
import java.util.*;
/**
 * <p>
@@ -199,4 +201,331 @@
    public List<Map<String, Object>> queryOrderList(Integer state, Integer driverId) throws Exception {
        return this.baseMapper.queryOrderList(state, driverId);
    }
    @Override
    public Map<String, Object> queryOrderInfo(Integer orderId) throws Exception {
        return this.baseMapper.queryOrderInfo(orderId);
    }
    @Override
    public ResultUtil confirmFees(Integer orderId, Integer type, Double parkingFee, Double crossingFee) throws Exception {
        TOrderTransfer orderPrivateCar = this.selectById(orderId);
        orderPrivateCar = this.setMoney(orderPrivateCar, 0D, 0D);//计算费用
        orderPrivateCar.setPayManner(type);
        orderPrivateCar.setParkMoney(null == parkingFee ? 0D : parkingFee);
        orderPrivateCar.setRoadTollMoney(null == crossingFee ? 0D : crossingFee);
        orderPrivateCar.setOrderMoney(orderPrivateCar.getOrderMoney() + orderPrivateCar.getParkMoney() + orderPrivateCar.getRoadTollMoney());
        orderPrivateCar.setState(7);
        this.updateById(orderPrivateCar);
        pushUtil.removeTask(orderId, 7);//删除定时任务,结束推送数据
        systemNoticeService.addSystemNotice(1, "司机已结束本次行程,谢谢使用", orderPrivateCar.getUserId());
        //回滚司机状态为空闲
        Driver driver = driverService.selectById(orderPrivateCar.getDriverId());
        driver.setState(2);
        driverService.updateById(driver);
        TOrderTransfer finalOrderTaxi = orderPrivateCar;
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, finalOrderTaxi.getUserId(), finalOrderTaxi.getId(), 7, finalOrderTaxi.getState());
                pushUtil.pushOrderState(2, finalOrderTaxi.getDriverId(), finalOrderTaxi.getId(), 7, finalOrderTaxi.getState());
            }
        }).start();
        TOrderTransfer finalOrderPrivateCar = orderPrivateCar;
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(pushMinistryOfTransport){//上传交通数据
                    pushMinistryOfTransportUtil.baseInfoVehicleTotalMile(finalOrderPrivateCar.getCarId());
                    pushMinistryOfTransportUtil.operateArrive(orderId);
                }
            }
        }).start();
        return ResultUtil.success();
    }
    @Override
    public Map<String, Object> queryMoneyInfo(Integer orderId) throws Exception {
        TOrderTransfer orderPrivateCar = this.selectById(orderId);
        if(orderPrivateCar.getState() == 5){//服务中的时候获取实时费用数据
            this.setMoney(orderPrivateCar, 0D, 0D);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("orderMoney", orderPrivateCar.getOrderMoney());//订单金额
        map.put("startMileage", orderPrivateCar.getStartMileage());//起步价
        map.put("startMoney", orderPrivateCar.getStartMoney());//起步价
        map.put("mileageKilometers", orderPrivateCar.getMileageKilometers());//里程费
        map.put("mileageMoney", orderPrivateCar.getMileageMoney());//里程费
        map.put("duration", orderPrivateCar.getDuration());//时长费
        map.put("durationMoney", orderPrivateCar.getDurationMoney());//时长费
        map.put("wait", orderPrivateCar.getWait());//等待费
        map.put("waitMoney", orderPrivateCar.getWaitMoney());//等待费
        map.put("longDistance", orderPrivateCar.getLongDistance());//远途费
        map.put("longDistanceMoney", orderPrivateCar.getLongDistanceMoney());//远途费
        map.put("parkMoney", orderPrivateCar.getParkMoney());//停车费
        map.put("roadTollMoney", orderPrivateCar.getRoadTollMoney());//过路费
        map.put("redPacketMoney", orderPrivateCar.getRedPacketMoney());//红包抵扣金额
        map.put("couponMoney", orderPrivateCar.getCouponMoney());//优惠券抵扣金额
        map.put("discountMoney", orderPrivateCar.getDiscountMoney());//折扣抵扣金额
        map.put("discount", orderPrivateCar.getDiscount());//折扣
        return map;
    }
    @Override
    public ResultUtil process(Integer orderId, Integer state, Double lon, Double lat, String address) throws Exception {
        TOrderTransfer orderPrivateCar = this.selectById(orderId);
        switch (state){
            case 3://出发前往预约点
                orderPrivateCar.setState(3);
                orderPrivateCar.setSetOutTime(new Date());
                systemNoticeService.addSystemNotice(1, "司机已出发,请耐心等待", orderPrivateCar.getUserId());
                break;
            case 4://到达预约点,等待客户上车
                orderPrivateCar.setState(4);
                orderPrivateCar.setArriveTime(new Date());
                systemNoticeService.addSystemNotice(1, "司机已到达您设置的预约地点,请及时上车", orderPrivateCar.getUserId());
                break;
            case 5://开始服务
                orderPrivateCar.setBoardingLon(lon);
                orderPrivateCar.setBoardingLat(lat);
                orderPrivateCar.setBoardingAddress(address);
                orderPrivateCar.setBoardingTime(new Date());
                orderPrivateCar.setState(5);
                orderPrivateCar.setStartServiceTime(new Date());
                pushUtil.pushDriverPosition(orderPrivateCar.getId(), 7);//主动推送司机定位
                break;
            case 6://结束服务(专车可以返回继续服务)不修改状态
                orderPrivateCar.setGetoffLon(lon);
                orderPrivateCar.setGetoffLat(lat);
                orderPrivateCar.setGetoffAddress(address);
                orderPrivateCar.setGetoffTime(new Date());
                orderPrivateCar.setEndServiceTime(new Date());
//                //设置结束的时候的经纬度
//                if (orderPrivateCar.getEndLat()!=null){
//                        orderPrivateCar.setEndLon(lon);
//                        orderPrivateCar.setEndLat(lat);
//                        orderPrivateCar.setEndAddress(address);
//                }
                break;
        }
        this.updateById(orderPrivateCar);
        // TODO: 2020/6/5 推送状态
        TOrderTransfer finalOrderPrivateCar = orderPrivateCar;
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, finalOrderPrivateCar.getUserId(), finalOrderPrivateCar.getId(), 1, finalOrderPrivateCar.getState());
                pushUtil.pushOrderState(2, finalOrderPrivateCar.getDriverId(), finalOrderPrivateCar.getId(), 1, finalOrderPrivateCar.getState());
                if(finalOrderPrivateCar.getState() == 5 && pushMinistryOfTransport){//上传数据
                    pushMinistryOfTransportUtil.operateDepart(orderId);
                }
            }
        }).start();
        return ResultUtil.success();
    }
    @Resource
    private SystemPriceMapper systemPriceMapper;
    public TOrderTransfer setMoney(TOrderTransfer orderPrivateCar, Double parkingFee, Double crossingFee) throws Exception {
        Map<String, Object> query1 = systemPriceMapper.query(orderPrivateCar.getCompanyId(), 7, orderPrivateCar.getServerCarModelId());
        //开始根据不同的方式计算金额
        double amount = 0;
        JSONObject jsonObject = JSON.parseObject(query1.get("content").toString());
        Double num1 = jsonObject.getDouble("num1");//起步价(元)
        Double num2 = jsonObject.getDouble("num2");//起步公里(公里)
        Double num3 = jsonObject.getDouble("num3");//起步时间(分钟)
        Double num4 = jsonObject.getDouble("num4");//里程费(元)
        Double num5 = jsonObject.getDouble("num5");//时长费(分钟)
        Double num6 = jsonObject.getDouble("num6");//等待费(分钟)
        Double num7 = jsonObject.getDouble("num7");//等待费(元)
        Double num8 = jsonObject.getDouble("num8");//远途费(公里)
        Double num9 = jsonObject.getDouble("num9");//远途费(公里)
        Double num10 = jsonObject.getDouble("num10");//远途费(元)
        Double num11 = jsonObject.getDouble("num11");//远途费(公里)
        Double num12 = jsonObject.getDouble("num12");//远途费(公里)
        Double num13 = jsonObject.getDouble("num13");//远途费(元)
        Double num14 = jsonObject.getDouble("num14");//远途费(公里)
        Double num15 = jsonObject.getDouble("num15");//远途费(元)
        String num16 = jsonObject.getString("num16");//夜间费(开始时间)
        Double num17 = jsonObject.getDouble("num17");//夜间费(元)
        Double num18 = jsonObject.getDouble("num18");//夜间费(元)
        Double num19 = jsonObject.getDouble("num19");//夜间费(元)
        Double num20 = jsonObject.getDouble("num20");//夜间费(元)
        Double num21 = jsonObject.getDouble("num21");//夜间费(元)
        Double num22 = jsonObject.getDouble("num22");//夜间费(元)
        String num23 = jsonObject.getString("num23");//高峰费(开始时间)
        String num24 = jsonObject.getString("num24");//高峰费(开始时间)
        Double num25 = jsonObject.getDouble("num25");//高峰费(元)
        Double num26 = jsonObject.getDouble("num26");//高峰费(元)
        Double num27 = jsonObject.getDouble("num27");//高峰费(元)
        Double num28 = jsonObject.getDouble("num28");//高峰费(元)
        Double num29 = jsonObject.getDouble("num29");//高峰费(元)
        Double num30 = jsonObject.getDouble("num30");//高峰费(元)
        Date date = new Date();
        double d = (null == orderPrivateCar.getMileage() ? 0D : orderPrivateCar.getMileage()) / 1000;//实际公里
        double t = ((orderPrivateCar.getEndServiceTime().getTime() - orderPrivateCar.getStartServiceTime().getTime()) / 60000) + 1;//实际时间(不满一分钟按一分钟算)
        double w = ((orderPrivateCar.getStartServiceTime().getTime() - orderPrivateCar.getArriveTime().getTime()) / 60000) + 1;//等待分钟(不满一分钟按一分钟算)
        double d1 = (d - num2) < 0 ? 0 : d - num2;//超出起步里程的公里
        double t1 = (t - num3) < 0 ? 0 : new BigDecimal(t - num3).setScale(0, BigDecimal.ROUND_UP).doubleValue();//超过起步分钟数的时间
        double w1 = (w - num6) < 0 ? 0 : new BigDecimal(w - num6).setScale(0, BigDecimal.ROUND_UP).doubleValue();//超出等待时间的时间
        double yt1 = 0;//远途1段
        double yt2 = 0;//远途2段
        double yt3 = 0;//远途3段
        //夜间服务处理逻辑
        Calendar s = Calendar.getInstance();
        s.setTime(date);
        s.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num16.split(" - ")[0].split(":")[0]));
        s.set(Calendar.MINUTE, Integer.valueOf(num16.split(" - ")[0].split(":")[1]));
        Calendar e = Calendar.getInstance();
        e.setTime(date);
        e.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num16.split(" - ")[1].split(":")[0]));
        e.set(Calendar.MINUTE, Integer.valueOf(num16.split(" - ")[1].split(":")[1]));
        if(date.getTime() > s.getTimeInMillis() && date.getTime() < e.getTimeInMillis()){
            if(d > num8.doubleValue() && d <= num9.doubleValue()){
                yt1 = num20 * (d - num8);
            }
            if(d > num9.doubleValue()){
                yt1 = num20 * (num9 - num8);
            }
            if(d > num11.doubleValue() && d <= num12.doubleValue()){
                yt2 = num21 * (d - num11);
            }
            if(d > num12.doubleValue()){
                yt2 = num21 * (num12 - num11);
            }
            if(d > num14.doubleValue()){
                yt3 = num22 * (d - num14);
            }
            amount = num17 + (d1 * num18) + (t1 * num19) + (w1 * num7) + yt1 + yt2 + yt3;
            orderPrivateCar.setStartMileage(num2);
            orderPrivateCar.setStartMoney(num17);//起步价
            orderPrivateCar.setMileageKilometers(new BigDecimal(d1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setMileageMoney(new BigDecimal(d1 * num18).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//里程费
            orderPrivateCar.setDuration(new BigDecimal(t1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setDurationMoney(new BigDecimal(t1 * num19).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//时长费
            orderPrivateCar.setWait(new BigDecimal(w1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setWaitMoney(new BigDecimal(w1 * num7).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//等待费
            orderPrivateCar.setLongDistance(new BigDecimal((d > num8) ? d - num8 : 0).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setLongDistanceMoney(new BigDecimal(yt1 + yt2 + yt3).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//远途费
            orderPrivateCar.setParkMoney(parkingFee);//停车费
            orderPrivateCar.setRoadTollMoney(crossingFee);//过路费
            orderPrivateCar.setRedPacketMoney(0D);//红包抵扣
            orderPrivateCar.setCouponMoney(0D);//优惠券抵扣
            orderPrivateCar.setDiscount(0D);//优惠抵扣
            orderPrivateCar.setPayMoney(0D);//支付金额
            orderPrivateCar.setOrderMoney(new BigDecimal(amount + parkingFee + crossingFee).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            return orderPrivateCar;
        }
        //高峰时段处理逻辑
        Calendar s1 = Calendar.getInstance();
        s1.setTime(date);
        s1.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num23.split(" - ")[0].split(":")[0]));
        s1.set(Calendar.MINUTE, Integer.valueOf(num23.split(" - ")[0].split(":")[1]));
        Calendar e1 = Calendar.getInstance();
        e1.setTime(date);
        e1.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num23.split(" - ")[1].split(":")[0]));
        e1.set(Calendar.MINUTE, Integer.valueOf(num23.split(" - ")[1].split(":")[1]));
        Calendar s2 = Calendar.getInstance();
        s2.setTime(date);
        s2.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num24.split(" - ")[0].split(":")[0]));
        s2.set(Calendar.MINUTE, Integer.valueOf(num24.split(" - ")[0].split(":")[1]));
        Calendar e2 = Calendar.getInstance();
        e2.setTime(date);
        e2.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num24.split(" - ")[1].split(":")[0]));
        e2.set(Calendar.MINUTE, Integer.valueOf(num24.split(" - ")[1].split(":")[1]));
        if((date.getTime() > s1.getTimeInMillis() && date.getTime() < e1.getTimeInMillis()) || (date.getTime() > s2.getTimeInMillis() && date.getTime() < e2.getTimeInMillis())){
            if(d > num8.doubleValue() && d <= num9.doubleValue()){
                yt1 = num28 * (d - num8);
            }
            if(d > num9.doubleValue()){
                yt1 = num28 * (num9 - num8);
            }
            if(d > num11.doubleValue() && d <= num12.doubleValue()){
                yt2 = num29 * (d - num11);
            }
            if(d > num12.doubleValue()){
                yt2 = num29 * (num12 - num11);
            }
            if(d > num14.doubleValue()){
                yt3 = num30 * (d - num14);
            }
            amount = num25 + (d1 * num26) + (t1 * num27) + (w1 * num7) + yt1 + yt2 + yt3;
            orderPrivateCar.setStartMileage(num2);
            orderPrivateCar.setStartMoney(num25);//起步价
            orderPrivateCar.setMileageKilometers(new BigDecimal(d1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setMileageMoney(new BigDecimal(d1 * num26).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//里程费
            orderPrivateCar.setDuration(new BigDecimal(t1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setDurationMoney(new BigDecimal(t1 * num27).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//时长费
            orderPrivateCar.setWait(new BigDecimal(w1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setWaitMoney(new BigDecimal(w1 * num7).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//等待费
            orderPrivateCar.setLongDistance(new BigDecimal((d > num8) ? d - num8 : 0).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setLongDistanceMoney(new BigDecimal(yt1 + yt2 + yt3).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//远途费
            orderPrivateCar.setParkMoney(parkingFee);//停车费
            orderPrivateCar.setRoadTollMoney(crossingFee);//过路费
            orderPrivateCar.setRedPacketMoney(0D);//红包抵扣
            orderPrivateCar.setCouponMoney(0D);//优惠券抵扣
            orderPrivateCar.setDiscount(0D);//优惠抵扣
            orderPrivateCar.setPayMoney(0D);//支付金额
            orderPrivateCar.setOrderMoney(new BigDecimal(amount + parkingFee + crossingFee).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            return orderPrivateCar;
        }
        //其他时间段的计算
        if(d > num8.doubleValue() && d <= num9.doubleValue()){
            yt1 = num10 * (d - num8);
        }
        if(d > num9.doubleValue()){
            yt1 = num10 * (num9 - num8);
        }
        if(d > num11.doubleValue() && d <= num12.doubleValue()){
            yt2 = num13 * (d - num11);
        }
        if(d > num12.doubleValue()){
            yt2 = num13 * (num12 - num11);
        }
        if(d > num14.doubleValue()){
            yt3 = num15 * (d - num14);
        }
        amount = num1 + (d1 * num4) + (t1 * num5) + (w1 * num7) + yt1 + yt2 + yt3;
        orderPrivateCar.setStartMileage(num2);
        orderPrivateCar.setStartMoney(num1);//起步价
        orderPrivateCar.setMileageKilometers(new BigDecimal(d1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
        orderPrivateCar.setMileageMoney(new BigDecimal(d1 * num4).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//里程费
        orderPrivateCar.setDuration(new BigDecimal(t1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
        orderPrivateCar.setDurationMoney(new BigDecimal(t1 * num5).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//时长费
        orderPrivateCar.setWait(new BigDecimal(w1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
        orderPrivateCar.setWaitMoney(new BigDecimal(w1 * num7).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//等待费
        orderPrivateCar.setLongDistance(new BigDecimal((d > num8) ? d - num8 : 0).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
        orderPrivateCar.setLongDistanceMoney(new BigDecimal(yt1 + yt2 + yt3).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//远途费
        orderPrivateCar.setParkMoney(parkingFee);//停车费
        orderPrivateCar.setRoadTollMoney(crossingFee);//过路费
        orderPrivateCar.setRedPacketMoney(0D);//红包抵扣
        orderPrivateCar.setCouponMoney(0D);//优惠券抵扣
        orderPrivateCar.setDiscount(0D);//优惠抵扣
        orderPrivateCar.setPayMoney(0D);//支付金额
        orderPrivateCar.setOrderMoney(new BigDecimal(amount + parkingFee + crossingFee).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
        return orderPrivateCar;
    }
}