72 lines
4.0 KiB
Go
72 lines
4.0 KiB
Go
package adminfinance
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func acceptedCheckoutSubquery(db *gorm.DB) *gorm.DB {
|
|
latest := db.Table("order_checkouts").Select("MAX(id) AS id").Where("status = ?", "accepted").Group("order_id")
|
|
return db.Table("order_checkouts AS oc").
|
|
Select("oc.*").
|
|
Joins("JOIN (?) AS latest ON latest.id = oc.id", latest)
|
|
}
|
|
|
|
func orderPaymentSubquery(db *gorm.DB) *gorm.DB {
|
|
return 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()).
|
|
Group("order_id")
|
|
}
|
|
|
|
func ownerWalletIncomeSubquery(db *gorm.DB) *gorm.DB {
|
|
return db.Table("wallet_ledger").
|
|
Select("order_id, COALESCE(SUM(amount_cent), 0) AS owner_wallet_income_amount_cent").
|
|
Where("direction = ? AND biz_type IN ? AND order_id IS NOT NULL", "in", []string{"owner_income", "deposit_compensation"}).
|
|
Group("order_id")
|
|
}
|
|
|
|
// ownerWalletIncomeForSettledOrdersSubquery 只为当前仪表盘区间内已结算订单汇总钱包入账。
|
|
// 原先的全表分组会随钱包流水积累持续变慢;此处先通过 settled_at 缩小订单集合,
|
|
// 再使用 wallet_ledger.order_id 索引关联对应流水。
|
|
func ownerWalletIncomeForSettledOrdersSubquery(db *gorm.DB, query DashboardQuery) *gorm.DB {
|
|
return db.Table("rental_orders AS scoped_order").
|
|
Select(`scoped_order.id AS order_id,
|
|
COALESCE(SUM(wallet.amount_cent), 0) AS owner_wallet_income_amount_cent`).
|
|
Joins(`LEFT JOIN wallet_ledger AS wallet
|
|
ON wallet.order_id = scoped_order.id
|
|
AND wallet.direction = ?
|
|
AND wallet.biz_type IN ?`, "in", []string{"owner_income", "deposit_compensation"}).
|
|
Where("scoped_order.settled_at >= ? AND scoped_order.settled_at <= ?", query.StartDate, query.EndDate).
|
|
Group("scoped_order.id")
|
|
}
|
|
|
|
// orderPaymentForSettledOrdersSubquery 只聚合当前结算区间订单的退款状态,
|
|
// 供仪表盘异常数判断使用,避免 financeDetailBaseQuery 在首页扫描所有历史付款单。
|
|
func orderPaymentForSettledOrdersSubquery(db *gorm.DB, query DashboardQuery) *gorm.DB {
|
|
return db.Table("rental_orders AS scoped_order").
|
|
Select(`scoped_order.id AS order_id,
|
|
COALESCE(SUM(CASE WHEN payment.biz_type IN ? AND payment.status = 'refunding' THEN payment.amount_cent ELSE 0 END), 0) AS refunding_amount_cent,
|
|
COALESCE(SUM(CASE WHEN payment.biz_type IN ? AND payment.status = 'failed' THEN payment.amount_cent ELSE 0 END), 0) AS failed_refund_amount_cent`, refundBizTypes(), refundBizTypes()).
|
|
Joins("LEFT JOIN payment_orders AS payment ON payment.order_id = scoped_order.id AND payment.biz_type IN ?", refundBizTypes()).
|
|
Where("scoped_order.settled_at >= ? AND scoped_order.settled_at <= ?", query.StartDate, query.EndDate).
|
|
Group("scoped_order.id")
|
|
}
|
|
|
|
// paymentOriginalAmountForRefundsInRangeSubquery 仅为当前区间内的退款单回查原支付金额。
|
|
// 退款统计原先会对全部历史支付单按 order_id 聚合;数据增长后这部分即使只看一天也会很慢。
|
|
func paymentOriginalAmountForRefundsInRangeSubquery(db *gorm.DB, query DashboardQuery) *gorm.DB {
|
|
refundOrders := db.Table("payment_orders AS refund").
|
|
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").
|
|
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")
|
|
}
|