xuhy
2026-04-17 4edaaf570a2cd8af8a9c35694a46289cdf9278b3
xp-robotaxi-automatic-pilot-boot/src/main/java/com/xiaopeng/automaticPilot/controller/OrderMissionController.java
@@ -6,16 +6,20 @@
import com.alibaba.fastjson2.JSONObject;
import com.xiaopeng.api.dto.automaticPilot.MqttPublishDto;
import com.xiaopeng.api.dto.automaticPilot.PushOrderDto;
import com.xiaopeng.api.dto.automaticPilot.VehicleEventDTO;
import com.xiaopeng.api.dto.map.OrderRouteAndFareDto;
import com.xiaopeng.api.dto.operation.LonLatDto;
import com.xiaopeng.api.dto.order.PopOrderDto;
import com.xiaopeng.api.dto.pushMessage.PushOrderRouteDTO;
import com.xiaopeng.api.dto.pushMessage.SendMessageDTO;
import com.xiaopeng.api.dto.vehicleConvoy.UpdateVehicleStateDto;
import com.xiaopeng.api.feignClient.automaticPilot.VehicleBuriedPointClient;
import com.xiaopeng.api.feignClient.map.XpMapClient;
import com.xiaopeng.api.feignClient.order.XpOrderClient;
import com.xiaopeng.api.feignClient.pushMessage.XpPushMessageClient;
import com.xiaopeng.api.feignClient.vehicleConvoy.XpVehicleClient;
import com.xiaopeng.api.model.order.XpOrder;
import com.xiaopeng.api.model.vehicleConvoy.XpVehicle;
import com.xiaopeng.api.model.order.XpOrder;import com.xiaopeng.api.model.vehicleConvoy.XpVehicle;
import com.xiaopeng.api.vo.SystemEventType;
import com.xiaopeng.api.vo.automaticPilot.VehiclePosition;
@@ -24,12 +28,14 @@
import com.xiaopeng.api.vo.order.OrderTrajectoryVo;
import com.xiaopeng.automaticPilot.util.XPUUtil;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.enums.CancelOrderSceneEnum;
import com.xiaopeng.common.core.enums.OrderStatusEnum;
import com.xiaopeng.common.core.enums.PushMessageTypeEnum;
import com.xiaopeng.common.core.utils.StringUtils;
import com.xiaopeng.common.redis.service.RedisService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
@@ -59,10 +65,13 @@
    @Resource
    private XpVehicleClient xpVehicleClient;
    @Resource
    private VehicleBuriedPointClient vehicleBuriedPointClient;
    @Resource(name = "hutoolSnowflake")
    private Snowflake snowflake;
    @PostMapping("/inner/pushOrder")
    @PostMapping("/logan/inner/pushOrder")
    public R<Boolean> pushOrder(@RequestBody PushOrderDto dto) {
        log.info("创建订单,推送XPU报文:{}", JSON.toJSONString(dto));
        //构建点位信息
        Waypoint.Builder pointBuilderStart = Waypoint.newBuilder();
        Waypoint.Builder pointBuilderEnd = Waypoint.newBuilder();
@@ -91,13 +100,14 @@
            pointBuilderEnd.setActionType(ActionType.DRIVE_THROUGH);
            geoPointBuilder = GeoPoint.newBuilder();
            geoPointBuilder.setLatitude(dto.getEndLatitude());
            geoPointBuilder.setLongitude(dto.getEndLatitude());
            geoPointBuilder.setLongitude(dto.getEndLongitude());
            pointBuilderEnd.setPoint(geoPointBuilder.build());
            taskBuilder.addPoints(pointBuilderEnd);
            taskBuilder.setSequence(1);
            taskBuilder.addOrderPhase(OrderPhase.ROUTE_TO_CRUISE);
            taskBuilder.setOrderPhase(OrderPhase.ROUTE_TO_CRUISE);
            taskListBuilder.addTasks(taskBuilder.build());
        }else{
        }else if(dto.getOrderType().equals(com.xiaopeng.api.vo.OrderType.USER_ORDER)){
            //乘客单
            for (int i = 1; i < 3; i++) {
                RtTask.Builder taskBuilder = RtTask.newBuilder();
                taskBuilder.setOrderId(dto.getOrderId().toString());
@@ -127,7 +137,68 @@
                    pointBuilderEnd.setPoint(geoPointBuilder.build());
                    taskBuilder.addPoints(pointBuilderEnd);
                    taskBuilder.setSequence(i);
                    taskBuilder.addOrderPhase(OrderPhase.ROUTE_TO_PICKUP);
                    taskBuilder.setOrderPhase(OrderPhase.ROUTE_TO_PICKUP);
                }else{
                    //构建上车点位置信息
                    pointBuilderStart.setWaypointId(dto.getOrderId() + "-SD-001");
                    pointBuilderStart.setPointType(PointType.PICK_UP_POINT);
                    pointBuilderStart.setActionType(ActionType.PICK_UP);
                    pointBuilderStart.setSequence(1);
                    GeoPoint.Builder geoPointBuilder = GeoPoint.newBuilder();
                    geoPointBuilder.setLatitude(dto.getStartLatitude());
                    geoPointBuilder.setLongitude(dto.getStartLongitude());
                    pointBuilderStart.setPoint(geoPointBuilder.build());
                    taskBuilder.addPoints(pointBuilderStart);
                    //构建下车点位置信息
                    pointBuilderEnd.setWaypointId(dto.getOrderId() + "-SD-002");
                    pointBuilderEnd.setPointType(PointType.END_POINT);
                    pointBuilderEnd.setActionType(ActionType.DROP_OFF);
                    pointBuilderEnd.setSequence(2);
                    pointBuilderEnd.setPoiName(dto.getEndPoiName());
                    geoPointBuilder = GeoPoint.newBuilder();
                    geoPointBuilder.setLatitude(dto.getEndLatitude());
                    geoPointBuilder.setLongitude(dto.getEndLongitude());
                    pointBuilderEnd.setPoint(geoPointBuilder.build());
                    taskBuilder.addPoints(pointBuilderEnd);
                    taskBuilder.setSequence(i);
                    taskBuilder.setOrderPhase(OrderPhase.ROUTE_TO_DESTINATION);
                }
                taskListBuilder.addTasks(taskBuilder.build());
            }
        }else if(dto.getOrderType().equals(com.xiaopeng.api.vo.OrderType.TEST_ORDER)){
            //测试单
            for (int i = 1; i < 3; i++) {
                RtTask.Builder taskBuilder = RtTask.newBuilder();
                taskBuilder.setOrderId(dto.getOrderId().toString());
                taskBuilder.setTaskId(snowflake.nextId() + "");
                taskBuilder.setTaskType(OrderType.ORDER_TYPE_ROBOTAXI);
                if(i == 1){
                    VehiclePosition vehiclePosition = mongoTemplate.findOne(new Query(Criteria.where("vin").is(dto.getVin())), VehiclePosition.class);
                    //构建车辆当前位置数据
                    pointBuilderStart.setWaypointId(dto.getOrderId() + "-JJ-001");
                    pointBuilderStart.setPointType(PointType.START_POINT);
                    pointBuilderStart.setActionType(ActionType.ACTION_UNSPECIFIED);
                    pointBuilderStart.setSequence(1);
                    GeoPoint.Builder geoPointBuilder = GeoPoint.newBuilder();
                    geoPointBuilder.setLatitude(vehiclePosition.getPoint().getY());
                    geoPointBuilder.setLongitude(vehiclePosition.getPoint().getX());
                    pointBuilderStart.setPoint(geoPointBuilder.build());
                    taskBuilder.addPoints(pointBuilderStart);
                    //构建上车点位置信息
                    pointBuilderEnd.setWaypointId(dto.getOrderId() + "-JJ-002");
                    pointBuilderEnd.setPointType(PointType.PICK_UP_POINT);
                    pointBuilderEnd.setActionType(ActionType.PICK_UP);
                    pointBuilderEnd.setSequence(2);
                    geoPointBuilder = GeoPoint.newBuilder();
                    geoPointBuilder.setLatitude(dto.getStartLatitude());
                    geoPointBuilder.setLongitude(dto.getStartLongitude());
                    pointBuilderEnd.setPoint(geoPointBuilder.build());
                    pointBuilderEnd.setPoiName(dto.getEndPoiName());
                    taskBuilder.addPoints(pointBuilderEnd);
                    taskBuilder.setSequence(i);
                    taskBuilder.setOrderPhase(OrderPhase.ROUTE_TO_PICKUP);
                }else{
                    //构建上车点位置信息
                    pointBuilderStart.setWaypointId(dto.getOrderId() + "-SD-001");
@@ -147,27 +218,60 @@
                    pointBuilderEnd.setSequence(2);
                    geoPointBuilder = GeoPoint.newBuilder();
                    geoPointBuilder.setLatitude(dto.getEndLatitude());
                    geoPointBuilder.setLongitude(dto.getEndLatitude());
                    geoPointBuilder.setLongitude(dto.getEndLongitude());
                    pointBuilderEnd.setPoint(geoPointBuilder.build());
                    taskBuilder.addPoints(pointBuilderEnd);
                    taskBuilder.setSequence(i);
                    taskBuilder.addOrderPhase(OrderPhase.ROUTE_TO_DESTINATION);
                    taskBuilder.setOrderPhase(OrderPhase.ROUTE_TO_DESTINATION);
                }
                taskListBuilder.addTasks(taskBuilder.build());
            }
        }else if(dto.getOrderType().equals(com.xiaopeng.api.vo.OrderType.TEST_CRUISE_ORDER)){
            //测试单巡游单
            RtTask.Builder taskBuilder = RtTask.newBuilder();
            taskBuilder.setOrderId(dto.getOrderId().toString());
            taskBuilder.setTaskId(snowflake.nextId() + "");
            taskBuilder.setTaskType(OrderType.ORDER_TYPE_CRUISE);
            //构建起点坐标数据
            pointBuilderStart.setWaypointId(dto.getOrderId() + "-001");
            pointBuilderStart.setPointType(PointType.START_POINT);
            pointBuilderStart.setSequence(1);
            pointBuilderStart.setActionType(ActionType.ACTION_UNSPECIFIED);
            GeoPoint.Builder geoPointBuilder = GeoPoint.newBuilder();
            geoPointBuilder.setLatitude(dto.getStartLatitude());
            geoPointBuilder.setLongitude(dto.getStartLongitude());
            pointBuilderStart.setPoint(geoPointBuilder.build());
            taskBuilder.addPoints(pointBuilderStart);
            //构建终点数据
            pointBuilderEnd.setWaypointId(dto.getOrderId() + "-002");
            pointBuilderEnd.setPointType(PointType.CRUISE_END_POINT);
            pointBuilderEnd.setSequence(2);
            pointBuilderEnd.setActionType(ActionType.DRIVE_THROUGH);
            geoPointBuilder = GeoPoint.newBuilder();
            geoPointBuilder.setLatitude(dto.getEndLatitude());
            geoPointBuilder.setLongitude(dto.getEndLongitude());
            pointBuilderEnd.setPoint(geoPointBuilder.build());
            taskBuilder.addPoints(pointBuilderEnd);
            taskBuilder.setSequence(1);
            taskBuilder.setOrderPhase(OrderPhase.ROUTE_TO_CRUISE);
            taskListBuilder.addTasks(taskBuilder.build());
        }
        long sequence = snowflake.nextId();
        MqttPublishDto mqttPublishDto = xpuUtil.cloudCreateOrder(taskListBuilder.build(), dto.getVin(),sequence);
        //先将车辆修改状态,防止在异步通知之前被其他订单选中
        if (!dto.getOrderId().contains("XU") && mqttPublishDto.getCode()== 200) {
        if(!dto.getOrderType().equals(com.xiaopeng.api.vo.OrderType.CRUISE_ORDER) &&
                !dto.getOrderType().equals(com.xiaopeng.api.vo.OrderType.TEST_CRUISE_ORDER)
                && mqttPublishDto.getCode()== 200){
            // 修改车子为接单状态
            xpVehicleClient.updateVehicleStatus(new UpdateVehicleStateDto(dto.getVin(), 1));
        }
        if(null != mqttPublishDto && mqttPublishDto.getCode() == 200){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("event", SystemEventType.CREATE_ORDER);
            jsonObject.put("orderType", dto.getOrderId().contains("XU") ? com.xiaopeng.api.vo.OrderType.CRUISE_ORDER : com.xiaopeng.api.vo.OrderType.USER_ORDER);
            jsonObject.put("orderType",  dto.getOrderType());
            jsonObject.put("orderId", dto.getOrderId());
            jsonObject.put("vin", dto.getVin());
            redisService.setCacheObject("CREATE_ORDER:" + dto.getOrderId().toString(), jsonObject.toJSONString(),30L, TimeUnit.MINUTES);
@@ -176,11 +280,13 @@
        return R.ok(false);
    }
    @PostMapping("/inner/cancelOrder")
    @PostMapping("/logan/inner/cancelOrder")
    R<Boolean> cancelOrder(@RequestBody PushOrderDto dto) {
        log.info("取消订单,推送XPU报文:{}", JSON.toJSONString(dto));
        String taskId = StringUtils.isNotEmpty(dto.getTaskId()) ? dto.getTaskId() : snowflake.nextId() + "";
        OrderOperation.Builder operationBuilder = OrderOperation.newBuilder();
        operationBuilder.setOrderId(dto.getOrderId().toString());
        operationBuilder.setTaskId(snowflake.nextId() + "");
        operationBuilder.setTaskId(taskId);
        if (dto.getOperation_type() != null) {
            operationBuilder.setOperationType(OrderOperationType.forNumber(dto.getOperation_type().getNumber()));
        }
@@ -190,8 +296,16 @@
        if (mqttPublishDto.getCode() == 200) {
            if(!dto.getOrderId().contains("XU")){
                // 正常订单处理
                redisService.setCacheObject(String.valueOf(sequence), "cancelOrder_" + dto.getOrderId() + "_" + dto.getVin(), 30l, TimeUnit.MINUTES);
                Map<String, Object> cancelContext = new HashMap<>();
                cancelContext.put("orderId", dto.getOrderId());
                cancelContext.put("vin", dto.getVin());
                cancelContext.put("cancelScene", StringUtils.isNotEmpty(dto.getCancelScene())
                        ? dto.getCancelScene()
                        : CancelOrderSceneEnum.NORMAL_CANCEL.getCode());
                redisService.setCacheObject("CANCEL_ORDER_CONTEXT:" + taskId,
                        JSONObject.toJSONString(cancelContext), 30L, TimeUnit.MINUTES);
                log.info("记录取消订单上下文成功,orderId: {}, vin: {}, taskId: {}, cancelScene: {}",
                        dto.getOrderId(), dto.getVin(), taskId, cancelContext.get("cancelScene"));
            }
            return R.ok(true);
        }
@@ -246,8 +360,8 @@
            vehicleId = 11L;//默认使用造的测试车辆
        }
        if(null == startLat || null == startLon){
            startLat = 30.583407D;
            startLon = 104.040251D;
            startLat = 23.146815D;
            startLon = 113.459765D;
        }
        XpVehicle xpVehicle = xpVehicleClient.queryById(vehicleId).getData();
        XpOrder xpOrder = null;
@@ -269,6 +383,7 @@
        destination.setLatitude(xpOrder.getStartLat());
        dto.setDestination(destination);
        dto.setRouteType(1);
        dto.setVehicleId(xpOrder.getVehicleId());
        R<RouteVo> routeVoR = xpMapClient.getOrderDistanceAndDuration(dto);
        if (200 != routeVoR.getCode()) {
            throw new RuntimeException("获取订单线路和运费失败");
@@ -336,15 +451,15 @@
        orderId = xpOrder.getId();
        String vin = xpVehicle.getVehicleVin();
        Long appUserId = xpOrder.getAppUserId();
//        Object cacheObject = redisService.getCacheObject("ORDER_NAVIGATION_START_DATA:" + xpOrder.getAppUserId());
//        RouteVo routeVo = JSON.parseObject(cacheObject.toString(), RouteVo.class);
        RouteVo routeVo =new RouteVo();
        ArrayList<LonLatDto> lonLatDtos = new ArrayList<>();
        LonLatDto lonLatDto1 = new LonLatDto();
        lonLatDto1.setLatitude(30.575685);
        lonLatDto1.setLongitude(104.001118);
        lonLatDtos.add(lonLatDto1);
        routeVo.setPolyline(lonLatDtos);
        Object cacheObject = redisService.getCacheObject("ORDER_NAVIGATION_START_DATA:" + xpOrder.getAppUserId());
        RouteVo routeVo = JSON.parseObject(cacheObject.toString(), RouteVo.class);
//        RouteVo routeVo =new RouteVo();
//        ArrayList<LonLatDto> lonLatDtos = new ArrayList<>();
//        LonLatDto lonLatDto1 = new LonLatDto();
//        lonLatDto1.setLatitude(30.575685);
//        lonLatDto1.setLongitude(104.001118);
//        lonLatDtos.add(lonLatDto1);
//        routeVo.setPolyline(lonLatDtos);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1000));
@@ -399,9 +514,10 @@
                    dto.setRouteType(1);
                    dto.setLongitude(longitude);
                    dto.setLatitude(latitude);
                    dto.setDuration(10);
                    dto.setDistance(2);
                    dto.setDuration(10.0);
                    dto.setDistance(2.0);
                    dto.setDirection(60);
                    dto.setMessageType(PushMessageTypeEnum.ORDER_ROUTE.getDesc());
                    xpPushMessageClient.pushOrderRoute(dto);
                }
@@ -472,19 +588,19 @@
            //主动推一次全程轨迹
            Object cacheObject = redisService.getCacheObject("ORDER_NAVIGATION_END_DATA:" + xpOrder.getAppUserId());
            RouteVo routeVo = JSON.parseObject(cacheObject.toString(), RouteVo.class);
            com.alibaba.fastjson2.JSONObject jsonObject = new com.alibaba.fastjson2.JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", "ORDER_ROUTE");
            jsonObject.put("timestamp", System.currentTimeMillis());
            com.alibaba.fastjson2.JSONObject content1 = new com.alibaba.fastjson2.JSONObject();
            JSONObject content1 = new JSONObject();
            content1.put("orderId", orderId.toString());
            content1.put("routeType", 1);
            com.alibaba.fastjson2.JSONObject route = new com.alibaba.fastjson2.JSONObject();
            JSONObject route = new JSONObject();
            route.put("distance", routeVo.getDistance());
            route.put("duration", routeVo.getDuration());
            route.put("routeID", routeVo.getRouteID());
            List<com.alibaba.fastjson2.JSONObject> collect = routeVo.getPolyline().stream().map(item -> {
                com.alibaba.fastjson2.JSONObject point = new com.alibaba.fastjson2.JSONObject();
            List<JSONObject> collect = routeVo.getPolyline().stream().map(item -> {
                JSONObject point = new JSONObject();
                point.put("latitude", item.getLatitude());
                point.put("longitude", item.getLongitude());
                return point;
@@ -525,6 +641,7 @@
        Long appUserId = xpOrder.getAppUserId();
        Object cacheObject = redisService.getCacheObject("ORDER_NAVIGATION_END_DATA:" + xpOrder.getAppUserId());
        RouteVo routeVo = JSON.parseObject(cacheObject.toString(), RouteVo.class);
        log.info("开始实施推送车辆位置和剩余导航线路 {} {}", xpOrder.getOrderNumber(), JSON.toJSONString(routeVo));
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1000));
        Long finalOrderId = orderId;
@@ -579,9 +696,10 @@
                    dto.setRouteType(1);
                    dto.setLongitude(longitude);
                    dto.setLatitude(latitude);
                    dto.setDuration(20);
                    dto.setDistance(10);
                    dto.setDuration(20.0);
                    dto.setDistance(10.0);
                    dto.setDirection(60);
                    dto.setMessageType(PushMessageTypeEnum.ORDER_ROUTE.getDesc());
                    xpPushMessageClient.pushOrderRoute(dto);
                    //存储历史轨迹
@@ -597,7 +715,7 @@
                //5秒钟循环一次
                try {
                    Thread.sleep(500);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }