|
|
|
@@ -14,14 +14,12 @@ func (r *Repository) Dashboard(ctx context.Context, query DashboardQuery) (*Dash
|
|
|
|
|
// 各板块的读取互不依赖。并行执行能避免单个请求串行等待二十余条统计 SQL,
|
|
|
|
|
// 首次打开或缓存失效时仍可尽快返回;每个查询继续复用 GORM 的连接池。
|
|
|
|
|
var (
|
|
|
|
|
dailyItems []FinanceDailyDTO
|
|
|
|
|
pickup *PickupSummaryDTO
|
|
|
|
|
mohong *MohongSummaryDTO
|
|
|
|
|
disbursement *DisbursementSummaryDTO
|
|
|
|
|
operatingExpense *OperatingExpenseSummaryDTO
|
|
|
|
|
daily dashboardDailyResult
|
|
|
|
|
pickup *PickupSummaryDTO
|
|
|
|
|
disbursement *DisbursementSummaryDTO
|
|
|
|
|
)
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
errCh := make(chan error, 5)
|
|
|
|
|
errCh := make(chan error, 3)
|
|
|
|
|
run := func(fn func() error) {
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
@@ -33,7 +31,7 @@ func (r *Repository) Dashboard(ctx context.Context, query DashboardQuery) (*Dash
|
|
|
|
|
}
|
|
|
|
|
run(func() error {
|
|
|
|
|
var err error
|
|
|
|
|
dailyItems, err = r.dailyItems(ctx, query)
|
|
|
|
|
daily, err = r.dailyItems(ctx, query)
|
|
|
|
|
return err
|
|
|
|
|
})
|
|
|
|
|
run(func() error {
|
|
|
|
@@ -41,21 +39,11 @@ func (r *Repository) Dashboard(ctx context.Context, query DashboardQuery) (*Dash
|
|
|
|
|
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 {
|
|
|
|
@@ -64,17 +52,14 @@ func (r *Repository) Dashboard(ctx context.Context, query DashboardQuery) (*Dash
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
summary, err := r.summary(ctx, query, pickup)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
summary := summaryWithPickup(daily.Summary, pickup)
|
|
|
|
|
result := &DashboardDTO{
|
|
|
|
|
Summary: *summary,
|
|
|
|
|
DailyItems: dailyItems,
|
|
|
|
|
Summary: summary,
|
|
|
|
|
DailyItems: daily.Items,
|
|
|
|
|
PickupSummary: *pickup,
|
|
|
|
|
MohongSummary: *mohong,
|
|
|
|
|
MohongSummary: daily.Mohong,
|
|
|
|
|
DisbursementSummary: *disbursement,
|
|
|
|
|
OperatingExpenseSummary: *operatingExpense,
|
|
|
|
|
OperatingExpenseSummary: daily.OperatingExpense,
|
|
|
|
|
GeneratedAt: timeutil.ShanghaiNow(),
|
|
|
|
|
}
|
|
|
|
|
r.storeDashboardCache(ctx, query, result)
|
|
|
|
@@ -179,157 +164,57 @@ func (r *Repository) mohongSummary(ctx context.Context, query DashboardQuery) (*
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
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 (?) 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
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", 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 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 + pickup.PendingAmountCent,
|
|
|
|
|
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 + pickup.PendingCount,
|
|
|
|
|
FinancialExceptionCount: settlement.FinancialExceptionCount,
|
|
|
|
|
}, nil
|
|
|
|
|
type dashboardDailyResult struct {
|
|
|
|
|
Items []FinanceDailyDTO
|
|
|
|
|
Summary FinanceSummaryDTO
|
|
|
|
|
Mohong MohongSummaryDTO
|
|
|
|
|
OperatingExpense OperatingExpenseSummaryDTO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]FinanceDailyDTO, error) {
|
|
|
|
|
func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) (dashboardDailyResult, error) {
|
|
|
|
|
db := r.db.WithContext(ctx)
|
|
|
|
|
payments, err := r.dailyPayments(ctx, query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
var (
|
|
|
|
|
payments []dailyPaymentRow
|
|
|
|
|
settlements []dailySettlementRow
|
|
|
|
|
estimates []dailyEstimatedRow
|
|
|
|
|
)
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
errCh := make(chan error, 3)
|
|
|
|
|
run := func(fn func() error) {
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
|
errCh <- err
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
paidOrders, err := r.dailyPaidOrders(ctx, query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
run(func() error {
|
|
|
|
|
var err error
|
|
|
|
|
payments, err = r.dailyPayments(ctx, query)
|
|
|
|
|
return err
|
|
|
|
|
})
|
|
|
|
|
run(func() error {
|
|
|
|
|
var err error
|
|
|
|
|
settlements, err = r.dailySettlements(ctx, query)
|
|
|
|
|
return err
|
|
|
|
|
})
|
|
|
|
|
run(func() error {
|
|
|
|
|
var err error
|
|
|
|
|
estimates, err = r.dailyEstimates(ctx, query)
|
|
|
|
|
return err
|
|
|
|
|
})
|
|
|
|
|
wg.Wait()
|
|
|
|
|
close(errCh)
|
|
|
|
|
for err := range errCh {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return dashboardDailyResult{}, 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", 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 {
|
|
|
|
|
return nil, err
|
|
|
|
|
paidOrders, err := r.dailyPaidOrders(ctx, query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return dashboardDailyResult{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pickupProfits := make([]dailyPickupProfitRow, 0)
|
|
|
|
@@ -341,7 +226,7 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
|
|
|
|
|
Where("completed_at >= ? AND completed_at <= ?", query.StartDate, query.EndDate).
|
|
|
|
|
Group("DATE(completed_at)").
|
|
|
|
|
Scan(&pickupProfits).Error; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
return dashboardDailyResult{}, err
|
|
|
|
|
}
|
|
|
|
|
pickupAdjustments := make([]dailyPickupAdjustmentRow, 0)
|
|
|
|
|
if err := db.Table("admin_pickup_financial_adjustments AS fa").
|
|
|
|
@@ -351,35 +236,23 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
|
|
|
|
|
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
|
|
|
|
|
return dashboardDailyResult{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取消提号按取消日统计,与「已付款订单数」中的提号创建口径区分开。
|
|
|
|
|
pickupCancelled, err := r.dailyPickupCancelled(ctx, query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
return dashboardDailyResult{}, 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
|
|
|
|
|
return dashboardDailyResult{}, err
|
|
|
|
|
}
|
|
|
|
|
operatingExpenses, err := r.dailyOperatingExpenses(ctx, query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
return dashboardDailyResult{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
itemsByDate := make(map[string]FinanceDailyDTO)
|
|
|
|
@@ -483,7 +356,142 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
|
|
|
|
|
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
|
|
|
|
|
return dashboardDailyResult{
|
|
|
|
|
Items: items,
|
|
|
|
|
Summary: summaryFromDailyRows(payments, settlements, estimates),
|
|
|
|
|
Mohong: mohongSummaryFromDailyRows(payments),
|
|
|
|
|
OperatingExpense: operatingExpenseSummaryFromDailyRows(operatingExpenses),
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Repository) dailyEstimates(ctx context.Context, query DashboardQuery) ([]dailyEstimatedRow, error) {
|
|
|
|
|
rows := make([]dailyEstimatedRow, 0)
|
|
|
|
|
err := r.db.WithContext(ctx).Table("rental_orders").
|
|
|
|
|
Select(`DATE(created_at) AS date,
|
|
|
|
|
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).
|
|
|
|
|
Group("DATE(created_at)").
|
|
|
|
|
Scan(&rows).Error
|
|
|
|
|
return rows, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Repository) dailySettlements(ctx context.Context, query DashboardQuery) ([]dailySettlementRow, error) {
|
|
|
|
|
db := r.db.WithContext(ctx)
|
|
|
|
|
rows := make([]dailySettlementRow, 0)
|
|
|
|
|
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,
|
|
|
|
|
COALESCE(SUM(CASE WHEN 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) >= ?
|
|
|
|
|
OR EXISTS (
|
|
|
|
|
SELECT 1 FROM payment_orders AS exception_payment
|
|
|
|
|
WHERE exception_payment.order_id = ro.id
|
|
|
|
|
AND exception_payment.biz_type IN ?
|
|
|
|
|
AND exception_payment.status IN ?
|
|
|
|
|
AND exception_payment.amount_cent > 0
|
|
|
|
|
)
|
|
|
|
|
THEN 1
|
|
|
|
|
ELSE 0
|
|
|
|
|
END), 0) AS financial_exception_count,
|
|
|
|
|
COUNT(ro.id) AS settled_order_count`, settlementDiffThresholdCent, refundBizTypes(), []string{"refunding", "failed"}).
|
|
|
|
|
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", ownerWalletIncomeForSettledOrdersSubquery(db, query)).
|
|
|
|
|
Where("ro.settled_at >= ? AND ro.settled_at <= ?", query.StartDate, query.EndDate).
|
|
|
|
|
Group("DATE(ro.settled_at)").
|
|
|
|
|
Scan(&rows).Error
|
|
|
|
|
return rows, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func summaryFromDailyRows(payments []dailyPaymentRow, settlements []dailySettlementRow, estimates []dailyEstimatedRow) FinanceSummaryDTO {
|
|
|
|
|
var summary FinanceSummaryDTO
|
|
|
|
|
for _, row := range payments {
|
|
|
|
|
summary.TotalFlowAmountCent += row.TotalFlowAmountCent
|
|
|
|
|
summary.TotalRefundAmountCent += row.TotalRefundAmountCent
|
|
|
|
|
summary.PendingRefundAmountCent += row.PendingRefundAmountCent
|
|
|
|
|
summary.SuccessfulPayCount += row.SuccessfulPayCount
|
|
|
|
|
summary.FullRefundCount += row.FullRefundCount
|
|
|
|
|
summary.PartialRefundCount += row.PartialRefundCount
|
|
|
|
|
summary.PendingRefundCount += row.PendingRefundCount
|
|
|
|
|
}
|
|
|
|
|
for _, row := range settlements {
|
|
|
|
|
summary.NormalOrderProfitAmountCent += row.PlatformIncomeAmountCent
|
|
|
|
|
summary.OwnerShouldIncomeAmountCent += row.OwnerShouldIncomeAmountCent
|
|
|
|
|
summary.OwnerWalletIncomeAmountCent += row.OwnerWalletIncomeAmountCent
|
|
|
|
|
summary.OwnerEffectiveSettlementAmountCent += row.OwnerEffectiveSettlementAmountCent
|
|
|
|
|
summary.OfflineSettlementAmountCent += row.OfflineSettlementAmountCent
|
|
|
|
|
summary.OfflineSettlementPendingAmountCent += row.OfflineSettlementPendingAmountCent
|
|
|
|
|
summary.OfflineSettlementPendingCount += row.OfflineSettlementPendingCount
|
|
|
|
|
summary.FinancialExceptionCount += row.FinancialExceptionCount
|
|
|
|
|
summary.SettledOrderCount += row.SettledOrderCount
|
|
|
|
|
}
|
|
|
|
|
for _, row := range estimates {
|
|
|
|
|
summary.EstimatedIncomeAmountCent += row.EstimatedIncomeAmountCent
|
|
|
|
|
summary.PendingSettleOrderCount += row.PendingSettleOrderCount
|
|
|
|
|
}
|
|
|
|
|
summary.ChannelNetAmountCent = summary.TotalFlowAmountCent - summary.TotalRefundAmountCent
|
|
|
|
|
summary.SuccessfulRefundCount = summary.FullRefundCount + summary.PartialRefundCount
|
|
|
|
|
summary.PlatformIncomeAmountCent = summary.NormalOrderProfitAmountCent
|
|
|
|
|
summary.SettlementDiffAmountCent = summary.OwnerShouldIncomeAmountCent - summary.OwnerEffectiveSettlementAmountCent
|
|
|
|
|
return summary
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func summaryWithPickup(summary FinanceSummaryDTO, pickup *PickupSummaryDTO) FinanceSummaryDTO {
|
|
|
|
|
if pickup == nil {
|
|
|
|
|
return summary
|
|
|
|
|
}
|
|
|
|
|
summary.PickupProfitAmountCent = pickup.ProfitAmountCent
|
|
|
|
|
summary.PickupCompletedCount = pickup.CompletedCount
|
|
|
|
|
summary.PlatformIncomeAmountCent = summary.NormalOrderProfitAmountCent + pickup.ProfitAmountCent
|
|
|
|
|
summary.OfflineSettlementPendingAmountCent += pickup.PendingAmountCent
|
|
|
|
|
summary.OfflineSettlementPendingCount += pickup.PendingCount
|
|
|
|
|
return summary
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mohongSummaryFromDailyRows(rows []dailyPaymentRow) MohongSummaryDTO {
|
|
|
|
|
var summary MohongSummaryDTO
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
summary.FlowAmountCent += row.MohongFlowAmountCent
|
|
|
|
|
summary.RefundAmountCent += row.MohongRefundAmountCent
|
|
|
|
|
summary.PaidOrderCount += row.MohongPaidOrderCount
|
|
|
|
|
}
|
|
|
|
|
summary.NetAmountCent = summary.FlowAmountCent - summary.RefundAmountCent
|
|
|
|
|
return summary
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func operatingExpenseSummaryFromDailyRows(rows []dailyOperatingExpenseRow) OperatingExpenseSummaryDTO {
|
|
|
|
|
var summary OperatingExpenseSummaryDTO
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
summary.AmountCent += row.AmountCent
|
|
|
|
|
summary.Count += row.Count
|
|
|
|
|
}
|
|
|
|
|
return summary
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// dailyPayments 按支付单创建日统计收付款流水与退款笔数,
|
|
|
|
@@ -500,7 +508,10 @@ func (r *Repository) dailyPayments(ctx context.Context, query DashboardQuery) ([
|
|
|
|
|
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`,
|
|
|
|
|
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,
|
|
|
|
|
COALESCE(SUM(CASE WHEN po.biz_type = 'mohong_pay' AND po.status = 'paid' THEN po.amount_cent ELSE 0 END), 0) AS mohong_flow_amount_cent,
|
|
|
|
|
COALESCE(SUM(CASE WHEN po.biz_type = 'mohong_refund' AND po.status = 'refunded' THEN po.amount_cent ELSE 0 END), 0) AS mohong_refund_amount_cent,
|
|
|
|
|
COALESCE(SUM(CASE WHEN po.biz_type = 'mohong_pay' AND po.status = 'paid' THEN 1 ELSE 0 END), 0) AS mohong_paid_order_count`,
|
|
|
|
|
payBizTypes(), refundBizTypes(), refundBizTypes(), payBizTypes(), refundBizTypes(), refundBizTypes(), refundBizTypes(), normalRefundBizTypes()).
|
|
|
|
|
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).
|
|
|
|
@@ -541,33 +552,6 @@ func (r *Repository) dailyPaidOrders(ctx context.Context, query DashboardQuery)
|
|
|
|
|
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
|
|
|
|
|
FinancialExceptionCount int64
|
|
|
|
|
SettledOrderCount int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type estimatedIncomeRow struct {
|
|
|
|
|
EstimatedIncomeAmountCent int64
|
|
|
|
|
PendingSettleOrderCount int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type dailyPaymentRow struct {
|
|
|
|
|
Date string
|
|
|
|
|
TotalFlowAmountCent int64
|
|
|
|
@@ -578,6 +562,9 @@ type dailyPaymentRow struct {
|
|
|
|
|
PartialRefundCount int64
|
|
|
|
|
PendingRefundCount int64
|
|
|
|
|
NormalFullRefundCount int64
|
|
|
|
|
MohongFlowAmountCent int64
|
|
|
|
|
MohongRefundAmountCent int64
|
|
|
|
|
MohongPaidOrderCount int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type dailyPickupCancelledRow struct {
|
|
|
|
@@ -599,6 +586,7 @@ type dailySettlementRow struct {
|
|
|
|
|
OfflineSettlementAmountCent int64
|
|
|
|
|
OfflineSettlementPendingAmountCent int64
|
|
|
|
|
OfflineSettlementPendingCount int64
|
|
|
|
|
FinancialExceptionCount int64
|
|
|
|
|
SettledOrderCount int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -617,4 +605,5 @@ type dailyPickupAdjustmentRow struct {
|
|
|
|
|
type dailyEstimatedRow struct {
|
|
|
|
|
Date string
|
|
|
|
|
EstimatedIncomeAmountCent int64
|
|
|
|
|
PendingSettleOrderCount int64
|
|
|
|
|
}
|
|
|
|
|