优化财务仪表盘加载与利润展示
This commit is contained in:
@@ -2,6 +2,7 @@ package adminfinance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
)
|
||||
@@ -10,30 +11,63 @@ func (r *Repository) Dashboard(ctx context.Context, query DashboardQuery) (*Dash
|
||||
if cached := r.loadDashboardCache(ctx, query); cached != nil {
|
||||
return cached, nil
|
||||
}
|
||||
dailyItems, err := r.dailyItems(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// 各板块的读取互不依赖。并行执行能避免单个请求串行等待二十余条统计 SQL,
|
||||
// 首次打开或缓存失效时仍可尽快返回;每个查询继续复用 GORM 的连接池。
|
||||
var (
|
||||
dailyItems []FinanceDailyDTO
|
||||
pickup *PickupSummaryDTO
|
||||
mohong *MohongSummaryDTO
|
||||
disbursement *DisbursementSummaryDTO
|
||||
operatingExpense *OperatingExpenseSummaryDTO
|
||||
)
|
||||
var wg sync.WaitGroup
|
||||
errCh := make(chan error, 5)
|
||||
run := func(fn func() error) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := fn(); err != nil {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
}
|
||||
pickup, err := r.pickupSummary(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
run(func() error {
|
||||
var err error
|
||||
dailyItems, err = r.dailyItems(ctx, query)
|
||||
return err
|
||||
})
|
||||
run(func() error {
|
||||
var err error
|
||||
pickup, err = r.pickupSummary(ctx, query)
|
||||
return err
|
||||
})
|
||||
run(func() error {
|
||||
var err error
|
||||
mohong, err = r.mohongSummary(ctx, query)
|
||||
return err
|
||||
})
|
||||
run(func() error {
|
||||
var err error
|
||||
disbursement, err = r.disbursementSummary(ctx, query)
|
||||
return err
|
||||
})
|
||||
run(func() error {
|
||||
var err error
|
||||
operatingExpense, err = r.operatingExpenseSummary(ctx, query)
|
||||
return err
|
||||
})
|
||||
wg.Wait()
|
||||
close(errCh)
|
||||
for err := range errCh {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
summary, err := r.summary(ctx, query, pickup)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mohong, err := r.mohongSummary(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
disbursement, err := r.disbursementSummary(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
operatingExpense, err := r.operatingExpenseSummary(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &DashboardDTO{
|
||||
Summary: *summary,
|
||||
DailyItems: dailyItems,
|
||||
@@ -126,6 +160,7 @@ func (r *Repository) mohongSummary(ctx context.Context, query DashboardQuery) (*
|
||||
func (r *Repository) summary(ctx context.Context, query DashboardQuery, pickup *PickupSummaryDTO) (*FinanceSummaryDTO, error) {
|
||||
db := r.db.WithContext(ctx)
|
||||
var payment paymentSummaryRow
|
||||
originalPayments := paymentOriginalAmountForRefundsInRangeSubquery(db, query)
|
||||
if err := db.Table("payment_orders AS po").
|
||||
Select(`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,
|
||||
@@ -135,12 +170,7 @@ func (r *Repository) summary(ctx context.Context, query DashboardQuery, pickup *
|
||||
COALESCE(SUM(CASE WHEN po.biz_type IN ? AND po.status = 'refunded' AND po.amount_cent < COALESCE(orig.amount_cent, 0) THEN 1 ELSE 0 END), 0) AS partial_refund_count,
|
||||
COALESCE(SUM(CASE WHEN po.biz_type IN ? AND po.status = 'refunding' THEN 1 ELSE 0 END), 0) AS pending_refund_count`,
|
||||
payBizTypes(), refundBizTypes(), refundBizTypes(), payBizTypes(), refundBizTypes(), refundBizTypes(), refundBizTypes()).
|
||||
Joins(`LEFT JOIN (
|
||||
SELECT order_id, MAX(amount_cent) AS amount_cent
|
||||
FROM payment_orders
|
||||
WHERE biz_type IN ? AND status = 'paid'
|
||||
GROUP BY order_id
|
||||
) AS orig ON orig.order_id = po.order_id`, payBizTypes()).
|
||||
Joins("LEFT JOIN (?) AS orig ON orig.order_id = po.order_id", originalPayments).
|
||||
Where("po.created_at >= ? AND po.created_at <= ?", query.StartDate, query.EndDate).
|
||||
Scan(&payment).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -164,32 +194,31 @@ func (r *Repository) summary(ctx context.Context, query DashboardQuery, pickup *
|
||||
THEN COALESCE(ro.offline_settlement_amount_cent, 0)
|
||||
ELSE 0
|
||||
END), 0) AS offline_settlement_pending_amount_cent,
|
||||
COALESCE(SUM(CASE WHEN ro.settlement_mode = 'platform_managed'
|
||||
COALESCE(SUM(CASE WHEN ro.settlement_mode = 'platform_managed'
|
||||
AND COALESCE(NULLIF(ro.offline_settlement_status, ''), 'none') = 'pending'
|
||||
AND COALESCE(ro.offline_settlement_amount_cent, 0) > 0
|
||||
THEN 1
|
||||
ELSE 0
|
||||
END), 0) AS offline_settlement_pending_count,
|
||||
COUNT(ro.id) AS settled_order_count`).
|
||||
COALESCE(SUM(CASE WHEN COALESCE(p.failed_refund_amount_cent, 0) > 0
|
||||
OR COALESCE(p.refunding_amount_cent, 0) > 0
|
||||
OR ro.refund_status = 'refunding'
|
||||
OR ABS(COALESCE(oc.owner_income_amount_cent, 0) - CASE WHEN ro.settlement_mode = 'platform_managed'
|
||||
THEN COALESCE(ro.offline_settlement_amount_cent, 0)
|
||||
ELSE COALESCE(w.owner_wallet_income_amount_cent, 0)
|
||||
END) >= ?
|
||||
THEN 1
|
||||
ELSE 0
|
||||
END), 0) AS financial_exception_count,
|
||||
COUNT(ro.id) AS settled_order_count`, settlementDiffThresholdCent).
|
||||
Joins("JOIN order_checkouts AS oc ON oc.order_id = ro.id AND oc.status = 'accepted'").
|
||||
Joins("LEFT JOIN (?) AS w ON w.order_id = ro.id", ownerWalletIncomeSubquery(db)).
|
||||
Joins("LEFT JOIN (?) AS w ON w.order_id = ro.id", ownerWalletIncomeForSettledOrdersSubquery(db, query)).
|
||||
Joins("LEFT JOIN (?) AS p ON p.order_id = ro.id", orderPaymentForSettledOrdersSubquery(db, query)).
|
||||
Where("ro.settled_at >= ? AND ro.settled_at <= ?", query.StartDate, query.EndDate).
|
||||
Scan(&settlement).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var exceptionCount int64
|
||||
if err := db.Table("(?) AS d", r.financeDetailBaseQuery(ctx, DetailQuery{
|
||||
DateType: "settled",
|
||||
StartDate: query.StartDate,
|
||||
EndDate: query.EndDate,
|
||||
})).
|
||||
Where("finance_status <> ?", financeStatusNormal).
|
||||
Where("finance_status <> ?", financeStatusOfflineSettlementPending).
|
||||
Count(&exceptionCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 预计收入:尚未结算订单的下单预估平台手续费之和,按下单时间落在查询区间内统计。
|
||||
var estimated estimatedIncomeRow
|
||||
if err := db.Table("rental_orders").
|
||||
@@ -232,7 +261,7 @@ func (r *Repository) summary(ctx context.Context, query DashboardQuery, pickup *
|
||||
SettledOrderCount: settlement.SettledOrderCount,
|
||||
PendingSettleOrderCount: estimated.PendingSettleOrderCount,
|
||||
OfflineSettlementPendingCount: settlement.OfflineSettlementPendingCount,
|
||||
FinancialExceptionCount: exceptionCount,
|
||||
FinancialExceptionCount: settlement.FinancialExceptionCount,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -274,7 +303,7 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
|
||||
END), 0) AS offline_settlement_pending_count,
|
||||
COUNT(ro.id) AS settled_order_count`).
|
||||
Joins("JOIN order_checkouts AS oc ON oc.order_id = ro.id AND oc.status = 'accepted'").
|
||||
Joins("LEFT JOIN (?) AS w ON w.order_id = ro.id", ownerWalletIncomeSubquery(db)).
|
||||
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(ro.settled_at)").
|
||||
Scan(&settlements).Error; err != nil {
|
||||
@@ -439,7 +468,8 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
|
||||
// normal_full_refund_count 只统计正常(租赁)订单的全额退款,排除撞车商城。
|
||||
func (r *Repository) dailyPayments(ctx context.Context, query DashboardQuery) ([]dailyPaymentRow, error) {
|
||||
rows := make([]dailyPaymentRow, 0)
|
||||
err := r.db.WithContext(ctx).Table("payment_orders AS po").
|
||||
db := r.db.WithContext(ctx)
|
||||
err := db.Table("payment_orders AS po").
|
||||
Select(`DATE(po.created_at) 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,
|
||||
@@ -450,12 +480,7 @@ func (r *Repository) dailyPayments(ctx context.Context, query DashboardQuery) ([
|
||||
COALESCE(SUM(CASE WHEN po.biz_type IN ? AND po.status = 'refunding' THEN 1 ELSE 0 END), 0) AS pending_refund_count,
|
||||
COALESCE(SUM(CASE WHEN po.biz_type IN ? AND po.status = 'refunded' AND po.amount_cent >= COALESCE(orig.amount_cent, 0) THEN 1 ELSE 0 END), 0) AS normal_full_refund_count`,
|
||||
payBizTypes(), refundBizTypes(), refundBizTypes(), payBizTypes(), refundBizTypes(), refundBizTypes(), refundBizTypes(), normalRefundBizTypes()).
|
||||
Joins(`LEFT JOIN (
|
||||
SELECT order_id, MAX(amount_cent) AS amount_cent
|
||||
FROM payment_orders
|
||||
WHERE biz_type IN ? AND status = 'paid'
|
||||
GROUP BY order_id
|
||||
) AS orig ON orig.order_id = po.order_id`, payBizTypes()).
|
||||
Joins("LEFT JOIN (?) AS orig ON orig.order_id = po.order_id", paymentOriginalAmountForRefundsInRangeSubquery(db, query)).
|
||||
Where("po.created_at >= ? AND po.created_at <= ?", query.StartDate, query.EndDate).
|
||||
Group("DATE(po.created_at)").
|
||||
Scan(&rows).Error
|
||||
@@ -512,6 +537,7 @@ type settlementSummaryRow struct {
|
||||
OfflineSettlementAmountCent int64
|
||||
OfflineSettlementPendingAmountCent int64
|
||||
OfflineSettlementPendingCount int64
|
||||
FinancialExceptionCount int64
|
||||
SettledOrderCount int64
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user