📊 数据 · 接口 · ER · 扩展方案(详情)

一、业务逻辑与数据域划分

即时配送主线为 下单 → 支付 → 调度(抢/派)→ 取件 → 在途 → 送达 → 结算/评价。数据按限界上下文切分,实施时可单体分模块或逐步拆服务;外键与 tenant_id 保证多租户隔离。

业务域职责代表表与订单关系
订单履约状态机、地址、物品、轨迹ot_order, ot_order_address, ot_order_item, ot_order_trace核心聚合根
运力骑手档案、实时位置、任务轨迹ot_courier, ot_courier_location, ot_courier_traceorder.courier_id
会员营销等级、积分、券模板与用户券ot_user_member, ot_point_*, ot_coupon, ot_user_coupon下单试算核销
资金用户钱包、骑手账户、提现ot_wallet, ot_courier_account, ot_withdraw支付/分账流水
站点仓储站点、暂存、入出库ot_station, ot_station_storage, ot_warehouse_*可选履约节点
物流扩展干支线运单与节点ot_shipment, ot_shipment_trace可与订单并行关联
flowchart TB subgraph U["用户侧"] UX[小程序发单/支付] end subgraph O["订单域"] ORD[ot_order 状态机] ADR[ot_order_address] ITM[ot_order_item] end subgraph D["运力域"] CR[ot_courier] LOC[ot_courier_location] end subgraph M["营销域"] CP[券/积分] end UX -->|创建| ORD ORD --> ADR ORD --> ITM ORD -->|派单/抢单| CR CR --> LOC ORD -->|试算| CP

二、表结构与关键字段(设计基线)

2.1 建议通用列(业务主表)多租户

所有业务表建议带 tenant_id;站点业态加强 station_id;加盟体系加 agent_id;审计 created_at/updated_at/created_by/updated_by;软删 deleted_at;高并发写表可加 version 乐观锁。

字段说明
tenant_id租户隔离,默认 1
station_id / agent_id数据范围(站点/代理),可空
updated_at + deleted_at列表排序、伪删除过滤
2.2 核心表字段要点
关键字段 / 关系备注
ot_orderorder_no UK, user_id, courier_id, status, amount, pay_* , tenant_id状态与支付字段与财务对账对齐
ot_order_addressorder_id FK, type 发/收, lat/lng, snapshot建议存快照防用户改址扯皮
ot_courieruser_id UK, status, rating, tenant_id与 C 端账号一对一
ot_courier_locationcourier_id FK, lat, lng, reported_at高频写,后期可迁时序库
ot_user_couponuser_id, coupon_id, order_id, status核销写幂等

逐表全字段database_schema.htmldatabase_schema_detail.md 为准;本页仅保留与业务流程强相关的骨架。

三、ER 关系图(ot_ · Mermaid)

3.1 核心关系(跑腿主线 + 营销 + 站点扩展)

下图覆盖 index 原摘要图并补充 钱包用户 关联;站点/仓储/物流为 Phase 4~5 扩展域。

erDiagram ot_user ||--o| ot_user_member : "member_profile" ot_user ||--o{ ot_order : "places" ot_user ||--o| ot_wallet : "wallet" ot_order ||--o{ ot_order_address : "addresses" ot_order ||--o{ ot_order_item : "items" ot_order ||--o{ ot_order_trace : "status_trace" ot_order }o--|| ot_courier : "assigned" ot_courier ||--o| ot_courier_location : "last_location" ot_courier ||--o{ ot_courier_trace : "order_trace" ot_courier ||--o| ot_courier_account : "ledger" ot_coupon ||--o{ ot_user_coupon : "issued" ot_user ||--o{ ot_user_coupon : "holds" ot_station ||--o{ ot_station_storage : "stores" ot_station ||--o{ ot_vehicle : "owns" ot_station ||--o{ ot_warehouse_in : "receives" ot_station ||--o{ ot_warehouse_out : "ships" ot_vehicle ||--o| ot_vehicle_location : "tracks" ot_shipment ||--o{ ot_shipment_trace : "nodes"

需要打印级静态图可使用 database_er.html 中的 SVG(文件名为历史 exp-er-core.svg,语义与上表一致,实施以 ot_ 为准)。

四、接口分层与约定(对齐业务)

4.1 鉴权与路径

推荐:用户端 Bearer(小程序 session / appJwt);骑手端 独立 scope;管理端 adminJwt + RBAC + 数据范围(tenant/station)。路径示例可与网关统一为 /app/ot/.../admin/ot/...(与历史 /app/exp/... 文档可并行,迁移时做别名)。

模块典型能力幂等/注意
用户订单quote 试算、create 下单、page、detail、cancelcreate 带 clientRequestId
用户支付预下单、回调通知、查单回调验签 + 业务幂等
用户会员/券可用券列表、积分流水、兑换券状态机与订单绑定
骑手大厅/任务hall、grab、pickup、deliver抢单分布式锁
骑手位置location/update 批量或单点限频 + 异步落库
管理调度monitor、dispatch/assign、撤回与订单状态事务一致
管理运营订单导出、骑手审核、红包配置、提现审核导出异步任务

完整路径清单见 api_list.html / api_list.md

五、扩展与升级备选方案

5.1 数据层
场景备选方案触发条件
订单表过大按 tenant_id + 时间分表 / 分库;冷数据归档单表行数或索引膨胀
位置写入过高Redis Geo + 批量刷盘;或独立时序库(TSDB)骑手规模与上报频率上升
报表拖慢 OLTP只读副本、CDC 到 ClickHouse/ES复杂统计与检索需求
强一致事件Outbox 表 + MQ,避免双写拆微服务后域间协作
5.2 接口与架构
场景备选方案说明
兼容旧客户端网关 /v1 /v2 路由 + 适配层字段只增不减,废弃走版本
开放生态OAuth2 + scope,BFF 聚合第三方站点接入
峰值下单排队、限流、库存预占与超时释放与营销叠加时尤需注意
5.3 模型演进

使用 Flyway/Liquibase 等迁移工具;大表加列优先在线 DDL;deleted_at 与物理删除策略在归档任务中单独立规。多租户从「单库 tenant 列」演进到「分库 per tenant」时,需配套连接路由与备份策略。