feat(adminfinance): 退款数量拆分为全额退款与部分退款

- 按退款金额与原支付金额比较判定全额/部分退款
- 看板总退款卡片与每日表格增加全额/部分退款展示
- 保留 successful_refund_count 作为两者合计
This commit is contained in:
yml2213
2026-07-02 18:05:33 +08:00
parent 512291f089
commit 30d7e6a7d2
4 changed files with 61 additions and 27 deletions
@@ -25,15 +25,22 @@ func (r *Repository) Dashboard(ctx context.Context, query DashboardQuery) (*Dash
func (r *Repository) summary(ctx context.Context, query DashboardQuery) (*FinanceSummaryDTO, error) {
db := r.db.WithContext(ctx)
var payment paymentSummaryRow
if err := db.Table("payment_orders").
Select(`COALESCE(SUM(CASE WHEN biz_type IN ('order_pay') AND status = 'paid' THEN amount_cent ELSE 0 END), 0) AS total_flow_amount_cent,
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunded' THEN amount_cent ELSE 0 END), 0) AS total_refund_amount_cent,
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunding' THEN amount_cent ELSE 0 END), 0) AS pending_refund_amount_cent,
COALESCE(SUM(CASE WHEN biz_type IN ('order_pay') AND status = 'paid' THEN 1 ELSE 0 END), 0) AS successful_pay_count,
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunded' THEN 1 ELSE 0 END), 0) AS successful_refund_count,
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunding' THEN 1 ELSE 0 END), 0) AS pending_refund_count`,
refundBizTypes(), refundBizTypes(), refundBizTypes(), refundBizTypes()).
Where("created_at >= ? AND created_at <= ?", query.StartDate, query.EndDate).
if err := db.Table("payment_orders AS po").
Select(`COALESCE(SUM(CASE WHEN po.biz_type IN ('order_pay') 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 ('order_pay') 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`,
refundBizTypes(), refundBizTypes(), refundBizTypes(), refundBizTypes(), refundBizTypes()).
Joins(`LEFT JOIN (
SELECT order_id, MAX(amount_cent) AS amount_cent
FROM payment_orders
WHERE biz_type = 'order_pay' AND status = 'paid'
GROUP BY order_id
) AS orig ON orig.order_id = po.order_id`).
Where("po.created_at >= ? AND po.created_at <= ?", query.StartDate, query.EndDate).
Scan(&payment).Error; err != nil {
return nil, err
}
@@ -85,7 +92,9 @@ func (r *Repository) summary(ctx context.Context, query DashboardQuery) (*Financ
SettlementDiffAmountCent: settlement.OwnerShouldIncomeAmountCent - settlement.OwnerWalletIncomeAmountCent,
EstimatedIncomeAmountCent: estimated.EstimatedIncomeAmountCent,
SuccessfulPayCount: payment.SuccessfulPayCount,
SuccessfulRefundCount: payment.SuccessfulRefundCount,
SuccessfulRefundCount: payment.FullRefundCount + payment.PartialRefundCount,
FullRefundCount: payment.FullRefundCount,
PartialRefundCount: payment.PartialRefundCount,
PendingRefundCount: payment.PendingRefundCount,
SettledOrderCount: settlement.SettledOrderCount,
PendingSettleOrderCount: estimated.PendingSettleOrderCount,
@@ -96,17 +105,24 @@ func (r *Repository) summary(ctx context.Context, query DashboardQuery) (*Financ
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").
Select(`DATE(created_at) AS date,
COALESCE(SUM(CASE WHEN biz_type IN ('order_pay') AND status = 'paid' THEN amount_cent ELSE 0 END), 0) AS total_flow_amount_cent,
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunded' THEN amount_cent ELSE 0 END), 0) AS total_refund_amount_cent,
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunding' THEN amount_cent ELSE 0 END), 0) AS pending_refund_amount_cent,
COALESCE(SUM(CASE WHEN biz_type IN ('order_pay') AND status = 'paid' THEN 1 ELSE 0 END), 0) AS successful_pay_count,
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunded' THEN 1 ELSE 0 END), 0) AS successful_refund_count,
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunding' THEN 1 ELSE 0 END), 0) AS pending_refund_count`,
refundBizTypes(), refundBizTypes(), refundBizTypes(), refundBizTypes()).
Where("created_at >= ? AND created_at <= ?", query.StartDate, query.EndDate).
Group("DATE(created_at)").
if err := db.Table("payment_orders AS po").
Select(`DATE(po.created_at) AS date,
COALESCE(SUM(CASE WHEN po.biz_type IN ('order_pay') 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 ('order_pay') 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`,
refundBizTypes(), refundBizTypes(), refundBizTypes(), refundBizTypes(), refundBizTypes()).
Joins(`LEFT JOIN (
SELECT order_id, MAX(amount_cent) AS amount_cent
FROM payment_orders
WHERE biz_type = 'order_pay' AND status = 'paid'
GROUP BY order_id
) AS orig ON orig.order_id = po.order_id`).
Where("po.created_at >= ? AND po.created_at <= ?", query.StartDate, query.EndDate).
Group("DATE(po.created_at)").
Scan(&payments).Error; err != nil {
return nil, err
}
@@ -153,7 +169,9 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
item.PendingRefundAmountCent = row.PendingRefundAmountCent
item.ChannelNetAmountCent = row.TotalFlowAmountCent - row.TotalRefundAmountCent
item.SuccessfulPayCount = row.SuccessfulPayCount
item.SuccessfulRefundCount = row.SuccessfulRefundCount
item.SuccessfulRefundCount = row.FullRefundCount + row.PartialRefundCount
item.FullRefundCount = row.FullRefundCount
item.PartialRefundCount = row.PartialRefundCount
item.PendingRefundCount = row.PendingRefundCount
itemsByDate[date] = item
}
@@ -188,7 +206,8 @@ type paymentSummaryRow struct {
TotalRefundAmountCent int64
PendingRefundAmountCent int64
SuccessfulPayCount int64
SuccessfulRefundCount int64
FullRefundCount int64
PartialRefundCount int64
PendingRefundCount int64
}
@@ -210,7 +229,8 @@ type dailyPaymentRow struct {
TotalRefundAmountCent int64
PendingRefundAmountCent int64
SuccessfulPayCount int64
SuccessfulRefundCount int64
FullRefundCount int64
PartialRefundCount int64
PendingRefundCount int64
}
@@ -37,6 +37,8 @@ type FinanceSummaryDTO struct {
EstimatedIncomeAmountCent int64 `json:"estimated_income_amount_cent"`
SuccessfulPayCount int64 `json:"successful_pay_count"`
SuccessfulRefundCount int64 `json:"successful_refund_count"`
FullRefundCount int64 `json:"full_refund_count"`
PartialRefundCount int64 `json:"partial_refund_count"`
PendingRefundCount int64 `json:"pending_refund_count"`
SettledOrderCount int64 `json:"settled_order_count"`
PendingSettleOrderCount int64 `json:"pending_settle_order_count"`
@@ -56,6 +58,8 @@ type FinanceDailyDTO struct {
EstimatedIncomeAmountCent int64 `json:"estimated_income_amount_cent"`
SuccessfulPayCount int64 `json:"successful_pay_count"`
SuccessfulRefundCount int64 `json:"successful_refund_count"`
FullRefundCount int64 `json:"full_refund_count"`
PartialRefundCount int64 `json:"partial_refund_count"`
PendingRefundCount int64 `json:"pending_refund_count"`
SettledOrderCount int64 `json:"settled_order_count"`
}