package service import ( "context" "gofaster/internal/auth/model" "gofaster/internal/auth/repository" "gorm.io/gorm" ) type UserService struct { repo repository.UserRepository db *gorm.DB } func NewUserService(repo repository.UserRepository, db *gorm.DB) *UserService { return &UserService{ repo: repo, db: db, } } func (s *UserService) CreateUser(ctx context.Context, user *model.User) error { return s.repo.Create(ctx, user) } func (s *UserService) GetUserByID(ctx context.Context, id uint) (*model.User, error) { return s.repo.GetByID(ctx, id) } func (s *UserService) UpdateUser(ctx context.Context, user *model.User) error { return s.repo.Update(ctx, user) } func (s *UserService) DeleteUser(ctx context.Context, id uint) error { return s.repo.Delete(ctx, id) } func (s *UserService) ListUsers(ctx context.Context, page, pageSize int) ([]*model.User, int64, error) { return s.repo.List(ctx, page, pageSize) } // GetByID 根据ID获取用户(无context版本,用于密码服务) func (s *UserService) GetByID(id uint) (*model.User, error) { ctx := context.Background() return s.repo.GetByID(ctx, id) } // Update 更新用户(无context版本,用于密码服务) func (s *UserService) Update(user *model.User) error { ctx := context.Background() return s.repo.Update(ctx, user) } // GetRoles 获取角色列表 func (s *UserService) GetRoles(ctx context.Context, page, pageSize int) ([]*model.Role, int64, error) { roleRepo := repository.NewRoleRepo(s.db) roles, total, err := roleRepo.List(page, pageSize) if err != nil { return nil, 0, err } // 转换类型 var rolePtrs []*model.Role for i := range roles { rolePtrs = append(rolePtrs, &roles[i]) } return rolePtrs, total, nil }