Migrate stats API to deals-based queries and add refresh button
This commit is contained in:
+83
-58
@@ -1,108 +1,133 @@
|
||||
const express = require('express');
|
||||
const { getDB } = require('../db');
|
||||
const express = require("express");
|
||||
const { getDB } = require("../db");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// GET /api/stats/overview - 总览统计
|
||||
router.get('/overview', (req, res) => {
|
||||
// GET /api/stats/overview — 总览统计(基于 deals)
|
||||
router.get("/overview", (req, res) => {
|
||||
const db = getDB();
|
||||
const { year, month } = req.query;
|
||||
|
||||
let dateCondition = '';
|
||||
let where = "";
|
||||
const params = [];
|
||||
|
||||
if (year) {
|
||||
dateCondition = 'WHERE strftime(\'%Y\', transaction_date) = ?';
|
||||
where = "WHERE strftime('%Y', d.deal_date) = ?";
|
||||
params.push(String(year));
|
||||
if (month) {
|
||||
dateCondition += ' AND strftime(\'%m\', transaction_date) = ?';
|
||||
params.push(String(month).padStart(2, '0'));
|
||||
where += " AND strftime('%m', d.deal_date) = ?";
|
||||
params.push(String(month).padStart(2, "0"));
|
||||
}
|
||||
}
|
||||
|
||||
const stats = db.prepare(`
|
||||
const stats = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT
|
||||
COUNT(*) as total_count,
|
||||
COALESCE(SUM(CASE WHEN direction = 'income' THEN amount ELSE 0 END), 0) as total_income,
|
||||
COALESCE(SUM(CASE WHEN direction = 'expense' THEN ABS(amount) ELSE 0 END), 0) as total_expense,
|
||||
COUNT(CASE WHEN reviewed = 0 AND ABS(amount) >= 100 THEN 1 END) as pending_review,
|
||||
COUNT(CASE WHEN reviewed = 1 THEN 1 END) as reviewed_count,
|
||||
COUNT(CASE WHEN matched_transaction_id IS NOT NULL THEN 1 END) as matched_count
|
||||
FROM transactions
|
||||
${dateCondition}
|
||||
`).get(...params);
|
||||
COALESCE(SUM(CASE WHEN d.direction = 'income' THEN d.amount ELSE 0 END), 0) as total_income,
|
||||
COALESCE(SUM(CASE WHEN d.direction = 'expense' THEN ABS(d.amount) ELSE 0 END), 0) as total_expense,
|
||||
COUNT(CASE WHEN d.reviewed = 0 AND ABS(d.amount) >= 100 THEN 1 END) as pending_review,
|
||||
COUNT(CASE WHEN d.reviewed = 1 THEN 1 END) as reviewed_count,
|
||||
COALESCE(SUM(d.match_count), 0) as matched_count
|
||||
FROM deals d
|
||||
${where}
|
||||
`,
|
||||
)
|
||||
.get(...params);
|
||||
|
||||
// 按分类统计支出
|
||||
const categoryStats = db.prepare(`
|
||||
// 按分类统计支出(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";
|
||||
|
||||
const categoryStats = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT
|
||||
category,
|
||||
d.category as category,
|
||||
COUNT(*) as count,
|
||||
SUM(ABS(amount)) as total_amount,
|
||||
AVG(ABS(amount)) as avg_amount
|
||||
FROM transactions
|
||||
${dateCondition}${dateCondition ? ' AND' : 'WHERE'} direction = 'expense' AND category IS NOT NULL
|
||||
GROUP BY category
|
||||
SUM(ABS(d.amount)) as total_amount,
|
||||
AVG(ABS(d.amount)) as avg_amount
|
||||
FROM deals d
|
||||
${catWhere}
|
||||
GROUP BY d.category
|
||||
ORDER BY total_amount DESC
|
||||
`).all(...params);
|
||||
`,
|
||||
)
|
||||
.all(...params);
|
||||
|
||||
// 按来源统计
|
||||
const sourceStats = db.prepare(`
|
||||
// 按来源统计(通过 deal_transactions 关联 transactions)
|
||||
const srcWhere = where ? where + "" : "";
|
||||
|
||||
const sourceStats = 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 transactions t
|
||||
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
|
||||
${dateCondition}
|
||||
${srcWhere}
|
||||
GROUP BY t.source_type
|
||||
ORDER BY total_expense DESC
|
||||
`).all(...params);
|
||||
`,
|
||||
)
|
||||
.all(...params);
|
||||
|
||||
res.json({
|
||||
overview: stats,
|
||||
categoryStats,
|
||||
sourceStats
|
||||
});
|
||||
res.json({ overview: stats, categoryStats, sourceStats });
|
||||
});
|
||||
|
||||
// GET /api/stats/monthly - 月度趋势
|
||||
router.get('/monthly', (req, res) => {
|
||||
// GET /api/stats/monthly — 月度趋势(基于 deals)
|
||||
router.get("/monthly", (req, res) => {
|
||||
const db = getDB();
|
||||
const { months = 12 } = req.query;
|
||||
const months = Math.abs(parseInt(req.query.months) || 12);
|
||||
|
||||
const monthlyData = db.prepare(`
|
||||
const monthlyData = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT
|
||||
strftime('%Y-%m', transaction_date) as month,
|
||||
strftime('%Y-%m', d.deal_date) as month,
|
||||
COUNT(*) as count,
|
||||
SUM(CASE WHEN direction = 'income' THEN amount ELSE 0 END) as income,
|
||||
SUM(CASE WHEN direction = 'expense' THEN ABS(amount) ELSE 0 END) as expense
|
||||
FROM transactions
|
||||
WHERE transaction_date >= DATE('now', '-${parseInt(months)} months')
|
||||
GROUP BY strftime('%Y-%m', transaction_date)
|
||||
SUM(CASE WHEN d.direction = 'income' THEN d.amount ELSE 0 END) as income,
|
||||
SUM(CASE WHEN d.direction = 'expense' THEN ABS(d.amount) ELSE 0 END) as expense
|
||||
FROM deals d
|
||||
WHERE d.deal_date >= DATE('now', ? || ' months')
|
||||
GROUP BY strftime('%Y-%m', d.deal_date)
|
||||
ORDER BY month ASC
|
||||
`).all();
|
||||
`,
|
||||
)
|
||||
.all("-" + months);
|
||||
|
||||
res.json(monthlyData);
|
||||
});
|
||||
|
||||
// GET /api/stats/daily - 每日统计
|
||||
router.get('/daily', (req, res) => {
|
||||
// GET /api/stats/daily — 每日统计(基于 deals)
|
||||
router.get("/daily", (req, res) => {
|
||||
const db = getDB();
|
||||
const { days = 30 } = req.query;
|
||||
const days = Math.abs(parseInt(req.query.days) || 30);
|
||||
|
||||
const dailyData = db.prepare(`
|
||||
const dailyData = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT
|
||||
transaction_date as date,
|
||||
d.deal_date as date,
|
||||
COUNT(*) as count,
|
||||
SUM(CASE WHEN direction = 'income' THEN amount ELSE 0 END) as income,
|
||||
SUM(CASE WHEN direction = 'expense' THEN ABS(amount) ELSE 0 END) as expense
|
||||
FROM transactions
|
||||
WHERE transaction_date >= DATE('now', '-${parseInt(days)} days')
|
||||
GROUP BY transaction_date
|
||||
SUM(CASE WHEN d.direction = 'income' THEN d.amount ELSE 0 END) as income,
|
||||
SUM(CASE WHEN d.direction = 'expense' THEN ABS(d.amount) ELSE 0 END) as expense
|
||||
FROM deals d
|
||||
WHERE d.deal_date >= DATE('now', ? || ' days')
|
||||
GROUP BY d.deal_date
|
||||
ORDER BY date ASC
|
||||
`).all();
|
||||
`,
|
||||
)
|
||||
.all("-" + days);
|
||||
|
||||
res.json(dailyData);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user