diff --git a/client/src/pages/Transactions.js b/client/src/pages/Transactions.js index 64f74c1..0f2764f 100644 --- a/client/src/pages/Transactions.js +++ b/client/src/pages/Transactions.js @@ -23,7 +23,7 @@ import { function fmtDatetime(dt) { if (!dt) return ""; - return String(dt).replace(/:00$/, "").substring(0, 16); + return String(dt).substring(0, 19); } const DEAL_FILTER_DEFAULT = { @@ -44,6 +44,8 @@ export default function Transactions() { total: 0, page: 1, totalPages: 0, + totalIncome: 0, + totalExpense: 0, }); const [filters, setFilters] = useState({ page: 1, @@ -68,12 +70,14 @@ export default function Transactions() { total: 0, page: 1, totalPages: 0, + totalIncome: 0, + totalExpense: 0, }); const [clearConfirm, setClearConfirm] = useState(false); const [editingDeal, setEditingDeal] = useState(null); const [dealFilters, setDealFilters] = useState({ ...DEAL_FILTER_DEFAULT }); - /* ── sort helpers ── */ + /* sort helpers */ function dealToggleSort(field) { setDealFilters((p) => ({ ...p, @@ -133,7 +137,7 @@ export default function Transactions() { setToast({ message: msg, type, key: Date.now() }); } - /* ── restore classify progress ── */ + /* restore classify progress */ useEffect(() => { const saved = sessionStorage.getItem("classifyTask"); if (!saved) return; @@ -226,7 +230,7 @@ export default function Transactions() { })); } - /* ── deal edit / clear ── */ + /* deal edit / clear */ async function handleUpdateDeal() { if (!editingDeal) return; try { @@ -310,7 +314,9 @@ export default function Transactions() { setClassifying(false); } - /* ════════════════════════════════ RENDER ════════════════════════════════ */ + /* ============================== RENDER ============================== */ + const hasTotals = data.totalIncome || data.totalExpense; + return (
@@ -318,7 +324,6 @@ export default function Transactions() {

查看、筛选、排序、对账和分类

- {/* 操作按钮行 */}
@@ -346,7 +351,7 @@ export default function Transactions() {
- {/* ── 交易记录 ── */} + {/* --- 交易记录 --- */}

交易记录

@@ -355,7 +360,6 @@ export default function Transactions() {
- {/* 筛选栏 */}
@@ -412,6 +416,27 @@ export default function Transactions() { onChange={(e) => updateFilter("keyword", e.target.value)} />
+ {hasTotals ? ( +
+ 筛选结果 · 收入{" "} + + +{(data.totalIncome || 0).toFixed(2)} + {" "} + · 支出{" "} + + -{(data.totalExpense || 0).toFixed(2)} + +
+ ) : null}
{loading ? ( @@ -459,12 +484,11 @@ export default function Transactions() { textAlign: "right", fontWeight: 600, whiteSpace: "nowrap", + color: + tx.direction === "income" + ? "var(--success)" + : "var(--danger)", }} - className={ - tx.direction === "income" - ? "stat-value income" - : "stat-value expense" - } > {formatAmount(tx.amount)} @@ -534,7 +558,7 @@ export default function Transactions() { )}
- {/* ── 关联交易 ── */} + {/* --- 关联交易 --- */}

关联交易

@@ -543,7 +567,6 @@ export default function Transactions() {
- {/* 筛选栏 */}
@@ -600,6 +623,27 @@ export default function Transactions() { onChange={(e) => updateDealFilter("keyword", e.target.value)} />
+ {deals.totalIncome || deals.totalExpense ? ( +
+ 筛选结果 · 收入{" "} + + +{(deals.totalIncome || 0).toFixed(2)} + {" "} + · 支出{" "} + + -{(deals.totalExpense || 0).toFixed(2)} + +
+ ) : null}
{dealLoading ? ( @@ -649,12 +693,11 @@ export default function Transactions() { textAlign: "right", fontWeight: 600, whiteSpace: "nowrap", + color: + parseFloat(d.amount) >= 0 + ? "var(--success)" + : "var(--danger)", }} - className={ - parseFloat(d.amount) >= 0 - ? "stat-value income" - : "stat-value expense" - } > {formatAmount(d.amount)} @@ -742,7 +785,7 @@ export default function Transactions() { )}
- {/* ── 清空确认弹窗 ── */} + {/* --- 清空确认弹窗 --- */} {clearConfirm && (
@@ -773,7 +816,7 @@ export default function Transactions() {
)} - {/* ── Toast ── */} + {/* --- Toast --- */} {toast && (
)} - {/* ── 编辑 Deal 弹窗 ── */} + {/* --- 编辑 Deal 弹窗 --- */} {editingDeal && (
{ .prepare(`SELECT COUNT(*) as c FROM deals d ${where}`) .get(...params).c; + const totalsResult = db + .prepare( + `SELECT COALESCE(SUM(CASE WHEN d.direction='income' THEN d.amount ELSE 0 END),0) as totalIncome, COALESCE(SUM(CASE WHEN d.direction='expense' THEN ABS(d.amount) ELSE 0 END),0) as totalExpense FROM deals d ${where}`, + ) + .get(...params); + const deals = db .prepare( ` @@ -92,6 +98,8 @@ router.get("/", (req, res) => { res.json({ deals, total, + totalIncome: totalsResult.totalIncome, + totalExpense: totalsResult.totalExpense, page: Number(page), totalPages: Math.ceil(total / limit), }); diff --git a/server/routes/transactions.js b/server/routes/transactions.js index d55db27..a04b216 100644 --- a/server/routes/transactions.js +++ b/server/routes/transactions.js @@ -92,6 +92,12 @@ router.get("/", (req, res) => { .prepare(`SELECT COUNT(*) as total FROM transactions t ${whereClause}`) .get(...params); + const totalsResult = db + .prepare( + `SELECT COALESCE(SUM(CASE WHEN t.direction='income' THEN t.amount ELSE 0 END),0) as totalIncome, COALESCE(SUM(CASE WHEN t.direction='expense' THEN ABS(t.amount) ELSE 0 END),0) as totalExpense FROM transactions t ${whereClause}`, + ) + .get(...params); + const transactions = db .prepare( ` @@ -108,6 +114,8 @@ router.get("/", (req, res) => { res.json({ transactions, total: countResult.total, + totalIncome: totalsResult.totalIncome, + totalExpense: totalsResult.totalExpense, page: parseInt(page), limit: parseInt(limit), totalPages: Math.ceil(countResult.total / parseInt(limit)),