feat(tenant): 按租户隔离支付退款与商户配置
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"sync"
|
||||
|
||||
ordermodule "hfb_sys/backend/internal/modules/order"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
)
|
||||
|
||||
@@ -14,7 +15,7 @@ func (r *Repository) Dashboard(ctx context.Context, query DashboardQuery) (*Dash
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
key := r.dashboardCacheKey(query)
|
||||
key := r.dashboardCacheKey(ctx, query)
|
||||
val, err, _ := r.sfGroup.Do(key, func() (any, error) {
|
||||
// 在 singleflight 内部二次检查缓存,避免排队等待的并发请求重复执行大聚合
|
||||
if cached := r.loadDashboardCache(ctx, query); cached != nil {
|
||||
@@ -90,6 +91,9 @@ func (r *Repository) calculateDashboard(ctx context.Context, query DashboardQuer
|
||||
|
||||
func (r *Repository) operatingExpenseSummary(ctx context.Context, query DashboardQuery) (*OperatingExpenseSummaryDTO, error) {
|
||||
var summary OperatingExpenseSummaryDTO
|
||||
if tenancy.IDFromContext(ctx) > 0 {
|
||||
return &summary, nil
|
||||
}
|
||||
err := r.db.WithContext(ctx).Table("operating_expenses").
|
||||
Select("COALESCE(SUM(amount_cent), 0) AS amount_cent, COUNT(id) AS count").
|
||||
Where("status = ?", "paid").
|
||||
@@ -103,6 +107,9 @@ func (r *Repository) operatingExpenseSummary(ctx context.Context, query Dashboar
|
||||
|
||||
// pickupSummary 线下提号统计,独立查询 admin_pickups 表,不混入正常订单口径。
|
||||
func (r *Repository) pickupSummary(ctx context.Context, query DashboardQuery) (*PickupSummaryDTO, error) {
|
||||
if tenancy.IDFromContext(ctx) > 0 {
|
||||
return &PickupSummaryDTO{}, nil
|
||||
}
|
||||
db := r.db.WithContext(ctx)
|
||||
var row struct {
|
||||
SettledAmountCent int64
|
||||
@@ -170,7 +177,11 @@ func (r *Repository) mohongSummary(ctx context.Context, query DashboardQuery) (*
|
||||
RefundAmountCent int64
|
||||
PaidOrderCount int64
|
||||
}
|
||||
if err := db.Table("payment_orders").
|
||||
paymentQuery := db.Table("payment_orders")
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
paymentQuery = paymentQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := paymentQuery.
|
||||
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`).
|
||||
@@ -240,25 +251,29 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) (dash
|
||||
}
|
||||
|
||||
pickupProfits := make([]dailyPickupProfitRow, 0)
|
||||
if err := db.Table("admin_pickups USE INDEX (idx_admin_pickups_dashboard_completed)").
|
||||
Select(`DATE_FORMAT(completed_at, '%Y-%m-%d') AS date,
|
||||
if tenancy.IDFromContext(ctx) == 0 {
|
||||
if err := db.Table("admin_pickups USE INDEX (idx_admin_pickups_dashboard_completed)").
|
||||
Select(`DATE_FORMAT(completed_at, '%Y-%m-%d') 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_FORMAT(completed_at, '%Y-%m-%d')").
|
||||
Scan(&pickupProfits).Error; err != nil {
|
||||
return dashboardDailyResult{}, err
|
||||
Where("status = ?", "completed").
|
||||
Where("completed_at >= ? AND completed_at <= ?", query.StartDate, query.EndDate).
|
||||
Group("DATE_FORMAT(completed_at, '%Y-%m-%d')").
|
||||
Scan(&pickupProfits).Error; err != nil {
|
||||
return dashboardDailyResult{}, err
|
||||
}
|
||||
}
|
||||
pickupAdjustments := make([]dailyPickupAdjustmentRow, 0)
|
||||
if err := db.Table("admin_pickup_financial_adjustments AS fa").
|
||||
Select(`DATE_FORMAT(fa.created_at, '%Y-%m-%d') AS date,
|
||||
if tenancy.IDFromContext(ctx) == 0 {
|
||||
if err := db.Table("admin_pickup_financial_adjustments AS fa").
|
||||
Select(`DATE_FORMAT(fa.created_at, '%Y-%m-%d') 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_FORMAT(fa.created_at, '%Y-%m-%d')").
|
||||
Scan(&pickupAdjustments).Error; err != nil {
|
||||
return dashboardDailyResult{}, err
|
||||
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_FORMAT(fa.created_at, '%Y-%m-%d')").
|
||||
Scan(&pickupAdjustments).Error; err != nil {
|
||||
return dashboardDailyResult{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// 取消提号按取消日统计,与「已付款订单数」中的提号创建口径区分开。
|
||||
@@ -388,13 +403,17 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) (dash
|
||||
|
||||
func (r *Repository) dailyEstimates(ctx context.Context, query DashboardQuery) ([]dailyEstimatedRow, error) {
|
||||
rows := make([]dailyEstimatedRow, 0)
|
||||
err := r.db.WithContext(ctx).Table("rental_orders USE INDEX (idx_rental_orders_dashboard_estimates)").
|
||||
orderQuery := r.db.WithContext(ctx).Table("rental_orders USE INDEX (idx_rental_orders_dashboard_estimates)").
|
||||
Select(`DATE_FORMAT(created_at, '%Y-%m-%d') 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).
|
||||
Where("created_at >= ? AND created_at <= ?", query.StartDate, query.EndDate)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
orderQuery = orderQuery.Where("(buyer_tenant_id = ? OR source_tenant_id = ?)", tenantID, tenantID)
|
||||
}
|
||||
err := orderQuery.
|
||||
Group("DATE_FORMAT(created_at, '%Y-%m-%d')").
|
||||
Scan(&rows).Error
|
||||
return rows, err
|
||||
@@ -403,7 +422,7 @@ func (r *Repository) dailyEstimates(ctx context.Context, query DashboardQuery) (
|
||||
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 USE INDEX (idx_rental_orders_dashboard_settled)").
|
||||
settlementQuery := db.Table("rental_orders AS ro USE INDEX (idx_rental_orders_dashboard_settled)").
|
||||
Select(`DATE_FORMAT(ro.settled_at, '%Y-%m-%d') AS date,
|
||||
COALESCE(SUM(oc.platform_fee_cent), 0) AS platform_income_amount_cent,
|
||||
COALESCE(SUM(CASE WHEN ro.compensation_plan = 'insurance' AND ro.insurance_fee_bps > 0
|
||||
@@ -459,7 +478,11 @@ func (r *Repository) dailySettlements(ctx context.Context, query DashboardQuery)
|
||||
// (由 dispute 模块写入),不纳入本指标,此处保持既有口径不变。
|
||||
Joins("JOIN order_checkouts AS oc USE INDEX (idx_order_checkouts_dashboard_accepted) ON oc.order_id = ro.id AND oc.status = ?", ordermodule.CheckoutStatusAccepted).
|
||||
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).
|
||||
Where("ro.settled_at >= ? AND ro.settled_at <= ?", query.StartDate, query.EndDate)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
settlementQuery = settlementQuery.Where("(ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)", tenantID, tenantID)
|
||||
}
|
||||
err := settlementQuery.
|
||||
Group("DATE_FORMAT(ro.settled_at, '%Y-%m-%d')").
|
||||
Scan(&rows).Error
|
||||
return rows, err
|
||||
@@ -541,7 +564,11 @@ func operatingExpenseSummaryFromDailyRows(rows []dailyOperatingExpenseRow) Opera
|
||||
func (r *Repository) dailyPayments(ctx context.Context, query DashboardQuery) ([]dailyPaymentRow, error) {
|
||||
rows := make([]dailyPaymentRow, 0)
|
||||
db := r.db.WithContext(ctx)
|
||||
err := db.Table("payment_orders AS po USE INDEX (idx_payment_orders_dashboard_created)").
|
||||
paymentQuery := db.Table("payment_orders AS po USE INDEX (idx_payment_orders_dashboard_created)")
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
paymentQuery = paymentQuery.Where("po.tenant_id = ?", tenantID)
|
||||
}
|
||||
err := paymentQuery.
|
||||
Select(`DATE_FORMAT(po.created_at, '%Y-%m-%d') 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,
|
||||
@@ -565,6 +592,9 @@ func (r *Repository) dailyPayments(ctx context.Context, query DashboardQuery) ([
|
||||
// dailyPickupCancelled 按取消日统计线下提号取消笔数。
|
||||
func (r *Repository) dailyPickupCancelled(ctx context.Context, query DashboardQuery) ([]dailyPickupCancelledRow, error) {
|
||||
rows := make([]dailyPickupCancelledRow, 0)
|
||||
if tenancy.IDFromContext(ctx) > 0 {
|
||||
return rows, nil
|
||||
}
|
||||
err := r.db.WithContext(ctx).Table("admin_pickups").
|
||||
Select(`DATE_FORMAT(cancelled_at, '%Y-%m-%d') AS date, COUNT(id) AS count`).
|
||||
Where("status = ?", "cancelled").
|
||||
@@ -577,17 +607,27 @@ func (r *Repository) dailyPickupCancelled(ctx context.Context, query DashboardQu
|
||||
// dailyPaidOrders 按实际付款日统计普通租赁订单,并将已完成的线下提号按完成日计入成交。
|
||||
func (r *Repository) dailyPaidOrders(ctx context.Context, query DashboardQuery) ([]dailyPaidOrderRow, error) {
|
||||
items := make([]dailyPaidOrderRow, 0)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
paymentTenantFilter := ""
|
||||
pickupTenantFilter := ""
|
||||
args := []any{query.StartDate, query.EndDate}
|
||||
if tenantID > 0 {
|
||||
paymentTenantFilter = " AND tenant_id = ?"
|
||||
pickupTenantFilter = " AND 1 = 0"
|
||||
args = append(args, tenantID)
|
||||
}
|
||||
args = append(args, query.StartDate, query.EndDate)
|
||||
err := r.db.WithContext(ctx).Table(`(
|
||||
SELECT DATE_FORMAT(paid_at, '%Y-%m-%d') AS date, COUNT(DISTINCT order_id) AS paid_order_count
|
||||
FROM payment_orders USE INDEX (idx_payment_orders_biz_status_paid_at_order)
|
||||
WHERE biz_type = 'order_pay' AND status = 'paid' AND paid_at >= ? AND paid_at <= ?
|
||||
WHERE biz_type = 'order_pay' AND status = 'paid' AND paid_at >= ? AND paid_at <= ?`+paymentTenantFilter+`
|
||||
GROUP BY DATE_FORMAT(paid_at, '%Y-%m-%d')
|
||||
UNION ALL
|
||||
SELECT DATE_FORMAT(completed_at, '%Y-%m-%d') AS date, COUNT(id) AS paid_order_count
|
||||
FROM admin_pickups USE INDEX (idx_admin_pickups_dashboard_completed)
|
||||
WHERE status = 'completed' AND completed_at >= ? AND completed_at <= ?
|
||||
WHERE status = 'completed' AND completed_at >= ? AND completed_at <= ?`+pickupTenantFilter+`
|
||||
GROUP BY DATE_FORMAT(completed_at, '%Y-%m-%d')
|
||||
) AS daily_paid_orders`, query.StartDate, query.EndDate, query.StartDate, query.EndDate).
|
||||
) AS daily_paid_orders`, args...).
|
||||
Select("date, SUM(paid_order_count) AS paid_order_count").
|
||||
Group("date").
|
||||
Scan(&items).Error
|
||||
|
||||
Reference in New Issue
Block a user