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 }