From bf0c6ce9b1ec8887959ad7925e2a28dac505fa4b Mon Sep 17 00:00:00 2001 From: fukunee Date: Wed, 6 May 2026 22:49:19 +0800 Subject: [PATCH] Add category filter to monthly stats query and UI --- client/src/hooks/useApi.js | 4 +- client/src/pages/Dashboard.js | 7 ++++ server/routes/stats.js | 74 +++++++++++++++++------------------ 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/client/src/hooks/useApi.js b/client/src/hooks/useApi.js index cf2cbd7..e7fb122 100644 --- a/client/src/hooks/useApi.js +++ b/client/src/hooks/useApi.js @@ -119,8 +119,8 @@ export async function getOverview(params = {}) { return res.data; } -export async function getMonthly(months = 12) { - const res = await api.get("/stats/monthly", { params: { months } }); +export async function getMonthly(months = 12, categories) { + const params = { months }; if (categories && categories.length > 0) params.categories = categories.join(","); const res = await api.get("/stats/monthly", { params }); return res.data; } diff --git a/client/src/pages/Dashboard.js b/client/src/pages/Dashboard.js index f4a5921..4b6a401 100644 --- a/client/src/pages/Dashboard.js +++ b/client/src/pages/Dashboard.js @@ -94,6 +94,13 @@ export default function Dashboard() { loadData(); }, []); + // 勾选变化 → 重新拉月度数据 + useEffect(() => { + if (!raw) return; + const cats = [...checked]; + getMonthly(12, cats).then(setMonthly).catch(console.error); + }, [checked, raw]); + async function loadData() { setLoading(true); try { diff --git a/server/routes/stats.js b/server/routes/stats.js index 1acf7f2..cca2875 100644 --- a/server/routes/stats.js +++ b/server/routes/stats.js @@ -22,65 +22,51 @@ router.get("/overview", (req, res) => { const stats = db .prepare( - ` - SELECT + `SELECT COUNT(*) as total_count, COALESCE(SUM(CASE WHEN d.direction = 'income' THEN d.amount ELSE 0 END), 0) as total_income, COALESCE(SUM(CASE WHEN d.direction = 'expense' THEN ABS(d.amount) ELSE 0 END), 0) as total_expense, COUNT(CASE WHEN d.reviewed = 0 AND ABS(d.amount) >= 100 THEN 1 END) as pending_review, COUNT(CASE WHEN d.reviewed = 1 THEN 1 END) as reviewed_count, COALESCE(SUM(d.match_count), 0) as matched_count - FROM deals d - ${where} - `, + FROM deals d ${where}`, ) .get(...params); - // 按分类统计支出(用于饼图) const catWhere = where - ? where + - " AND d.direction = 'expense' AND d.category IS NOT NULL" + ? where + " AND d.direction = 'expense' AND d.category IS NOT NULL" : "WHERE d.direction = 'expense' AND d.category IS NOT NULL"; const categoryStats = db .prepare( - ` - SELECT + `SELECT d.category as category, COUNT(*) as count, SUM(ABS(d.amount)) as total_amount, AVG(ABS(d.amount)) as avg_amount - FROM deals d - ${catWhere} + FROM deals d ${catWhere} GROUP BY d.category - ORDER BY total_amount DESC - `, + ORDER BY total_amount DESC`, ) .all(...params); - // 按分类汇总(收入+支出) const catBreakWhere = where - ? where + - " AND d.category IS NOT NULL" + ? where + " AND d.category IS NOT NULL" : "WHERE d.category IS NOT NULL"; const categoryBreakdown = db .prepare( - ` - SELECT + `SELECT 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} + FROM deals d ${catBreakWhere} GROUP BY d.category - ORDER BY (total_income + total_expense) DESC - `, + ORDER BY (total_income + total_expense) DESC`, ) .all(...params); - // 为每个分类查最大收入/支出 Deal const maxIncomeStmt = db.prepare( `SELECT id, amount, counterparty, deal_date, ai_description FROM deals WHERE category=? AND direction='income' @@ -100,26 +86,38 @@ router.get("/overview", (req, res) => { res.json({ overview: stats, categoryStats, categoryBreakdown }); }); -// GET /api/stats/monthly — 月度趋势(基于 deals) +// GET /api/stats/monthly — 月度趋势(可选 categories 过滤) router.get("/monthly", (req, res) => { const db = getDB(); const months = Math.abs(parseInt(req.query.months) || 12); + let catFilter = ""; + const params = ["-" + months]; + + if (req.query.categories) { + const cats = req.query.categories + .split(",") + .map((c) => c.trim()) + .filter(Boolean); + if (cats.length > 0) { + catFilter = " AND d.category IN (" + cats.map(() => "?").join(",") + ")"; + params.push(...cats); + } + } + const monthlyData = db .prepare( - ` - SELECT + `SELECT strftime('%Y-%m', d.deal_date) as month, COUNT(*) as count, - SUM(CASE WHEN d.direction = 'income' THEN d.amount ELSE 0 END) as income, - SUM(CASE WHEN d.direction = 'expense' THEN ABS(d.amount) ELSE 0 END) as expense + SUM(CASE WHEN d.direction='income' THEN d.amount ELSE 0 END) as income, + SUM(CASE WHEN d.direction='expense' THEN ABS(d.amount) ELSE 0 END) as expense FROM deals d - WHERE d.deal_date >= DATE('now', ? || ' months') + WHERE d.deal_date >= DATE('now', ? || ' months')${catFilter} GROUP BY strftime('%Y-%m', d.deal_date) - ORDER BY month ASC - `, + ORDER BY month ASC`, ) - .all("-" + months); + .all(...params); res.json(monthlyData); }); @@ -131,17 +129,15 @@ router.get("/daily", (req, res) => { const dailyData = db .prepare( - ` - SELECT + `SELECT d.deal_date as date, COUNT(*) as count, - SUM(CASE WHEN d.direction = 'income' THEN d.amount ELSE 0 END) as income, - SUM(CASE WHEN d.direction = 'expense' THEN ABS(d.amount) ELSE 0 END) as expense + SUM(CASE WHEN d.direction='income' THEN d.amount ELSE 0 END) as income, + SUM(CASE WHEN d.direction='expense' THEN ABS(d.amount) ELSE 0 END) as expense FROM deals d WHERE d.deal_date >= DATE('now', ? || ' days') GROUP BY d.deal_date - ORDER BY date ASC - `, + ORDER BY date ASC`, ) .all("-" + days);