liujie
2026-02-13 21e0b91aa7282adc2bd5738c25fd610d78081e86
feat(order): 集成雪花算法生成唯一序列号并扩展任务订单功能

- 在OrderMissionController中引入SnowFlake工具类生成唯一序列号
- 修改cloudCreateOrder方法调用以传入生成的序列号参数
- 为RTTask实体类添加vin字段用于存储车辆识别码
- 更新XPUUtil工具类以支持序列号传递到消息头中
1个文件已添加
4个文件已修改
136 ■■■■■ 已修改文件
xp-robotaxi-api/src/main/java/com/xiaopeng/api/vo/automaticPilot/proto/down/cloudCreateOrder/RTTask.java 4 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
xp-robotaxi-automaticPilot-boot/src/main/java/com/xiaopeng/automaticPilot/controller/OrderMissionController.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
xp-robotaxi-automaticPilot-boot/src/main/java/com/xiaopeng/automaticPilot/util/XPUUtil.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
xp-robotaxi-common/xp-common-core/src/main/java/com/xiaopeng/common/core/utils/SnowFlake.java 111 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
xp-robotaxi-operation-boot/src/main/java/com/xiaopeng/operation/utils/TaskOrderUtil.java 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
xp-robotaxi-api/src/main/java/com/xiaopeng/api/vo/automaticPilot/proto/down/cloudCreateOrder/RTTask.java
@@ -17,6 +17,10 @@
     */
    private String order_id;
    /**
     * 车辆vin
     */
    private String vin;
    /**
     * 导航点列表
     */
    private List<NavigationWaypoint> points;
xp-robotaxi-automaticPilot-boot/src/main/java/com/xiaopeng/automaticPilot/controller/OrderMissionController.java
@@ -7,6 +7,7 @@
import com.xiaopeng.api.vo.automaticPilot.proto.down.cloudCreateOrder.TaskType;
import com.xiaopeng.automaticPilot.util.XPUUtil;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.utils.SnowFlake;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
@@ -39,8 +40,10 @@
        task.setTask_id(System.currentTimeMillis()+ "");
        task.setTask_type(TaskType.ROBOTAXI);
        task.setStatus(TaskStatus.PENDING);
        ResponseMessage responseMessage = xpuUtil.cloudCreateOrder(task);
        SnowFlake snowFlake = new SnowFlake(0, 0);
        snowFlake.withStartTimestamp(1420041600000L);
        long sequence = snowFlake.nextId();
        ResponseMessage responseMessage = xpuUtil.cloudCreateOrder(task,sequence);
        if (responseMessage.getCode()== StatusCode.OK) {
            return R.ok(true);
        }
xp-robotaxi-automaticPilot-boot/src/main/java/com/xiaopeng/automaticPilot/util/XPUUtil.java
@@ -35,14 +35,16 @@
     * @param task
     * @return
     */
    public ResponseMessage cloudCreateOrder(RTTask task) {
    public ResponseMessage cloudCreateOrder(RTTask task,Long sequence) {
        MsgContent msgContent = new MsgContent();
        // 消息头
        RtMsgHeader header = new RtMsgHeader();
        header.setMsg_id(UUID.randomUUID().toString());
        header.setTimestamp(System.currentTimeMillis());
        header.setCmd(RTCmdType.CLOUD_CREATE_ORDER);
        header.setSequence(1L);
        header.setSequence(sequence);
        header.setType(MessageType.MESSAGE_TYPE_RESPONSE);
        msgContent.setHeader(header);
        // 消息内容
xp-robotaxi-common/xp-common-core/src/main/java/com/xiaopeng/common/core/utils/SnowFlake.java
New file
@@ -0,0 +1,111 @@
package com.xiaopeng.common.core.utils;
/**
 * see @https://raw.githubusercontent.com/beyondfengyu/SnowFlake/master/SnowFlake.java
 * Created by Lox on 2017/8/21.
 */
public class SnowFlake {
    /**
     * 起始的时间戳
     */
    private static long START_STMP = 1503305692631L;
    /**
     * 每一部分占用的位数
     */
    private final static long SEQUENCE_BIT = 12; //序列号占用的位数
    private final static long MACHINE_BIT = 5;   //机器标识占用的位数
    private final static long DATACENTER_BIT = 5;//数据中心占用的位数
    /**
     * 每一部分的最大值
     */
    public final static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT);
    public final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
    private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);
    /**
     * 每一部分向左的位移
     */
    private final static long MACHINE_LEFT = SEQUENCE_BIT;
    private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
    private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;
    private long datacenterId;  //数据中心
    private long machineId;     //机器标识
    private long sequence = 0L; //序列号
    private long lastStmp = -1L;//上一次时间戳
    public SnowFlake(long datacenterId, long machineId) {
        if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) {
            throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
        }
        if (machineId > MAX_MACHINE_NUM || machineId < 0) {
            throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0");
        }
        this.datacenterId = datacenterId;
        this.machineId = machineId;
    }
    /**
     * 设置初始时间,各业务方的旧算法有可能初始时间不一致,请使用
     * @param timestamp 设定的新初始时间戳
     * @return 当前对象
     */
    public SnowFlake withStartTimestamp(long timestamp) {
        SnowFlake.START_STMP = timestamp;
        return this;
    }
    /**
     * 产生下一个ID
     *
     * @return
     */
    public synchronized long nextId() {
        long currStmp = getNewstmp();
        if (currStmp < lastStmp) {
            throw new RuntimeException("Clock moved backwards.  Refusing to generate id");
        }
        if (currStmp == lastStmp) {
            //相同毫秒内,序列号自增
            sequence = (sequence + 1) & MAX_SEQUENCE;
            //同一毫秒的序列数已经达到最大
            if (sequence == 0L) {
                currStmp = getNextMill();
            }
        } else {
            //不同毫秒内,序列号置为0
            sequence = 0L;
        }
        lastStmp = currStmp;
        return (currStmp - START_STMP) << TIMESTMP_LEFT //时间戳部分
                | datacenterId << DATACENTER_LEFT       //数据中心部分
                | machineId << MACHINE_LEFT             //机器标识部分
                | sequence;                             //序列号部分
    }
    private long getNextMill() {
        long mill = getNewstmp();
        while (mill <= lastStmp) {
            mill = getNewstmp();
        }
        return mill;
    }
    private long getNewstmp() {
        return System.currentTimeMillis();
    }
//    public static void main(String[] args) {
//        SnowFlake snowFlake = new SnowFlake(0, 0);
//        snowFlake.withStartTimestamp(1420041600000L);
//
//        for (int i = 0; i < (1 << 5); i++) {
//            System.out.println(snowFlake.nextId());
//        }
//    }
}
xp-robotaxi-operation-boot/src/main/java/com/xiaopeng/operation/utils/TaskOrderUtil.java
@@ -641,6 +641,11 @@
                        BaseVehicleUserDto baseVehicleUserDto = userVehicleClient.addTaxiPassenger(addTaxiPassengerVo);
                        if (baseVehicleUserDto!=null && baseVehicleUserDto.getCode() == 200) {
                            // TODO 下单
                            // 调用推送服务  orderId + carVin
                            // 绑定成功  弹出这个队列第一个订单
                            redisService.popFirstElement(quoteName);
                            return;
@@ -680,6 +685,9 @@
                        addTaxiPassengerVo.setAppId("");
                        BaseVehicleUserDto baseVehicleUserDto = userVehicleClient.addTaxiPassenger(addTaxiPassengerVo);
                        if (baseVehicleUserDto.getCode() == 200) {
                            redisService.popFirstElement(quoteName);
                            lock.unlock();
                            return;