new api
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user