tenant: admin batch topup

This commit is contained in:
2025-12-19 13:55:20 +08:00
parent 86a1a0a2cc
commit 17d51d5ed2
11 changed files with 771 additions and 3 deletions

View File

@@ -42,7 +42,8 @@ func (t *tenant) AdminTenantUsersPage(ctx context.Context, tenantID int64, filte
conds = append(conds, tbl.UserID.Eq(*filter.UserID))
}
if filter.Role != nil && *filter.Role != "" {
conds = append(conds, tbl.Role.Contains(string(*filter.Role)))
// role 字段为 PostgreSQL text[]:使用数组参数才能正确生成 `@> '{"tenant_admin"}'` 语义。
conds = append(conds, tbl.Role.Contains(types.NewArray([]consts.TenantUserRole{*filter.Role})))
}
if filter.Status != nil && *filter.Status != "" {
conds = append(conds, tbl.Status.Eq(*filter.Status))
@@ -225,6 +226,19 @@ func (t *tenant) Pager(ctx context.Context, filter *dto.TenantFilter) (*requests
}
func (t *tenant) TenantUserCountMapping(ctx context.Context, tenantIds []int64) (map[int64]int64, error) {
// 关键语义:返回值必须包含入参中的所有 tenant_id。
// 即便该租户当前没有成员,也应返回 count=0便于调用方直接取值而无需额外补全逻辑。
result := make(map[int64]int64, len(tenantIds))
for _, id := range tenantIds {
if id <= 0 {
continue
}
result[id] = 0
}
if len(result) == 0 {
return result, nil
}
tbl, query := models.TenantUserQuery.QueryContext(ctx)
var items []struct {
@@ -243,7 +257,6 @@ func (t *tenant) TenantUserCountMapping(ctx context.Context, tenantIds []int64)
return nil, err
}
result := make(map[int64]int64)
for _, item := range items {
result[item.TenantID] = item.Count
}
@@ -252,6 +265,19 @@ func (t *tenant) TenantUserCountMapping(ctx context.Context, tenantIds []int64)
// TenantUserBalanceMapping
func (t *tenant) TenantUserBalanceMapping(ctx context.Context, tenantIds []int64) (map[int64]int64, error) {
// 关键语义:返回值必须包含入参中的所有 tenant_id。
// 即便该租户当前没有成员,也应返回 balance=0保持调用方逻辑一致。
result := make(map[int64]int64, len(tenantIds))
for _, id := range tenantIds {
if id <= 0 {
continue
}
result[id] = 0
}
if len(result) == 0 {
return result, nil
}
tbl, query := models.TenantUserQuery.QueryContext(ctx)
var items []struct {
@@ -270,7 +296,6 @@ func (t *tenant) TenantUserBalanceMapping(ctx context.Context, tenantIds []int64
return nil, err
}
result := make(map[int64]int64)
for _, item := range items {
result[item.TenantID] = item.Balance
}