perf(finance): 优化财务仪表盘聚合查询
This commit is contained in:
@@ -403,7 +403,7 @@ func (r *Repository) dailyEstimates(ctx context.Context, query DashboardQuery) (
|
||||
func (r *Repository) dailySettlements(ctx context.Context, query DashboardQuery) ([]dailySettlementRow, error) {
|
||||
db := r.db.WithContext(ctx)
|
||||
rows := make([]dailySettlementRow, 0)
|
||||
err := db.Table("rental_orders AS ro").
|
||||
err := db.Table("rental_orders AS ro USE INDEX (idx_rental_orders_dashboard_settled)").
|
||||
Select(`DATE_FORMAT(ro.settled_at, '%Y-%m-%d') AS date,
|
||||
COALESCE(SUM(oc.platform_fee_cent), 0) AS platform_income_amount_cent,
|
||||
COALESCE(SUM(oc.owner_income_amount_cent), 0) AS owner_should_income_amount_cent,
|
||||
@@ -445,7 +445,7 @@ func (r *Repository) dailySettlements(ctx context.Context, query DashboardQuery)
|
||||
COUNT(ro.id) AS settled_order_count`, settlementDiffThresholdCent, refundBizTypes(), []string{"refunding", "failed"}).
|
||||
// 统计口径:只算"正常结账确认"的订单。仲裁结案的结账单终态是 settled
|
||||
// (由 dispute 模块写入),不纳入本指标,此处保持既有口径不变。
|
||||
Joins("JOIN order_checkouts AS oc ON oc.order_id = ro.id AND oc.status = ?", ordermodule.CheckoutStatusAccepted).
|
||||
Joins("JOIN order_checkouts AS oc USE INDEX (idx_order_checkouts_dashboard_accepted) ON oc.order_id = ro.id AND oc.status = ?", ordermodule.CheckoutStatusAccepted).
|
||||
Joins("LEFT JOIN (?) AS w ON w.order_id = ro.id", ownerWalletIncomeForSettledOrdersSubquery(db, query)).
|
||||
Where("ro.settled_at >= ? AND ro.settled_at <= ?", query.StartDate, query.EndDate).
|
||||
Group("DATE_FORMAT(ro.settled_at, '%Y-%m-%d')").
|
||||
@@ -523,7 +523,7 @@ func operatingExpenseSummaryFromDailyRows(rows []dailyOperatingExpenseRow) Opera
|
||||
func (r *Repository) dailyPayments(ctx context.Context, query DashboardQuery) ([]dailyPaymentRow, error) {
|
||||
rows := make([]dailyPaymentRow, 0)
|
||||
db := r.db.WithContext(ctx)
|
||||
err := db.Table("payment_orders AS po").
|
||||
err := db.Table("payment_orders AS po USE INDEX (idx_payment_orders_dashboard_created)").
|
||||
Select(`DATE_FORMAT(po.created_at, '%Y-%m-%d') AS date,
|
||||
COALESCE(SUM(CASE WHEN po.biz_type IN ? AND po.status = 'paid' THEN po.amount_cent ELSE 0 END), 0) AS total_flow_amount_cent,
|
||||
COALESCE(SUM(CASE WHEN po.biz_type IN ? AND po.status = 'refunded' THEN po.amount_cent ELSE 0 END), 0) AS total_refund_amount_cent,
|
||||
|
||||
@@ -50,11 +50,11 @@ func ownerWalletIncomeForSettledOrdersSubquery(db *gorm.DB, query DashboardQuery
|
||||
// paymentOriginalAmountForRefundsInRangeSubquery 仅为当前区间内的退款单回查原支付金额。
|
||||
// 退款统计原先会对全部历史支付单按 order_id 聚合;数据增长后这部分即使只看一天也会很慢。
|
||||
func paymentOriginalAmountForRefundsInRangeSubquery(db *gorm.DB, query DashboardQuery) *gorm.DB {
|
||||
refundOrders := db.Table("payment_orders AS refund").
|
||||
refundOrders := db.Table("payment_orders AS refund USE INDEX (idx_payment_orders_dashboard_created)").
|
||||
Select("DISTINCT refund.order_id").
|
||||
Where("refund.biz_type IN ? AND refund.status = ?", refundBizTypes(), "refunded").
|
||||
Where("refund.created_at >= ? AND refund.created_at <= ?", query.StartDate, query.EndDate)
|
||||
return db.Table("payment_orders AS paid").
|
||||
return db.Table("payment_orders AS paid USE INDEX (idx_payment_orders_dashboard_order)").
|
||||
Select("paid.order_id, MAX(paid.amount_cent) AS amount_cent").
|
||||
Joins("JOIN (?) AS refund_order ON refund_order.order_id = paid.order_id", refundOrders).
|
||||
Where("paid.biz_type IN ? AND paid.status = ?", payBizTypes(), "paid").
|
||||
|
||||
Reference in New Issue
Block a user