feat(admin-user): 增加所属站点展示与筛选

This commit is contained in:
yml
2026-11-15 19:41:51 +08:00
parent 609264919d
commit 0024b64dca
9 changed files with 197 additions and 21 deletions
@@ -60,13 +60,14 @@ func (r *Repository) List(ctx context.Context, page, pageSize int, query ListQue
offset := (page - 1) * pageSize
var rows []userRow
listTx := r.db.WithContext(ctx).Table("users AS u").
Select(`u.*,
Select(`u.*, COALESCE(t.code, '') AS tenant_code, COALESCE(t.name, '') AS tenant_name,
COALESCE(o.order_count, 0) AS order_count,
COALESCE(l.listing_count, 0) AS listing_count,
COALESCE(d.dispute_count, 0) AS dispute_count,
COALESCE(df.deposit_free_used_cent, 0) AS deposit_free_used_cent,
COALESCE(w.available_balance_cent, 0) AS available_balance_cent,
COALESCE(w.frozen_balance_cent, 0) AS frozen_balance_cent`).
Joins("LEFT JOIN tenants AS t ON t.id = u.tenant_id").
Joins("LEFT JOIN (SELECT user_id, COUNT(*) AS order_count FROM (SELECT renter_id AS user_id FROM rental_orders UNION ALL SELECT owner_id AS user_id FROM rental_orders) AS order_users GROUP BY user_id) AS o ON o.user_id = u.id").
Joins("LEFT JOIN (SELECT owner_id AS user_id, COUNT(*) AS listing_count FROM rental_listings GROUP BY owner_id) AS l ON l.user_id = u.id").
Joins("LEFT JOIN (SELECT user_id, COUNT(*) AS dispute_count FROM (SELECT initiator_id AS user_id FROM disputes UNION ALL SELECT target_user_id AS user_id FROM disputes) AS dispute_users GROUP BY user_id) AS d ON d.user_id = u.id").
@@ -86,7 +87,8 @@ func (r *Repository) List(ctx context.Context, page, pageSize int, query ListQue
if page == 1 && len(rows) < pageSize {
total = int64(len(rows))
} else {
countTx := applyUserFilter(r.db.WithContext(ctx).Table("users AS u"), query, growthConfig)
countTx := applyUserFilter(r.db.WithContext(ctx).Table("users AS u").
Joins("LEFT JOIN tenants AS t ON t.id = u.tenant_id"), query, growthConfig)
if err := countTx.Count(&total).Error; err != nil {
return nil, err
}
@@ -104,6 +106,9 @@ func applyUserFilter(tx *gorm.DB, query ListQuery, growthConfig rentergrowth.Con
if query.Status != "" {
tx = tx.Where("u.status = ?", query.Status)
}
if query.TenantID > 0 {
tx = tx.Where("u.tenant_id = ?", query.TenantID)
}
depositFreeQuota := query.DepositFreeQuota
if depositFreeQuota == "" && query.HasDepositFreeQuota {
depositFreeQuota = "has"
@@ -136,10 +141,10 @@ func applyUserFilter(tx *gorm.DB, query ListQuery, growthConfig rentergrowth.Con
} else if id, err := strconv.ParseUint(keyword, 10, 64); err == nil && id > 0 && keyword[0] != '0' && id < 100000000 {
// 纯数字 ID:一并按用户 ID 精确匹配
like := "%" + keyword + "%"
tx = tx.Where("u.id = ? OR u.phone LIKE ? OR u.nickname LIKE ?", id, like, like)
tx = tx.Where("u.id = ? OR u.phone LIKE ? OR u.nickname LIKE ? OR t.name LIKE ? OR t.code LIKE ?", id, like, like, like, like)
} else {
like := "%" + keyword + "%"
tx = tx.Where("u.phone LIKE ? OR u.nickname LIKE ?", like, like)
tx = tx.Where("u.phone LIKE ? OR u.nickname LIKE ? OR t.name LIKE ? OR t.code LIKE ?", like, like, like, like)
}
}
return tx
@@ -529,13 +534,14 @@ func (r *Repository) Find(ctx context.Context, userID uint64) (*UserDTO, error)
}
var row userRow
err = r.db.WithContext(ctx).Table("users AS u").
Select(`u.*,
Select(`u.*, COALESCE(t.code, '') AS tenant_code, COALESCE(t.name, '') AS tenant_name,
COALESCE(o.order_count, 0) AS order_count,
COALESCE(l.listing_count, 0) AS listing_count,
COALESCE(d.dispute_count, 0) AS dispute_count,
COALESCE(df.deposit_free_used_cent, 0) AS deposit_free_used_cent,
COALESCE(w.available_balance_cent, 0) AS available_balance_cent,
COALESCE(w.frozen_balance_cent, 0) AS frozen_balance_cent`).
Joins("LEFT JOIN tenants AS t ON t.id = u.tenant_id").
Joins("LEFT JOIN (SELECT user_id, COUNT(*) AS order_count FROM (SELECT renter_id AS user_id FROM rental_orders UNION ALL SELECT owner_id AS user_id FROM rental_orders) AS order_users GROUP BY user_id) AS o ON o.user_id = u.id").
Joins("LEFT JOIN (SELECT owner_id AS user_id, COUNT(*) AS listing_count FROM rental_listings GROUP BY owner_id) AS l ON l.user_id = u.id").
Joins("LEFT JOIN (SELECT user_id, COUNT(*) AS dispute_count FROM (SELECT initiator_id AS user_id FROM disputes UNION ALL SELECT target_user_id AS user_id FROM disputes) AS dispute_users GROUP BY user_id) AS d ON d.user_id = u.id").
@@ -634,6 +640,8 @@ func (r *Repository) AdjustWallet(ctx context.Context, adminID uint64, userID ui
type userRow struct {
model.User
TenantCode string
TenantName string
OrderCount int64
ListingCount int64
DisputeCount int64
@@ -660,6 +668,9 @@ func (row userRow) toDTO(growthConfig rentergrowth.Config) UserDTO {
}
return UserDTO{
ID: row.ID,
TenantID: row.TenantID,
TenantCode: row.TenantCode,
TenantName: row.TenantName,
Phone: row.Phone,
Nickname: row.Nickname,
RealnameStatus: row.RealnameStatus,