优化订单管理
This commit is contained in:
@@ -83,6 +83,15 @@ type AdminActionRequest struct {
|
||||
Reason string `json:"reason" binding:"required"`
|
||||
}
|
||||
|
||||
type AdminOrderQuery struct {
|
||||
Page int
|
||||
PageSize int
|
||||
Keyword string
|
||||
Status string
|
||||
HandoffStatus string
|
||||
SettlementStatus string
|
||||
}
|
||||
|
||||
type AuditMeta = auditlog.Meta
|
||||
|
||||
type PaginatedResult struct {
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
)
|
||||
|
||||
func (h *Handler) AdminList(c *gin.Context) {
|
||||
page, pageSize := parsePagination(c)
|
||||
result, err := h.service.ListAdmin(c.Request.Context(), page, pageSize)
|
||||
query := parseAdminOrderQuery(c)
|
||||
result, err := h.service.ListAdmin(c.Request.Context(), query)
|
||||
if err != nil {
|
||||
writeOrderError(c, err)
|
||||
return
|
||||
|
||||
@@ -2,6 +2,7 @@ package order
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/middleware"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
@@ -50,3 +51,15 @@ func parsePagination(c *gin.Context) (int, int) {
|
||||
}
|
||||
return page, pageSize
|
||||
}
|
||||
|
||||
func parseAdminOrderQuery(c *gin.Context) AdminOrderQuery {
|
||||
page, pageSize := parsePagination(c)
|
||||
return AdminOrderQuery{
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
Keyword: strings.TrimSpace(c.Query("keyword")),
|
||||
Status: strings.TrimSpace(c.Query("status")),
|
||||
HandoffStatus: strings.TrimSpace(c.Query("handoff_status")),
|
||||
SettlementStatus: strings.TrimSpace(c.Query("settlement_status")),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package order
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -33,16 +34,25 @@ func (r *Repository) ListForUser(ctx context.Context, userID uint64) ([]OrderDTO
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (r *Repository) ListAdmin(ctx context.Context, page, pageSize int) (*PaginatedResult, error) {
|
||||
func (r *Repository) ListAdmin(ctx context.Context, query AdminOrderQuery) (*PaginatedResult, error) {
|
||||
var total int64
|
||||
db := r.db.WithContext(ctx)
|
||||
if err := r.adminQuery(ctx).Count(&total).Error; err != nil {
|
||||
countDB := applyAdminOrderFilters(r.adminOrderJoinQuery(ctx), query)
|
||||
if err := countDB.Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
page := query.Page
|
||||
pageSize := query.PageSize
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 {
|
||||
pageSize = 20
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
var rows []orderRow
|
||||
err := r.adminQuery(ctx).
|
||||
err := applyAdminOrderFilters(r.adminQuery(ctx), query).
|
||||
Order("o.id DESC").
|
||||
Limit(pageSize).
|
||||
Offset(offset).
|
||||
@@ -67,6 +77,45 @@ func (r *Repository) ListAdmin(ctx context.Context, page, pageSize int) (*Pagina
|
||||
}, nil
|
||||
}
|
||||
|
||||
func applyAdminOrderFilters(db *gorm.DB, query AdminOrderQuery) *gorm.DB {
|
||||
if query.Status != "" {
|
||||
db = db.Where("o.status = ?", query.Status)
|
||||
}
|
||||
if query.HandoffStatus != "" {
|
||||
db = db.Where("o.handoff_status = ?", query.HandoffStatus)
|
||||
}
|
||||
if query.SettlementStatus != "" {
|
||||
db = db.Where("o.settlement_status = ?", query.SettlementStatus)
|
||||
}
|
||||
if keyword := strings.TrimSpace(query.Keyword); keyword != "" {
|
||||
like := "%" + keyword + "%"
|
||||
if id, err := strconv.ParseUint(keyword, 10, 64); err == nil {
|
||||
db = db.Where(
|
||||
`(o.order_no LIKE ? OR l.listing_no LIKE ? OR a.title LIKE ? OR owner.phone LIKE ? OR renter.phone LIKE ? OR o.id = ? OR o.listing_id = ? OR o.owner_id = ? OR o.renter_id = ?)`,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
id,
|
||||
id,
|
||||
id,
|
||||
id,
|
||||
)
|
||||
} else {
|
||||
db = db.Where(
|
||||
`(o.order_no LIKE ? OR l.listing_no LIKE ? OR a.title LIKE ? OR owner.phone LIKE ? OR renter.phone LIKE ?)`,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
like,
|
||||
)
|
||||
}
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func (r *Repository) FindAdmin(ctx context.Context, orderID uint64) (*OrderDTO, error) {
|
||||
var row orderRow
|
||||
db := r.db.WithContext(ctx)
|
||||
@@ -191,8 +240,12 @@ func (r *Repository) baseQuery(ctx context.Context) *gorm.DB {
|
||||
}
|
||||
|
||||
func (r *Repository) adminQuery(ctx context.Context) *gorm.DB {
|
||||
return r.adminOrderJoinQuery(ctx).
|
||||
Select("o.*, l.listing_no, a.title, a.server_region, a.login_platform, owner.phone AS owner_phone, renter.phone AS renter_phone")
|
||||
}
|
||||
|
||||
func (r *Repository) adminOrderJoinQuery(ctx context.Context) *gorm.DB {
|
||||
return r.db.WithContext(ctx).Table("rental_orders AS o").
|
||||
Select("o.*, l.listing_no, a.title, a.server_region, a.login_platform, owner.phone AS owner_phone, renter.phone AS renter_phone").
|
||||
Joins("JOIN rental_listings AS l ON l.id = o.listing_id").
|
||||
Joins("JOIN game_accounts AS a ON a.id = o.account_id").
|
||||
Joins("JOIN users AS owner ON owner.id = o.owner_id").
|
||||
|
||||
@@ -145,11 +145,11 @@ func (s *Service) ListForUser(ctx context.Context, userID uint64) ([]OrderDTO, e
|
||||
return s.repo.ListForUser(ctx, userID)
|
||||
}
|
||||
|
||||
func (s *Service) ListAdmin(ctx context.Context, page, pageSize int) (*PaginatedResult, error) {
|
||||
func (s *Service) ListAdmin(ctx context.Context, query AdminOrderQuery) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.ListAdmin(ctx, page, pageSize)
|
||||
return s.repo.ListAdmin(ctx, query)
|
||||
}
|
||||
|
||||
func (s *Service) FindAdmin(ctx context.Context, orderID uint64) (*OrderDTO, error) {
|
||||
|
||||
Reference in New Issue
Block a user