version 0.1
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
const express = require('express');
|
||||
const { getDB } = require('../db');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// GET /api/stats/overview - 总览统计
|
||||
router.get('/overview', (req, res) => {
|
||||
const db = getDB();
|
||||
const { year, month } = req.query;
|
||||
|
||||
let dateCondition = '';
|
||||
const params = [];
|
||||
|
||||
if (year) {
|
||||
dateCondition = 'WHERE strftime(\'%Y\', transaction_date) = ?';
|
||||
params.push(String(year));
|
||||
if (month) {
|
||||
dateCondition += ' AND strftime(\'%m\', transaction_date) = ?';
|
||||
params.push(String(month).padStart(2, '0'));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// 按分类统计支出
|
||||
const categoryStats = db.prepare(`
|
||||
SELECT
|
||||
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
|
||||
ORDER BY total_amount DESC
|
||||
`).all(...params);
|
||||
|
||||
// 按来源统计
|
||||
const sourceStats = db.prepare(`
|
||||
SELECT
|
||||
t.source_type,
|
||||
s.source_name,
|
||||
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
|
||||
LEFT JOIN sources s ON s.id = t.source_id
|
||||
${dateCondition}
|
||||
GROUP BY t.source_type
|
||||
ORDER BY total_expense DESC
|
||||
`).all(...params);
|
||||
|
||||
res.json({
|
||||
overview: stats,
|
||||
categoryStats,
|
||||
sourceStats
|
||||
});
|
||||
});
|
||||
|
||||
// GET /api/stats/monthly - 月度趋势
|
||||
router.get('/monthly', (req, res) => {
|
||||
const db = getDB();
|
||||
const { months = 12 } = req.query;
|
||||
|
||||
const monthlyData = db.prepare(`
|
||||
SELECT
|
||||
strftime('%Y-%m', transaction_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)
|
||||
ORDER BY month ASC
|
||||
`).all();
|
||||
|
||||
res.json(monthlyData);
|
||||
});
|
||||
|
||||
// GET /api/stats/daily - 每日统计
|
||||
router.get('/daily', (req, res) => {
|
||||
const db = getDB();
|
||||
const { days = 30 } = req.query;
|
||||
|
||||
const dailyData = db.prepare(`
|
||||
SELECT
|
||||
transaction_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
|
||||
ORDER BY date ASC
|
||||
`).all();
|
||||
|
||||
res.json(dailyData);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user