feat: 老板驾驶舱增加门店利润排名 - TOP5赚钱/BOTTOM5亏损,可点击跳转门店详情

This commit is contained in:
freedakgmail
2026-07-30 14:37:38 +08:00
parent 10c64a2ef2
commit 2df4f0799b
2 changed files with 78 additions and 0 deletions
+25
View File
@@ -539,4 +539,29 @@ router.get('/overview/profit-waterfall', async (req: AuthRequest, res) => {
}
})
// 门店利润排名
router.get('/overview/store-profit-ranking', async (req: AuthRequest, res) => {
try {
const result = await query(`
SELECT
r.store_code,
r.store_name,
round(r.received::numeric, 2) AS received,
round(COALESCE(e.actual_food_cost, 0)::numeric, 2) AS food_cost,
round(COALESCE(e.operating_expense, 0)::numeric, 2) AS expense,
round((r.received - COALESCE(e.actual_food_cost, 0) - COALESCE(e.operating_expense, 0))::numeric, 2) AS net_profit,
round((r.received - COALESCE(e.actual_food_cost, 0) - COALESCE(e.operating_expense, 0)) / nullif(r.received, 0) * 100, 2) AS net_margin_pct,
r.risk_level
FROM analytics.mv_store_risk_rating r
LEFT JOIN analytics.mv_store_operating_expense_monthly e
ON r.store_code = e.sales_store_code AND e.report_month = DATE '2026-04-01'
WHERE r.received IS NOT NULL AND r.received > 0
ORDER BY net_profit DESC
`)
sendSuccess(res, result.rows)
} catch (err: any) {
sendError(res, err.message)
}
})
export default router