From 1598455f5faaf5c24a6d85c99158f4b174db45fb Mon Sep 17 00:00:00 2001 From: fukunee Date: Wed, 6 May 2026 02:02:39 +0800 Subject: [PATCH] update route, reducing unnecessary complexity. Update Dashboard and Review pages, refactor statistics endpoint Replace source-based summary with categorized breakdown on Dashboard, remove batch confirm and individual confirm actions from Review page, and update server stats route to return category breakdown with max income/expense details --- client/src/pages/Dashboard.js | 139 ++++++++++++++++++++++++---------- client/src/pages/Review.js | 55 ++------------ server/routes/stats.js | 50 +++++++----- 3 files changed, 134 insertions(+), 110 deletions(-) diff --git a/client/src/pages/Dashboard.js b/client/src/pages/Dashboard.js index 7450b9d..3ab58b4 100644 --- a/client/src/pages/Dashboard.js +++ b/client/src/pages/Dashboard.js @@ -1,11 +1,5 @@ import React, { useState, useEffect } from "react"; -import { - TrendingUp, - TrendingDown, - AlertCircle, - FileText, - RefreshCcw, -} from "lucide-react"; +import { RefreshCcw } from "lucide-react"; import { BarChart, Bar, @@ -18,7 +12,12 @@ import { Pie, Cell, } from "recharts"; -import { getOverview, getMonthly, formatAmount } from "../hooks/useApi"; +import { + getOverview, + getMonthly, + formatAmount, + formatDate, +} from "../hooks/useApi"; const COLORS = [ "#4f46e5", @@ -35,6 +34,9 @@ const COLORS = [ "#e11d48", ]; +// 不参与支出饼图的分类 +const EXCLUDED_CATEGORIES = ["投资理财"]; + export default function Dashboard() { const [overview, setOverview] = useState(null); const [monthly, setMonthly] = useState([]); @@ -62,13 +64,15 @@ export default function Dashboard() { if (loading) { return (
-
- 加载中... +
加载中...
); } const ov = overview?.overview || {}; + const pieData = (overview?.categoryStats || []).filter( + (c) => !EXCLUDED_CATEGORIES.includes(c.category), + ); return (
@@ -183,11 +187,11 @@ export default function Dashboard() {

🏷️ 支出分类占比

- {overview?.categoryStats?.length > 0 ? ( + {pieData.length > 0 ? ( - {overview.categoryStats.slice(0, 8).map((_, idx) => ( + {pieData.slice(0, 8).map((_, idx) => ( ))} @@ -214,52 +218,103 @@ export default function Dashboard() {
- {/* Source Summary */} + {/* Category Breakdown Table */}
-

🏦 各来源汇总

+

📋 分类收支明细

- {overview?.sourceStats?.length > 0 ? ( + {overview?.categoryBreakdown?.length > 0 ? (
- - - - - + + + + + + + - {overview.sourceStats.map((s, i) => ( + {overview.categoryBreakdown.map((cat, i) => ( - - + - + + ))} diff --git a/client/src/pages/Review.js b/client/src/pages/Review.js index d34fd3e..2ab38a1 100644 --- a/client/src/pages/Review.js +++ b/client/src/pages/Review.js @@ -1,9 +1,8 @@ import React, { useState, useEffect } from "react"; -import { CheckCircle, Edit3, Loader2 } from "lucide-react"; +import { Edit3, Loader2 } from "lucide-react"; import { getDeals, updateDeal, - batchConfirmDeals, formatAmount, formatDate, SOURCE_TYPE_MAP, @@ -21,7 +20,7 @@ export default function Review() { const [totalPages, setTotalPages] = useState(0); const [categories, setCategories] = useState([]); const [editingDeal, setEditingDeal] = useState(null); - const [showReviewed, setShowReviewed] = useState(false); + const [showReviewed, setShowReviewed] = useState(true); const [keyword, setKeyword] = useState(""); useEffect(() => { @@ -60,7 +59,6 @@ export default function Review() { if (keyword) params.keyword = keyword; const result = await getDeals(params); - if (append) { setDeals((prev) => [...prev, ...result.deals]); } else { @@ -69,7 +67,6 @@ export default function Review() { setTotal(result.total); setPage(result.page); setTotalPages(result.totalPages); - updateBadge(result, isFirstPage); } catch (err) {} setLoading(false); @@ -98,13 +95,6 @@ export default function Review() { loadReviewDeals(page + 1, true); } - async function handleConfirm(deal) { - try { - await updateDeal(deal.id, { reviewed: 1 }); - loadReviewDeals(1, false); - } catch (err) {} - } - async function handleUpdateDeal(id, updates) { try { await updateDeal(id, updates); @@ -113,21 +103,6 @@ export default function Review() { } catch (err) {} } - async function handleConfirmAll() { - const unreviewed = deals.filter((d) => !d.reviewed); - if (unreviewed.length === 0) return; - if ( - !window.confirm( - `确定要批量确认当前已加载的 ${unreviewed.length} 笔交易吗?(共 ${total} 笔待确认)`, - ) - ) - return; - try { - await batchConfirmDeals(unreviewed.map((d) => d.id)); - loadReviewDeals(1, false); - } catch (err) {} - } - const hasMore = page < totalPages; return ( @@ -181,11 +156,6 @@ export default function Review() { )} - {!showReviewed && deals.length > 0 && ( - - )} {/* Review List */} @@ -206,7 +176,6 @@ export default function Review() { > {formatAmount(deal.amount)} -
{deal.ai_description || deal.description || "无描述"} @@ -237,7 +206,6 @@ export default function Review() { )}
-
{deal.transactions && deal.transactions.length > 0 && ( setEditingDeal({ ...deal })} > - 编辑 + {deal.reviewed ? "修改" : "编辑"} - {!deal.reviewed && ( - - )}
))} @@ -278,12 +238,8 @@ export default function Review() { <> + style={{ animation: "spin 0.8s linear infinite" }} + />{" "} 加载中... ) : ( @@ -362,7 +318,6 @@ export default function Review() { )} - {/* Show associated transactions in modal */} {editingDeal.transactions && editingDeal.transactions.length > 0 && (
{ ) .get(...params); - // 按分类统计支出(from deals) + // 按分类统计支出(用于饼图) const catWhere = where ? where + " AND d.direction = 'expense' AND d.category IS NOT NULL" : "WHERE d.direction = 'expense' AND d.category IS NOT NULL"; @@ -57,31 +57,45 @@ router.get("/overview", (req, res) => { ) .all(...params); - // 按来源统计(通过 deal_transactions 关联 transactions) - const srcWhere = where ? where + "" : ""; + // 按分类汇总(收入+支出),排除投资理财 + const catBreakWhere = where + ? where + " AND d.category IS NOT NULL AND d.category != '投资理财'" + : "WHERE d.category IS NOT NULL AND d.category != '投资理财'"; - const sourceStats = db + const categoryBreakdown = db .prepare( ` SELECT - t.source_type, - s.source_name, - COUNT(DISTINCT dt.deal_id) as deal_count, - COUNT(*) as count, - SUM(CASE WHEN t.direction = 'income' THEN t.amount ELSE 0 END) as total_income, - SUM(CASE WHEN t.direction = 'expense' THEN ABS(t.amount) ELSE 0 END) as total_expense - FROM deal_transactions dt - JOIN deals d ON d.id = dt.deal_id - JOIN transactions t ON t.id = dt.transaction_id - LEFT JOIN sources s ON s.id = t.source_id - ${srcWhere} - GROUP BY t.source_type - ORDER BY total_expense DESC + d.category, + COUNT(*) as deal_count, + SUM(CASE WHEN d.direction='income' THEN d.amount ELSE 0 END) as total_income, + SUM(CASE WHEN d.direction='expense' THEN ABS(d.amount) ELSE 0 END) as total_expense + FROM deals d + ${catBreakWhere} + GROUP BY d.category + ORDER BY (total_income + total_expense) DESC `, ) .all(...params); - res.json({ overview: stats, categoryStats, sourceStats }); + // 为每个分类查最大收入/支出 Deal + const maxIncomeStmt = db.prepare( + `SELECT id, amount, counterparty, deal_date, ai_description + FROM deals WHERE category=? AND direction='income' + ORDER BY amount DESC LIMIT 1`, + ); + const maxExpenseStmt = db.prepare( + `SELECT id, amount, counterparty, deal_date, ai_description + FROM deals WHERE category=? AND direction='expense' + ORDER BY ABS(amount) DESC LIMIT 1`, + ); + + for (const cat of categoryBreakdown) { + cat.max_income = maxIncomeStmt.get(cat.category) || null; + cat.max_expense = maxExpenseStmt.get(cat.category) || null; + } + + res.json({ overview: stats, categoryStats, categoryBreakdown }); }); // GET /api/stats/monthly — 月度趋势(基于 deals)
来源交易笔数收入合计支出合计净额分类笔数收入支出净额最大收入最大支出
- - {s.source_type === "alipay" && "支付宝"} - {s.source_type === "wechat" && "微信支付"} - {s.source_type === "bank_debit" && "银行借记卡"} - {s.source_type === "bank_credit" && "银行贷记卡"} - {s.source_type === "abc" && "农业银行"} - + {cat.category} {s.count} - {formatAmount(s.total_income)} + {cat.deal_count} + {cat.total_income > 0 + ? formatAmount(cat.total_income) + : "--"} - {formatAmount(s.total_expense)} + + {cat.total_expense > 0 + ? formatAmount(-cat.total_expense) + : "--"} = 0 - ? "stat-value income" - : "stat-value expense" - } + style={{ + textAlign: "right", + fontWeight: 600, + color: + cat.total_income - cat.total_expense >= 0 + ? "var(--success)" + : "var(--danger)", + }} > - {formatAmount(s.total_income - s.total_expense)} + {formatAmount(cat.total_income - cat.total_expense)} + + {cat.max_income ? ( + + + {formatAmount(cat.max_income.amount)} + + + {cat.max_income.counterparty || ""} + + + ) : ( + + -- + + )} + + {cat.max_expense ? ( + + + {formatAmount(cat.max_expense.amount)} + + + {cat.max_expense.counterparty || ""} + + + ) : ( + + -- + + )}