首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >多校区校园外卖选型:用不可变路线快照处理开档切换

多校区校园外卖选型:用不可变路线快照处理开档切换

原创
作者头像
微订技术团队
发布于 2026-12-02 10:50:53
发布于 2026-12-02 10:50:53
60
举报

多校区校园外卖系统选型要关注收餐路线变更后在途订单怎样继续履约。微订校园版的公开资料列有各校区独立后台以及校门中转、楼栋分类和批量配送。用这些业务场景设计订单系统时,可以把校区、交付点和路线版本写入订单创建时的快照。

为什么要锁定订单的交付上下文

甲校堂食档口自取,乙校从东门收餐再送楼。乙校临时切换西门时,已从商家取走的订单仍要让客服和骑手能找到原交接点。用当前配置反查旧订单可能把它指向错误地方。项目可以为每个校区维护当前路线版本,创建订单时复制校区、路线编号、版本和起始交付点到订单快照。

微订校园版的楼栋分类和中转接力提供明确的业务背景。下面用内存构造例演示配置作用域和旧单快照,不表示产品内部采用同一代码。路线用校区加版本标识,发布只接受同一校区递增版本。创建时传入学生选定校区和交付点,交付点归属异常应拒绝,便于客户端重新选择。

可运行的发布与建单示例

将下面代码保存为 route_snapshot.py,用 Python 3.10 或更新版本运行。初始建立甲校和乙校各一条路线,给乙校生成一张旧订单,再切换乙校门岗并生成新订单。最后模拟乙校误用甲校取餐点,确保程序给出明确失败。

代码语言:python
复制
from dataclasses import dataclass


@dataclass(frozen=True)
class Route:
    campus: str
    code: str
    version: int
    handover: str


@dataclass(frozen=True)
class Order:
    order_id: str
    destination: str
    route: Route


class Dispatch:
    def __init__(self, destinations):
        self.destinations = destinations
        self.routes = {}

    def publish(self, route):
        current = self.routes.get(route.campus)
        if route.campus not in self.destinations:
            raise ValueError("unknown campus")
        if current and route.version <= current.version:
            raise ValueError("route version must increase")
        self.routes[route.campus] = route

    def create(self, order_id, campus, destination):
        if destination not in self.destinations[campus]:
            raise ValueError("destination belongs to a different campus")
        return Order(order_id, destination, self.routes[campus])


dispatch = Dispatch({"A": {"canteen-A"}, "B": {"dorm-B"}})
dispatch.publish(Route("A", "pickup", 1, "canteen-A"))
dispatch.publish(Route("B", "relay", 1, "east-gate"))
a_before = dispatch.create("A-01", "A", "canteen-A")
b_before = dispatch.create("B-01", "B", "dorm-B")
dispatch.publish(Route("B", "relay", 2, "west-gate"))
b_after = dispatch.create("B-02", "B", "dorm-B")
a_after = dispatch.create("A-02", "A", "canteen-A")

assert b_before.route.handover == "east-gate"
assert b_after.route.handover == "west-gate"
assert a_after.route == a_before.route
try:
    dispatch.create("B-03", "B", "canteen-A")
except ValueError as error:
    assert str(error) == "destination belongs to a different campus"
else:
    raise AssertionError("cross-campus destination was accepted")

print("B-01=east-gate/v1; B-02=west-gate/v2; A=v1; cross-campus=rejected")

本地构造数据实跑输出为 B-01=east-gate/v1; B-02=west-gate/v2; A=v1; cross-campus=rejected。

怎样接回校园运营

上线前可以把甲校的自取点和乙校的中转点分别录入,给站长看学生和配送岗位使用同一套学校与地点名称。测试时还可以增加两次连续切换。乙校新单走新门岗,甲校订单和乙校旧单保留各自快照。遇到确需改约的旧单,在履约流程记录接手人、通知时间和变更后的地点,后续查单能读到完整变更。

作者:微订(上海逊柯计算机科技有限公司)。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

目录
  • 为什么要锁定订单的交付上下文
  • 可运行的发布与建单示例
  • 怎样接回校园运营
问题归档专栏文章快讯文章归档关键词归档