feat(adminfinance): 新增预计收入指标并优化明细查询性能
- 财务看板增加预计收入与待结算订单数统计 - 明细列表 COUNT 改为仅扫描 rental_orders 主表,避免 5-JOIN 子查询 - 统一前后端结算差异判定阈值为常量 5 分 - 修复日期选择器使用 UTC 日期导致东八区凌晨偏移前一天的问题
This commit is contained in:
@@ -62,6 +62,18 @@ func (r *Repository) summary(ctx context.Context, query DashboardQuery) (*Financ
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 预计收入:尚未结算订单的下单预估平台手续费之和,按下单时间落在查询区间内统计。
|
||||
var estimated estimatedIncomeRow
|
||||
if err := db.Table("rental_orders").
|
||||
Select(`COALESCE(SUM(platform_fee_cent), 0) AS estimated_income_amount_cent,
|
||||
COUNT(id) AS pending_settle_order_count`).
|
||||
Where("settlement_status NOT IN ?", settledSettlementStatuses()).
|
||||
Where("status NOT IN ?", nonBillableOrderStatuses()).
|
||||
Where("created_at >= ? AND created_at <= ?", query.StartDate, query.EndDate).
|
||||
Scan(&estimated).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FinanceSummaryDTO{
|
||||
TotalFlowAmountCent: payment.TotalFlowAmountCent,
|
||||
TotalRefundAmountCent: payment.TotalRefundAmountCent,
|
||||
@@ -71,10 +83,12 @@ func (r *Repository) summary(ctx context.Context, query DashboardQuery) (*Financ
|
||||
OwnerShouldIncomeAmountCent: settlement.OwnerShouldIncomeAmountCent,
|
||||
OwnerWalletIncomeAmountCent: settlement.OwnerWalletIncomeAmountCent,
|
||||
SettlementDiffAmountCent: settlement.OwnerShouldIncomeAmountCent - settlement.OwnerWalletIncomeAmountCent,
|
||||
EstimatedIncomeAmountCent: estimated.EstimatedIncomeAmountCent,
|
||||
SuccessfulPayCount: payment.SuccessfulPayCount,
|
||||
SuccessfulRefundCount: payment.SuccessfulRefundCount,
|
||||
PendingRefundCount: payment.PendingRefundCount,
|
||||
SettledOrderCount: settlement.SettledOrderCount,
|
||||
PendingSettleOrderCount: estimated.PendingSettleOrderCount,
|
||||
FinancialExceptionCount: exceptionCount,
|
||||
}, nil
|
||||
}
|
||||
@@ -112,6 +126,19 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 每日预计收入:尚未结算订单的下单预估平台手续费,按下单日期归集,口径与 summary 一致。
|
||||
estimates := make([]dailyEstimatedRow, 0)
|
||||
if err := db.Table("rental_orders").
|
||||
Select(`DATE(created_at) AS date,
|
||||
COALESCE(SUM(platform_fee_cent), 0) AS estimated_income_amount_cent`).
|
||||
Where("settlement_status NOT IN ?", settledSettlementStatuses()).
|
||||
Where("status NOT IN ?", nonBillableOrderStatuses()).
|
||||
Where("created_at >= ? AND created_at <= ?", query.StartDate, query.EndDate).
|
||||
Group("DATE(created_at)").
|
||||
Scan(&estimates).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
itemsByDate := make(map[string]FinanceDailyDTO)
|
||||
for day := dayStart(query.StartDate); !day.After(query.EndDate); day = day.AddDate(0, 0, 1) {
|
||||
date := day.Format("2006-01-02")
|
||||
@@ -141,6 +168,13 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
|
||||
item.SettledOrderCount = row.SettledOrderCount
|
||||
itemsByDate[date] = item
|
||||
}
|
||||
for _, row := range estimates {
|
||||
date := dailyDateKey(row.Date)
|
||||
item := itemsByDate[date]
|
||||
item.Date = date
|
||||
item.EstimatedIncomeAmountCent = row.EstimatedIncomeAmountCent
|
||||
itemsByDate[date] = item
|
||||
}
|
||||
|
||||
items := make([]FinanceDailyDTO, 0, len(itemsByDate))
|
||||
for day := dayStart(query.StartDate); !day.After(query.EndDate); day = day.AddDate(0, 0, 1) {
|
||||
@@ -165,6 +199,11 @@ type settlementSummaryRow struct {
|
||||
SettledOrderCount int64
|
||||
}
|
||||
|
||||
type estimatedIncomeRow struct {
|
||||
EstimatedIncomeAmountCent int64
|
||||
PendingSettleOrderCount int64
|
||||
}
|
||||
|
||||
type dailyPaymentRow struct {
|
||||
Date string
|
||||
TotalFlowAmountCent int64
|
||||
@@ -182,3 +221,8 @@ type dailySettlementRow struct {
|
||||
OwnerWalletIncomeAmountCent int64
|
||||
SettledOrderCount int64
|
||||
}
|
||||
|
||||
type dailyEstimatedRow struct {
|
||||
Date string
|
||||
EstimatedIncomeAmountCent int64
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user