feat(admin-user): 增加所属站点展示与筛选
This commit is contained in:
@@ -4,6 +4,9 @@ import "time"
|
||||
|
||||
type UserDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
TenantCode string `json:"tenant_code"`
|
||||
TenantName string `json:"tenant_name"`
|
||||
Phone string `json:"phone"`
|
||||
Nickname string `json:"nickname"`
|
||||
RealnameStatus string `json:"realname_status"`
|
||||
@@ -75,6 +78,7 @@ type WalletAdjustRequest struct {
|
||||
// ListQuery 用户列表筛选条件。Keyword 同时匹配手机号/昵称(模糊)与用户 ID(精确)。
|
||||
type ListQuery struct {
|
||||
Keyword string
|
||||
TenantID uint64
|
||||
Status string
|
||||
HasDepositFreeQuota bool
|
||||
QualityOwnerOnly bool
|
||||
|
||||
@@ -39,6 +39,7 @@ func (h *Handler) List(c *gin.Context) {
|
||||
page, pageSize := parsePagination(c)
|
||||
query := ListQuery{
|
||||
Keyword: strings.TrimSpace(c.Query("keyword")),
|
||||
TenantID: parseOptionalUint(c.Query("tenant_id")),
|
||||
Status: strings.TrimSpace(c.Query("status")),
|
||||
HasDepositFreeQuota: parseBoolQuery(c.Query("has_deposit_free_quota")),
|
||||
QualityOwnerOnly: parseBoolQuery(c.Query("quality_owner_only")),
|
||||
@@ -54,6 +55,14 @@ func (h *Handler) List(c *gin.Context) {
|
||||
response.OK(c, result)
|
||||
}
|
||||
|
||||
func parseOptionalUint(value string) uint64 {
|
||||
parsed, err := strconv.ParseUint(strings.TrimSpace(value), 10, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
func parseBoolQuery(value string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "1", "true", "yes", "on":
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -207,6 +207,51 @@ func TestListFiltersByDepositQuotaOwnerTypeAndGrowthLevel(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListIncludesTenantAndSupportsTenantSearchAndFilter(t *testing.T) {
|
||||
db := setupAdminUserTestDB(t)
|
||||
tenants := []model.Tenant{
|
||||
{Code: "dachui", Name: "大锤商行", Status: "active"},
|
||||
{Code: "jieyou", Name: "解忧商行", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatalf("创建租户失败:%v", err)
|
||||
}
|
||||
users := []model.User{
|
||||
{TenantID: tenants[0].ID, Phone: "15614832213", Nickname: "大锤同号用户", Status: "active"},
|
||||
{TenantID: tenants[1].ID, Phone: "15614832213", Nickname: "解忧同号用户", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatalf("创建跨站同号用户失败:%v", err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
|
||||
result, err := repo.List(t.Context(), 1, 20, ListQuery{Keyword: "解忧"})
|
||||
if err != nil {
|
||||
t.Fatalf("按站点名称搜索失败:%v", err)
|
||||
}
|
||||
if result.Total != 1 {
|
||||
t.Fatalf("按站点名称搜索总数 = %d, want 1", result.Total)
|
||||
}
|
||||
items := result.Items.([]UserDTO)
|
||||
if items[0].TenantID != tenants[1].ID ||
|
||||
items[0].TenantCode != "jieyou" ||
|
||||
items[0].TenantName != "解忧商行" {
|
||||
t.Fatalf("用户站点信息不正确:%+v", items[0])
|
||||
}
|
||||
|
||||
result, err = repo.List(t.Context(), 1, 20, ListQuery{TenantID: tenants[0].ID})
|
||||
if err != nil {
|
||||
t.Fatalf("按站点筛选失败:%v", err)
|
||||
}
|
||||
if result.Total != 1 {
|
||||
t.Fatalf("按站点筛选总数 = %d, want 1", result.Total)
|
||||
}
|
||||
items = result.Items.([]UserDTO)
|
||||
if items[0].TenantCode != "dachui" {
|
||||
t.Fatalf("按站点筛选结果 = %+v, want dachui", items[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdjustGrowthPointsWritesRepeatableLedgers(t *testing.T) {
|
||||
db := setupAdminUserTestDB(t)
|
||||
user := model.User{
|
||||
@@ -537,6 +582,7 @@ func setupAdminUserTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db := database.NewTestDB()
|
||||
if err := database.AutoMigrateIfMissing(db,
|
||||
&model.Tenant{},
|
||||
&model.User{},
|
||||
&model.UserRealname{},
|
||||
&model.AuditLog{},
|
||||
|
||||
Reference in New Issue
Block a user