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 {
|
||||
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 (
|
||||
<div className="loading">
|
||||
<div className="spinner"></div>
|
||||
加载中...
|
||||
<div className="spinner"></div>加载中...
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const ov = overview?.overview || {};
|
||||
const pieData = (overview?.categoryStats || []).filter(
|
||||
(c) => !EXCLUDED_CATEGORIES.includes(c.category),
|
||||
);
|
||||
|
||||
return (
|
||||
<div>
|
||||
@@ -183,11 +187,11 @@ export default function Dashboard() {
|
||||
<div className="card-header">
|
||||
<h3>🏷️ 支出分类占比</h3>
|
||||
</div>
|
||||
{overview?.categoryStats?.length > 0 ? (
|
||||
{pieData.length > 0 ? (
|
||||
<ResponsiveContainer width="100%" height={280}>
|
||||
<PieChart>
|
||||
<Pie
|
||||
data={overview.categoryStats.slice(0, 8)}
|
||||
data={pieData.slice(0, 8)}
|
||||
cx="50%"
|
||||
cy="50%"
|
||||
outerRadius={100}
|
||||
@@ -198,7 +202,7 @@ export default function Dashboard() {
|
||||
}
|
||||
labelLine={false}
|
||||
>
|
||||
{overview.categoryStats.slice(0, 8).map((_, idx) => (
|
||||
{pieData.slice(0, 8).map((_, idx) => (
|
||||
<Cell key={idx} fill={COLORS[idx % COLORS.length]} />
|
||||
))}
|
||||
</Pie>
|
||||
@@ -214,52 +218,103 @@ export default function Dashboard() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Source Summary */}
|
||||
{/* Category Breakdown Table */}
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3>🏦 各来源汇总</h3>
|
||||
<h3>📋 分类收支明细</h3>
|
||||
</div>
|
||||
{overview?.sourceStats?.length > 0 ? (
|
||||
{overview?.categoryBreakdown?.length > 0 ? (
|
||||
<div className="table-container">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>来源</th>
|
||||
<th>交易笔数</th>
|
||||
<th>收入合计</th>
|
||||
<th>支出合计</th>
|
||||
<th>净额</th>
|
||||
<th>分类</th>
|
||||
<th style={{ textAlign: "right" }}>笔数</th>
|
||||
<th style={{ textAlign: "right" }}>收入</th>
|
||||
<th style={{ textAlign: "right" }}>支出</th>
|
||||
<th style={{ textAlign: "right" }}>净额</th>
|
||||
<th>最大收入</th>
|
||||
<th>最大支出</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{overview.sourceStats.map((s, i) => (
|
||||
{overview.categoryBreakdown.map((cat, i) => (
|
||||
<tr key={i}>
|
||||
<td>
|
||||
<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>
|
||||
<span className="tag tag-matched">{cat.category}</span>
|
||||
</td>
|
||||
<td>{s.count}</td>
|
||||
<td style={{ color: "var(--success)" }}>
|
||||
{formatAmount(s.total_income)}
|
||||
<td style={{ textAlign: "right" }}>{cat.deal_count}</td>
|
||||
<td style={{ textAlign: "right", color: "var(--success)" }}>
|
||||
{cat.total_income > 0
|
||||
? formatAmount(cat.total_income)
|
||||
: "--"}
|
||||
</td>
|
||||
<td style={{ color: "var(--danger)" }}>
|
||||
{formatAmount(s.total_expense)}
|
||||
<td style={{ textAlign: "right", color: "var(--danger)" }}>
|
||||
{cat.total_expense > 0
|
||||
? formatAmount(-cat.total_expense)
|
||||
: "--"}
|
||||
</td>
|
||||
<td
|
||||
className={
|
||||
s.total_income - s.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)}
|
||||
</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>
|
||||
</tr>
|
||||
))}
|
||||
|
||||
@@ -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() {
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
{!showReviewed && deals.length > 0 && (
|
||||
<button className="btn btn-success btn-sm" onClick={handleConfirmAll}>
|
||||
<CheckCircle size={14} /> 全部确认
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Review List */}
|
||||
@@ -206,7 +176,6 @@ export default function Review() {
|
||||
>
|
||||
{formatAmount(deal.amount)}
|
||||
</div>
|
||||
|
||||
<div className="info-col">
|
||||
<div className="title">
|
||||
{deal.ai_description || deal.description || "无描述"}
|
||||
@@ -237,7 +206,6 @@ export default function Review() {
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="action-col">
|
||||
{deal.transactions && deal.transactions.length > 0 && (
|
||||
<span
|
||||
@@ -251,16 +219,8 @@ export default function Review() {
|
||||
className="btn btn-secondary btn-sm"
|
||||
onClick={() => setEditingDeal({ ...deal })}
|
||||
>
|
||||
<Edit3 size={12} /> 编辑
|
||||
<Edit3 size={12} /> {deal.reviewed ? "修改" : "编辑"}
|
||||
</button>
|
||||
{!deal.reviewed && (
|
||||
<button
|
||||
className="btn btn-success btn-sm"
|
||||
onClick={() => handleConfirm(deal)}
|
||||
>
|
||||
<CheckCircle size={12} /> 确认
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
@@ -278,12 +238,8 @@ export default function Review() {
|
||||
<>
|
||||
<Loader2
|
||||
size={14}
|
||||
className="spinner"
|
||||
style={{
|
||||
border: "none",
|
||||
animation: "spin 0.8s linear infinite",
|
||||
}}
|
||||
/>
|
||||
style={{ animation: "spin 0.8s linear infinite" }}
|
||||
/>{" "}
|
||||
加载中...
|
||||
</>
|
||||
) : (
|
||||
@@ -362,7 +318,6 @@ export default function Review() {
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Show associated transactions in modal */}
|
||||
{editingDeal.transactions &&
|
||||
editingDeal.transactions.length > 0 && (
|
||||
<div
|
||||
|
||||
+32
-18
@@ -36,7 +36,7 @@ router.get("/overview", (req, res) => {
|
||||
)
|
||||
.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)
|
||||
|
||||
Reference in New Issue
Block a user