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:
+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