From 588fd7b47b6caf30562e1267a27f5898a5c2671f Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期一, 27 四月 2026 17:34:41 +0800
Subject: [PATCH] Merge branch 'master' of http://120.76.84.145:10101/gitblit/r/java/YueKeDuo

---
 DriverYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/crossCity/server/impl/LineShiftDriverServiceImpl.java |  297 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 288 insertions(+), 9 deletions(-)

diff --git a/DriverYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/crossCity/server/impl/LineShiftDriverServiceImpl.java b/DriverYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/crossCity/server/impl/LineShiftDriverServiceImpl.java
index 69f1525..1aa349d 100644
--- a/DriverYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/crossCity/server/impl/LineShiftDriverServiceImpl.java
+++ b/DriverYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/crossCity/server/impl/LineShiftDriverServiceImpl.java
@@ -1,42 +1,44 @@
 package com.stylefeng.guns.modular.crossCity.server.impl;
 
-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;
 import com.stylefeng.guns.modular.crossCity.server.ILineService;
 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.DriverShiftListWarpper;
-import com.stylefeng.guns.modular.crossCity.warpper.LineShiftItemWarpper;
-import com.stylefeng.guns.modular.crossCity.warpper.OtherDriverShiftWarpper;
-import com.stylefeng.guns.modular.crossCity.warpper.ScanOrderDetailWarpper;
+import com.stylefeng.guns.modular.crossCity.warpper.*;
 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.dao.OrderEvaluateMapper;
+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;
 
 
 @Service
@@ -65,6 +67,12 @@
 
     @Resource
     private CarServiceMapper carServiceMapper;
+
+    @Resource
+    private DriverSelfTripMapper driverSelfTripMapper;
+
+    @Resource
+    private OrderEvaluateMapper orderEvaluateMapper;
 
     @Autowired
     private IOrderCrossCityService orderCrossCityService;
@@ -442,12 +450,170 @@
         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.setEndCityCode(warpper.getEndCityCode());
+        trip.setEndCityName(warpper.getEndCityName());
+        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) {
+        LineShiftDriver lineShiftDriver = lineShiftDriverMapper.selectById(lineShiftDriverId);
+        if (lineShiftDriver == null) {
+            return ResultUtil.error("班次信息不存在");
+        }
+        if (lineShiftDriver.getLineShiftId() != null && lineShiftDriver.getLineShiftId() == -1) {
+            return buildSelfTripDetail(lineShiftDriver);
+        }
+
         // 1. 查询基础数据
         Map<String, Object> data = lineShiftDriverMapper.getDetail(lineShiftDriverId);
         if (data == null || data.isEmpty()) {
@@ -460,6 +626,7 @@
         // 司机信息
         result.setHeadImgUrl(data.get("headImgUrl") != null ? data.get("headImgUrl").toString() : null);
         result.setDriverName(data.get("driverName") != null ? data.get("driverName").toString() : null);
+        result.setSex(data.get("sex") != null ? Integer.parseInt(data.get("sex").toString()) : null);
         result.setCarLicensePlate(data.get("carLicensePlate") != null ? data.get("carLicensePlate").toString() : null);
         result.setCarBrand(data.get("carBrand") != null ? data.get("carBrand").toString() : null);
         result.setCarColor(data.get("carColor") != null ? data.get("carColor").toString() : null);
@@ -514,4 +681,116 @@
 
         return ResultUtil.success(result);
     }
+
+    private ResultUtil<ScanOrderDetailWarpper> buildSelfTripDetail(LineShiftDriver lineShiftDriver) {
+        if (lineShiftDriver.getCustomerId() == null) {
+            return ResultUtil.error("自主行程信息不存在");
+        }
+        DriverSelfTrip trip = driverSelfTripMapper.selectById(lineShiftDriver.getCustomerId());
+        if (trip == null || trip.getState() == null || trip.getState() != 1) {
+            return ResultUtil.error("自主行程信息不存在");
+        }
+        Driver driver = driverService.selectById(lineShiftDriver.getDriverId());
+        if (driver == null) {
+            return ResultUtil.error("司机信息不存在");
+        }
+        Car car = null;
+        CarModel carModel = null;
+        CarService carServiceData = null;
+        if (driver.getCarId() != null) {
+            car = carService.selectById(driver.getCarId());
+            if (car != null && car.getCarModelId() != null) {
+                carModel = carModelMapper.selectById(car.getCarModelId());
+            }
+            carServiceData = carServiceMapper.query(3, driver.getCarId());
+        }
+
+        ScanOrderDetailWarpper result = new ScanOrderDetailWarpper();
+        result.setHeadImgUrl(driver.getHeadImgUrl());
+        String name = driver.getName();
+        result.setDriverName(ToolUtil.isEmpty(name) ? "" : name.substring(0, 1) + "师傅");
+        result.setSex(driver.getSex());
+        result.setCarLicensePlate(car == null ? null : car.getCarLicensePlate());
+        result.setCarBrand(carModel == null ? null : carModel.getName());
+        result.setCarColor(car == null ? null : car.getCarColor());
+        Double score = orderEvaluateMapper.queryCount(driver.getId());
+        result.setScore(score == null ? 0D : score);
+        int orderCount = orderCrossCityService.selectCount(
+                new EntityWrapper<OrderCrossCity>()
+                        .eq("driverId", driver.getId())
+                        .eq("state", 9)
+        );
+        result.setOrderCount(orderCount);
+
+        result.setLineId(-1);
+        result.setLineName(trip.getStartCityName() + "-" + trip.getEndCityName());
+        result.setUnitPrice(null);
+        if (lineShiftDriver.getDay() != null) {
+            result.setDay(new SimpleDateFormat("yyyy-MM-dd").format(lineShiftDriver.getDay()));
+        }
+        result.setStartTime(trip.getStartTime());
+        result.setEndTime(trip.getStartTime());
+        result.setTotalSeat(lineShiftDriver.getTotalSeat());
+        result.setLaveSeat(lineShiftDriver.getLaveSeat());
+        result.setLaveSeatNumber(lineShiftDriver.getLaveSeatNumber());
+
+        result.setStartSiteId(null);
+        result.setStartSiteName(trip.getStartAddress());
+        result.setEndSiteId(null);
+        result.setEndSiteName(null);
+        result.setStartCityName(trip.getStartCityName());
+        result.setEndCityName(null);
+        result.setStartLonLat(trip.getStartLon() == null || trip.getStartLat() == null ? null : trip.getStartLon() + "," + trip.getStartLat());
+        result.setEndLonLat(null);
+
+
+        if (ToolUtil.isNotEmpty(trip.getDestJson())) {
+            try {
+                JSONArray array = JSON.parseArray(trip.getDestJson());
+                if (array != null && !array.isEmpty()) {
+                    JSONObject first = array.getJSONObject(0);
+                    if (first != null) {
+                        result.setEndSiteName(first.getString("endAddress"));
+                        result.setEndCityName(first.getString("endCityName"));
+                        Double endLon = first.getDouble("endLon");
+                        Double endLat = first.getDouble("endLat");
+                        if (endLon != null && endLat != null) {
+                            result.setEndLonLat(endLon + "," + endLat);
+                        }
+                        Double price = first.getDouble("price");
+                        if (price != null) {
+                            result.setUnitPrice(price);
+                        }
+                    }
+                    for (int i = 0; i < array.size(); i++) {
+                        JSONObject jsonObject = array.getJSONObject(i);
+                        jsonObject.put("range",3000D);
+                    }
+                    result.setEndDetailList(array);
+                }
+            } catch (Exception ignored) {
+            }
+        }
+
+        result.setOtherFee(null);
+        result.setServerCarModelId(carServiceData == null ? null : carServiceData.getServerCarModelId());
+        result.setDriverId(driver.getId());
+        result.setLineShiftDriverId(lineShiftDriver.getId());
+
+        result.setStartDefaultPointAddress(trip.getStartAddress());
+        result.setStartDefaultPointLon(trip.getStartLon());
+        result.setStartDefaultPointLat(trip.getStartLat());
+        result.setEndDefaultPointAddress(result.getEndSiteName());
+        if (ToolUtil.isNotEmpty(result.getEndLonLat()) && result.getEndLonLat().contains(",")) {
+            String[] split = result.getEndLonLat().split(",");
+            if (split.length == 2) {
+                try {
+                    result.setEndDefaultPointLon(Double.valueOf(split[0]));
+                    result.setEndDefaultPointLat(Double.valueOf(split[1]));
+                } catch (Exception ignored) {
+                }
+            }
+        }
+        return ResultUtil.success(result);
+    }
 }

--
Gitblit v1.7.1