538 lines
23 KiB
Go
538 lines
23 KiB
Go
package adminfinance
|
|
|
|
import (
|
|
"context"
|
|
|
|
"hfb_sys/backend/internal/timeutil"
|
|
)
|
|
|
|
func (r *Repository) Dashboard(ctx context.Context, query DashboardQuery) (*DashboardDTO, error) {
|
|
dailyItems, err := r.dailyItems(ctx, query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pickup, err := r.pickupSummary(ctx, query)
|
|
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
|
|
}
|
|
return &DashboardDTO{
|
|
Summary: *summary,
|
|
DailyItems: dailyItems,
|
|
PickupSummary: *pickup,
|
|
MohongSummary: *mohong,
|
|
DisbursementSummary: *disbursement,
|
|
GeneratedAt: timeutil.ShanghaiNow(),
|
|
}, nil
|
|
}
|
|
|
|
// pickupSummary 线下提号统计,独立查询 admin_pickups 表,不混入正常订单口径。
|
|
func (r *Repository) pickupSummary(ctx context.Context, query DashboardQuery) (*PickupSummaryDTO, error) {
|
|
db := r.db.WithContext(ctx)
|
|
var row struct {
|
|
SettledAmountCent int64
|
|
ProfitAmountCent int64
|
|
CompletedCount int64
|
|
}
|
|
if err := db.Table("admin_pickups").
|
|
Select(`COALESCE(SUM(settle_amount_cent), 0) AS settled_amount_cent, COALESCE(SUM(profit_amount_cent), 0) AS profit_amount_cent, COUNT(id) AS completed_count`).
|
|
Where("status = ?", "completed").
|
|
Where("completed_at >= ? AND completed_at <= ?", query.StartDate, query.EndDate).
|
|
Scan(&row).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
var adjustments struct {
|
|
SettledAmountCent int64
|
|
ProfitAmountCent int64
|
|
}
|
|
if err := db.Table("admin_pickup_financial_adjustments AS fa").
|
|
Select(`COALESCE(SUM(fa.settle_delta_cent), 0) AS settled_amount_cent,
|
|
COALESCE(SUM(fa.profit_delta_cent), 0) AS profit_amount_cent`).
|
|
Joins("JOIN admin_pickups AS p ON p.id = fa.pickup_id AND p.status = ?", "completed").
|
|
Where("fa.created_at >= ? AND fa.created_at <= ?", query.StartDate, query.EndDate).
|
|
Scan(&adjustments).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
var inProgress int64
|
|
if err := db.Table("admin_pickups").Where("status = ?", "picking_up").Count(&inProgress).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &PickupSummaryDTO{
|
|
SettledAmountCent: row.SettledAmountCent + adjustments.SettledAmountCent,
|
|
ProfitAmountCent: row.ProfitAmountCent + adjustments.ProfitAmountCent,
|
|
CompletedCount: row.CompletedCount,
|
|
InProgressCount: inProgress,
|
|
}, nil
|
|
}
|
|
|
|
// mohongSummary 撞车商城按支付订单统计交易流水,不计入平台利润。
|
|
func (r *Repository) mohongSummary(ctx context.Context, query DashboardQuery) (*MohongSummaryDTO, error) {
|
|
db := r.db.WithContext(ctx)
|
|
var row struct {
|
|
FlowAmountCent int64
|
|
RefundAmountCent int64
|
|
PaidOrderCount int64
|
|
}
|
|
if err := db.Table("payment_orders").
|
|
Select(`COALESCE(SUM(CASE WHEN biz_type = 'mohong_pay' AND status = 'paid' THEN amount_cent ELSE 0 END), 0) AS flow_amount_cent,
|
|
COALESCE(SUM(CASE WHEN biz_type = 'mohong_refund' AND status = 'refunded' THEN amount_cent ELSE 0 END), 0) AS refund_amount_cent,
|
|
COALESCE(SUM(CASE WHEN biz_type = 'mohong_pay' AND status = 'paid' THEN 1 ELSE 0 END), 0) AS paid_order_count`).
|
|
Where("created_at >= ? AND created_at <= ?", query.StartDate, query.EndDate).
|
|
Scan(&row).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &MohongSummaryDTO{
|
|
FlowAmountCent: row.FlowAmountCent,
|
|
RefundAmountCent: row.RefundAmountCent,
|
|
NetAmountCent: row.FlowAmountCent - row.RefundAmountCent,
|
|
PaidOrderCount: row.PaidOrderCount,
|
|
}, nil
|
|
}
|
|
|
|
func (r *Repository) summary(ctx context.Context, query DashboardQuery, pickup *PickupSummaryDTO) (*FinanceSummaryDTO, error) {
|
|
db := r.db.WithContext(ctx)
|
|
var payment paymentSummaryRow
|
|
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,
|
|
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).
|
|
Scan(&payment).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var settlement settlementSummaryRow
|
|
if err := db.Table("rental_orders AS ro").
|
|
Select(`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,
|
|
COALESCE(SUM(COALESCE(w.owner_wallet_income_amount_cent, 0)), 0) AS owner_wallet_income_amount_cent,
|
|
COALESCE(SUM(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), 0) AS owner_effective_settlement_amount_cent,
|
|
COALESCE(SUM(CASE WHEN ro.settlement_mode = 'platform_managed'
|
|
THEN COALESCE(ro.offline_settlement_amount_cent, 0)
|
|
ELSE 0
|
|
END), 0) AS offline_settlement_amount_cent,
|
|
COALESCE(SUM(CASE WHEN ro.settlement_mode = 'platform_managed'
|
|
AND COALESCE(NULLIF(ro.offline_settlement_status, ''), 'none') = 'pending'
|
|
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'
|
|
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`).
|
|
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)).
|
|
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").
|
|
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
|
|
}
|
|
|
|
pickupProfitAmountCent := int64(0)
|
|
pickupCompletedCount := int64(0)
|
|
if pickup != nil {
|
|
pickupProfitAmountCent = pickup.ProfitAmountCent
|
|
pickupCompletedCount = pickup.CompletedCount
|
|
}
|
|
return &FinanceSummaryDTO{
|
|
TotalFlowAmountCent: payment.TotalFlowAmountCent,
|
|
TotalRefundAmountCent: payment.TotalRefundAmountCent,
|
|
PendingRefundAmountCent: payment.PendingRefundAmountCent,
|
|
ChannelNetAmountCent: payment.TotalFlowAmountCent - payment.TotalRefundAmountCent,
|
|
PlatformIncomeAmountCent: settlement.PlatformIncomeAmountCent + pickupProfitAmountCent,
|
|
NormalOrderProfitAmountCent: settlement.PlatformIncomeAmountCent,
|
|
PickupProfitAmountCent: pickupProfitAmountCent,
|
|
PickupCompletedCount: pickupCompletedCount,
|
|
OwnerShouldIncomeAmountCent: settlement.OwnerShouldIncomeAmountCent,
|
|
OwnerWalletIncomeAmountCent: settlement.OwnerWalletIncomeAmountCent,
|
|
OwnerEffectiveSettlementAmountCent: settlement.OwnerEffectiveSettlementAmountCent,
|
|
OfflineSettlementAmountCent: settlement.OfflineSettlementAmountCent,
|
|
OfflineSettlementPendingAmountCent: settlement.OfflineSettlementPendingAmountCent,
|
|
SettlementDiffAmountCent: settlement.OwnerShouldIncomeAmountCent - settlement.OwnerEffectiveSettlementAmountCent,
|
|
EstimatedIncomeAmountCent: estimated.EstimatedIncomeAmountCent,
|
|
SuccessfulPayCount: payment.SuccessfulPayCount,
|
|
SuccessfulRefundCount: payment.FullRefundCount + payment.PartialRefundCount,
|
|
FullRefundCount: payment.FullRefundCount,
|
|
PartialRefundCount: payment.PartialRefundCount,
|
|
PendingRefundCount: payment.PendingRefundCount,
|
|
SettledOrderCount: settlement.SettledOrderCount,
|
|
PendingSettleOrderCount: estimated.PendingSettleOrderCount,
|
|
OfflineSettlementPendingCount: settlement.OfflineSettlementPendingCount,
|
|
FinancialExceptionCount: exceptionCount,
|
|
}, nil
|
|
}
|
|
|
|
func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]FinanceDailyDTO, error) {
|
|
db := r.db.WithContext(ctx)
|
|
payments, err := r.dailyPayments(ctx, query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
paidOrders, err := r.dailyPaidOrders(ctx, query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
settlements := make([]dailySettlementRow, 0)
|
|
if err := db.Table("rental_orders AS ro").
|
|
Select(`DATE(ro.settled_at) 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,
|
|
COALESCE(SUM(COALESCE(w.owner_wallet_income_amount_cent, 0)), 0) AS owner_wallet_income_amount_cent,
|
|
COALESCE(SUM(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), 0) AS owner_effective_settlement_amount_cent,
|
|
COALESCE(SUM(CASE WHEN ro.settlement_mode = 'platform_managed'
|
|
THEN COALESCE(ro.offline_settlement_amount_cent, 0)
|
|
ELSE 0
|
|
END), 0) AS offline_settlement_amount_cent,
|
|
COALESCE(SUM(CASE WHEN ro.settlement_mode = 'platform_managed'
|
|
AND COALESCE(NULLIF(ro.offline_settlement_status, ''), 'none') = 'pending'
|
|
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'
|
|
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`).
|
|
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)).
|
|
Where("ro.settled_at >= ? AND ro.settled_at <= ?", query.StartDate, query.EndDate).
|
|
Group("DATE(ro.settled_at)").
|
|
Scan(&settlements).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pickupProfits := make([]dailyPickupProfitRow, 0)
|
|
if err := db.Table("admin_pickups").
|
|
Select(`DATE(completed_at) AS date,
|
|
COALESCE(SUM(profit_amount_cent), 0) AS profit_amount_cent,
|
|
COUNT(id) AS completed_count`).
|
|
Where("status = ?", "completed").
|
|
Where("completed_at >= ? AND completed_at <= ?", query.StartDate, query.EndDate).
|
|
Group("DATE(completed_at)").
|
|
Scan(&pickupProfits).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
pickupAdjustments := make([]dailyPickupAdjustmentRow, 0)
|
|
if err := db.Table("admin_pickup_financial_adjustments AS fa").
|
|
Select(`DATE(fa.created_at) AS date,
|
|
COALESCE(SUM(fa.profit_delta_cent), 0) AS profit_amount_cent`).
|
|
Joins("JOIN admin_pickups AS p ON p.id = fa.pickup_id AND p.status = ?", "completed").
|
|
Where("fa.created_at >= ? AND fa.created_at <= ?", query.StartDate, query.EndDate).
|
|
Group("DATE(fa.created_at)").
|
|
Scan(&pickupAdjustments).Error; err != nil {
|
|
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").
|
|
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
|
|
}
|
|
|
|
disbursements, err := r.dailyDisbursements(ctx, query)
|
|
if 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")
|
|
itemsByDate[date] = FinanceDailyDTO{Date: date}
|
|
}
|
|
for _, row := range payments {
|
|
date := dailyDateKey(row.Date)
|
|
item := itemsByDate[date]
|
|
item.Date = date
|
|
item.TotalFlowAmountCent = row.TotalFlowAmountCent
|
|
item.TotalRefundAmountCent = row.TotalRefundAmountCent
|
|
item.PendingRefundAmountCent = row.PendingRefundAmountCent
|
|
item.ChannelNetAmountCent = row.TotalFlowAmountCent - row.TotalRefundAmountCent
|
|
item.SuccessfulPayCount = row.SuccessfulPayCount
|
|
item.SuccessfulRefundCount = row.FullRefundCount + row.PartialRefundCount
|
|
item.FullRefundCount = row.FullRefundCount
|
|
item.PartialRefundCount = row.PartialRefundCount
|
|
item.PendingRefundCount = row.PendingRefundCount
|
|
item.NormalFullRefundCount = row.NormalFullRefundCount
|
|
itemsByDate[date] = item
|
|
}
|
|
for _, row := range paidOrders {
|
|
date := dailyDateKey(row.Date)
|
|
item := itemsByDate[date]
|
|
item.Date = date
|
|
item.PaidOrderCount = row.PaidOrderCount
|
|
itemsByDate[date] = item
|
|
}
|
|
for _, row := range settlements {
|
|
date := dailyDateKey(row.Date)
|
|
item := itemsByDate[date]
|
|
item.Date = date
|
|
item.PlatformIncomeAmountCent = row.PlatformIncomeAmountCent
|
|
item.OwnerShouldIncomeAmountCent = row.OwnerShouldIncomeAmountCent
|
|
item.OwnerWalletIncomeAmountCent = row.OwnerWalletIncomeAmountCent
|
|
item.OwnerEffectiveSettlementAmountCent = row.OwnerEffectiveSettlementAmountCent
|
|
item.OfflineSettlementAmountCent = row.OfflineSettlementAmountCent
|
|
item.OfflineSettlementPendingAmountCent = row.OfflineSettlementPendingAmountCent
|
|
item.SettlementDiffAmountCent = row.OwnerShouldIncomeAmountCent - row.OwnerEffectiveSettlementAmountCent
|
|
item.SettledOrderCount = row.SettledOrderCount
|
|
item.SalesCount = row.SettledOrderCount
|
|
item.OfflineSettlementPendingCount = row.OfflineSettlementPendingCount
|
|
itemsByDate[date] = item
|
|
}
|
|
for _, row := range pickupProfits {
|
|
date := dailyDateKey(row.Date)
|
|
item := itemsByDate[date]
|
|
item.Date = date
|
|
item.PlatformIncomeAmountCent += row.ProfitAmountCent
|
|
item.PickupCompletedCount = row.CompletedCount
|
|
item.SalesCount += row.CompletedCount
|
|
itemsByDate[date] = item
|
|
}
|
|
for _, row := range pickupAdjustments {
|
|
date := dailyDateKey(row.Date)
|
|
item := itemsByDate[date]
|
|
item.Date = date
|
|
item.PlatformIncomeAmountCent += row.ProfitAmountCent
|
|
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]
|
|
item.Date = date
|
|
item.EstimatedIncomeAmountCent = row.EstimatedIncomeAmountCent
|
|
itemsByDate[date] = item
|
|
}
|
|
for _, row := range disbursements {
|
|
date := dailyDateKey(row.Date)
|
|
item := itemsByDate[date]
|
|
item.Date = date
|
|
item.OfflineSettlementPaidAmountCent = row.OfflineSettlementPaidAmountCent
|
|
item.OfflineSettlementPaidCount = row.OfflineSettlementPaidCount
|
|
item.WithdrawalPaidAmountCent = row.WithdrawalPaidAmountCent
|
|
item.WithdrawalPaidCount = row.WithdrawalPaidCount
|
|
item.ManualPaidAmountCent = row.ManualPaidAmountCent
|
|
item.ManualPaidCount = row.ManualPaidCount
|
|
item.DisbursementPaidAmountCent = row.OfflineSettlementPaidAmountCent + row.WithdrawalPaidAmountCent + row.ManualPaidAmountCent
|
|
item.DisbursementPaidCount = row.OfflineSettlementPaidCount + row.WithdrawalPaidCount + row.ManualPaidCount
|
|
itemsByDate[date] = item
|
|
}
|
|
|
|
items := make([]FinanceDailyDTO, 0, len(itemsByDate))
|
|
for day := dayStart(query.StartDate); !day.After(query.EndDate); day = day.AddDate(0, 0, 1) {
|
|
items = append(items, itemsByDate[day.Format("2006-01-02")])
|
|
}
|
|
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)
|
|
err := r.db.WithContext(ctx).Table(`(
|
|
SELECT DATE(paid_at) AS date, COUNT(DISTINCT order_id) AS paid_order_count
|
|
FROM payment_orders
|
|
WHERE biz_type = 'order_pay' AND status = 'paid' AND paid_at >= ? AND paid_at <= ?
|
|
GROUP BY DATE(paid_at)
|
|
UNION ALL
|
|
SELECT DATE(created_at) AS date, COUNT(id) AS paid_order_count
|
|
FROM admin_pickups
|
|
WHERE created_at >= ? AND created_at <= ?
|
|
GROUP BY DATE(created_at)
|
|
) AS daily_paid_orders`, query.StartDate, query.EndDate, query.StartDate, query.EndDate).
|
|
Select("date, SUM(paid_order_count) AS paid_order_count").
|
|
Group("date").
|
|
Scan(&items).Error
|
|
return items, err
|
|
}
|
|
|
|
type paymentSummaryRow struct {
|
|
TotalFlowAmountCent int64
|
|
TotalRefundAmountCent int64
|
|
PendingRefundAmountCent int64
|
|
SuccessfulPayCount int64
|
|
FullRefundCount int64
|
|
PartialRefundCount int64
|
|
PendingRefundCount int64
|
|
}
|
|
|
|
type settlementSummaryRow struct {
|
|
PlatformIncomeAmountCent int64
|
|
OwnerShouldIncomeAmountCent int64
|
|
OwnerWalletIncomeAmountCent int64
|
|
OwnerEffectiveSettlementAmountCent int64
|
|
OfflineSettlementAmountCent int64
|
|
OfflineSettlementPendingAmountCent int64
|
|
OfflineSettlementPendingCount int64
|
|
SettledOrderCount int64
|
|
}
|
|
|
|
type estimatedIncomeRow struct {
|
|
EstimatedIncomeAmountCent int64
|
|
PendingSettleOrderCount int64
|
|
}
|
|
|
|
type dailyPaymentRow struct {
|
|
Date string
|
|
TotalFlowAmountCent int64
|
|
TotalRefundAmountCent int64
|
|
PendingRefundAmountCent int64
|
|
SuccessfulPayCount int64
|
|
FullRefundCount int64
|
|
PartialRefundCount int64
|
|
PendingRefundCount int64
|
|
NormalFullRefundCount int64
|
|
}
|
|
|
|
type dailyPickupCancelledRow struct {
|
|
Date string
|
|
Count int64
|
|
}
|
|
|
|
type dailyPaidOrderRow struct {
|
|
Date string
|
|
PaidOrderCount int64
|
|
}
|
|
|
|
type dailySettlementRow struct {
|
|
Date string
|
|
PlatformIncomeAmountCent int64
|
|
OwnerShouldIncomeAmountCent int64
|
|
OwnerWalletIncomeAmountCent int64
|
|
OwnerEffectiveSettlementAmountCent int64
|
|
OfflineSettlementAmountCent int64
|
|
OfflineSettlementPendingAmountCent int64
|
|
OfflineSettlementPendingCount int64
|
|
SettledOrderCount int64
|
|
}
|
|
|
|
type dailyPickupProfitRow struct {
|
|
Date string
|
|
ProfitAmountCent int64
|
|
CompletedCount int64
|
|
}
|
|
|
|
// dailyPickupAdjustmentRow 调整按调整创建日进入当日利润,避免回写历史完成日报。
|
|
type dailyPickupAdjustmentRow struct {
|
|
Date string
|
|
ProfitAmountCent int64
|
|
}
|
|
|
|
type dailyEstimatedRow struct {
|
|
Date string
|
|
EstimatedIncomeAmountCent int64
|
|
}
|