闪行生活 · 海外本地生活服务平台建设方案 ShanXing Life · Overseas Local-Life Platform Solution SOLUTION PROPOSAL · BILINGUAL · V1.0
实时切换Instant switch
05
DATA MODEL

数据模型与表说明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
  }
          
图 5-1 用户、商户、门店、商品、主订单与四类业务子单。User, merchant, store, product, main order and four business sub-orders.

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
  }
          
图 5-2 支付单、分账明细、复式分录、结算单与提现单。Payment, split, double entries, settlement and payout.

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 accountphone, email, password_hash, kyc_status, locale, currency, market_id
sys_user_identity第三方身份Social identitiesuser_id, provider, open_id, union_id;UNIQUE(provider, open_id)
sys_role / sys_permission后台角色与权限点Roles and permission pointscode, name, scope;role_id, perm_key
sys_admin_user后台账号Admin accountsuser_id, display_name, dept, mfa_enabled, status
sys_market市场包Market packscode, country, currency, timezone, tax_rule, payment_channels(jsonb)
sys_config参数配置Configurationmarket_id, key, value(jsonb), version, effective_from
i18n_resource多语言词条Translation stringskey, locale, value, status, version;UNIQUE(key, locale)
onboarding_application入驻申请Onboarding applicationsapplicant_type, applicant_id, status, step, reviewer, reject_reason

B. 供给与主数据B. Supply and master data

表名 Table用途 Purpose关键字段 Key fields
driver司机档案Driver profileuser_id, city_id, license_no, licence_expiry, online_status, rating, frozen
driver_vehicle司机与车辆绑定Driver-vehicle bindingdriver_id, vehicle_id, bind_status, effective_from
rider骑手档案Rider profileuser_id, city_id, cert_status, online_status, deposit_balance, frozen
merchant商户主体Merchant entityowner_user_id, brand, category, status, commission_rate, settlement_cycle
store门店Storemerchant_id, name, address, geo(point), business_status, prepare_minutes
partner股东 / 合伙人Partneruser_id, level, city_id, contract_no, share_rate, status
vehicle车辆台账Vehicle ledgerplate_no, model, owner_type, store_id, status, mileage, insurance_expiry
rental_vehicle可租车辆与门店库存Rental inventoryvehicle_id, store_id, daily_price, deposit, status, available_from

C. 商品与定价C. Catalog and pricing

表名 Table用途 Purpose关键字段 Key fields
category业务 / 商品分类Business / product categorybiz_type, parent_id, name, sort, status
product_spu商品 SPUProduct SPUstore_id, name, category_id, description, image, status
product_sku规格与价格SKU and pricespu_id, spec(jsonb), price, currency, stock, status
store_inventory库存流水Inventory ledgersku_id, change_type, qty, before_qty, after_qty, biz_no
price_rule计价规则Pricing rulesbiz_type, city_id, base_fare, per_km, per_min, surge(jsonb), version
rental_rate_plan租车价格计划Rental rate planmodel, daily, weekly, monthly, deposit, insurance_fee, mileage_limit

D. 订单与履约D. Orders and fulfillment

表名 Table用途 Purpose关键字段 Key fields
order_main统一主订单Main orderorder_no, biz_type, user_id, store_id, currency, amounts, pay_mode, status, market_id
order_item订单商品快照Item snapshotorder_id, sku_id, name_snapshot, spec_snapshot, qty, unit_price, amount
order_address地址快照Address snapshotorder_id, type, contact, phone_enc, address, lat, lng
order_status_log状态变更日志Status logorder_id, from_status, to_status, operator_type, operator_id, reason
ride_order出行子单Ride sub-orderorder_id, vehicle_type, est_km, actual_km, actual_min, start_point, end_point, fare_detail
delivery_order配送子单Delivery sub-orderorder_id, store_id, rider_id, pickup_code, promise_at, delivered_at, rider_fee
dinein_order到店子单Dine-in sub-orderorder_id, store_id, meal_type, guests, table_no, redeem_code, redeemed_at
rental_order租车子单Rental sub-orderorder_id, vehicle_id, pickup_store, return_store, pickup_at, return_at, deposit
order_review评价Reviewsorder_id, reviewer_id, target_type, target_id, score, tags, content, reply
after_sale售后单After-salesorder_id, type, amount, reason, evidence(jsonb), status, handler

E. 支付、账本与对账E. Payment, ledger and reconciliation

表名 Table用途 Purpose关键字段 Key fields
payment_order支付单Payment orderpay_no, order_no, channel, amount, currency, status, idempotency_key
payment_transaction渠道交易流水Channel transactionspayment_id, channel_txn_id, type, amount, status, raw(jsonb)
refund_order退款单Refundsrefund_no, order_no, payment_id, amount, reason, status, channel_refund_id
split_rule分账规则Split rulesbiz_type, market_id, city_id, partner_level, platform_rate, partner_rate, version
order_split订单分账明细Order splitsorder_no, rule_id, beneficiary_type, beneficiary_id, split_rate, split_amount, status
ledger_account资金账户Ledger accountsaccount_no, owner_type, owner_id, currency, balance, frozen_balance, version
ledger_entry复式分录Double entriesvoucher_no, account_id, direction, amount, balance_after, biz_no, reversal_of
settlement_bill结算单Settlement billsbill_no, owner_type, owner_id, period, gross, commission, adjust, net, status
payout_account收款账户Payout accountsowner_type, owner_id, type, account_no_enc, holder, is_default, verified
payout_order提现单Payout orderspayout_no, owner_type, owner_id, account_id, amount, fee, status, channel_txn_id
reconciliation_detail对账明细Reconciliation detailtask_id, channel_txn_id, internal_no, channel_amount, internal_amount, status
reconciliation_diff差异单Variance ticketsdetail_id, diff_type, amount, handler, status, remark, resolved_at

F. 营销、消息与风控F. Marketing, messaging and risk

表名 Table用途 Purpose关键字段 Key fields
coupon_template券模板Coupon templatesname, type, biz_scope, discount, threshold, validity, total, per_user
coupon_instance用户券实例Coupon instancestemplate_id, user_id, code, status, claimed_at, used_at, order_no
campaign营销活动Campaignsname, type, scope, start_at, end_at, budget, rules(jsonb), status
points_account积分账户Points accountsuser_id, balance, frozen; UNIQUE(user_id)
message站内信In-app messagesuser_id, type, title_key, body_args(jsonb), read_at, biz_no
im_thread / im_message会话与消息Chat threads and messagesbiz_type, order_no, participants;thread_id, sender_id, content, read_at
ticket客服工单Support ticketsuser_id, order_no, type, status, assignee, sla_due_at
risk_event风控事件Risk eventsuser_id, event_type, score, action, detail(jsonb), created_at

6.5 核心字段字典6.5 Core field dictionaries

order_main

字段 Field类型 Type约束 Constraint说明 Description
order_novarchar(32)UK, NOT NULL业务订单号,前缀标识业务与市场Business order number with business and market prefix
biz_typevarchar(24)NOT NULLRIDE / DELIVERY / DINEIN / RENTAL / LOGISTICS / HOTEL / FLIGHTRIDE / DELIVERY / DINEIN / RENTAL / LOGISTICS / HOTEL / FLIGHT
user_idbigintFK, NOT NULL下单用户Ordering user
merchant_id / store_idbigintNULL所属商户与门店Merchant and store
market_id / city_idbigintNOT NULL用于分库、分账、税务与报表Used for sharding, split, tax and reporting
currencychar(3)NOT NULLISO 4217,如 SGD / USD / AEDISO 4217, e.g. SGD / USD / AED
payable_amountnumeric(18,4)NOT NULL应付 = 原价 − 优惠 + 税费 + 附加费Payable = total − discount + tax + surcharge
pay_modevarchar(12)NOT NULLONLINE / OFFLINEONLINE / OFFLINE
order_statusvarchar(24)NOT NULL统一状态机,由订单服务独占写入Unified state machine, written only by the order service
fx_rate_snapshotjsonbNULL下单时汇率快照FX snapshot at order time

payment_order

字段 Field类型 Type约束 Constraint说明 Description
pay_novarchar(32)UK支付单号Payment number
order_novarchar(32)FK, INDEX关联主订单Linked main order
channelvarchar(24)NOT NULLCARD / APPLE_PAY / PAYPAL / WALLET / BANK_TRANSFER / CASHCARD / APPLE_PAY / PAYPAL / WALLET / BANK_TRANSFER / CASH
amount / currencynumeric(18,4) / char(3)NOT NULL支付金额与币种Amount and currency
statusvarchar(16)NOT NULLINIT / PENDING / SUCCESS / FAILED / REFUNDEDINIT / PENDING / SUCCESS / FAILED / REFUNDED
idempotency_keyvarchar(64)UK防重复扣款Prevents duplicate charges
channel_txn_idvarchar(64)INDEX渠道流水号,用于对账Channel transaction ID for reconciliation
expires_at / paid_attimestamptzNULL支付超时与成功时间Payment expiry and success time

ledger_entry

字段 Field类型 Type约束 Constraint说明 Description
voucher_novarchar(32)FK, INDEX记账凭证号,一张凭证至少两行Voucher number with at least two lines
account_idbigintFK, INDEX资金账户Ledger account
directionchar(1)NOT NULLD 借 / C 贷,同一凭证必须借贷平衡D debit / C credit; vouchers must balance
amountnumeric(18,4)> 0分录金额Entry amount
balance_afternumeric(18,4)NOT NULL记账后余额Balance after posting
biz_type / biz_novarchar / varcharNOT NULL业务类型与单号Business type and number
reversal_ofbigintNULL冲正时指向原分录,禁止修改原记录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_spuGiST 地理索引 + 城市 + 状态复合索引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.

建模红线Modeling red lines
① 不允许在业务表直接改余额;② 不允许跨服务直连数据库;③ 不允许用浮点数存金额;④ 不允许删除或修改已入账分录;⑤ 不允许在没有幂等键的情况下调用支付或提现接口。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.