优化订单管理
This commit is contained in:
@@ -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").
|
||||
|
||||
Reference in New Issue
Block a user