feat: 强化用量计费状态机,新增钱包每日消费汇总分类账

- 将 usage.billing_status 默认值从 settled 改为 pending,完善
  pending -> settled/void 的状态转换逻辑,确保终态不可逆
- 新增 WalletDailyUsageLedger 模型和聚合服务,按账单日汇总
  每个钱包的消费金额、请求数和 token 用量
- 前端钱包中心页面集成每日消费流水展示,支持与充值记录混合
  排序和分页
- 新增两个数据库迁移:修复历史数据状态一致性、创建每日汇总表
- 补充计费状态机单元测试

Closes #218

Co-authored-by: LewisPen <LewisPen@nyadoo.com>
This commit is contained in:
fawney19
2026-03-11 15:05:11 +08:00
parent 6235c772ac
commit 04ab4bd9f2
25 changed files with 1212 additions and 121 deletions

View File

@@ -60,6 +60,36 @@ export interface WalletTransactionsResponse extends WalletBalanceResponse {
offset: number
}
export interface DailyUsageRecord {
id?: string | null
date: string | null
timezone?: string | null
total_cost: number
total_requests: number
input_tokens: number
output_tokens: number
cache_creation_tokens: number
cache_read_tokens: number
first_finalized_at?: string | null
last_finalized_at?: string | null
aggregated_at?: string | null
is_today: boolean
}
export type FlowItem =
| { type: 'transaction'; data: WalletTransaction }
| { type: 'daily_usage'; data: DailyUsageRecord }
export interface WalletFlowResponse extends WalletBalanceResponse {
today_entry: DailyUsageRecord | null
items: FlowItem[]
total: number
limit: number
offset: number
}
export type TodayCostResponse = DailyUsageRecord
export interface PaymentOrder {
id: string
order_no: string
@@ -131,6 +161,16 @@ export const walletApi = {
return response.data
},
async getFlow(params?: { limit?: number; offset?: number }): Promise<WalletFlowResponse> {
const response = await apiClient.get<WalletFlowResponse>('/api/wallet/flow', { params })
return response.data
},
async getTodayCost(): Promise<TodayCostResponse> {
const response = await apiClient.get<TodayCostResponse>('/api/wallet/today-cost')
return response.data
},
async createRechargeOrder(payload: WalletRechargeCreateRequest): Promise<{
order: PaymentOrder
payment_instructions: Record<string, unknown>