This commit is contained in:
董海洋
2026-05-26 09:18:48 +08:00
parent 12b582ec64
commit ff40282dc1
19 changed files with 703 additions and 8 deletions
+30
View File
@@ -0,0 +1,30 @@
const { query } = require('../config/database')
async function getTodayStats(ctx) {
const today = new Date()
const todayStart = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')} 00:00:00`
const todayEnd = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')} 23:59:59`
const orderResult = await query(
'SELECT COUNT(*) as orderCount, COALESCE(SUM(total_price), 0) as totalSales FROM orders WHERE created_at >= ? AND created_at <= ? AND status IN ("paid", "completed")',
[todayStart, todayEnd]
)
const customerResult = await query(
'SELECT COUNT(DISTINCT user_id) as customerCount FROM orders WHERE created_at >= ? AND created_at <= ?',
[todayStart, todayEnd]
)
ctx.body = {
code: 200,
data: {
sales: orderResult[0].totalSales,
orders: orderResult[0].orderCount,
customers: customerResult[0].customerCount
}
}
}
module.exports = {
getTodayStats
}