130 lines
3.5 KiB
Go
130 lines
3.5 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"tyapi-server/internal/domains/user/entities"
|
|
"tyapi-server/internal/domains/user/repositories"
|
|
)
|
|
|
|
// UserService 用户领域服务
|
|
type UserService struct {
|
|
userRepo repositories.UserRepository
|
|
enterpriseService *EnterpriseService
|
|
logger *zap.Logger
|
|
}
|
|
|
|
// NewUserService 创建用户领域服务
|
|
func NewUserService(
|
|
userRepo repositories.UserRepository,
|
|
enterpriseService *EnterpriseService,
|
|
logger *zap.Logger,
|
|
) *UserService {
|
|
return &UserService{
|
|
userRepo: userRepo,
|
|
enterpriseService: enterpriseService,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// IsPhoneRegistered 检查手机号是否已注册
|
|
func (s *UserService) IsPhoneRegistered(ctx context.Context, phone string) (bool, error) {
|
|
_, err := s.userRepo.GetByPhone(ctx, phone)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// GetUserWithEnterpriseInfo 获取用户信息(包含企业信息)
|
|
func (s *UserService) GetUserWithEnterpriseInfo(ctx context.Context, userID string) (*entities.User, error) {
|
|
// 通过企业服务获取用户信息(包含企业信息)
|
|
return s.enterpriseService.GetUserWithEnterpriseInfo(ctx, userID)
|
|
}
|
|
|
|
// GetUserByID 根据ID获取用户信息
|
|
func (s *UserService) GetUserByID(ctx context.Context, userID string) (*entities.User, error) {
|
|
user, err := s.userRepo.GetByID(ctx, userID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("用户不存在: %w", err)
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
// GetUserByPhone 根据手机号获取用户信息
|
|
func (s *UserService) GetUserByPhone(ctx context.Context, phone string) (*entities.User, error) {
|
|
user, err := s.userRepo.GetByPhone(ctx, phone)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("用户不存在: %w", err)
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
// UpdateUser 更新用户信息
|
|
func (s *UserService) UpdateUser(ctx context.Context, user *entities.User) error {
|
|
if err := s.userRepo.Update(ctx, *user); err != nil {
|
|
s.logger.Error("更新用户信息失败", zap.Error(err))
|
|
return fmt.Errorf("更新用户信息失败: %w", err)
|
|
}
|
|
|
|
s.logger.Info("用户信息更新成功",
|
|
zap.String("user_id", user.ID),
|
|
zap.String("phone", user.Phone),
|
|
)
|
|
|
|
return nil
|
|
}
|
|
|
|
// ChangePassword 修改用户密码
|
|
func (s *UserService) ChangePassword(ctx context.Context, userID, oldPassword, newPassword string) error {
|
|
user, err := s.userRepo.GetByID(ctx, userID)
|
|
if err != nil {
|
|
return fmt.Errorf("用户不存在: %w", err)
|
|
}
|
|
|
|
if err := user.ChangePassword(oldPassword, newPassword, newPassword); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.userRepo.Update(ctx, user); err != nil {
|
|
s.logger.Error("密码修改失败", zap.Error(err))
|
|
return fmt.Errorf("密码修改失败: %w", err)
|
|
}
|
|
|
|
s.logger.Info("密码修改成功",
|
|
zap.String("user_id", userID),
|
|
)
|
|
|
|
return nil
|
|
}
|
|
|
|
// ValidateUser 验证用户信息
|
|
func (s *UserService) ValidateUser(ctx context.Context, userID string) error {
|
|
user, err := s.userRepo.GetByID(ctx, userID)
|
|
if err != nil {
|
|
return fmt.Errorf("用户不存在: %w", err)
|
|
}
|
|
|
|
// 这里可以添加更多的用户验证逻辑
|
|
if user.Phone == "" {
|
|
return fmt.Errorf("用户手机号不能为空")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetUserStats 获取用户统计信息
|
|
func (s *UserService) GetUserStats(ctx context.Context) (map[string]interface{}, error) {
|
|
// 这里可以添加用户统计逻辑
|
|
stats := map[string]interface{}{
|
|
"total_users": 0, // 需要实现具体的统计逻辑
|
|
"active_users": 0,
|
|
"new_users_today": 0,
|
|
}
|
|
|
|
return stats, nil
|
|
}
|