This commit is contained in:
2025-07-20 20:53:26 +08:00
parent 83bf9aea7d
commit 8ad1d7288e
158 changed files with 18156 additions and 13188 deletions

View File

@@ -0,0 +1,185 @@
package services
import (
"context"
"errors"
"fmt"
"strings"
"go.uber.org/zap"
"tyapi-server/internal/domains/product/entities"
"tyapi-server/internal/domains/product/repositories"
)
// ProductManagementService 产品管理领域服务
// 负责产品的基本管理操作,包括创建、查询、更新等
type ProductManagementService struct {
productRepo repositories.ProductRepository
categoryRepo repositories.ProductCategoryRepository
logger *zap.Logger
}
// NewProductManagementService 创建产品管理领域服务
func NewProductManagementService(
productRepo repositories.ProductRepository,
categoryRepo repositories.ProductCategoryRepository,
logger *zap.Logger,
) *ProductManagementService {
return &ProductManagementService{
productRepo: productRepo,
categoryRepo: categoryRepo,
logger: logger,
}
}
// CreateProduct 创建产品
func (s *ProductManagementService) CreateProduct(ctx context.Context, product *entities.Product) (*entities.Product, error) {
// 验证产品信息
if err := s.ValidateProduct(product); err != nil {
return nil, err
}
// 验证产品编号唯一性
if err := s.ValidateProductCode(product.Code, ""); err != nil {
return nil, err
}
// 创建产品
createdProduct, err := s.productRepo.Create(ctx, *product)
if err != nil {
s.logger.Error("创建产品失败", zap.Error(err))
return nil, fmt.Errorf("创建产品失败: %w", err)
}
s.logger.Info("产品创建成功",
zap.String("product_id", createdProduct.ID),
zap.String("product_name", createdProduct.Name),
)
return &createdProduct, nil
}
// GetProductByID 根据ID获取产品
func (s *ProductManagementService) GetProductByID(ctx context.Context, productID string) (*entities.Product, error) {
product, err := s.productRepo.GetByID(ctx, productID)
if err != nil {
return nil, fmt.Errorf("产品不存在: %w", err)
}
return &product, nil
}
// GetProductWithCategory 获取产品及其分类信息
func (s *ProductManagementService) GetProductWithCategory(ctx context.Context, productID string) (*entities.Product, error) {
product, err := s.productRepo.GetByID(ctx, productID)
if err != nil {
return nil, fmt.Errorf("产品不存在: %w", err)
}
// 加载分类信息
if product.CategoryID != "" {
category, err := s.categoryRepo.GetByID(ctx, product.CategoryID)
if err == nil {
product.Category = &category
}
}
return &product, nil
}
// UpdateProduct 更新产品
func (s *ProductManagementService) UpdateProduct(ctx context.Context, product *entities.Product) error {
// 验证产品信息
if err := s.ValidateProduct(product); err != nil {
return err
}
// 验证产品编号唯一性(排除自己)
if err := s.ValidateProductCode(product.Code, product.ID); err != nil {
return err
}
if err := s.productRepo.Update(ctx, *product); err != nil {
s.logger.Error("更新产品失败", zap.Error(err))
return fmt.Errorf("更新产品失败: %w", err)
}
s.logger.Info("产品更新成功",
zap.String("product_id", product.ID),
zap.String("product_name", product.Name),
)
return nil
}
// DeleteProduct 删除产品
func (s *ProductManagementService) DeleteProduct(ctx context.Context, productID string) error {
if err := s.productRepo.Delete(ctx, productID); err != nil {
s.logger.Error("删除产品失败", zap.Error(err))
return fmt.Errorf("删除产品失败: %w", err)
}
s.logger.Info("产品删除成功", zap.String("product_id", productID))
return nil
}
// GetVisibleProducts 获取可见产品列表
func (s *ProductManagementService) GetVisibleProducts(ctx context.Context) ([]*entities.Product, error) {
return s.productRepo.FindVisible(ctx)
}
// GetEnabledProducts 获取启用产品列表
func (s *ProductManagementService) GetEnabledProducts(ctx context.Context) ([]*entities.Product, error) {
return s.productRepo.FindEnabled(ctx)
}
// GetProductsByCategory 根据分类获取产品
func (s *ProductManagementService) GetProductsByCategory(ctx context.Context, categoryID string) ([]*entities.Product, error) {
return s.productRepo.FindByCategoryID(ctx, categoryID)
}
// ValidateProduct 验证产品
func (s *ProductManagementService) ValidateProduct(product *entities.Product) error {
if product == nil {
return errors.New("产品不能为空")
}
if strings.TrimSpace(product.Name) == "" {
return errors.New("产品名称不能为空")
}
if strings.TrimSpace(product.Code) == "" {
return errors.New("产品编号不能为空")
}
if product.Price < 0 {
return errors.New("产品价格不能为负数")
}
// 验证分类是否存在
if product.CategoryID != "" {
category, err := s.categoryRepo.GetByID(context.Background(), product.CategoryID)
if err != nil {
return fmt.Errorf("产品分类不存在: %w", err)
}
if !category.IsValid() {
return errors.New("产品分类已禁用或删除")
}
}
return nil
}
// ValidateProductCode 验证产品编号唯一性
func (s *ProductManagementService) ValidateProductCode(code string, excludeID string) error {
if strings.TrimSpace(code) == "" {
return errors.New("产品编号不能为空")
}
existingProduct, err := s.productRepo.FindByCode(context.Background(), code)
if err == nil && existingProduct != nil && existingProduct.ID != excludeID {
return errors.New("产品编号已存在")
}
return nil
}

View File

@@ -1,151 +0,0 @@
package services
import (
"errors"
"fmt"
"strings"
"tyapi-server/internal/domains/product/entities"
"tyapi-server/internal/domains/product/repositories"
)
// ProductService 产品领域服务
type ProductService struct {
productRepo repositories.ProductRepository
categoryRepo repositories.ProductCategoryRepository
subscriptionRepo repositories.SubscriptionRepository
}
// NewProductService 创建产品领域服务
func NewProductService(
productRepo repositories.ProductRepository,
categoryRepo repositories.ProductCategoryRepository,
subscriptionRepo repositories.SubscriptionRepository,
) *ProductService {
return &ProductService{
productRepo: productRepo,
categoryRepo: categoryRepo,
subscriptionRepo: subscriptionRepo,
}
}
// ValidateProduct 验证产品
func (s *ProductService) ValidateProduct(product *entities.Product) error {
if product == nil {
return errors.New("产品不能为空")
}
if strings.TrimSpace(product.Name) == "" {
return errors.New("产品名称不能为空")
}
if strings.TrimSpace(product.Code) == "" {
return errors.New("产品编号不能为空")
}
if product.Price < 0 {
return errors.New("产品价格不能为负数")
}
// 验证分类是否存在
if product.CategoryID != "" {
category, err := s.categoryRepo.GetByID(nil, product.CategoryID)
if err != nil {
return fmt.Errorf("产品分类不存在: %w", err)
}
if !category.IsValid() {
return errors.New("产品分类已禁用或删除")
}
}
return nil
}
// ValidateProductCode 验证产品编号唯一性
func (s *ProductService) ValidateProductCode(code string, excludeID string) error {
if strings.TrimSpace(code) == "" {
return errors.New("产品编号不能为空")
}
existingProduct, err := s.productRepo.FindByCode(nil, code)
if err == nil && existingProduct != nil && existingProduct.ID != excludeID {
return errors.New("产品编号已存在")
}
return nil
}
// CanUserSubscribeProduct 检查用户是否可以订阅产品
func (s *ProductService) CanUserSubscribeProduct(userID string, productID string) (bool, error) {
// 检查产品是否存在且可订阅
product, err := s.productRepo.GetByID(nil, productID)
if err != nil {
return false, fmt.Errorf("产品不存在: %w", err)
}
if !product.CanBeSubscribed() {
return false, errors.New("产品不可订阅")
}
// 检查用户是否已有该产品的订阅
existingSubscription, err := s.subscriptionRepo.FindByUserAndProduct(nil, userID, productID)
if err == nil && existingSubscription != nil {
return false, errors.New("用户已有该产品的订阅")
}
return true, nil
}
// GetProductWithCategory 获取产品及其分类信息
func (s *ProductService) GetProductWithCategory(productID string) (*entities.Product, error) {
product, err := s.productRepo.GetByID(nil, productID)
if err != nil {
return nil, fmt.Errorf("产品不存在: %w", err)
}
// 加载分类信息
if product.CategoryID != "" {
category, err := s.categoryRepo.GetByID(nil, product.CategoryID)
if err == nil {
product.Category = &category
}
}
return &product, nil
}
// GetVisibleProducts 获取可见产品列表
func (s *ProductService) GetVisibleProducts() ([]*entities.Product, error) {
return s.productRepo.FindVisible(nil)
}
// GetEnabledProducts 获取启用产品列表
func (s *ProductService) GetEnabledProducts() ([]*entities.Product, error) {
return s.productRepo.FindEnabled(nil)
}
// GetProductsByCategory 根据分类获取产品
func (s *ProductService) GetProductsByCategory(categoryID string) ([]*entities.Product, error) {
return s.productRepo.FindByCategoryID(nil, categoryID)
}
// GetProductStats 获取产品统计信息
func (s *ProductService) GetProductStats() (map[string]int64, error) {
stats := make(map[string]int64)
total, err := s.productRepo.CountByCategory(nil, "")
if err == nil {
stats["total"] = total
}
enabled, err := s.productRepo.CountEnabled(nil)
if err == nil {
stats["enabled"] = enabled
}
visible, err := s.productRepo.CountVisible(nil)
if err == nil {
stats["visible"] = visible
}
return stats, nil
}

View File

@@ -0,0 +1,144 @@
package services
import (
"context"
"errors"
"fmt"
"go.uber.org/zap"
"tyapi-server/internal/domains/product/entities"
"tyapi-server/internal/domains/product/repositories"
)
// ProductSubscriptionService 产品订阅领域服务
// 负责产品订阅相关的业务逻辑,包括订阅验证、订阅管理等
type ProductSubscriptionService struct {
productRepo repositories.ProductRepository
subscriptionRepo repositories.SubscriptionRepository
logger *zap.Logger
}
// NewProductSubscriptionService 创建产品订阅领域服务
func NewProductSubscriptionService(
productRepo repositories.ProductRepository,
subscriptionRepo repositories.SubscriptionRepository,
logger *zap.Logger,
) *ProductSubscriptionService {
return &ProductSubscriptionService{
productRepo: productRepo,
subscriptionRepo: subscriptionRepo,
logger: logger,
}
}
// CanUserSubscribeProduct 检查用户是否可以订阅产品
func (s *ProductSubscriptionService) CanUserSubscribeProduct(ctx context.Context, userID string, productID string) (bool, error) {
// 检查产品是否存在且可订阅
product, err := s.productRepo.GetByID(ctx, productID)
if err != nil {
return false, fmt.Errorf("产品不存在: %w", err)
}
if !product.CanBeSubscribed() {
return false, errors.New("产品不可订阅")
}
// 检查用户是否已有该产品的订阅
existingSubscription, err := s.subscriptionRepo.FindByUserAndProduct(ctx, userID, productID)
if err == nil && existingSubscription != nil {
return false, errors.New("用户已有该产品的订阅")
}
return true, nil
}
// CreateSubscription 创建订阅
func (s *ProductSubscriptionService) CreateSubscription(ctx context.Context, userID, productID string) (*entities.Subscription, error) {
// 检查是否可以订阅
canSubscribe, err := s.CanUserSubscribeProduct(ctx, userID, productID)
if err != nil {
return nil, err
}
if !canSubscribe {
return nil, errors.New("无法订阅该产品")
}
// 获取产品信息以获取价格
product, err := s.productRepo.GetByID(ctx, productID)
if err != nil {
return nil, fmt.Errorf("产品不存在: %w", err)
}
// 创建订阅
subscription := &entities.Subscription{
UserID: userID,
ProductID: productID,
Price: product.Price,
}
createdSubscription, err := s.subscriptionRepo.Create(ctx, *subscription)
if err != nil {
s.logger.Error("创建订阅失败", zap.Error(err))
return nil, fmt.Errorf("创建订阅失败: %w", err)
}
s.logger.Info("订阅创建成功",
zap.String("subscription_id", createdSubscription.ID),
zap.String("user_id", userID),
zap.String("product_id", productID),
)
return &createdSubscription, nil
}
// GetUserSubscriptions 获取用户订阅列表
func (s *ProductSubscriptionService) GetUserSubscriptions(ctx context.Context, userID string) ([]*entities.Subscription, error) {
return s.subscriptionRepo.FindByUserID(ctx, userID)
}
// GetSubscriptionByID 根据ID获取订阅
func (s *ProductSubscriptionService) GetSubscriptionByID(ctx context.Context, subscriptionID string) (*entities.Subscription, error) {
subscription, err := s.subscriptionRepo.GetByID(ctx, subscriptionID)
if err != nil {
return nil, fmt.Errorf("订阅不存在: %w", err)
}
return &subscription, nil
}
// CancelSubscription 取消订阅
func (s *ProductSubscriptionService) CancelSubscription(ctx context.Context, subscriptionID string) error {
// 由于订阅实体没有状态字段,这里直接删除订阅
if err := s.subscriptionRepo.Delete(ctx, subscriptionID); err != nil {
s.logger.Error("取消订阅失败", zap.Error(err))
return fmt.Errorf("取消订阅失败: %w", err)
}
s.logger.Info("订阅取消成功",
zap.String("subscription_id", subscriptionID),
)
return nil
}
// GetProductStats 获取产品统计信息
func (s *ProductSubscriptionService) GetProductStats(ctx context.Context) (map[string]int64, error) {
stats := make(map[string]int64)
total, err := s.productRepo.CountByCategory(ctx, "")
if err == nil {
stats["total"] = total
}
enabled, err := s.productRepo.CountEnabled(ctx)
if err == nil {
stats["enabled"] = enabled
}
visible, err := s.productRepo.CountVisible(ctx)
if err == nil {
stats["visible"] = visible
}
return stats, nil
}