完成剩余模块 Context 超时控制改造
This commit is contained in:
@@ -25,7 +25,7 @@ func (h *Handler) Dashboard(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
item, err := h.service.Dashboard(query)
|
||||
item, err := h.service.Dashboard(c.Request.Context(), query)
|
||||
if err != nil {
|
||||
writeFinanceError(c, err)
|
||||
return
|
||||
@@ -38,7 +38,7 @@ func (h *Handler) Details(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
result, err := h.service.Details(query)
|
||||
result, err := h.service.Details(c.Request.Context(), query)
|
||||
if err != nil {
|
||||
writeFinanceError(c, err)
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package adminfinance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
@@ -16,12 +17,12 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) Dashboard(query DashboardQuery) (*DashboardDTO, error) {
|
||||
dailyItems, err := r.dailyItems(query)
|
||||
func (r *Repository) Dashboard(ctx context.Context, query DashboardQuery) (*DashboardDTO, error) {
|
||||
dailyItems, err := r.dailyItems(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
summary, err := r.summary(query)
|
||||
summary, err := r.summary(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -32,10 +33,11 @@ func (r *Repository) Dashboard(query DashboardQuery) (*DashboardDTO, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Details(query DetailQuery) (*PaginatedResult, error) {
|
||||
db := r.financeDetailBaseQuery(query)
|
||||
func (r *Repository) Details(ctx context.Context, query DetailQuery) (*PaginatedResult, error) {
|
||||
baseDB := r.db.WithContext(ctx)
|
||||
db := r.financeDetailBaseQuery(ctx, query)
|
||||
var total int64
|
||||
countDB := r.db.Table("(?) AS finance_rows", db)
|
||||
countDB := baseDB.Table("(?) AS finance_rows", db)
|
||||
if err := countDB.Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -52,9 +54,10 @@ func (r *Repository) Details(query DetailQuery) (*PaginatedResult, error) {
|
||||
return &PaginatedResult{Items: items, Total: total, Page: query.Page, PageSize: query.PageSize}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) summary(query DashboardQuery) (*FinanceSummaryDTO, error) {
|
||||
func (r *Repository) summary(ctx context.Context, query DashboardQuery) (*FinanceSummaryDTO, error) {
|
||||
db := r.db.WithContext(ctx)
|
||||
var payment paymentSummaryRow
|
||||
if err := r.db.Table("payment_orders").
|
||||
if err := db.Table("payment_orders").
|
||||
Select(`COALESCE(SUM(CASE WHEN biz_type IN ('order_pay', 'wallet_recharge') AND status = 'paid' THEN amount_cent ELSE 0 END), 0) AS total_flow_amount_cent,
|
||||
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunded' THEN amount_cent ELSE 0 END), 0) AS total_refund_amount_cent,
|
||||
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunding' THEN amount_cent ELSE 0 END), 0) AS pending_refund_amount_cent,
|
||||
@@ -68,20 +71,20 @@ func (r *Repository) summary(query DashboardQuery) (*FinanceSummaryDTO, error) {
|
||||
}
|
||||
|
||||
var settlement settlementSummaryRow
|
||||
if err := r.db.Table("rental_orders AS ro").
|
||||
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,
|
||||
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(r.db)).
|
||||
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 := r.db.Table("(?) AS d", r.financeDetailBaseQuery(DetailQuery{
|
||||
if err := db.Table("(?) AS d", r.financeDetailBaseQuery(ctx, DetailQuery{
|
||||
DateType: "settled",
|
||||
StartDate: query.StartDate,
|
||||
EndDate: query.EndDate,
|
||||
@@ -108,9 +111,10 @@ func (r *Repository) summary(query DashboardQuery) (*FinanceSummaryDTO, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) dailyItems(query DashboardQuery) ([]FinanceDailyDTO, error) {
|
||||
func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]FinanceDailyDTO, error) {
|
||||
db := r.db.WithContext(ctx)
|
||||
payments := make([]dailyPaymentRow, 0)
|
||||
if err := r.db.Table("payment_orders").
|
||||
if err := db.Table("payment_orders").
|
||||
Select(`DATE(created_at) AS date,
|
||||
COALESCE(SUM(CASE WHEN biz_type IN ('order_pay', 'wallet_recharge') AND status = 'paid' THEN amount_cent ELSE 0 END), 0) AS total_flow_amount_cent,
|
||||
COALESCE(SUM(CASE WHEN biz_type IN ? AND status = 'refunded' THEN amount_cent ELSE 0 END), 0) AS total_refund_amount_cent,
|
||||
@@ -126,14 +130,14 @@ func (r *Repository) dailyItems(query DashboardQuery) ([]FinanceDailyDTO, error)
|
||||
}
|
||||
|
||||
settlements := make([]dailySettlementRow, 0)
|
||||
if err := r.db.Table("rental_orders AS ro").
|
||||
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,
|
||||
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(r.db)).
|
||||
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 {
|
||||
@@ -175,8 +179,8 @@ func (r *Repository) dailyItems(query DashboardQuery) ([]FinanceDailyDTO, error)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (r *Repository) financeDetailBaseQuery(query DetailQuery) *gorm.DB {
|
||||
db := r.db.Table("rental_orders AS ro").
|
||||
func (r *Repository) financeDetailBaseQuery(ctx context.Context, query DetailQuery) *gorm.DB {
|
||||
db := r.db.WithContext(ctx).Table("rental_orders AS ro").
|
||||
Select(`ro.id AS order_id, ro.order_no, ro.status AS order_status, ro.settlement_status, ro.refund_status,
|
||||
ro.renter_id, COALESCE(ru.phone, '') AS renter_phone, COALESCE(ru.nickname, '') AS renter_nickname,
|
||||
ro.owner_id, COALESCE(ou.phone, '') AS owner_phone, COALESCE(ou.nickname, '') AS owner_nickname,
|
||||
@@ -202,9 +206,9 @@ func (r *Repository) financeDetailBaseQuery(query DetailQuery) *gorm.DB {
|
||||
ro.created_at, ro.settled_at`).
|
||||
Joins("LEFT JOIN users AS ru ON ru.id = ro.renter_id").
|
||||
Joins("LEFT JOIN users AS ou ON ou.id = ro.owner_id").
|
||||
Joins("LEFT JOIN (?) AS oc ON oc.order_id = ro.id", acceptedCheckoutSubquery(r.db)).
|
||||
Joins("LEFT JOIN (?) AS p ON p.order_id = ro.id", orderPaymentSubquery(r.db)).
|
||||
Joins("LEFT JOIN (?) AS w ON w.order_id = ro.id", ownerWalletIncomeSubquery(r.db))
|
||||
Joins("LEFT JOIN (?) AS oc ON oc.order_id = ro.id", acceptedCheckoutSubquery(r.db.WithContext(ctx))).
|
||||
Joins("LEFT JOIN (?) AS p ON p.order_id = ro.id", orderPaymentSubquery(r.db.WithContext(ctx))).
|
||||
Joins("LEFT JOIN (?) AS w ON w.order_id = ro.id", ownerWalletIncomeSubquery(r.db.WithContext(ctx)))
|
||||
|
||||
if query.OrderNo != "" {
|
||||
db = db.Where("ro.order_no = ?", query.OrderNo)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package adminfinance
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
|
||||
@@ -12,14 +15,14 @@ func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) Dashboard(query DashboardQuery) (*DashboardDTO, error) {
|
||||
func (s *Service) Dashboard(ctx context.Context, query DashboardQuery) (*DashboardDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.Dashboard(query)
|
||||
return s.repo.Dashboard(ctx, query)
|
||||
}
|
||||
|
||||
func (s *Service) Details(query DetailQuery) (*PaginatedResult, error) {
|
||||
func (s *Service) Details(ctx context.Context, query DetailQuery) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -32,5 +35,5 @@ func (s *Service) Details(query DetailQuery) (*PaginatedResult, error) {
|
||||
if query.PageSize > 100 {
|
||||
query.PageSize = 100
|
||||
}
|
||||
return s.repo.Details(query)
|
||||
return s.repo.Details(ctx, query)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user