refactor(web): unify bills as UIBill, remove BillRecord

This commit is contained in:
clz
2026-01-18 21:14:54 +08:00
parent c61691249f
commit 65ea2fa477
13 changed files with 484 additions and 613 deletions

View File

@@ -1,6 +1,7 @@
<script lang="ts">
import { onMount } from 'svelte';
import { fetchBills, fetchMonthlyStats, checkHealth, type CleanedBill, type MonthlyStat } from '$lib/api';
import { cleanedBillToUIBill, type UIBill } from '$lib/models/bill';
import { Button } from '$lib/components/ui/button';
import { Badge } from '$lib/components/ui/badge';
import * as Card from '$lib/components/ui/card';
@@ -74,25 +75,9 @@
return `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
}
// 将 CleanedBill 转换为分析服务需要的格式
function toAnalysisRecords(bills: CleanedBill[]) {
return bills.map(bill => ({
time: bill.time,
category: bill.category,
merchant: bill.merchant,
description: bill.description,
income_expense: bill.income_expense,
amount: String(bill.amount),
payment_method: bill.pay_method,
status: bill.status,
remark: bill.remark,
needs_review: bill.review_level,
}));
}
// 派生分析数据
let analysisRecords = $derived(isDemo ? demoRecords : toAnalysisRecords(records));
let allAnalysisRecords = $derived(isDemo ? demoRecords : toAnalysisRecords(allRecords)); // 全部数据用于每日趋势图
// 派生分析数据(统一成 UIBill
let analysisRecords: UIBill[] = $derived.by(() => (isDemo ? demoRecords : records.map(cleanedBillToUIBill)));
let allAnalysisRecords: UIBill[] = $derived.by(() => (isDemo ? demoRecords : allRecords.map(cleanedBillToUIBill)));
let categoryStats = $derived(calculateCategoryStats(analysisRecords));
let dailyExpenseData = $derived(calculateDailyExpenseData(analysisRecords));