修改service包

This commit is contained in:
2025-11-05 19:57:30 +08:00
parent bd4f5b83e1
commit 203be4307d
28 changed files with 576 additions and 461 deletions

View File

@@ -1,6 +1,7 @@
package service
import (
"context"
"errors"
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
@@ -9,51 +10,53 @@ import (
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
"gorm.io/gorm"
)
// UserService 定义用户服务接口
type UserService interface {
CreateUser(req *dto.CreateUserRequest) (*dto.CreateUserResponse, error)
Login(req *dto.LoginRequest) (*dto.LoginResponse, error)
SendTestNotification(userID uint, req *dto.SendTestNotificationRequest) error
CreateUser(ctx context.Context, req *dto.CreateUserRequest) (*dto.CreateUserResponse, error)
Login(ctx context.Context, req *dto.LoginRequest) (*dto.LoginResponse, error)
SendTestNotification(ctx context.Context, userID uint, req *dto.SendTestNotificationRequest) error
}
// userService 实现了 UserService 接口
type userService struct {
ctx context.Context
userRepo repository.UserRepository
tokenService token.Service
notifyService domain_notify.Service
logger *logs.Logger
}
// NewUserService 创建并返回一个新的 UserService 实例
func NewUserService(
ctx context.Context,
userRepo repository.UserRepository,
tokenService token.Service,
notifyService domain_notify.Service,
logger *logs.Logger,
) UserService {
return &userService{
ctx: ctx,
userRepo: userRepo,
tokenService: tokenService,
notifyService: notifyService,
logger: logger,
}
}
// CreateUser 创建新用户
func (s *userService) CreateUser(req *dto.CreateUserRequest) (*dto.CreateUserResponse, error) {
func (s *userService) CreateUser(ctx context.Context, req *dto.CreateUserRequest) (*dto.CreateUserResponse, error) {
serviceCtx, logger := logs.Trace(ctx, s.ctx, "CreateUser")
user := &models.User{
Username: req.Username,
Password: req.Password, // 密码会在 BeforeSave 钩子中哈希
}
if err := s.userRepo.Create(user); err != nil {
s.logger.Errorf("创建用户: 创建用户失败: %v", err)
if err := s.userRepo.Create(serviceCtx, user); err != nil {
logger.Errorf("创建用户: 创建用户失败: %v", err)
// 尝试查询用户,以判断是否是用户名重复导致的错误
_, findErr := s.userRepo.FindByUsername(req.Username)
_, findErr := s.userRepo.FindByUsername(serviceCtx, req.Username)
if findErr == nil { // 如果能找到用户,说明是用户名重复
return nil, errors.New("用户名已存在")
}
@@ -69,14 +72,15 @@ func (s *userService) CreateUser(req *dto.CreateUserRequest) (*dto.CreateUserRes
}
// Login 用户登录
func (s *userService) Login(req *dto.LoginRequest) (*dto.LoginResponse, error) {
func (s *userService) Login(ctx context.Context, req *dto.LoginRequest) (*dto.LoginResponse, error) {
serviceCtx, logger := logs.Trace(ctx, s.ctx, "Login")
// 使用新的方法,通过唯一标识符(用户名、邮箱等)查找用户
user, err := s.userRepo.FindUserForLogin(req.Identifier)
user, err := s.userRepo.FindUserForLogin(serviceCtx, req.Identifier)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("登录凭证不正确")
}
s.logger.Errorf("登录: 查询用户失败: %v", err)
logger.Errorf("登录: 查询用户失败: %v", err)
return nil, errors.New("登录失败")
}
@@ -87,7 +91,7 @@ func (s *userService) Login(req *dto.LoginRequest) (*dto.LoginResponse, error) {
// 登录成功,生成 JWT token
tokenString, err := s.tokenService.GenerateToken(user.ID)
if err != nil {
s.logger.Errorf("登录: 生成令牌失败: %v", err)
logger.Errorf("登录: 生成令牌失败: %v", err)
return nil, errors.New("登录失败,无法生成认证信息")
}
@@ -99,12 +103,13 @@ func (s *userService) Login(req *dto.LoginRequest) (*dto.LoginResponse, error) {
}
// SendTestNotification 发送测试通知
func (s *userService) SendTestNotification(userID uint, req *dto.SendTestNotificationRequest) error {
err := s.notifyService.SendTestMessage(userID, req.Type)
func (s *userService) SendTestNotification(ctx context.Context, userID uint, req *dto.SendTestNotificationRequest) error {
serviceCtx, logger := logs.Trace(ctx, s.ctx, "SendTestNotification")
err := s.notifyService.SendTestMessage(serviceCtx, userID, req.Type)
if err != nil {
s.logger.Errorf("发送测试通知: 服务层调用失败: %v", err)
logger.Errorf("发送测试通知: 服务层调用失败: %v", err)
return errors.New("发送测试消息失败: " + err.Error())
}
s.logger.Infof("发送测试通知: 成功为用户 %d 发送类型为 %s 的测试消息", userID, req.Type)
logger.Infof("发送测试通知: 成功为用户 %d 发送类型为 %s 的测试消息", userID, req.Type)
return nil
}