longz
2026-04-22 22b3f30587ad0e8e53ca0dd824c045f8118cda04
DriverYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/crossCity/server/impl/LineShiftDriverServiceImpl.java
@@ -3,13 +3,16 @@
import cn.hutool.core.date.DateUnit;
import cn.hutool.cron.CronUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.crossCity.dao.DriverSelfTripMapper;
import com.stylefeng.guns.modular.crossCity.dao.LinePriceMapper;
import com.stylefeng.guns.modular.crossCity.dao.LineShiftDriverMapper;
import com.stylefeng.guns.modular.crossCity.dao.LineShiftMapper;
import com.stylefeng.guns.modular.crossCity.model.DriverSelfTrip;
import com.stylefeng.guns.modular.crossCity.model.LineShift;
import com.stylefeng.guns.modular.crossCity.model.LineShiftDriver;
import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity;
@@ -17,6 +20,7 @@
import com.stylefeng.guns.modular.crossCity.server.ILineShiftDriverService;
import com.stylefeng.guns.modular.crossCity.server.ILineSiteService;
import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService;
import com.stylefeng.guns.modular.crossCity.warpper.AddSelfTripWarpper;
import com.stylefeng.guns.modular.crossCity.warpper.DriverShiftListWarpper;
import com.stylefeng.guns.modular.crossCity.warpper.LineShiftItemWarpper;
import com.stylefeng.guns.modular.crossCity.warpper.OtherDriverShiftWarpper;
@@ -24,17 +28,23 @@
import com.stylefeng.guns.modular.system.dao.CarModelMapper;
import com.stylefeng.guns.modular.system.dao.CarServiceMapper;
import com.stylefeng.guns.modular.system.dao.ServerCarModelMapper;
import com.stylefeng.guns.modular.system.model.Car;
import com.stylefeng.guns.modular.system.model.CarModel;
import com.stylefeng.guns.modular.system.model.CarService;
import com.stylefeng.guns.modular.system.model.Driver;
import com.stylefeng.guns.modular.system.service.ICarService;
import com.stylefeng.guns.modular.system.service.IDriverService;
import com.stylefeng.guns.modular.system.util.DateUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.stream.Collectors;
@@ -65,6 +75,9 @@
    @Resource
    private CarServiceMapper carServiceMapper;
    @Resource
    private DriverSelfTripMapper driverSelfTripMapper;
    @Autowired
    private IOrderCrossCityService orderCrossCityService;
@@ -442,11 +455,155 @@
        if (driverShiftId == null) {
            return new ArrayList<>();
        }
        LineShiftDriver current = lineShiftDriverMapper.selectById(driverShiftId);
        if (current == null || (current.getLineShiftId() != null && current.getLineShiftId() == -1)) {
            return new ArrayList<>();
        }
        List<Map<String, Object>> rows = lineShiftDriverMapper.queryOtherDriverShiftListWithShiftId(driverShiftId);
        return OtherDriverShiftWarpper.fromMaps(rows);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultUtil<Map<String, Object>> addSelfTrip(Integer driverId, AddSelfTripWarpper warpper) throws Exception {
        if (driverId == null) {
            return ResultUtil.tokenErr();
        }
        if (warpper == null) {
            return ResultUtil.paranErr();
        }
        if (!isSelfTripEnabled(driverId)) {
            return ResultUtil.error("未开启自主行程");
        }
        if (ToolUtil.isEmpty(warpper.getDay()) || ToolUtil.isEmpty(warpper.getStartTime())
                || warpper.getSeat() == null
                || ToolUtil.isEmpty(warpper.getStartAddress())
                || warpper.getStartLon() == null
                || warpper.getStartLat() == null
                || ToolUtil.isEmpty(warpper.getDestJson())) {
            return ResultUtil.paranErr();
        }
        LocalDate day;
        try {
            day = LocalDate.parse(warpper.getDay(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        } catch (DateTimeParseException e) {
            return ResultUtil.error("出发日期格式错误");
        }
        LocalDate today = LocalDate.now();
        if (day.isBefore(today)) {
            return ResultUtil.error("日期不能早于今天");
        }
        if (day.isAfter(today.plusDays(6))) {
            return ResultUtil.error("只能新增7天内的自主行程");
        }
        if (!warpper.getStartTime().matches("^([01]\\d|2[0-3]):[0-5]\\d$")) {
            return ResultUtil.error("出发时间格式错误");
        }
        Integer carSeat = getCurrentCarSeat(driverId);
        if (carSeat == null || carSeat <= 0) {
            return ResultUtil.error("当前车辆车型座位数异常");
        }
        if (warpper.getSeat() < 1 || warpper.getSeat() > carSeat) {
            return ResultUtil.error("座位数超出当前车型可用范围");
        }
        String normalizedDestJson = validateAndNormalizeDestJson(warpper.getDestJson());
        if (normalizedDestJson == null) {
            return ResultUtil.error("目的地信息格式不正确");
        }
        DriverSelfTrip trip = new DriverSelfTrip();
        trip.setDriverId(driverId);
        trip.setDay(java.sql.Date.valueOf(day));
        trip.setStartTime(warpper.getStartTime());
        trip.setSeat(warpper.getSeat());
        trip.setStartCityCode(warpper.getStartCityCode());
        trip.setStartCityName(warpper.getStartCityName());
        trip.setStartAddress(warpper.getStartAddress());
        trip.setStartLon(warpper.getStartLon());
        trip.setStartLat(warpper.getStartLat());
        trip.setDestJson(normalizedDestJson);
        trip.setState(1);
        trip.setInsertTime(new Date());
        trip.setUpdateTime(new Date());
        driverSelfTripMapper.insert(trip);
        LineShiftDriver lineShiftDriver = new LineShiftDriver();
        lineShiftDriver.setLineShiftId(-1);
        lineShiftDriver.setCustomerId(trip.getId());
        lineShiftDriver.setDriverId(driverId);
        lineShiftDriver.setDay(java.sql.Date.valueOf(day));
        lineShiftDriver.setLaveSeat(warpper.getSeat());
        lineShiftDriver.setTotalSeat(warpper.getSeat());
        lineShiftDriver.setLaveSeatNumber(buildSeatNumber(warpper.getSeat()));
        lineShiftDriver.setInserTime(new Date());
        lineShiftDriver.setAssignType(1);
        lineShiftDriverMapper.insert(lineShiftDriver);
        Map<String, Object> result = new HashMap<>(2);
        result.put("lineShiftDriverId", lineShiftDriver.getId());
        result.put("customerId", trip.getId());
        return ResultUtil.success(result);
    }
    private boolean isSelfTripEnabled(Integer driverId) throws Exception {
        Map<String, Object> info = driverService.queryInfo(driverId);
        Object value = info == null ? null : info.get("isOpenDriverSelfTrip");
        return value != null && "1".equals(String.valueOf(value));
    }
    private Integer getCurrentCarSeat(Integer driverId) {
        Driver driver = driverService.selectById(driverId);
        if (driver == null || driver.getCarId() == null) {
            return null;
        }
        Car car = carService.selectById(driver.getCarId());
        if (car == null || car.getCarModelId() == null) {
            return null;
        }
        CarModel carModel = carModelMapper.selectById(car.getCarModelId());
        return carModel == null ? null : carModel.getSeat();
    }
    private String validateAndNormalizeDestJson(String destJson) {
        try {
            JSONArray array = JSON.parseArray(destJson);
            if (array == null || array.isEmpty()) {
                return null;
            }
            for (int i = 0; i < array.size(); i++) {
                JSONObject item = array.getJSONObject(i);
                if (item == null) {
                    return null;
                }
                String endAddress = item.getString("endAddress");
                Double endLon = item.getDouble("endLon");
                Double endLat = item.getDouble("endLat");
                Double price = item.getDouble("price");
                if (ToolUtil.isEmpty(endAddress) || endLon == null || endLat == null || price == null || price < 0) {
                    return null;
                }
            }
            return array.toJSONString();
        } catch (Exception e) {
            return null;
        }
    }
    private String buildSeatNumber(Integer seat) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= seat; i++) {
            if (i > 1) {
                sb.append(",");
            }
            sb.append(i);
        }
        return sb.toString();
    }
    @Override
    public ResultUtil<ScanOrderDetailWarpper> getDetail(Integer lineShiftDriverId) {
        // 1. 查询基础数据
        Map<String, Object> data = lineShiftDriverMapper.getDetail(lineShiftDriverId);