数据模型与表说明Data Model
数据模型遵循三条铁律:业务单据不可变、资金分录只追加、所有金额带币种。以下给出领域关系、表清单、核心字段字典与索引 / 合规策略。Three rules: business documents are immutable, ledger entries are append-only, and every amount carries a currency. This section covers relationships, tables, key fields, indexing and compliance.
6.1 建模原则6.1 Modeling principles
统一订单Unified order
order_main 承载通用字段,业务子单 1:1 扩展,避免宽表与空字段。order_main holds shared fields; sub-orders extend 1:1 to avoid wide tables and nulls.
金额带币种Amount + currency
统一 numeric(18,4) + currency,禁止 float / double,展示精度与结算精度分离。numeric(18,4) plus currency; no floats; display and settlement precision separated.
单据快照Snapshots
商品、价格、地址、分账比例在下单时写入快照,主数据变更不影响历史订单。Product, price, address and split ratio are snapshotted at order time.
只追加与冲正Append-only
分录不可修改;错误以反向分录冲正;状态变更写事件日志并保留操作人。Entries are immutable; reversals are new entries; state changes log the operator.
6.2 交易与供给域 ER6.2 Trade and supply ER
erDiagram
USER ||--o{ ORDER_MAIN : places
USER ||--o| DRIVER : acts_as
USER ||--o| RIDER : acts_as
USER ||--o| MERCHANT : owns
MERCHANT ||--o{ STORE : operates
STORE ||--o{ PRODUCT_SKU : sells
ORDER_MAIN ||--o{ ORDER_ITEM : contains
ORDER_MAIN ||--o| RIDE_ORDER : extends
ORDER_MAIN ||--o| DELIVERY_ORDER : extends
ORDER_MAIN ||--o| DINEIN_ORDER : extends
ORDER_MAIN ||--o| RENTAL_ORDER : extends
DRIVER ||--o{ RIDE_ORDER : fulfills
RIDER ||--o{ DELIVERY_ORDER : delivers
STORE ||--o{ DINEIN_ORDER : serves
ORDER_MAIN {
bigint id PK
varchar order_no UK
varchar biz_type
bigint user_id FK
varchar currency
numeric payable_amount
varchar order_status
timestamptz created_at
}
6.3 资金域 ER6.3 Money domain ER
erDiagram
ORDER_MAIN ||--o{ PAYMENT_ORDER : paid_by
PAYMENT_ORDER ||--o{ PAYMENT_TRANSACTION : records
ORDER_MAIN ||--o{ ORDER_SPLIT : splits
ORDER_SPLIT }o--|| LEDGER_ACCOUNT : credits
LEDGER_ACCOUNT ||--o{ LEDGER_ENTRY : posts
SETTLEMENT_BILL ||--o{ PAYOUT_ORDER : pays
PARTNER ||--o{ ORDER_SPLIT : receives
PAYMENT_ORDER {
bigint id PK
varchar pay_no UK
varchar order_no FK
varchar channel
numeric amount
varchar status
varchar idempotency_key UK
}
ORDER_SPLIT {
bigint id PK
varchar order_no FK
bigint rule_id FK
varchar beneficiary_type
bigint beneficiary_id
numeric split_rate
numeric split_amount
varchar status
}
LEDGER_ENTRY {
bigint id PK
varchar voucher_no FK
bigint account_id FK
varchar direction
numeric amount
numeric balance_after
varchar biz_no
}
SETTLEMENT_BILL {
bigint id PK
varchar bill_no UK
varchar owner_type
bigint owner_id
numeric gross_amount
numeric commission_amount
numeric net_amount
varchar status
}
6.4 核心表清单6.4 Core table inventory
以下为 50 余张核心表,另有日志、临时与供应商缓存表未列出;字段类型以 PostgreSQL 16 为例。Around 50 core tables are listed; logs, temporary and supplier cache tables are omitted. Types follow PostgreSQL 16.
A. 身份、权限与平台配置A. Identity, permissions and platform config
| 表名 Table | 用途 Purpose | 关键字段 Key fields |
|---|---|---|
| sys_user | 统一账户Unified account | phone, email, password_hash, kyc_status, locale, currency, market_id |
| sys_user_identity | 第三方身份Social identities | user_id, provider, open_id, union_id;UNIQUE(provider, open_id) |
| sys_role / sys_permission | 后台角色与权限点Roles and permission points | code, name, scope;role_id, perm_key |
| sys_admin_user | 后台账号Admin accounts | user_id, display_name, dept, mfa_enabled, status |
| sys_market | 市场包Market packs | code, country, currency, timezone, tax_rule, payment_channels(jsonb) |
| sys_config | 参数配置Configuration | market_id, key, value(jsonb), version, effective_from |
| i18n_resource | 多语言词条Translation strings | key, locale, value, status, version;UNIQUE(key, locale) |
| onboarding_application | 入驻申请Onboarding applications | applicant_type, applicant_id, status, step, reviewer, reject_reason |
B. 供给与主数据B. Supply and master data
| 表名 Table | 用途 Purpose | 关键字段 Key fields |
|---|---|---|
| driver | 司机档案Driver profile | user_id, city_id, license_no, licence_expiry, online_status, rating, frozen |
| driver_vehicle | 司机与车辆绑定Driver-vehicle binding | driver_id, vehicle_id, bind_status, effective_from |
| rider | 骑手档案Rider profile | user_id, city_id, cert_status, online_status, deposit_balance, frozen |
| merchant | 商户主体Merchant entity | owner_user_id, brand, category, status, commission_rate, settlement_cycle |
| store | 门店Store | merchant_id, name, address, geo(point), business_status, prepare_minutes |
| partner | 股东 / 合伙人Partner | user_id, level, city_id, contract_no, share_rate, status |
| vehicle | 车辆台账Vehicle ledger | plate_no, model, owner_type, store_id, status, mileage, insurance_expiry |
| rental_vehicle | 可租车辆与门店库存Rental inventory | vehicle_id, store_id, daily_price, deposit, status, available_from |
C. 商品与定价C. Catalog and pricing
| 表名 Table | 用途 Purpose | 关键字段 Key fields |
|---|---|---|
| category | 业务 / 商品分类Business / product category | biz_type, parent_id, name, sort, status |
| product_spu | 商品 SPUProduct SPU | store_id, name, category_id, description, image, status |
| product_sku | 规格与价格SKU and price | spu_id, spec(jsonb), price, currency, stock, status |
| store_inventory | 库存流水Inventory ledger | sku_id, change_type, qty, before_qty, after_qty, biz_no |
| price_rule | 计价规则Pricing rules | biz_type, city_id, base_fare, per_km, per_min, surge(jsonb), version |
| rental_rate_plan | 租车价格计划Rental rate plan | model, daily, weekly, monthly, deposit, insurance_fee, mileage_limit |
D. 订单与履约D. Orders and fulfillment
| 表名 Table | 用途 Purpose | 关键字段 Key fields |
|---|---|---|
| order_main | 统一主订单Main order | order_no, biz_type, user_id, store_id, currency, amounts, pay_mode, status, market_id |
| order_item | 订单商品快照Item snapshot | order_id, sku_id, name_snapshot, spec_snapshot, qty, unit_price, amount |
| order_address | 地址快照Address snapshot | order_id, type, contact, phone_enc, address, lat, lng |
| order_status_log | 状态变更日志Status log | order_id, from_status, to_status, operator_type, operator_id, reason |
| ride_order | 出行子单Ride sub-order | order_id, vehicle_type, est_km, actual_km, actual_min, start_point, end_point, fare_detail |
| delivery_order | 配送子单Delivery sub-order | order_id, store_id, rider_id, pickup_code, promise_at, delivered_at, rider_fee |
| dinein_order | 到店子单Dine-in sub-order | order_id, store_id, meal_type, guests, table_no, redeem_code, redeemed_at |
| rental_order | 租车子单Rental sub-order | order_id, vehicle_id, pickup_store, return_store, pickup_at, return_at, deposit |
| order_review | 评价Reviews | order_id, reviewer_id, target_type, target_id, score, tags, content, reply |
| after_sale | 售后单After-sales | order_id, type, amount, reason, evidence(jsonb), status, handler |
E. 支付、账本与对账E. Payment, ledger and reconciliation
| 表名 Table | 用途 Purpose | 关键字段 Key fields |
|---|---|---|
| payment_order | 支付单Payment order | pay_no, order_no, channel, amount, currency, status, idempotency_key |
| payment_transaction | 渠道交易流水Channel transactions | payment_id, channel_txn_id, type, amount, status, raw(jsonb) |
| refund_order | 退款单Refunds | refund_no, order_no, payment_id, amount, reason, status, channel_refund_id |
| split_rule | 分账规则Split rules | biz_type, market_id, city_id, partner_level, platform_rate, partner_rate, version |
| order_split | 订单分账明细Order splits | order_no, rule_id, beneficiary_type, beneficiary_id, split_rate, split_amount, status |
| ledger_account | 资金账户Ledger accounts | account_no, owner_type, owner_id, currency, balance, frozen_balance, version |
| ledger_entry | 复式分录Double entries | voucher_no, account_id, direction, amount, balance_after, biz_no, reversal_of |
| settlement_bill | 结算单Settlement bills | bill_no, owner_type, owner_id, period, gross, commission, adjust, net, status |
| payout_account | 收款账户Payout accounts | owner_type, owner_id, type, account_no_enc, holder, is_default, verified |
| payout_order | 提现单Payout orders | payout_no, owner_type, owner_id, account_id, amount, fee, status, channel_txn_id |
| reconciliation_detail | 对账明细Reconciliation detail | task_id, channel_txn_id, internal_no, channel_amount, internal_amount, status |
| reconciliation_diff | 差异单Variance tickets | detail_id, diff_type, amount, handler, status, remark, resolved_at |
F. 营销、消息与风控F. Marketing, messaging and risk
| 表名 Table | 用途 Purpose | 关键字段 Key fields |
|---|---|---|
| coupon_template | 券模板Coupon templates | name, type, biz_scope, discount, threshold, validity, total, per_user |
| coupon_instance | 用户券实例Coupon instances | template_id, user_id, code, status, claimed_at, used_at, order_no |
| campaign | 营销活动Campaigns | name, type, scope, start_at, end_at, budget, rules(jsonb), status |
| points_account | 积分账户Points accounts | user_id, balance, frozen; UNIQUE(user_id) |
| message | 站内信In-app messages | user_id, type, title_key, body_args(jsonb), read_at, biz_no |
| im_thread / im_message | 会话与消息Chat threads and messages | biz_type, order_no, participants;thread_id, sender_id, content, read_at |
| ticket | 客服工单Support tickets | user_id, order_no, type, status, assignee, sla_due_at |
| risk_event | 风控事件Risk events | user_id, event_type, score, action, detail(jsonb), created_at |
6.5 核心字段字典6.5 Core field dictionaries
order_main
| 字段 Field | 类型 Type | 约束 Constraint | 说明 Description |
|---|---|---|---|
| order_no | varchar(32) | UK, NOT NULL | 业务订单号,前缀标识业务与市场Business order number with business and market prefix |
| biz_type | varchar(24) | NOT NULL | RIDE / DELIVERY / DINEIN / RENTAL / LOGISTICS / HOTEL / FLIGHTRIDE / DELIVERY / DINEIN / RENTAL / LOGISTICS / HOTEL / FLIGHT |
| user_id | bigint | FK, NOT NULL | 下单用户Ordering user |
| merchant_id / store_id | bigint | NULL | 所属商户与门店Merchant and store |
| market_id / city_id | bigint | NOT NULL | 用于分库、分账、税务与报表Used for sharding, split, tax and reporting |
| currency | char(3) | NOT NULL | ISO 4217,如 SGD / USD / AEDISO 4217, e.g. SGD / USD / AED |
| payable_amount | numeric(18,4) | NOT NULL | 应付 = 原价 − 优惠 + 税费 + 附加费Payable = total − discount + tax + surcharge |
| pay_mode | varchar(12) | NOT NULL | ONLINE / OFFLINEONLINE / OFFLINE |
| order_status | varchar(24) | NOT NULL | 统一状态机,由订单服务独占写入Unified state machine, written only by the order service |
| fx_rate_snapshot | jsonb | NULL | 下单时汇率快照FX snapshot at order time |
payment_order
| 字段 Field | 类型 Type | 约束 Constraint | 说明 Description |
|---|---|---|---|
| pay_no | varchar(32) | UK | 支付单号Payment number |
| order_no | varchar(32) | FK, INDEX | 关联主订单Linked main order |
| channel | varchar(24) | NOT NULL | CARD / APPLE_PAY / PAYPAL / WALLET / BANK_TRANSFER / CASHCARD / APPLE_PAY / PAYPAL / WALLET / BANK_TRANSFER / CASH |
| amount / currency | numeric(18,4) / char(3) | NOT NULL | 支付金额与币种Amount and currency |
| status | varchar(16) | NOT NULL | INIT / PENDING / SUCCESS / FAILED / REFUNDEDINIT / PENDING / SUCCESS / FAILED / REFUNDED |
| idempotency_key | varchar(64) | UK | 防重复扣款Prevents duplicate charges |
| channel_txn_id | varchar(64) | INDEX | 渠道流水号,用于对账Channel transaction ID for reconciliation |
| expires_at / paid_at | timestamptz | NULL | 支付超时与成功时间Payment expiry and success time |
ledger_entry
| 字段 Field | 类型 Type | 约束 Constraint | 说明 Description |
|---|---|---|---|
| voucher_no | varchar(32) | FK, INDEX | 记账凭证号,一张凭证至少两行Voucher number with at least two lines |
| account_id | bigint | FK, INDEX | 资金账户Ledger account |
| direction | char(1) | NOT NULL | D 借 / C 贷,同一凭证必须借贷平衡D debit / C credit; vouchers must balance |
| amount | numeric(18,4) | > 0 | 分录金额Entry amount |
| balance_after | numeric(18,4) | NOT NULL | 记账后余额Balance after posting |
| biz_type / biz_no | varchar / varchar | NOT NULL | 业务类型与单号Business type and number |
| reversal_of | bigint | NULL | 冲正时指向原分录,禁止修改原记录Points to the original entry; originals are immutable |
6.6 索引、分区与生命周期6.6 Indexing, partitioning and lifecycle
| 对象 Object | 策略 Strategy | 理由 Rationale |
|---|---|---|
| order_main | 按 created_at 月分区;超阈值后按 user_id 哈希分库Monthly partition by created_at; hash sharding by user_id when threshold is reached | 订单随时间增长,用户维度查询高频Orders grow over time; user queries dominate |
| ledger_entry | 按 occurred_at 月分区;account_id + occurred_at 复合索引Monthly partition by occurred_at; composite index on account_id + occurred_at | 账务按账户与时间查询且需长期保留Account-and-time queries with long retention |
| trip_track | 按天分区,90 天后转冷存储Daily partition, cold storage after 90 days | 数据量大、价值随时间衰减High volume, value decays over time |
| message / im_message | 按 user_id 哈希 + 时间索引;历史归档Hash by user_id with time index; history archived | 读多写多,避免单表膨胀Read/write heavy; avoids table bloat |
| store / product_spu | GiST 地理索引 + 城市 + 状态复合索引GiST geo index plus city and status composite | 附近门店与在售商品检索Nearby stores and available products |
保留周期Retention
订单与账务 7 年;支付与对账 7 年;轨迹 90 天;IM 2 年;日志 180 天;营销埋点 13 个月。Orders and ledgers 7 years; payments and reconciliation 7 years; tracking 90 days; IM 2 years; logs 180 days; analytics 13 months.
隐私处理Privacy
手机号、证件号、银行卡号字段级加密;展示脱敏;日志禁止打印明文;导出需审批与水印。Field-level encryption for phone, ID and bank numbers; masked display; no plaintext in logs; approved and watermarked exports.
① 不允许在业务表直接改余额;② 不允许跨服务直连数据库;③ 不允许用浮点数存金额;④ 不允许删除或修改已入账分录;⑤ 不允许在没有幂等键的情况下调用支付或提现接口。Never mutate balances outside the ledger; no cross-service direct DB access; no floating point for money; never edit or delete posted entries; never call payment or payout APIs without an idempotency key.