---
title: "完整例子：Aurora 订阅市场"
---

> Documentation Index
> Fetch the complete documentation index at: https://koine.systems/llms.txt
> Use this file to discover all available pages before exploring further.

# 完整例子：Aurora 订阅市场

## 1. 业务背景

Aurora 是一个订阅市场。客户向卖家购买订阅计划，平台负责订单、发票、收款、退款、拒付和争议处理。一个订单从草稿开始，经下单、开票、支付，之后可能退款或拒付；订阅有独立的试用和正式状态；订单行金额带币种；平台以 USD 结算，支付时按入账时点的历史汇率折算并保存；卖家服务半径使用长度 quantity 和 unit literal。

这个模型按业务顺序组织：

- 先描述客户、卖家、订单、订阅、发票和争议这些业务对象。
- 再写订单归属、状态、金额、时间和跨对象关联。
- 最后写业务动作、角色视角和可回放场景。

## 2. 领域模型

```koine part
// Aurora 订阅市场：客户购买卖家的订阅计划，平台处理订单、发票和支付。
module market {
  // 购买订阅的客户。
  entity Customer
  // 提供订阅计划的卖家。
  entity Seller
  // 一次购买，承载订单状态和支付结果。
  entity Order
  // 订单中的一行，记录购买项和金额。
  entity Line
  // 客户持有的订阅，独立于单次订单。
  entity Subscription
  // 订单对应的发票，支付和退款会改变其状态。
  entity Invoice
  // 支付争议，记录与订单相关的争议状态。
  entity Dispute
}

// 结算币种。
value Currency = { USD, CNY, EUR }

// 订单生命周期：草稿 -> 已下单 -> 已支付 -> 退款/拒付。
value OrderStatus = { Draft, Placed, Paid, Refunded, ChargedBack }

// 订阅生命周期：试用、正式、逾期、暂停、取消。
value SubscriptionStatus = { Trial, Active, PastDue, Paused, Cancelled }

// 发票生命周期：草稿、已开具、已支付、作废。
value InvoiceStatus = { Draft, Issued, Paid, Void }

// 争议生命周期。
value DisputeStatus = { Open, Won, Lost, Closed }

// 金额 + 币种。产品值没有身份，按字段比较。
value Money = {
  amount: Decimal,
  currency: Currency
} where amount >= 0

// 服务距离的量纲和单位。
quantity Length

unit Meter : Length = 1

unit Kilometer : Length = 1000 Meter

// 客户在销售上下文中的别名；与 Customer 是同一实体类型。
alias Buyer = Customer
```

### 参与者与归属

```koine part
/**
 * @gloss {order} 由 {customer} 下单
 * @example 客户 c1 在 2024-01-01 下单，产生订单 o1：placed_by(o1, c1)
 */
relation placed_by(order: Order, customer: Buyer)

/**
 * @gloss {order} 由 {seller} 销售
 * @example 订单 o1 由卖家 s1 销售：sold_by(o1, s1)
 */
relation sold_by(order: Order, seller: Seller)

/**
 * @gloss {line} 属于 {order}
 * @example 订单 o1 包含订阅行 l1：line_in(l1, o1)
 */
relation line_in(line: Line, order: Order)

/**
 * @gloss {sub} 的订阅客户是 {customer}
 * @example 客户 c1 持有试用中的订阅 sub1：subscription_for(sub1, c1)
 */
relation subscription_for(sub: Subscription, customer: Customer)

/**
 * @gloss {sub} 对应 {order}
 * @example 订阅 sub1 由订单 o1 开通：subscription_order(sub1, o1)
 */
relation subscription_order(sub: Subscription, order: Order)

/**
 * @gloss {invoice} 对应 {order}
 * @example 平台为订单 o1 开具发票 inv1：invoice_for(inv1, o1)
 */
relation invoice_for(invoice: Invoice, order: Order)

/**
 * @gloss {dispute} 对应 {order}
 * @example 客户就订单 o1 的扣款发起争议 d1：dispute_for(d1, o1)
 */
relation dispute_for(dispute: Dispute, order: Order)
```

```mermaid
flowchart LR
  c1["c1: Customer"] ---|placed_by| o1["o1: Order"]
  s1["s1: Seller"] ---|sold_by| o1
  l1["l1: Line"] ---|line_in| o1
  sub1["sub1: Subscription"] ---|subscription_for| c1
  sub1 ---|subscription_order| o1
  inv1["inv1: Invoice"] ---|invoice_for| o1
  d1["d1: Dispute"] ---|dispute_for| o1
```

这张图只画本节 7 个关系在初始场景 s0（见[初始业务场景](#初始业务场景)）上的取值：节点是 s0 里的实例，边上的标签是本节的 relation 名，没有实体属性、方向和基数。结构本身不说明作用：placed_by 支撑归属校验与客户订单统计（place/refund/order_count）；line_in 支撑订单聚合（order_total/line_count/cheapest_line）；sold_by 支撑卖家侧归属查询；subscription_for 支撑订阅的归属和激活（subscribe）；subscription_order 记录订阅由哪张订单开通；invoice_for 支撑开票、支付、退款和拒付（issue_invoice/pay/refund/chargeback）；dispute_for 支撑争议归属查询。

### 订单、订阅和发票状态

```koine part
/**
 * @gloss {order} 的当前状态是 {value}
 * @example 支付成功后订单 o1 进入 Paid 状态：status(o1, Paid)
 */
state relation status(order: Order, value: OrderStatus) {
  key total (order)
}

/**
 * @gloss {line} 的金额是 {value}
 * @example 订阅行 l1 的 CNY 标价是 100.00：line_money(l1, Money { amount: 100.00, currency: CNY })
 */
state relation line_money(line: Line, value: Money) {
  key total (line)
}

/**
 * @gloss {order} 的结算币种是 {value}
 * @example 订单 o1 以 CNY 结算，行金额先折算再汇总：order_currency(o1, CNY)
 */
state relation order_currency(order: Order, value: Currency) {
  key total (order)
}

/**
 * @gloss {seller} 的服务半径是 {value}
 * @example 卖家 s1 只服务 10 公里以内的客户：service_radius(s1, 10 Kilometer)
 */
state relation service_radius(seller: Seller, value: Length) {
  key total (seller)
}

/**
 * @gloss {order} 在 {value} 下单
 * @example o1 在 2024-01-01T00:00:00Z 下单：placed_at(o1, 2024-01-01T00:00:00Z)
 */
state relation placed_at(order: Order, value: Timestamp) {
  key partial (order)
}

/**
 * @gloss {order} 曾经支付过
 * @example o1 支付后发生退款，paid(o1) 仍为真，status(o1, Refunded)
 */
derived relation paid(order: Order) {
  exists (t: Timestamp). paid_at(order, t)
}

/**
 * @gloss {order} 在 {value} 支付
 * @example o1 在 2024-01-01T00:00:00Z 支付，退款窗口从该时点起算 30 天：paid_at(o1, 2024-01-01T00:00:00Z)
 */
state relation paid_at(order: Order, value: Timestamp) {
  key partial (order)
}

/**
 * @gloss {order} 曾经退款
 * @example o1 支付后退款，refunded(o1) 标记退款发生过：refunded(o1)
 */
derived relation refunded(order: Order) {
  exists (t: Timestamp). refunded_at(order, t)
}

/**
 * @gloss {order} 在 {value} 退款
 * @example 退款发生在支付后第 3 天：refunded_at(o1, 2024-01-04T00:00:00Z)
 */
state relation refunded_at(order: Order, value: Timestamp) {
  key partial (order)
}

/**
 * @gloss {order} 曾经拒付
 * @example 银行扣回 o1 的款项，charged_back(o1) 为真且不能再退款：charged_back(o1)
 */
derived relation charged_back(order: Order) {
  exists (t: Timestamp). charged_back_at(order, t)
}

/**
 * @gloss {order} 在 {value} 拒付
 * @example 拒付发生在支付后第 7 天：charged_back_at(o1, 2024-01-08T00:00:00Z)
 */
state relation charged_back_at(order: Order, value: Timestamp) {
  key partial (order)
}

/**
 * @gloss {sub} 的当前订阅状态是 {value}
 * @example 试用结束后订阅 sub1 转正：subscription_status(sub1, Active)
 */
state relation subscription_status(sub: Subscription, value: SubscriptionStatus) {
  key total (sub)
}

/**
 * @gloss {sub} 在 {value} 变更订阅状态
 * @example 订阅在 2024-01-15T00:00:00Z 转正：subscription_status_at(sub1, 2024-01-15T00:00:00Z)
 */
state relation subscription_status_at(sub: Subscription, value: Timestamp) {
  key partial (sub)
}

/**
 * @gloss {invoice} 的当前发票状态是 {value}
 * @example 支付成功后发票 inv1 从 Issued 变为 Paid：invoice_status(inv1, Paid)
 */
state relation invoice_status(invoice: Invoice, value: InvoiceStatus) {
  key total (invoice)
}

/**
 * @gloss {dispute} 的当前争议状态是 {value}
 * @example 争议 d1 立案后等待裁决：dispute_status(d1, Open)
 */
state relation dispute_status(dispute: Dispute, value: DisputeStatus) {
  key total (dispute)
}

/**
 * @gloss {currency} 的当前基准汇率是 {rate}（1 单位 currency 值多少 USD）
 * @example 平台以 USD 结算，当前 CNY 基准汇率是 0.25：exchange_rate(CNY, 0.25)
 */
state relation exchange_rate(currency: Currency, rate: Decimal) {
  key total (currency)
}

/**
 * @gloss {currency} 在 {at} 的基准汇率是 {rate}
 * @example 支付入账读取 2024-01-01 的 CNY 历史汇率：exchange_rate_at(CNY, 2024-01-01T00:00:00Z, 0.25)
 */
state relation exchange_rate_at(currency: Currency, at: Timestamp, rate: Decimal) {
  key partial (currency, at)
}

/**
 * @gloss {order} 在 {at} 从 {from} 折算到 {to}，使用汇率 {rate}
 * @example 支付时把 o1 的 CNY 金额按 0.25 折算为 USD 入账：applied_rate(o1, CNY, USD, 0.25, 2024-01-01T00:00:00Z)
 */
state relation applied_rate(
  order: Order,
  from: Currency,
  to: Currency,
  rate: Decimal,
  at: Timestamp
) {
  key partial (order)
}

/**
 * @gloss {order} 的入账金额是 {value}（结算币种）
 * @example 100 CNY 按 0.25 折算，入账金额是 25.00 USD：settled_amount(o1, 25.00)
 */
state relation settled_amount(order: Order, value: Decimal) {
  key partial (order)
}
```

### 业务计算

```koine part
// 金额字段读取。
define amount_of(money: Money) -> Decimal {
  money.amount
}

// 币种字段读取。
define currency_of(money: Money) -> Currency {
  money.currency
}

// 平台结算币种：业务决定以 USD 入账。
define settlement_currency() -> Currency {
  USD
}

// 基准币汇率：from 到 to；显式保留 8 位小数。
define rate_to(from: Currency, to: Currency) -> Decimal {
  divide(exchange_rate(from), exchange_rate(to), 8, HalfEven)
}

// 行金额折算到目标币种。
define amount_in(line: Line, to: Currency) -> Decimal {
  amount_of(line_money(line)) * rate_to(currency_of(line_money(line)), to)
}

// 订单总额：汇总订单内所有行，折算到订单币种。
define order_total(order: Order) -> Money {
  Money {
    amount: sum(amount_in(line, order_currency(order)) for line: Line where line_in(line, order)),
    currency: order_currency(order)
  }
}

// 订单行数。
define line_count(order: Order) -> Int {
  count(line: Line where line_in(line, order))
}

// 订单中最低的行金额（折算到订单币种）；空行集合的 min 会报诊断。
define cheapest_line(order: Order) -> Money {
  Money {
    amount: min(amount_in(line, order_currency(order)) for line: Line where line_in(line, order)),
    currency: order_currency(order)
  }
}

// 是否存在大额行，用于风控示例（折算到 USD）。
define any_large_line(order: Order) -> Bool {
  any(amount_in(line, USD) > 1000 for line: Line where line_in(line, order))
}

// 订单是否已有已开具的发票。
define has_issued_invoice(order: Order) -> Bool {
  exists (i: Invoice). invoice_for(i, order) and invoice_status(i) == Issued
}

// 退款资格：已支付、在 30 天窗口内、未退款且未拒付。
define refundable(order: Order) -> Bool {
  status(order) == Paid
  and exists (t: Timestamp). paid_at(order, t) and now() - t <= 30 days
  and not refunded(order)
  and not charged_back(order)
}

// 客户名下的订单数。
define order_count(customer: Customer) -> Int {
  count(order: Order where placed_by(order, customer))
}
```

### 业务不变量

```koine part
// 退款必须以支付成功为前提。
axiom RefundRequiresPayment {
  forall (o: Order). refunded(o) implies paid(o)
}

// 拒付必须以支付成功为前提。
axiom ChargebackRequiresPayment {
  forall (o: Order). charged_back(o) implies paid(o)
}

// 同一订单不能既退款又拒付。
axiom NoDoubleRefund {
  forall (o: Order). not (refunded(o) and charged_back(o))
}

// 当前状态为 Paid 时，必须存在支付记录。
axiom PaidStatusRequiresPayment {
  forall (o: Order). status(o, Paid) implies paid(o)
}

// 当前状态为 Refunded 时，必须存在退款记录。
axiom RefundedStatusRequiresRefund {
  forall (o: Order). status(o, Refunded) implies refunded(o)
}

// 当前状态为 ChargedBack 时，必须存在拒付记录。
axiom ChargedBackStatusRequiresChargeback {
  forall (o: Order). status(o, ChargedBack) implies charged_back(o)
}
```

### 业务动作

```koine part
// 客户下单：校验订单归属和草稿状态，把订单提交为待支付。
transition place(order: Order, actor: Customer) {
  require placed_by(order, actor)
  require status(order) == Draft
  status(order, Placed)
  placed_at(order, now())
}

// 平台开票：已下单订单才能开票；一张发票只开一次。
transition issue_invoice(invoice: Invoice, order: Order) {
  require invoice_for(invoice, order)
  require status(order) == Placed
  require invoice_status(invoice) == Draft
  invoice_status(invoice, Issued)
}

// 支付成功：发票已开具且订单待支付时，按入账时点保存汇率和结算金额。
transition pay(order: Order, invoice: Invoice, rate: Decimal) {
  require invoice_for(invoice, order)
  require invoice_status(invoice) == Issued
  require status(order) == Placed
  require exchange_rate_at(order_currency(order), now(), rate)
  status(order, Paid)
  paid_at(order, now())
  invoice_status(invoice, Paid)
  applied_rate(order, order_currency(order), settlement_currency(), rate, now())
  settled_amount(order, amount_of(order_total(order)) * rate)
}

// 客户退款：校验订单归属、发票已支付和 30 天退款窗口，关闭发票。
transition refund(order: Order, invoice: Invoice, actor: Customer) {
  require placed_by(order, actor)
  require invoice_for(invoice, order)
  require invoice_status(invoice) == Paid
  require refundable(order)
  status(order, Refunded)
  refunded_at(order, now())
  invoice_status(invoice, Void)
}

// 支付渠道拒付：已支付订单才能拒付，关闭发票。
transition chargeback(order: Order, invoice: Invoice) {
  require invoice_for(invoice, order)
  require invoice_status(invoice) == Paid
  require status(order) == Paid
  status(order, ChargedBack)
  charged_back_at(order, now())
  invoice_status(invoice, Void)
}

// 订阅转正式：试用客户确认订阅。
transition subscribe(sub: Subscription, actor: Customer) {
  require subscription_for(sub, actor)
  require subscription_status(sub) == Trial
  subscription_status(sub, Active)
  subscription_status_at(sub, now())
}

// 推进业务时间：用于退款窗口、截止时间等规则。
transition wait(d: Duration) {
  time = now() + d
}
```

### 角色视角

```koine part
// 客户视角：订单状态、支付/退款和发票状态。
view customer = { status, paid, refunded, invoice_status }

// 财务视角：支付、退款、拒付、汇率和发票。
view finance  = { paid, refunded, charged_back, exchange_rate, applied_rate, settled_amount, invoice_status }

// 风控视角：拒付和争议。
view risk     = { charged_back, dispute_status }

// 运营视角：订单、订阅和发票状态。
view ops      = { status, subscription_status, invoice_status, order_currency, service_radius }
```

### 初始业务场景

```koine part
// 初始业务场景：一张草稿订单、一张草稿发票，订阅处于试用。
state s0 {
  time 2024-01-01T00:00:00Z

  c1: Customer
  s1: Seller
  o1: Order
  l1: Line
  sub1: Subscription
  inv1: Invoice
  d1: Dispute

  placed_by(o1, c1)
  sold_by(o1, s1)
  line_in(l1, o1)
  line_money(l1, Money { amount: 100.00, currency: CNY })
  order_currency(o1, CNY)
  service_radius(s1, 10 Kilometer)
  status(o1, Draft)

  subscription_for(sub1, c1)
  subscription_order(sub1, o1)
  subscription_status(sub1, Trial)

  invoice_for(inv1, o1)
  invoice_status(inv1, Draft)

  dispute_for(d1, o1)
  dispute_status(d1, Open)

  exchange_rate(USD, 1.00)
  exchange_rate(CNY, 0.25)
  exchange_rate(EUR, 1.08)
  exchange_rate_at(USD, 2024-01-01T00:00:00Z, 1.00)
  exchange_rate_at(CNY, 2024-01-01T00:00:00Z, 0.25)
  exchange_rate_at(EUR, 2024-01-01T00:00:00Z, 1.08)
}

// 默认从这张草稿订单开始。
init s0
```

### 可回放场景

```koine part
// 正常退款：下单 -> 开票 -> 支付 -> 退款。
scenario refund_flow {
  start s0
  do place(o1, c1)
  do issue_invoice(inv1, o1)
  do pay(o1, inv1, 0.25)
  do refund(o1, inv1, c1)
}

// 超过 30 天的退款会被拒绝。
scenario late_refund {
  start s0
  do place(o1, c1)
  do issue_invoice(inv1, o1)
  do pay(o1, inv1, 0.25)
  do wait(31 days)
  do refund(o1, inv1, c1)
}

// 支付后发生拒付。
scenario chargeback_flow {
  start s0
  do place(o1, c1)
  do issue_invoice(inv1, o1)
  do pay(o1, inv1, 0.25)
  do chargeback(o1, inv1)
}

// 试用订阅转为正式订阅。
scenario subscribe_flow {
  start s0
  do subscribe(sub1, c1)
}
```

## 3. 业务边界

- 退款窗口固定为 30 天；未建模部分退款、多次退款、退款手续费和人工审批。
- 金额是多币种产品值；订单总额按订单币种折算；支付时保存历史汇率和结算金额。
- 结算币种固定为 USD；未建模多结算币种、部分退款和汇兑损益。
- 拒付和退款互斥；争议只保留初始状态，未建模争议裁决流程。
- 汇率同时保留当前快照和带时间的历史行；示例只使用入账时点的一条历史汇率。
- 服务半径使用 `Length` 和 unit literal；未建模服务区几何、距离计算和位置数据。
- 订单、订阅和发票状态是当前快照；完整审计历史需要事件记录。
- 支付、退款、拒付同时保留当前状态和历史标志，用来区分“现在是什么”和“曾经发生过什么”。

实现和语言层面的边界见[验证与实验计划](https://koine.systems/notes/validation-experiments/)。

## 4. 规范与相关

- [验证与实验计划](https://koine.systems/notes/validation-experiments/)
- [基准与能力问题](https://koine.systems/notes/benchmark-suite/)
- [关系语义与历史分析](https://koine.systems/notes/relation-semantics/)
- [逻辑时钟与顺序原型](https://koine.systems/notes/logical-clock-prototype/)
- [图书馆](https://koine.systems/examples/library/)
- [审批流](https://koine.systems/examples/approval/)
- [金额、汇率与产品值](https://koine.systems/examples/money/)

Source: https://koine.systems/examples/market/index.mdx
