feat(tenant): 按租户隔离支付退款与商户配置
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package adminfinance
|
||||
|
||||
import (
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -14,15 +16,18 @@ func acceptedCheckoutForOrdersSubquery(db *gorm.DB, orderIDs []uint64) *gorm.DB
|
||||
}
|
||||
|
||||
func orderPaymentForOrdersSubquery(db *gorm.DB, orderIDs []uint64) *gorm.DB {
|
||||
return db.Table("payment_orders").
|
||||
query := db.Table("payment_orders").
|
||||
Select(`order_id,
|
||||
COALESCE(SUM(CASE WHEN biz_type = 'order_pay' AND status = 'paid' THEN amount_cent ELSE 0 END), 0) AS paid_amount_cent,
|
||||
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunded' THEN amount_cent ELSE 0 END), 0) AS refunded_amount_cent,
|
||||
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunding' THEN amount_cent ELSE 0 END), 0) AS refunding_amount_cent,
|
||||
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'failed' THEN amount_cent ELSE 0 END), 0) AS failed_refund_amount_cent`,
|
||||
refundBizTypes(), refundBizTypes(), refundBizTypes()).
|
||||
Where("order_id IN ?", orderIDs).
|
||||
Group("order_id")
|
||||
Where("order_id IN ?", orderIDs)
|
||||
if tenantID := tenancy.IDFromContext(db.Statement.Context); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query.Group("order_id")
|
||||
}
|
||||
|
||||
func ownerWalletIncomeForOrdersSubquery(db *gorm.DB, orderIDs []uint64) *gorm.DB {
|
||||
@@ -50,13 +55,20 @@ func ownerWalletIncomeForSettledOrdersSubquery(db *gorm.DB, query DashboardQuery
|
||||
// paymentOriginalAmountForRefundsInRangeSubquery 仅为当前区间内的退款单回查原支付金额。
|
||||
// 退款统计原先会对全部历史支付单按 order_id 聚合;数据增长后这部分即使只看一天也会很慢。
|
||||
func paymentOriginalAmountForRefundsInRangeSubquery(db *gorm.DB, query DashboardQuery) *gorm.DB {
|
||||
tenantID := tenancy.IDFromContext(db.Statement.Context)
|
||||
refundOrders := db.Table("payment_orders AS refund USE INDEX (idx_payment_orders_dashboard_created)").
|
||||
Select("DISTINCT refund.order_id").
|
||||
Where("refund.biz_type IN ? AND refund.status = ?", refundBizTypes(), "refunded").
|
||||
Where("refund.created_at >= ? AND refund.created_at <= ?", query.StartDate, query.EndDate)
|
||||
return db.Table("payment_orders AS paid USE INDEX (idx_payment_orders_dashboard_order)").
|
||||
if tenantID > 0 {
|
||||
refundOrders = refundOrders.Where("refund.tenant_id = ?", tenantID)
|
||||
}
|
||||
paidQuery := db.Table("payment_orders AS paid USE INDEX (idx_payment_orders_dashboard_order)").
|
||||
Select("paid.order_id, MAX(paid.amount_cent) AS amount_cent").
|
||||
Joins("JOIN (?) AS refund_order ON refund_order.order_id = paid.order_id", refundOrders).
|
||||
Where("paid.biz_type IN ? AND paid.status = ?", payBizTypes(), "paid").
|
||||
Group("paid.order_id")
|
||||
Where("paid.biz_type IN ? AND paid.status = ?", payBizTypes(), "paid")
|
||||
if tenantID > 0 {
|
||||
paidQuery = paidQuery.Where("paid.tenant_id = ?", tenantID)
|
||||
}
|
||||
return paidQuery.Group("paid.order_id")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user