2025-12-18 13:50:39 +08:00
|
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2025-12-29 10:03:27 +08:00
|
|
|
|
"database/sql"
|
2025-12-29 19:23:49 +08:00
|
|
|
|
"errors"
|
2025-12-29 10:03:27 +08:00
|
|
|
|
"sort"
|
2025-12-25 20:52:47 +08:00
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
dbent "github.com/Wei-Shaw/sub2api/ent"
|
|
|
|
|
|
dbuser "github.com/Wei-Shaw/sub2api/ent/user"
|
|
|
|
|
|
"github.com/Wei-Shaw/sub2api/ent/userallowedgroup"
|
|
|
|
|
|
"github.com/Wei-Shaw/sub2api/ent/usersubscription"
|
2025-12-24 21:07:21 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
2025-12-29 10:03:27 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
2025-12-18 13:50:39 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-25 20:52:47 +08:00
|
|
|
|
type userRepository struct {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
client *dbent.Client
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
func NewUserRepository(client *dbent.Client, sqlDB *sql.DB) service.UserRepository {
|
|
|
|
|
|
return newUserRepositoryWithSQL(client, sqlDB)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-31 14:51:58 +08:00
|
|
|
|
func newUserRepositoryWithSQL(client *dbent.Client, _ sqlExecutor) *userRepository {
|
|
|
|
|
|
return &userRepository{client: client}
|
2025-12-29 10:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *userRepository) Create(ctx context.Context, userIn *service.User) error {
|
|
|
|
|
|
if userIn == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 19:23:49 +08:00
|
|
|
|
// 统一使用 ent 的事务:保证用户与允许分组的更新原子化,
|
|
|
|
|
|
// 并避免基于 *sql.Tx 手动构造 ent client 导致的 ExecQuerier 断言错误。
|
|
|
|
|
|
tx, err := r.client.Tx(ctx)
|
|
|
|
|
|
if err != nil && !errors.Is(err, dbent.ErrTxStarted) {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-12-29 10:03:27 +08:00
|
|
|
|
|
2025-12-29 19:23:49 +08:00
|
|
|
|
var txClient *dbent.Client
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
|
txClient = tx.Client()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 已处于外部事务中(ErrTxStarted),复用当前 client 并由调用方负责提交/回滚。
|
|
|
|
|
|
txClient = r.client
|
2025-12-26 15:40:24 +08:00
|
|
|
|
}
|
2025-12-29 10:03:27 +08:00
|
|
|
|
|
|
|
|
|
|
created, err := txClient.User.Create().
|
|
|
|
|
|
SetEmail(userIn.Email).
|
|
|
|
|
|
SetUsername(userIn.Username).
|
|
|
|
|
|
SetWechat(userIn.Wechat).
|
|
|
|
|
|
SetNotes(userIn.Notes).
|
|
|
|
|
|
SetPasswordHash(userIn.PasswordHash).
|
|
|
|
|
|
SetRole(userIn.Role).
|
|
|
|
|
|
SetBalance(userIn.Balance).
|
|
|
|
|
|
SetConcurrency(userIn.Concurrency).
|
|
|
|
|
|
SetStatus(userIn.Status).
|
|
|
|
|
|
Save(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return translatePersistenceError(err, nil, service.ErrEmailExists)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 19:23:49 +08:00
|
|
|
|
if err := r.syncUserAllowedGroupsWithClient(ctx, txClient, created.ID, userIn.AllowedGroups); err != nil {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 19:23:49 +08:00
|
|
|
|
if tx != nil {
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
applyUserEntityToService(userIn, created)
|
|
|
|
|
|
return nil
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 15:40:24 +08:00
|
|
|
|
func (r *userRepository) GetByID(ctx context.Context, id int64) (*service.User, error) {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
m, err := r.client.User.Query().Where(dbuser.IDEQ(id)).Only(ctx)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
if err != nil {
|
2025-12-25 20:52:47 +08:00
|
|
|
|
return nil, translatePersistenceError(err, service.ErrUserNotFound, nil)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
2025-12-29 10:03:27 +08:00
|
|
|
|
|
|
|
|
|
|
out := userEntityToService(m)
|
|
|
|
|
|
groups, err := r.loadAllowedGroups(ctx, []int64{id})
|
2025-12-31 14:11:57 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := groups[id]; ok {
|
|
|
|
|
|
out.AllowedGroups = v
|
2025-12-29 10:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
return out, nil
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 15:40:24 +08:00
|
|
|
|
func (r *userRepository) GetByEmail(ctx context.Context, email string) (*service.User, error) {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
m, err := r.client.User.Query().Where(dbuser.EmailEQ(email)).Only(ctx)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
if err != nil {
|
2025-12-25 20:52:47 +08:00
|
|
|
|
return nil, translatePersistenceError(err, service.ErrUserNotFound, nil)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
out := userEntityToService(m)
|
|
|
|
|
|
groups, err := r.loadAllowedGroups(ctx, []int64{m.ID})
|
2025-12-31 14:11:57 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := groups[m.ID]; ok {
|
|
|
|
|
|
out.AllowedGroups = v
|
2025-12-29 10:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
return out, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *userRepository) Update(ctx context.Context, userIn *service.User) error {
|
|
|
|
|
|
if userIn == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 19:23:49 +08:00
|
|
|
|
// 使用 ent 事务包裹用户更新与 allowed_groups 同步,避免跨层事务不一致。
|
|
|
|
|
|
tx, err := r.client.Tx(ctx)
|
|
|
|
|
|
if err != nil && !errors.Is(err, dbent.ErrTxStarted) {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-12-29 10:03:27 +08:00
|
|
|
|
|
2025-12-29 19:23:49 +08:00
|
|
|
|
var txClient *dbent.Client
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
|
txClient = tx.Client()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 已处于外部事务中(ErrTxStarted),复用当前 client 并由调用方负责提交/回滚。
|
|
|
|
|
|
txClient = r.client
|
2025-12-29 10:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updated, err := txClient.User.UpdateOneID(userIn.ID).
|
|
|
|
|
|
SetEmail(userIn.Email).
|
|
|
|
|
|
SetUsername(userIn.Username).
|
|
|
|
|
|
SetWechat(userIn.Wechat).
|
|
|
|
|
|
SetNotes(userIn.Notes).
|
|
|
|
|
|
SetPasswordHash(userIn.PasswordHash).
|
|
|
|
|
|
SetRole(userIn.Role).
|
|
|
|
|
|
SetBalance(userIn.Balance).
|
|
|
|
|
|
SetConcurrency(userIn.Concurrency).
|
|
|
|
|
|
SetStatus(userIn.Status).
|
|
|
|
|
|
Save(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return translatePersistenceError(err, service.ErrUserNotFound, service.ErrEmailExists)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 19:23:49 +08:00
|
|
|
|
if err := r.syncUserAllowedGroupsWithClient(ctx, txClient, updated.ID, userIn.AllowedGroups); err != nil {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 19:23:49 +08:00
|
|
|
|
if tx != nil {
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
userIn.UpdatedAt = updated.UpdatedAt
|
|
|
|
|
|
return nil
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 20:52:47 +08:00
|
|
|
|
func (r *userRepository) Delete(ctx context.Context, id int64) error {
|
2025-12-29 16:57:50 +08:00
|
|
|
|
affected, err := r.client.User.Delete().Where(dbuser.IDEQ(id)).Exec(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return translatePersistenceError(err, service.ErrUserNotFound, nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
if affected == 0 {
|
|
|
|
|
|
return service.ErrUserNotFound
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 15:40:24 +08:00
|
|
|
|
func (r *userRepository) List(ctx context.Context, params pagination.PaginationParams) ([]service.User, *pagination.PaginationResult, error) {
|
2025-12-18 13:50:39 +08:00
|
|
|
|
return r.ListWithFilters(ctx, params, "", "", "")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 15:40:24 +08:00
|
|
|
|
func (r *userRepository) ListWithFilters(ctx context.Context, params pagination.PaginationParams, status, role, search string) ([]service.User, *pagination.PaginationResult, error) {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
q := r.client.User.Query()
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
|
|
if status != "" {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
q = q.Where(dbuser.StatusEQ(status))
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
if role != "" {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
q = q.Where(dbuser.RoleEQ(role))
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
if search != "" {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
q = q.Where(
|
|
|
|
|
|
dbuser.Or(
|
|
|
|
|
|
dbuser.EmailContainsFold(search),
|
|
|
|
|
|
dbuser.UsernameContainsFold(search),
|
|
|
|
|
|
dbuser.WechatContainsFold(search),
|
|
|
|
|
|
),
|
2025-12-23 11:26:22 +08:00
|
|
|
|
)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
total, err := q.Clone().Count(ctx)
|
|
|
|
|
|
if err != nil {
|
2025-12-18 13:50:39 +08:00
|
|
|
|
return nil, nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
users, err := q.
|
|
|
|
|
|
Offset(params.Offset()).
|
|
|
|
|
|
Limit(params.Limit()).
|
|
|
|
|
|
Order(dbent.Desc(dbuser.FieldID)).
|
|
|
|
|
|
All(ctx)
|
|
|
|
|
|
if err != nil {
|
2025-12-18 13:50:39 +08:00
|
|
|
|
return nil, nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
outUsers := make([]service.User, 0, len(users))
|
|
|
|
|
|
if len(users) == 0 {
|
|
|
|
|
|
return outUsers, paginationResultFromTotal(int64(total), params), nil
|
|
|
|
|
|
}
|
2025-12-23 11:03:10 +08:00
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
userIDs := make([]int64, 0, len(users))
|
|
|
|
|
|
userMap := make(map[int64]*service.User, len(users))
|
|
|
|
|
|
for i := range users {
|
|
|
|
|
|
userIDs = append(userIDs, users[i].ID)
|
|
|
|
|
|
u := userEntityToService(users[i])
|
|
|
|
|
|
outUsers = append(outUsers, *u)
|
|
|
|
|
|
userMap[u.ID] = &outUsers[len(outUsers)-1]
|
|
|
|
|
|
}
|
2025-12-23 11:03:10 +08:00
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
// Batch load active subscriptions with groups to avoid N+1.
|
|
|
|
|
|
subs, err := r.client.UserSubscription.Query().
|
|
|
|
|
|
Where(
|
|
|
|
|
|
usersubscription.UserIDIn(userIDs...),
|
|
|
|
|
|
usersubscription.StatusEQ(service.SubscriptionStatusActive),
|
|
|
|
|
|
).
|
|
|
|
|
|
WithGroup().
|
|
|
|
|
|
All(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
|
}
|
2025-12-26 15:40:24 +08:00
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
for i := range subs {
|
|
|
|
|
|
if u, ok := userMap[subs[i].UserID]; ok {
|
|
|
|
|
|
u.Subscriptions = append(u.Subscriptions, *userSubscriptionEntityToService(subs[i]))
|
|
|
|
|
|
}
|
2025-12-23 11:03:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
allowedGroupsByUser, err := r.loadAllowedGroups(ctx, userIDs)
|
2025-12-31 14:11:57 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
for id, u := range userMap {
|
|
|
|
|
|
if groups, ok := allowedGroupsByUser[id]; ok {
|
|
|
|
|
|
u.AllowedGroups = groups
|
2025-12-29 10:03:27 +08:00
|
|
|
|
}
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
return outUsers, paginationResultFromTotal(int64(total), params), nil
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 20:52:47 +08:00
|
|
|
|
func (r *userRepository) UpdateBalance(ctx context.Context, id int64, amount float64) error {
|
2025-12-31 14:11:57 +08:00
|
|
|
|
client := clientFromContext(ctx, r.client)
|
|
|
|
|
|
n, err := client.User.Update().Where(dbuser.IDEQ(id)).AddBalance(amount).Save(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return translatePersistenceError(err, service.ErrUserNotFound, nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
if n == 0 {
|
|
|
|
|
|
return service.ErrUserNotFound
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 20:52:47 +08:00
|
|
|
|
func (r *userRepository) DeductBalance(ctx context.Context, id int64, amount float64) error {
|
2025-12-31 14:11:57 +08:00
|
|
|
|
client := clientFromContext(ctx, r.client)
|
|
|
|
|
|
n, err := client.User.Update().
|
2025-12-29 10:03:27 +08:00
|
|
|
|
Where(dbuser.IDEQ(id), dbuser.BalanceGTE(amount)).
|
|
|
|
|
|
AddBalance(-amount).
|
|
|
|
|
|
Save(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
2025-12-29 10:03:27 +08:00
|
|
|
|
if n == 0 {
|
2025-12-25 20:52:47 +08:00
|
|
|
|
return service.ErrInsufficientBalance
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 20:52:47 +08:00
|
|
|
|
func (r *userRepository) UpdateConcurrency(ctx context.Context, id int64, amount int) error {
|
2025-12-31 14:11:57 +08:00
|
|
|
|
client := clientFromContext(ctx, r.client)
|
|
|
|
|
|
n, err := client.User.Update().Where(dbuser.IDEQ(id)).AddConcurrency(amount).Save(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return translatePersistenceError(err, service.ErrUserNotFound, nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
if n == 0 {
|
|
|
|
|
|
return service.ErrUserNotFound
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 20:52:47 +08:00
|
|
|
|
func (r *userRepository) ExistsByEmail(ctx context.Context, email string) (bool, error) {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
return r.client.User.Query().Where(dbuser.EmailEQ(email)).Exist(ctx)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 20:52:47 +08:00
|
|
|
|
func (r *userRepository) RemoveGroupFromAllowedGroups(ctx context.Context, groupID int64) (int64, error) {
|
2025-12-31 14:11:57 +08:00
|
|
|
|
// 仅操作 user_allowed_groups 联接表,legacy users.allowed_groups 列已弃用。
|
|
|
|
|
|
affected, err := r.client.UserAllowedGroup.Delete().
|
2025-12-29 10:03:27 +08:00
|
|
|
|
Where(userallowedgroup.GroupIDEQ(groupID)).
|
|
|
|
|
|
Exec(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
2025-12-31 14:11:57 +08:00
|
|
|
|
return int64(affected), nil
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
2025-12-20 15:11:43 +08:00
|
|
|
|
|
2025-12-26 15:40:24 +08:00
|
|
|
|
func (r *userRepository) GetFirstAdmin(ctx context.Context) (*service.User, error) {
|
2025-12-29 10:03:27 +08:00
|
|
|
|
m, err := r.client.User.Query().
|
|
|
|
|
|
Where(
|
|
|
|
|
|
dbuser.RoleEQ(service.RoleAdmin),
|
|
|
|
|
|
dbuser.StatusEQ(service.StatusActive),
|
|
|
|
|
|
).
|
|
|
|
|
|
Order(dbent.Asc(dbuser.FieldID)).
|
|
|
|
|
|
First(ctx)
|
2025-12-20 15:11:43 +08:00
|
|
|
|
if err != nil {
|
2025-12-25 20:52:47 +08:00
|
|
|
|
return nil, translatePersistenceError(err, service.ErrUserNotFound, nil)
|
2025-12-20 15:11:43 +08:00
|
|
|
|
}
|
2025-12-26 15:40:24 +08:00
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
out := userEntityToService(m)
|
|
|
|
|
|
groups, err := r.loadAllowedGroups(ctx, []int64{m.ID})
|
2025-12-31 14:11:57 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := groups[m.ID]; ok {
|
|
|
|
|
|
out.AllowedGroups = v
|
2025-12-29 10:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
return out, nil
|
2025-12-26 15:40:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
func (r *userRepository) loadAllowedGroups(ctx context.Context, userIDs []int64) (map[int64][]int64, error) {
|
|
|
|
|
|
out := make(map[int64][]int64, len(userIDs))
|
|
|
|
|
|
if len(userIDs) == 0 {
|
|
|
|
|
|
return out, nil
|
|
|
|
|
|
}
|
2025-12-26 15:40:24 +08:00
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
rows, err := r.client.UserAllowedGroup.Query().
|
|
|
|
|
|
Where(userallowedgroup.UserIDIn(userIDs...)).
|
|
|
|
|
|
All(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for i := range rows {
|
|
|
|
|
|
out[rows[i].UserID] = append(out[rows[i].UserID], rows[i].GroupID)
|
2025-12-26 15:40:24 +08:00
|
|
|
|
}
|
2025-12-29 10:03:27 +08:00
|
|
|
|
|
|
|
|
|
|
for userID := range out {
|
|
|
|
|
|
sort.Slice(out[userID], func(i, j int) bool { return out[userID][i] < out[userID][j] })
|
2025-12-26 15:40:24 +08:00
|
|
|
|
}
|
2025-12-29 10:03:27 +08:00
|
|
|
|
|
|
|
|
|
|
return out, nil
|
2025-12-26 15:40:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 19:23:49 +08:00
|
|
|
|
// syncUserAllowedGroupsWithClient 在 ent client/事务内同步用户允许分组:
|
2025-12-31 14:11:57 +08:00
|
|
|
|
// 仅操作 user_allowed_groups 联接表,legacy users.allowed_groups 列已弃用。
|
2025-12-29 19:23:49 +08:00
|
|
|
|
func (r *userRepository) syncUserAllowedGroupsWithClient(ctx context.Context, client *dbent.Client, userID int64, groupIDs []int64) error {
|
|
|
|
|
|
if client == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Keep join table as the source of truth for reads.
|
|
|
|
|
|
if _, err := client.UserAllowedGroup.Delete().Where(userallowedgroup.UserIDEQ(userID)).Exec(ctx); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unique := make(map[int64]struct{}, len(groupIDs))
|
|
|
|
|
|
for _, id := range groupIDs {
|
|
|
|
|
|
if id <= 0 {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
unique[id] = struct{}{}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(unique) > 0 {
|
|
|
|
|
|
creates := make([]*dbent.UserAllowedGroupCreate, 0, len(unique))
|
|
|
|
|
|
for groupID := range unique {
|
|
|
|
|
|
creates = append(creates, client.UserAllowedGroup.Create().SetUserID(userID).SetGroupID(groupID))
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := client.UserAllowedGroup.
|
|
|
|
|
|
CreateBulk(creates...).
|
|
|
|
|
|
OnConflictColumns(userallowedgroup.FieldUserID, userallowedgroup.FieldGroupID).
|
|
|
|
|
|
DoNothing().
|
|
|
|
|
|
Exec(ctx); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 10:03:27 +08:00
|
|
|
|
func applyUserEntityToService(dst *service.User, src *dbent.User) {
|
2025-12-26 15:40:24 +08:00
|
|
|
|
if dst == nil || src == nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
dst.ID = src.ID
|
|
|
|
|
|
dst.CreatedAt = src.CreatedAt
|
|
|
|
|
|
dst.UpdatedAt = src.UpdatedAt
|
2025-12-20 15:11:43 +08:00
|
|
|
|
}
|