diff --git a/client/src/App.css b/client/src/App.css index 751e05d..c09e09a 100644 --- a/client/src/App.css +++ b/client/src/App.css @@ -248,6 +248,17 @@ body { font-size: 12px; } +.btn:disabled { + opacity: 0.4; + cursor: not-allowed; + filter: grayscale(0.6); +} + +.btn:disabled:hover { + opacity: 0.4; + filter: grayscale(0.6); +} + /* Form elements */ .form-group { margin-bottom: 16px; diff --git a/client/src/hooks/useApi.js b/client/src/hooks/useApi.js index ff1506b..83f2478 100644 --- a/client/src/hooks/useApi.js +++ b/client/src/hooks/useApi.js @@ -113,11 +113,6 @@ export async function clearDeals() { return res.data; } -export async function getNeedsClassify() { - const res = await api.get("/transactions/needs-classify"); - return res.data; -} - // Transactions export async function getTransactions(params = {}) { const res = await api.get("/transactions", { params }); @@ -149,9 +144,12 @@ export async function classifyTransactions(ids) { } export async function classifyAll() { - const res = await api.post("/transactions/classify-all", null, { - timeout: 600000, - }); + const res = await api.post("/transactions/classify-all"); + return res.data; +} + +export async function getClassifyStatus() { + const res = await api.get("/transactions/classify-status"); return res.data; } @@ -161,10 +159,7 @@ export async function getOverview(params = {}) { return res.data; } -export async function getMonthly(months = 12, categories) { - const params = { months }; - if (categories && categories.length > 0) - params.categories = categories.join(","); +export async function getMonthly(params = {}) { const res = await api.get("/stats/monthly", { params }); return res.data; } @@ -207,15 +202,6 @@ export async function updateCategory(id, data) { const res = await api.put(`/config/categories/${id}`, data); return res.data; } -export async function reclassifyAffected(ids) { - const res = await api.post( - "/config/categories/reclassify-affected", - { ids }, - { timeout: 300000 }, - ); - return res.data; -} - export function formatAmount(amount) { if (amount === null || amount === undefined) return "--"; const n = Number(amount); diff --git a/client/src/pages/Dashboard.js b/client/src/pages/Dashboard.js index 492d848..66097e3 100644 --- a/client/src/pages/Dashboard.js +++ b/client/src/pages/Dashboard.js @@ -1,11 +1,4 @@ -import React, { - useState, - useEffect, - useMemo, - useRef, - useCallback, -} from "react"; -import { RefreshCcw } from "lucide-react"; +import React, { useState, useEffect, useMemo } from "react"; import { BarChart, Bar, @@ -35,7 +28,41 @@ const COLORS = [ "#e11d48", ]; -const DEFAULT_UNCHECKED = ["投资理财", "转账红包"]; +const DEFAULT_UNCHECKED = ["投资理财", "转账红包", "投资赠与"]; +// 单独成卡片的分类(默认不勾选,仍可在明细表里手动勾选) +const GIFT_CATEGORY = "投资赠与"; + +// 财年:以每年 2 月 1 日 00:00:00 为结算点 +// 财年 Y = [Y-02-01 00:00:00, (Y+1)-01-31 23:59:59] +const FY_START_MONTH = 2; + +function fiscalYearOf(value) { + if (!value) return null; + const d = + value instanceof Date ? value : new Date(String(value).replace(" ", "T")); + if (Number.isNaN(d.getTime())) return null; + return d.getMonth() + 1 >= FY_START_MONTH + ? d.getFullYear() + : d.getFullYear() - 1; +} + +function fiscalRange(fy) { + const y = Number(fy); + return { + start_date: `${y}-02-01 00:00:00`, + end_date: `${y + 1}-01-31 23:59:59`, + }; +} + +function buildFiscalYears(range) { + if (!range?.min_date || !range?.max_date) return []; + const lo = fiscalYearOf(range.min_date); + const hi = fiscalYearOf(range.max_date); + if (lo === null || hi === null) return []; + const years = []; + for (let y = hi; y >= lo; y--) years.push(y); + return years; +} const RADIAN = Math.PI / 180; function renderPieLabel({ cx, cy, midAngle, outerRadius, percent, category }) { @@ -90,12 +117,50 @@ export default function Dashboard() { const [monthly, setMonthly] = useState([]); const [loading, setLoading] = useState(true); const [checked, setChecked] = useState(() => new Set()); - const allRef = useRef(null); + const [fy, setFy] = useState(null); // number=财年 | "all" | null=未初始化 + const [fyOptions, setFyOptions] = useState([]); + // 初始化:先取全量日期范围,枚举可选财年并默认选最新一个 useEffect(() => { - loadData(); + (async () => { + try { + const ov = await getOverview(); + const years = buildFiscalYears(ov?.dateRange); + setFyOptions(years); + setFy(years.length ? years[0] : "all"); + } catch (e) { + console.error(e); + } + })(); }, []); + // 财年变化 -> 重新拉取总览 + useEffect(() => { + if (fy === null) return; + let cancelled = false; + (async () => { + setLoading(true); + try { + const params = fy === "all" ? {} : fiscalRange(fy); + const ov = await getOverview(params); + if (cancelled) return; + setRaw(ov); + const allCats = new Set( + (ov?.categoryBreakdown || []).map((c) => c.category), + ); + for (const c of DEFAULT_UNCHECKED) allCats.delete(c); + setChecked(allCats); + } catch (e) { + console.error(e); + } + if (!cancelled) setLoading(false); + })(); + return () => { + cancelled = true; + }; + }, [fy]); + + // 勾选分类 / 财年变化 -> 重新拉取月度趋势 useEffect(() => { if (!raw) return; const cats = [...checked]; @@ -103,25 +168,10 @@ export default function Dashboard() { setMonthly([]); return; } - getMonthly(12, cats).then(setMonthly).catch(console.error); - }, [checked, raw]); - - async function loadData() { - setLoading(true); - try { - const [ov, mo] = await Promise.all([getOverview(), getMonthly(12)]); - setRaw(ov); - setMonthly(mo); - const allCats = new Set( - (ov?.categoryBreakdown || []).map((c) => c.category), - ); - for (const c of DEFAULT_UNCHECKED) allCats.delete(c); - setChecked(allCats); - } catch (e) { - console.error(e); - } - setLoading(false); - } + const params = { categories: cats.join(",") }; + if (fy !== "all" && fy != null) Object.assign(params, fiscalRange(fy)); + getMonthly(params).then(setMonthly).catch(console.error); + }, [checked, raw, fy]); function toggleCat(cat) { setChecked((prev) => { @@ -147,12 +197,26 @@ export default function Dashboard() { if (!raw) return null; const cb = raw.categoryBreakdown || []; const cs = raw.categoryStats || []; + // 勾选分类参与统计(投资赠与默认不勾选,用户想算进去可手动勾) const sel = cb.filter((c) => checked.has(c.category)); const totalIncome = sel.reduce((s, c) => s + c.total_income, 0); const totalExpense = sel.reduce((s, c) => s + c.total_expense, 0); const dealCount = sel.reduce((s, c) => s + c.deal_count, 0); const pieStats = cs.filter((c) => checked.has(c.category)); - return { sel, totalIncome, totalExpense, dealCount, pieStats }; + + // 投资赠与单独一张卡片:净额 = 收入 - 支出 + const gift = cb.find((c) => c.category === GIFT_CATEGORY) || null; + const giftNet = gift ? gift.total_income - gift.total_expense : 0; + + return { + sel, + totalIncome, + totalExpense, + dealCount, + pieStats, + gift, + giftNet, + }; }, [raw, checked]); const ov = raw?.overview || {}; @@ -173,15 +237,39 @@ export default function Dashboard() { >

📊 财务仪表盘

-

勾选分类后,所有统计基于选中项计算

+

+ 勾选分类后,所有统计基于选中项计算 + {fy && fy !== "all" + ? ` · 财年 ${fy}(${fy}-02-01 ~ ${Number(fy) + 1}-01-31)` + : " · 全部时间"} +

+
+
+ + 财年 + +
-
@@ -199,6 +287,15 @@ export default function Dashboard() {
{checked.size} 个分类合计
+
+
🎁 投资赠与
+
= 0 ? "income" : "expense"}`} + > + {formatAmount(derived?.giftNet ?? 0)} +
+
净额,默认不勾选
+
📈 净收支
) : ( - {cat.name} + <> + {cat.name} + {cat.is_locked ? ( + + 🔒 内置 + + ) : null} + )} @@ -439,6 +450,8 @@ export default function SettingsPage() {