fix: 统计概览接口补充 expenseCount/incomeCount/dailyIncome 字段
- 后端 stats/overview 接口返回字段与前端 Overview 类型不一致 - 前端页面使用了 expenseCount、incomeCount、dailyIncome 但后端未返回 - 导致统计页收入标签切换时日均和笔数显示为 0 - 补充 SQL 聚合查询,返回完整字段
This commit is contained in:
@@ -72,16 +72,27 @@ router.get('/overview', async (req: AuthRequest, res: Response) => {
|
||||
`SELECT
|
||||
COALESCE(SUM(CASE WHEN t.type = 'expense' THEN t.amount ELSE 0 END), 0) as expense,
|
||||
COALESCE(SUM(CASE WHEN t.type = 'income' THEN t.amount ELSE 0 END), 0) as income,
|
||||
COUNT(*) as count
|
||||
COUNT(*) as count,
|
||||
SUM(CASE WHEN t.type = 'expense' THEN 1 ELSE 0 END) as expenseCount,
|
||||
SUM(CASE WHEN t.type = 'income' THEN 1 ELSE 0 END) as incomeCount
|
||||
FROM transactions t ${result.where}`,
|
||||
[...result.params, startDate, endDate]
|
||||
)
|
||||
const row = (rows as any[])[0]
|
||||
const daily = elapsedDays > 0 ? Math.round(row.expense / elapsedDays) : 0
|
||||
const dailyIncome = elapsedDays > 0 ? Math.round(row.income / elapsedDays) : 0
|
||||
|
||||
res.json({
|
||||
code: 0,
|
||||
data: { expense: row.expense, income: row.income, count: row.count, daily }
|
||||
data: {
|
||||
expense: row.expense,
|
||||
income: row.income,
|
||||
count: row.count,
|
||||
daily,
|
||||
dailyIncome,
|
||||
expenseCount: row.expenseCount,
|
||||
incomeCount: row.incomeCount
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('[Stats] overview error:', err)
|
||||
|
||||
Reference in New Issue
Block a user