|
|
|
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) {
|
|
|
|
// 计算偏移量
|
|
|
|
offset := (page - 1) * pageSize
|
|
|
|
return s.repo.List(ctx, offset, 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.NewRoleRepository(s.db)
|
|
|
|
offset := (page - 1) * pageSize
|
|
|
|
return roleRepo.List(ctx, offset, pageSize)
|
|
|
|
}
|