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
This commit is contained in:
@@ -1,11 +1,5 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import {
|
import { RefreshCcw } from "lucide-react";
|
||||||
TrendingUp,
|
|
||||||
TrendingDown,
|
|
||||||
AlertCircle,
|
|
||||||
FileText,
|
|
||||||
RefreshCcw,
|
|
||||||
} from "lucide-react";
|
|
||||||
import {
|
import {
|
||||||
BarChart,
|
BarChart,
|
||||||
Bar,
|
Bar,
|
||||||
@@ -18,7 +12,12 @@ import {
|
|||||||
Pie,
|
Pie,
|
||||||
Cell,
|
Cell,
|
||||||
} from "recharts";
|
} from "recharts";
|
||||||
import { getOverview, getMonthly, formatAmount } from "../hooks/useApi";
|
import {
|
||||||
|
getOverview,
|
||||||
|
getMonthly,
|
||||||
|
formatAmount,
|
||||||
|
formatDate,
|
||||||
|
} from "../hooks/useApi";
|
||||||
|
|
||||||
const COLORS = [
|
const COLORS = [
|
||||||
"#4f46e5",
|
"#4f46e5",
|
||||||
@@ -35,6 +34,9 @@ const COLORS = [
|
|||||||
"#e11d48",
|
"#e11d48",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// 不参与支出饼图的分类
|
||||||
|
const EXCLUDED_CATEGORIES = ["投资理财"];
|
||||||
|
|
||||||
export default function Dashboard() {
|
export default function Dashboard() {
|
||||||
const [overview, setOverview] = useState(null);
|
const [overview, setOverview] = useState(null);
|
||||||
const [monthly, setMonthly] = useState([]);
|
const [monthly, setMonthly] = useState([]);
|
||||||
@@ -62,13 +64,15 @@ export default function Dashboard() {
|
|||||||
if (loading) {
|
if (loading) {
|
||||||
return (
|
return (
|
||||||
<div className="loading">
|
<div className="loading">
|
||||||
<div className="spinner"></div>
|
<div className="spinner"></div>加载中...
|
||||||
加载中...
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ov = overview?.overview || {};
|
const ov = overview?.overview || {};
|
||||||
|
const pieData = (overview?.categoryStats || []).filter(
|
||||||
|
(c) => !EXCLUDED_CATEGORIES.includes(c.category),
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
@@ -183,11 +187,11 @@ export default function Dashboard() {
|
|||||||
<div className="card-header">
|
<div className="card-header">
|
||||||
<h3>🏷️ 支出分类占比</h3>
|
<h3>🏷️ 支出分类占比</h3>
|
||||||
</div>
|
</div>
|
||||||
{overview?.categoryStats?.length > 0 ? (
|
{pieData.length > 0 ? (
|
||||||
<ResponsiveContainer width="100%" height={280}>
|
<ResponsiveContainer width="100%" height={280}>
|
||||||
<PieChart>
|
<PieChart>
|
||||||
<Pie
|
<Pie
|
||||||
data={overview.categoryStats.slice(0, 8)}
|
data={pieData.slice(0, 8)}
|
||||||
cx="50%"
|
cx="50%"
|
||||||
cy="50%"
|
cy="50%"
|
||||||
outerRadius={100}
|
outerRadius={100}
|
||||||
@@ -198,7 +202,7 @@ export default function Dashboard() {
|
|||||||
}
|
}
|
||||||
labelLine={false}
|
labelLine={false}
|
||||||
>
|
>
|
||||||
{overview.categoryStats.slice(0, 8).map((_, idx) => (
|
{pieData.slice(0, 8).map((_, idx) => (
|
||||||
<Cell key={idx} fill={COLORS[idx % COLORS.length]} />
|
<Cell key={idx} fill={COLORS[idx % COLORS.length]} />
|
||||||
))}
|
))}
|
||||||
</Pie>
|
</Pie>
|
||||||
@@ -214,52 +218,103 @@ export default function Dashboard() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Source Summary */}
|
{/* Category Breakdown Table */}
|
||||||
<div className="card">
|
<div className="card">
|
||||||
<div className="card-header">
|
<div className="card-header">
|
||||||
<h3>🏦 各来源汇总</h3>
|
<h3>📋 分类收支明细</h3>
|
||||||
</div>
|
</div>
|
||||||
{overview?.sourceStats?.length > 0 ? (
|
{overview?.categoryBreakdown?.length > 0 ? (
|
||||||
<div className="table-container">
|
<div className="table-container">
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>来源</th>
|
<th>分类</th>
|
||||||
<th>交易笔数</th>
|
<th style={{ textAlign: "right" }}>笔数</th>
|
||||||
<th>收入合计</th>
|
<th style={{ textAlign: "right" }}>收入</th>
|
||||||
<th>支出合计</th>
|
<th style={{ textAlign: "right" }}>支出</th>
|
||||||
<th>净额</th>
|
<th style={{ textAlign: "right" }}>净额</th>
|
||||||
|
<th>最大收入</th>
|
||||||
|
<th>最大支出</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{overview.sourceStats.map((s, i) => (
|
{overview.categoryBreakdown.map((cat, i) => (
|
||||||
<tr key={i}>
|
<tr key={i}>
|
||||||
<td>
|
<td>
|
||||||
<span
|
<span className="tag tag-matched">{cat.category}</span>
|
||||||
className={`source-type-label source-${s.source_type}`}
|
|
||||||
>
|
|
||||||
{s.source_type === "alipay" && "支付宝"}
|
|
||||||
{s.source_type === "wechat" && "微信支付"}
|
|
||||||
{s.source_type === "bank_debit" && "银行借记卡"}
|
|
||||||
{s.source_type === "bank_credit" && "银行贷记卡"}
|
|
||||||
{s.source_type === "abc" && "农业银行"}
|
|
||||||
</span>
|
|
||||||
</td>
|
</td>
|
||||||
<td>{s.count}</td>
|
<td style={{ textAlign: "right" }}>{cat.deal_count}</td>
|
||||||
<td style={{ color: "var(--success)" }}>
|
<td style={{ textAlign: "right", color: "var(--success)" }}>
|
||||||
{formatAmount(s.total_income)}
|
{cat.total_income > 0
|
||||||
|
? formatAmount(cat.total_income)
|
||||||
|
: "--"}
|
||||||
</td>
|
</td>
|
||||||
<td style={{ color: "var(--danger)" }}>
|
<td style={{ textAlign: "right", color: "var(--danger)" }}>
|
||||||
{formatAmount(s.total_expense)}
|
{cat.total_expense > 0
|
||||||
|
? formatAmount(-cat.total_expense)
|
||||||
|
: "--"}
|
||||||
</td>
|
</td>
|
||||||
<td
|
<td
|
||||||
className={
|
style={{
|
||||||
s.total_income - s.total_expense >= 0
|
textAlign: "right",
|
||||||
? "stat-value income"
|
fontWeight: 600,
|
||||||
: "stat-value expense"
|
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)}
|
||||||
|
</td>
|
||||||
|
<td style={{ fontSize: 12, maxWidth: 180 }}>
|
||||||
|
{cat.max_income ? (
|
||||||
|
<span
|
||||||
|
title={`${cat.max_income.counterparty || ""} ${cat.max_income.ai_description || ""}`}
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
style={{ color: "var(--success)", fontWeight: 500 }}
|
||||||
|
>
|
||||||
|
{formatAmount(cat.max_income.amount)}
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
style={{
|
||||||
|
color: "var(--text-secondary)",
|
||||||
|
marginLeft: 4,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{cat.max_income.counterparty || ""}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<span style={{ color: "var(--text-secondary)" }}>
|
||||||
|
--
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
<td style={{ fontSize: 12, maxWidth: 180 }}>
|
||||||
|
{cat.max_expense ? (
|
||||||
|
<span
|
||||||
|
title={`${cat.max_expense.counterparty || ""} ${cat.max_expense.ai_description || ""}`}
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
style={{ color: "var(--danger)", fontWeight: 500 }}
|
||||||
|
>
|
||||||
|
{formatAmount(cat.max_expense.amount)}
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
style={{
|
||||||
|
color: "var(--text-secondary)",
|
||||||
|
marginLeft: 4,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{cat.max_expense.counterparty || ""}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<span style={{ color: "var(--text-secondary)" }}>
|
||||||
|
--
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import { CheckCircle, Edit3, Loader2 } from "lucide-react";
|
import { Edit3, Loader2 } from "lucide-react";
|
||||||
import {
|
import {
|
||||||
getDeals,
|
getDeals,
|
||||||
updateDeal,
|
updateDeal,
|
||||||
batchConfirmDeals,
|
|
||||||
formatAmount,
|
formatAmount,
|
||||||
formatDate,
|
formatDate,
|
||||||
SOURCE_TYPE_MAP,
|
SOURCE_TYPE_MAP,
|
||||||
@@ -21,7 +20,7 @@ export default function Review() {
|
|||||||
const [totalPages, setTotalPages] = useState(0);
|
const [totalPages, setTotalPages] = useState(0);
|
||||||
const [categories, setCategories] = useState([]);
|
const [categories, setCategories] = useState([]);
|
||||||
const [editingDeal, setEditingDeal] = useState(null);
|
const [editingDeal, setEditingDeal] = useState(null);
|
||||||
const [showReviewed, setShowReviewed] = useState(false);
|
const [showReviewed, setShowReviewed] = useState(true);
|
||||||
const [keyword, setKeyword] = useState("");
|
const [keyword, setKeyword] = useState("");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -60,7 +59,6 @@ export default function Review() {
|
|||||||
if (keyword) params.keyword = keyword;
|
if (keyword) params.keyword = keyword;
|
||||||
|
|
||||||
const result = await getDeals(params);
|
const result = await getDeals(params);
|
||||||
|
|
||||||
if (append) {
|
if (append) {
|
||||||
setDeals((prev) => [...prev, ...result.deals]);
|
setDeals((prev) => [...prev, ...result.deals]);
|
||||||
} else {
|
} else {
|
||||||
@@ -69,7 +67,6 @@ export default function Review() {
|
|||||||
setTotal(result.total);
|
setTotal(result.total);
|
||||||
setPage(result.page);
|
setPage(result.page);
|
||||||
setTotalPages(result.totalPages);
|
setTotalPages(result.totalPages);
|
||||||
|
|
||||||
updateBadge(result, isFirstPage);
|
updateBadge(result, isFirstPage);
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
@@ -98,13 +95,6 @@ export default function Review() {
|
|||||||
loadReviewDeals(page + 1, true);
|
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) {
|
async function handleUpdateDeal(id, updates) {
|
||||||
try {
|
try {
|
||||||
await updateDeal(id, updates);
|
await updateDeal(id, updates);
|
||||||
@@ -113,21 +103,6 @@ export default function Review() {
|
|||||||
} catch (err) {}
|
} 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;
|
const hasMore = page < totalPages;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -181,11 +156,6 @@ export default function Review() {
|
|||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
</span>
|
</span>
|
||||||
{!showReviewed && deals.length > 0 && (
|
|
||||||
<button className="btn btn-success btn-sm" onClick={handleConfirmAll}>
|
|
||||||
<CheckCircle size={14} /> 全部确认
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Review List */}
|
{/* Review List */}
|
||||||
@@ -206,7 +176,6 @@ export default function Review() {
|
|||||||
>
|
>
|
||||||
{formatAmount(deal.amount)}
|
{formatAmount(deal.amount)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="info-col">
|
<div className="info-col">
|
||||||
<div className="title">
|
<div className="title">
|
||||||
{deal.ai_description || deal.description || "无描述"}
|
{deal.ai_description || deal.description || "无描述"}
|
||||||
@@ -237,7 +206,6 @@ export default function Review() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="action-col">
|
<div className="action-col">
|
||||||
{deal.transactions && deal.transactions.length > 0 && (
|
{deal.transactions && deal.transactions.length > 0 && (
|
||||||
<span
|
<span
|
||||||
@@ -251,16 +219,8 @@ export default function Review() {
|
|||||||
className="btn btn-secondary btn-sm"
|
className="btn btn-secondary btn-sm"
|
||||||
onClick={() => setEditingDeal({ ...deal })}
|
onClick={() => setEditingDeal({ ...deal })}
|
||||||
>
|
>
|
||||||
<Edit3 size={12} /> 编辑
|
<Edit3 size={12} /> {deal.reviewed ? "修改" : "编辑"}
|
||||||
</button>
|
</button>
|
||||||
{!deal.reviewed && (
|
|
||||||
<button
|
|
||||||
className="btn btn-success btn-sm"
|
|
||||||
onClick={() => handleConfirm(deal)}
|
|
||||||
>
|
|
||||||
<CheckCircle size={12} /> 确认
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
@@ -278,12 +238,8 @@ export default function Review() {
|
|||||||
<>
|
<>
|
||||||
<Loader2
|
<Loader2
|
||||||
size={14}
|
size={14}
|
||||||
className="spinner"
|
style={{ animation: "spin 0.8s linear infinite" }}
|
||||||
style={{
|
/>{" "}
|
||||||
border: "none",
|
|
||||||
animation: "spin 0.8s linear infinite",
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
加载中...
|
加载中...
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
@@ -362,7 +318,6 @@ export default function Review() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Show associated transactions in modal */}
|
|
||||||
{editingDeal.transactions &&
|
{editingDeal.transactions &&
|
||||||
editingDeal.transactions.length > 0 && (
|
editingDeal.transactions.length > 0 && (
|
||||||
<div
|
<div
|
||||||
|
|||||||
+32
-18
@@ -36,7 +36,7 @@ router.get("/overview", (req, res) => {
|
|||||||
)
|
)
|
||||||
.get(...params);
|
.get(...params);
|
||||||
|
|
||||||
// 按分类统计支出(from deals)
|
// 按分类统计支出(用于饼图)
|
||||||
const catWhere = where
|
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";
|
: "WHERE d.direction = 'expense' AND d.category IS NOT NULL";
|
||||||
@@ -57,31 +57,45 @@ router.get("/overview", (req, res) => {
|
|||||||
)
|
)
|
||||||
.all(...params);
|
.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(
|
.prepare(
|
||||||
`
|
`
|
||||||
SELECT
|
SELECT
|
||||||
t.source_type,
|
d.category,
|
||||||
s.source_name,
|
COUNT(*) as deal_count,
|
||||||
COUNT(DISTINCT dt.deal_id) as deal_count,
|
SUM(CASE WHEN d.direction='income' THEN d.amount ELSE 0 END) as total_income,
|
||||||
COUNT(*) as count,
|
SUM(CASE WHEN d.direction='expense' THEN ABS(d.amount) ELSE 0 END) as total_expense
|
||||||
SUM(CASE WHEN t.direction = 'income' THEN t.amount ELSE 0 END) as total_income,
|
FROM deals d
|
||||||
SUM(CASE WHEN t.direction = 'expense' THEN ABS(t.amount) ELSE 0 END) as total_expense
|
${catBreakWhere}
|
||||||
FROM deal_transactions dt
|
GROUP BY d.category
|
||||||
JOIN deals d ON d.id = dt.deal_id
|
ORDER BY (total_income + total_expense) DESC
|
||||||
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
|
|
||||||
`,
|
`,
|
||||||
)
|
)
|
||||||
.all(...params);
|
.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)
|
// GET /api/stats/monthly — 月度趋势(基于 deals)
|
||||||
|
|||||||
Reference in New Issue
Block a user