Scenario 2: 查看消費統計與 Top 3 商品
User Story: US-303: 客戶訂單歷史與統計
When: 執行操作
API 請求
GET /api/v1/customers/cust-001/stats
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Then: 預期結果
響應 Payload
{
"totalOrders": 12,
"totalSpent": 8500,
"averageOrderAmount": 708,
"lastOrderDate": "2025-10-31T10:00:00Z",
"topProducts": [
{
"productId": "prod-001",
"productName": "玫瑰花束(12 朵)",
"purchaseCount": 5,
"percentage": 42
},
{
"productId": "prod-002",
"productName": "百合花束(6 朵)",
"purchaseCount": 3,
"percentage": 25
},
{
"productId": "prod-003",
"productName": "祝賀花籃",
"purchaseCount": 2,
"percentage": 17
}
],
"monthlyTrend": [
{ "month": "2024-11", "amount": 500 },
{ "month": "2024-12", "amount": 800 },
{ "month": "2025-01", "amount": 1200 },
{ "month": "2025-02", "amount": 0 },
{ "month": "2025-03", "amount": 600 },
{ "month": "2025-04", "amount": 900 },
{ "month": "2025-05", "amount": 1100 },
{ "month": "2025-06", "amount": 0 },
{ "month": "2025-07", "amount": 750 },
{ "month": "2025-08", "amount": 850 },
{ "month": "2025-09", "amount": 600 },
{ "month": "2025-10", "amount": 1200 }
]
}
業務規則驗證
✅ Top 3 商品計算
- 統計所有已完成訂單的商品項目
- 依購買次數降序排列
- 計算佔比: 購買次數 / 總訂單數(含商品的訂單)
✅ 消費趨勢計算
- 統計近 12 個月的消費金額
- 僅計算「已完成」且「已支付」的訂單
- 月份無訂單時顯示 $0
驗證重點
- Top 3 商品正確計算
- 購買次數與佔比正確
- 近 12 個月消費趨勢正確
- 無訂單月份顯示 $0