puzhibing
2026-04-11 b6f3de8451867d85242b2bd706fcfa4a65df4ff7
xp-robotaxi-order-boot/src/main/java/com/xiaopeng/order/service/impl/XpOrderServiceImpl.java
@@ -59,6 +59,7 @@
import com.xiaopeng.common.core.constant.Constants;
import com.xiaopeng.common.core.constant.HttpStatus;
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.exception.ServiceException;
@@ -135,6 +136,9 @@
@Service
@RequiredArgsConstructor(onConstructor_ = @Lazy)
public class XpOrderServiceImpl extends ServiceImpl<XpOrderMapper, XpOrder> implements IXpOrderService {
    private static final String REASSIGN_CANCEL_CONTEXT_PREFIX = "REASSIGN_CANCEL_CONTEXT:";
    private static final long REASSIGN_CANCEL_CONTEXT_TTL_MINUTES = 5L;
    private final UserResourcesClient userResourcesClient;
    private final XpPointClient xpPointClient;
@@ -590,6 +594,8 @@
        vo.setEndAddress(order.getEndAddress());
        vo.setOrderMoney(order.getOrderMoney());
        vo.setStatus(order.getStatus());
        vo.setStartPointName(order.getStartPointName());
        vo.setEndPointName(order.getEndPointName());
        // 用户信息
        if (Objects.nonNull(order.getAppUserId()) && userInfoMap != null && userInfoMap.containsKey(order.getAppUserId().toString())) {
@@ -962,11 +968,16 @@
                    // 6.1.1 取消原订单指令
                    PushOrderDto cancelDto = new PushOrderDto();
                    cancelDto.setTaskId(IdUtil.getSnowflakeNextIdStr());
                    cancelDto.setOrderId(order.getId().toString());
                    cancelDto.setVin(oldVehicle.getVehicleVin());
                    cancelDto.setOperation_type(OrderOperationType.CLOUD_CANCEL_ORDER);
                    cancelDto.setCancelScene(CancelOrderSceneEnum.REASSIGN_OLD_VEHICLE.getCode());
                    orderMissionClient.cancelOrder(cancelDto);
                    log.info("已取消原车辆订单指令,订单ID: {}, VIN: {}", orderId, oldVehicle.getVehicleVin());
                    cacheReassignCancelContext(orderId, oldVehicleId, newVehicleId,
                            oldVehicle.getVehicleVin(), currentStatus);
                    log.info("已取消原车辆订单指令,订单ID: {}, VIN: {}, taskId: {}, cancelScene: {}",
                            orderId, oldVehicle.getVehicleVin(), cancelDto.getTaskId(), cancelDto.getCancelScene());
                    // 6.1.2 解绑乘客与原车辆关系
                    if (Objects.nonNull(order.getAppUserId())) {
@@ -1031,6 +1042,8 @@
        if (Objects.nonNull(newFleetId)) {
            order.setFleetId(newFleetId);
        }
        //重置订单状态为【待接单】
        order.setStatus(1);
        this.updateById(order);
        // 10. 保存改派记录
@@ -1088,10 +1101,39 @@
            log.info("管理后台改派订单,车辆状态修改1 {}", newVehicle.getPlateNumber());
            log.info("已将新车辆状态设为已接单,车辆ID: {}", newVehicleId);
            //12.向小程序用户推送订单改派通知
            Map<String, Object> params = new HashMap<>();
            Map<String, Object> content = new HashMap<>();
            content.put("orderId", order.getId().toString());
            content.put("status", order.getStatus());
            params.put("type", PushMessageTypeEnum.ORDER_REASSIGN.getDesc());
            params.put("content", com.alibaba.fastjson2.JSONObject.toJSONString(content));
            params.put("timestamp", new Date().getTime() + "");
            String message = JSONObject.toJSONString(params);
            SendMessageDTO sendMessageDTO = new SendMessageDTO();
            sendMessageDTO.setMessage(message);
            sendMessageDTO.setAppUserId(String.valueOf(order.getAppUserId()));
            xpPushMessageClient.sendStatus(sendMessageDTO);
        } catch (Exception e) {
            log.error("处理新车辆失败,车辆ID: {},错误: {}", newVehicleId, e.getMessage());
        }
    }
    private void cacheReassignCancelContext(Long orderId, Long oldVehicleId, Long newVehicleId,
                                            String oldVehicleVin, Integer preReassignStatus) {
        JSONObject context = new JSONObject();
        context.put("orderId", orderId);
        context.put("oldVehicleId", oldVehicleId);
        context.put("newVehicleId", newVehicleId);
        context.put("oldVehicleVin", oldVehicleVin);
        context.put("cancelScene", CancelOrderSceneEnum.REASSIGN_OLD_VEHICLE.getCode());
        context.put("operationTime", System.currentTimeMillis());
        context.put("preReassignStatus", preReassignStatus);
        redisService.setCacheObject(REASSIGN_CANCEL_CONTEXT_PREFIX + orderId, context.toJSONString(),
                REASSIGN_CANCEL_CONTEXT_TTL_MINUTES, TimeUnit.MINUTES);
        log.info("记录改派取消上下文成功,orderId: {}, oldVehicleId: {}, newVehicleId: {}, preReassignStatus: {}",
                orderId, oldVehicleId, newVehicleId, preReassignStatus);
    }
    /**
@@ -1658,6 +1700,7 @@
                        new ImmutableMap.Builder<String, String>()
                                .put("id", xpOrder.getId().toString())
                                .put("appid", appid)
                                .put("orderType", OrderType.USER_ORDER)
                                .build();
                QuartzManager.addJob(
                        EndTripJob.class,
@@ -1673,6 +1716,7 @@
                        new ImmutableMap.Builder<String, String>()
                                .put("id", xpOrder.getId().toString())
                                .put("appid", appid)
                                .put("orderType", OrderType.USER_ORDER)
                                .build();
                QuartzManager.addJob(
                        AddCruiseMissionJob.class,
@@ -3010,7 +3054,25 @@
                xpTestOrder.setStartTestPointId(null);
                xpTestOrderService.updateById(xpTestOrder);
                redisService.setCacheObject("ORDER_STATUS:" + xpTestOrder.getId(), 2, 24L, TimeUnit.HOURS);
                redisService.setCacheObject("TEST_ORDER_STATUS:" + xpTestOrder.getId(), 2, 24L, TimeUnit.HOURS);
            }
        }
        if (OrderType.TEST_CRUISE_ORDER.equals(orderType)) {
            XpTestOrder xpTestOrder = xpTestOrderService.getById(orderId);
            //订单为待接单状态
            if (xpTestOrder.getStatus() < 4) {
                xpTestOrder.setAcceptanceTime(LocalDateTime.now());
                R<XpVehicle> byVehicleVin = xpVehicleClient.findByVehicleVin(dto.getXpVehicleVin());
                XpVehicle vehicle = null;
                if (byVehicleVin.getCode() == 200) {
                    vehicle = byVehicleVin.getData();
                    xpTestOrder.setVehicleId(vehicle.getId());
                    xpTestOrder.setFleetId(vehicle.getFleetId());
                }
                xpTestOrder.setStatus(6);
                xpTestOrder.setStartTestPointId(null);
                xpTestOrderService.updateById(xpTestOrder);
                redisService.setCacheObject("TEST_ORDER_STATUS:" + xpTestOrder.getId(), 6, 24L, TimeUnit.HOURS);
            }
        }
    }