@@ -95,8 +156,18 @@ export default function Dashboard() {
formatter={(value) => formatAmount(value)}
labelStyle={{ fontWeight: 600 }}
/>
-
-
+
+
) : (
@@ -122,7 +193,9 @@ export default function Dashboard() {
outerRadius={100}
dataKey="total_amount"
nameKey="category"
- label={({ category, percent }) => `${category} ${(percent * 100).toFixed(0)}%`}
+ label={({ category, percent }) =>
+ `${category} ${(percent * 100).toFixed(0)}%`
+ }
labelLine={false}
>
{overview.categoryStats.slice(0, 8).map((_, idx) => (
@@ -162,18 +235,30 @@ export default function Dashboard() {
{overview.sourceStats.map((s, i) => (
|
-
- {s.source_type === 'alipay' && '支付宝'}
- {s.source_type === 'wechat' && '微信支付'}
- {s.source_type === 'bank_debit' && '银行借记卡'}
- {s.source_type === 'bank_credit' && '银行贷记卡'}
- {s.source_type === 'abc' && '农业银行'}
+
+ {s.source_type === "alipay" && "支付宝"}
+ {s.source_type === "wechat" && "微信支付"}
+ {s.source_type === "bank_debit" && "银行借记卡"}
+ {s.source_type === "bank_credit" && "银行贷记卡"}
+ {s.source_type === "abc" && "农业银行"}
|
{s.count} |
- {formatAmount(s.total_income)} |
- {formatAmount(s.total_expense)} |
- = 0 ? 'stat-value income' : 'stat-value expense'}>
+ |
+ {formatAmount(s.total_income)}
+ |
+
+ {formatAmount(s.total_expense)}
+ |
+ = 0
+ ? "stat-value income"
+ : "stat-value expense"
+ }
+ >
{formatAmount(s.total_income - s.total_expense)}
|
diff --git a/server/routes/stats.js b/server/routes/stats.js
index e37b89f..a9c189e 100644
--- a/server/routes/stats.js
+++ b/server/routes/stats.js
@@ -1,108 +1,133 @@
-const express = require('express');
-const { getDB } = require('../db');
+const express = require("express");
+const { getDB } = require("../db");
const router = express.Router();
-// GET /api/stats/overview - 总览统计
-router.get('/overview', (req, res) => {
+// GET /api/stats/overview — 总览统计(基于 deals)
+router.get("/overview", (req, res) => {
const db = getDB();
const { year, month } = req.query;
- let dateCondition = '';
+ let where = "";
const params = [];
if (year) {
- dateCondition = 'WHERE strftime(\'%Y\', transaction_date) = ?';
+ where = "WHERE strftime('%Y', d.deal_date) = ?";
params.push(String(year));
if (month) {
- dateCondition += ' AND strftime(\'%m\', transaction_date) = ?';
- params.push(String(month).padStart(2, '0'));
+ where += " AND strftime('%m', d.deal_date) = ?";
+ params.push(String(month).padStart(2, "0"));
}
}
- const stats = db.prepare(`
+ const stats = db
+ .prepare(
+ `
SELECT
COUNT(*) as total_count,
- COALESCE(SUM(CASE WHEN direction = 'income' THEN amount ELSE 0 END), 0) as total_income,
- COALESCE(SUM(CASE WHEN direction = 'expense' THEN ABS(amount) ELSE 0 END), 0) as total_expense,
- COUNT(CASE WHEN reviewed = 0 AND ABS(amount) >= 100 THEN 1 END) as pending_review,
- COUNT(CASE WHEN reviewed = 1 THEN 1 END) as reviewed_count,
- COUNT(CASE WHEN matched_transaction_id IS NOT NULL THEN 1 END) as matched_count
- FROM transactions
- ${dateCondition}
- `).get(...params);
+ 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}
+ `,
+ )
+ .get(...params);
- // 按分类统计支出
- const categoryStats = db.prepare(`
+ // 按分类统计支出(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";
+
+ const categoryStats = db
+ .prepare(
+ `
SELECT
- category,
+ d.category as category,
COUNT(*) as count,
- SUM(ABS(amount)) as total_amount,
- AVG(ABS(amount)) as avg_amount
- FROM transactions
- ${dateCondition}${dateCondition ? ' AND' : 'WHERE'} direction = 'expense' AND category IS NOT NULL
- GROUP BY category
+ SUM(ABS(d.amount)) as total_amount,
+ AVG(ABS(d.amount)) as avg_amount
+ FROM deals d
+ ${catWhere}
+ GROUP BY d.category
ORDER BY total_amount DESC
- `).all(...params);
+ `,
+ )
+ .all(...params);
- // 按来源统计
- const sourceStats = db.prepare(`
+ // 按来源统计(通过 deal_transactions 关联 transactions)
+ const srcWhere = where ? where + "" : "";
+
+ const sourceStats = 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 transactions t
+ 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
- ${dateCondition}
+ ${srcWhere}
GROUP BY t.source_type
ORDER BY total_expense DESC
- `).all(...params);
+ `,
+ )
+ .all(...params);
- res.json({
- overview: stats,
- categoryStats,
- sourceStats
- });
+ res.json({ overview: stats, categoryStats, sourceStats });
});
-// GET /api/stats/monthly - 月度趋势
-router.get('/monthly', (req, res) => {
+// GET /api/stats/monthly — 月度趋势(基于 deals)
+router.get("/monthly", (req, res) => {
const db = getDB();
- const { months = 12 } = req.query;
+ const months = Math.abs(parseInt(req.query.months) || 12);
- const monthlyData = db.prepare(`
+ const monthlyData = db
+ .prepare(
+ `
SELECT
- strftime('%Y-%m', transaction_date) as month,
+ strftime('%Y-%m', d.deal_date) as month,
COUNT(*) as count,
- SUM(CASE WHEN direction = 'income' THEN amount ELSE 0 END) as income,
- SUM(CASE WHEN direction = 'expense' THEN ABS(amount) ELSE 0 END) as expense
- FROM transactions
- WHERE transaction_date >= DATE('now', '-${parseInt(months)} months')
- GROUP BY strftime('%Y-%m', transaction_date)
+ 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')
+ GROUP BY strftime('%Y-%m', d.deal_date)
ORDER BY month ASC
- `).all();
+ `,
+ )
+ .all("-" + months);
res.json(monthlyData);
});
-// GET /api/stats/daily - 每日统计
-router.get('/daily', (req, res) => {
+// GET /api/stats/daily — 每日统计(基于 deals)
+router.get("/daily", (req, res) => {
const db = getDB();
- const { days = 30 } = req.query;
+ const days = Math.abs(parseInt(req.query.days) || 30);
- const dailyData = db.prepare(`
+ const dailyData = db
+ .prepare(
+ `
SELECT
- transaction_date as date,
+ d.deal_date as date,
COUNT(*) as count,
- SUM(CASE WHEN direction = 'income' THEN amount ELSE 0 END) as income,
- SUM(CASE WHEN direction = 'expense' THEN ABS(amount) ELSE 0 END) as expense
- FROM transactions
- WHERE transaction_date >= DATE('now', '-${parseInt(days)} days')
- GROUP BY transaction_date
+ 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
- `).all();
+ `,
+ )
+ .all("-" + days);
res.json(dailyData);
});