feat(finance): 仪表盘平台利润显示订单数量,七天表格新增退全款/取消提号列

This commit is contained in:
yml2213
2026-08-15 13:04:45 +08:00
parent d75ebe4192
commit ac47006711
6 changed files with 218 additions and 23 deletions
@@ -168,8 +168,10 @@ func (r *Repository) summary(ctx context.Context, query DashboardQuery, pickup *
}
pickupProfitAmountCent := int64(0)
pickupCompletedCount := int64(0)
if pickup != nil {
pickupProfitAmountCent = pickup.ProfitAmountCent
pickupCompletedCount = pickup.CompletedCount
}
return &FinanceSummaryDTO{
TotalFlowAmountCent: payment.TotalFlowAmountCent,
@@ -179,6 +181,7 @@ func (r *Repository) summary(ctx context.Context, query DashboardQuery, pickup *
PlatformIncomeAmountCent: settlement.PlatformIncomeAmountCent + pickupProfitAmountCent,
NormalOrderProfitAmountCent: settlement.PlatformIncomeAmountCent,
PickupProfitAmountCent: pickupProfitAmountCent,
PickupCompletedCount: pickupCompletedCount,
OwnerShouldIncomeAmountCent: settlement.OwnerShouldIncomeAmountCent,
OwnerWalletIncomeAmountCent: settlement.OwnerWalletIncomeAmountCent,
OwnerEffectiveSettlementAmountCent: settlement.OwnerEffectiveSettlementAmountCent,
@@ -200,26 +203,8 @@ func (r *Repository) summary(ctx context.Context, query DashboardQuery, pickup *
func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]FinanceDailyDTO, error) {
db := r.db.WithContext(ctx)
payments := make([]dailyPaymentRow, 0)
if 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,
COALESCE(SUM(CASE WHEN po.biz_type IN ? AND po.status = 'refunding' THEN po.amount_cent ELSE 0 END), 0) AS pending_refund_amount_cent,
COALESCE(SUM(CASE WHEN po.biz_type IN ? AND po.status = 'paid' THEN 1 ELSE 0 END), 0) AS successful_pay_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 full_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 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()).
Where("po.created_at >= ? AND po.created_at <= ?", query.StartDate, query.EndDate).
Group("DATE(po.created_at)").
Scan(&payments).Error; err != nil {
payments, err := r.dailyPayments(ctx, query)
if err != nil {
return nil, err
}
paidOrders, err := r.dailyPaidOrders(ctx, query)
@@ -273,6 +258,12 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
return nil, err
}
// 取消提号按取消日统计,与「已付款订单数」中的提号创建口径区分开。
pickupCancelled, err := r.dailyPickupCancelled(ctx, query)
if err != nil {
return nil, err
}
// 每日预计收入:尚未结算订单的下单预估平台手续费,按下单日期归集,口径与 summary 一致。
estimates := make([]dailyEstimatedRow, 0)
if err := db.Table("rental_orders").
@@ -309,6 +300,7 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
item.FullRefundCount = row.FullRefundCount
item.PartialRefundCount = row.PartialRefundCount
item.PendingRefundCount = row.PendingRefundCount
item.NormalFullRefundCount = row.NormalFullRefundCount
itemsByDate[date] = item
}
for _, row := range paidOrders {
@@ -343,6 +335,13 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
item.SalesCount += row.CompletedCount
itemsByDate[date] = item
}
for _, row := range pickupCancelled {
date := dailyDateKey(row.Date)
item := itemsByDate[date]
item.Date = date
item.PickupCancelledCount = row.Count
itemsByDate[date] = item
}
for _, row := range estimates {
date := dailyDateKey(row.Date)
item := itemsByDate[date]
@@ -372,6 +371,45 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
return items, nil
}
// dailyPayments 按支付单创建日统计收付款流水与退款笔数,
// 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").
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,
COALESCE(SUM(CASE WHEN po.biz_type IN ? AND po.status = 'refunding' THEN po.amount_cent ELSE 0 END), 0) AS pending_refund_amount_cent,
COALESCE(SUM(CASE WHEN po.biz_type IN ? AND po.status = 'paid' THEN 1 ELSE 0 END), 0) AS successful_pay_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 full_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 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,
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()).
Where("po.created_at >= ? AND po.created_at <= ?", query.StartDate, query.EndDate).
Group("DATE(po.created_at)").
Scan(&rows).Error
return rows, err
}
// dailyPickupCancelled 按取消日统计线下提号取消笔数。
func (r *Repository) dailyPickupCancelled(ctx context.Context, query DashboardQuery) ([]dailyPickupCancelledRow, error) {
rows := make([]dailyPickupCancelledRow, 0)
err := r.db.WithContext(ctx).Table("admin_pickups").
Select(`DATE(cancelled_at) AS date, COUNT(id) AS count`).
Where("status = ?", "cancelled").
Where("cancelled_at >= ? AND cancelled_at <= ?", query.StartDate, query.EndDate).
Group("DATE(cancelled_at)").
Scan(&rows).Error
return rows, err
}
// dailyPaidOrders 按实际付款日统计普通租赁订单,并将创建的线下提号视为已付款成交。
func (r *Repository) dailyPaidOrders(ctx context.Context, query DashboardQuery) ([]dailyPaidOrderRow, error) {
items := make([]dailyPaidOrderRow, 0)
@@ -427,6 +465,12 @@ type dailyPaymentRow struct {
FullRefundCount int64
PartialRefundCount int64
PendingRefundCount int64
NormalFullRefundCount int64
}
type dailyPickupCancelledRow struct {
Date string
Count int64
}
type dailyPaidOrderRow struct {