v0.1
This commit is contained in:
161
internal/domains/product/services/product_api_config_service.go
Normal file
161
internal/domains/product/services/product_api_config_service.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"tyapi-server/internal/domains/product/entities"
|
||||
"tyapi-server/internal/domains/product/repositories"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// ProductApiConfigService 产品API配置领域服务接口
|
||||
type ProductApiConfigService interface {
|
||||
// 根据产品ID获取API配置
|
||||
GetApiConfigByProductID(ctx context.Context, productID string) (*entities.ProductApiConfig, error)
|
||||
|
||||
// 根据产品代码获取API配置
|
||||
GetApiConfigByProductCode(ctx context.Context, productCode string) (*entities.ProductApiConfig, error)
|
||||
|
||||
// 批量获取产品API配置
|
||||
GetApiConfigsByProductIDs(ctx context.Context, productIDs []string) ([]*entities.ProductApiConfig, error)
|
||||
|
||||
// 创建产品API配置
|
||||
CreateApiConfig(ctx context.Context, config *entities.ProductApiConfig) error
|
||||
|
||||
// 更新产品API配置
|
||||
UpdateApiConfig(ctx context.Context, config *entities.ProductApiConfig) error
|
||||
|
||||
// 删除产品API配置
|
||||
DeleteApiConfig(ctx context.Context, configID string) error
|
||||
|
||||
// 检查产品API配置是否存在
|
||||
ExistsByProductID(ctx context.Context, productID string) (bool, error)
|
||||
}
|
||||
|
||||
// ProductApiConfigServiceImpl 产品API配置领域服务实现
|
||||
type ProductApiConfigServiceImpl struct {
|
||||
apiConfigRepo repositories.ProductApiConfigRepository
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewProductApiConfigService 创建产品API配置领域服务
|
||||
func NewProductApiConfigService(
|
||||
apiConfigRepo repositories.ProductApiConfigRepository,
|
||||
logger *zap.Logger,
|
||||
) ProductApiConfigService {
|
||||
return &ProductApiConfigServiceImpl{
|
||||
apiConfigRepo: apiConfigRepo,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// GetApiConfigByProductID 根据产品ID获取API配置
|
||||
func (s *ProductApiConfigServiceImpl) GetApiConfigByProductID(ctx context.Context, productID string) (*entities.ProductApiConfig, error) {
|
||||
s.logger.Debug("获取产品API配置", zap.String("product_id", productID))
|
||||
|
||||
config, err := s.apiConfigRepo.FindByProductID(ctx, productID)
|
||||
if err != nil {
|
||||
s.logger.Error("获取产品API配置失败", zap.Error(err), zap.String("product_id", productID))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// GetApiConfigByProductCode 根据产品代码获取API配置
|
||||
func (s *ProductApiConfigServiceImpl) GetApiConfigByProductCode(ctx context.Context, productCode string) (*entities.ProductApiConfig, error) {
|
||||
s.logger.Debug("根据产品代码获取API配置", zap.String("product_code", productCode))
|
||||
|
||||
config, err := s.apiConfigRepo.FindByProductCode(ctx, productCode)
|
||||
if err != nil {
|
||||
s.logger.Error("根据产品代码获取API配置失败", zap.Error(err), zap.String("product_code", productCode))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// GetApiConfigsByProductIDs 批量获取产品API配置
|
||||
func (s *ProductApiConfigServiceImpl) GetApiConfigsByProductIDs(ctx context.Context, productIDs []string) ([]*entities.ProductApiConfig, error) {
|
||||
s.logger.Debug("批量获取产品API配置", zap.Strings("product_ids", productIDs))
|
||||
|
||||
configs, err := s.apiConfigRepo.FindByProductIDs(ctx, productIDs)
|
||||
if err != nil {
|
||||
s.logger.Error("批量获取产品API配置失败", zap.Error(err), zap.Strings("product_ids", productIDs))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
// CreateApiConfig 创建产品API配置
|
||||
func (s *ProductApiConfigServiceImpl) CreateApiConfig(ctx context.Context, config *entities.ProductApiConfig) error {
|
||||
s.logger.Debug("创建产品API配置", zap.String("product_id", config.ProductID))
|
||||
|
||||
// 检查是否已存在配置
|
||||
exists, err := s.apiConfigRepo.ExistsByProductID(ctx, config.ProductID)
|
||||
if err != nil {
|
||||
s.logger.Error("检查产品API配置是否存在失败", zap.Error(err), zap.String("product_id", config.ProductID))
|
||||
return err
|
||||
}
|
||||
|
||||
if exists {
|
||||
return entities.NewValidationError("产品API配置已存在")
|
||||
}
|
||||
|
||||
// 验证配置
|
||||
if err := config.Validate(); err != nil {
|
||||
s.logger.Error("产品API配置验证失败", zap.Error(err), zap.String("product_id", config.ProductID))
|
||||
return err
|
||||
}
|
||||
|
||||
// 保存配置
|
||||
err = s.apiConfigRepo.Create(ctx, *config)
|
||||
if err != nil {
|
||||
s.logger.Error("创建产品API配置失败", zap.Error(err), zap.String("product_id", config.ProductID))
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("产品API配置创建成功", zap.String("product_id", config.ProductID))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateApiConfig 更新产品API配置
|
||||
func (s *ProductApiConfigServiceImpl) UpdateApiConfig(ctx context.Context, config *entities.ProductApiConfig) error {
|
||||
s.logger.Debug("更新产品API配置", zap.String("config_id", config.ID))
|
||||
|
||||
// 验证配置
|
||||
if err := config.Validate(); err != nil {
|
||||
s.logger.Error("产品API配置验证失败", zap.Error(err), zap.String("config_id", config.ID))
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新配置
|
||||
err := s.apiConfigRepo.Update(ctx, *config)
|
||||
if err != nil {
|
||||
s.logger.Error("更新产品API配置失败", zap.Error(err), zap.String("config_id", config.ID))
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("产品API配置更新成功", zap.String("config_id", config.ID))
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteApiConfig 删除产品API配置
|
||||
func (s *ProductApiConfigServiceImpl) DeleteApiConfig(ctx context.Context, configID string) error {
|
||||
s.logger.Debug("删除产品API配置", zap.String("config_id", configID))
|
||||
|
||||
err := s.apiConfigRepo.Delete(ctx, configID)
|
||||
if err != nil {
|
||||
s.logger.Error("删除产品API配置失败", zap.Error(err), zap.String("config_id", configID))
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("产品API配置删除成功", zap.String("config_id", configID))
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExistsByProductID 检查产品API配置是否存在
|
||||
func (s *ProductApiConfigServiceImpl) ExistsByProductID(ctx context.Context, productID string) (bool, error) {
|
||||
return s.apiConfigRepo.ExistsByProductID(ctx, productID)
|
||||
}
|
||||
@@ -8,8 +8,11 @@ import (
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"tyapi-server/internal/application/product/dto/commands"
|
||||
"tyapi-server/internal/domains/product/entities"
|
||||
"tyapi-server/internal/domains/product/repositories"
|
||||
"tyapi-server/internal/domains/product/repositories/queries"
|
||||
"tyapi-server/internal/shared/interfaces"
|
||||
)
|
||||
|
||||
// ProductManagementService 产品管理领域服务
|
||||
@@ -69,6 +72,13 @@ func (s *ProductManagementService) GetProductByID(ctx context.Context, productID
|
||||
return &product, nil
|
||||
}
|
||||
|
||||
func (s *ProductManagementService) GetProductByCode(ctx context.Context, productCode string) (*entities.Product, error) {
|
||||
product, err := s.productRepo.FindByCode(ctx, productCode)
|
||||
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)
|
||||
@@ -84,9 +94,107 @@ func (s *ProductManagementService) GetProductWithCategory(ctx context.Context, p
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是组合包,加载子产品信息
|
||||
if product.IsPackage {
|
||||
packageItems, err := s.productRepo.GetPackageItems(ctx, productID)
|
||||
if err == nil {
|
||||
product.PackageItems = packageItems
|
||||
}
|
||||
}
|
||||
|
||||
return &product, nil
|
||||
}
|
||||
|
||||
// GetPackageItems 获取组合包项目列表
|
||||
func (s *ProductManagementService) GetPackageItems(ctx context.Context, packageID string) ([]*entities.ProductPackageItem, error) {
|
||||
packageItems, err := s.productRepo.GetPackageItems(ctx, packageID)
|
||||
if err != nil {
|
||||
s.logger.Error("获取组合包项目失败", zap.Error(err))
|
||||
return nil, fmt.Errorf("获取组合包项目失败: %w", err)
|
||||
}
|
||||
return packageItems, nil
|
||||
}
|
||||
|
||||
// CreatePackageItem 创建组合包项目
|
||||
func (s *ProductManagementService) CreatePackageItem(ctx context.Context, packageItem *entities.ProductPackageItem) error {
|
||||
if err := s.productRepo.CreatePackageItem(ctx, packageItem); err != nil {
|
||||
s.logger.Error("创建组合包项目失败", zap.Error(err))
|
||||
return fmt.Errorf("创建组合包项目失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("组合包项目创建成功",
|
||||
zap.String("package_id", packageItem.PackageID),
|
||||
zap.String("product_id", packageItem.ProductID),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPackageItemByID 根据ID获取组合包项目
|
||||
func (s *ProductManagementService) GetPackageItemByID(ctx context.Context, itemID string) (*entities.ProductPackageItem, error) {
|
||||
packageItem, err := s.productRepo.GetPackageItemByID(ctx, itemID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("组合包项目不存在: %w", err)
|
||||
}
|
||||
return packageItem, nil
|
||||
}
|
||||
|
||||
// UpdatePackageItem 更新组合包项目
|
||||
func (s *ProductManagementService) UpdatePackageItem(ctx context.Context, packageItem *entities.ProductPackageItem) error {
|
||||
if err := s.productRepo.UpdatePackageItem(ctx, packageItem); err != nil {
|
||||
s.logger.Error("更新组合包项目失败", zap.Error(err))
|
||||
return fmt.Errorf("更新组合包项目失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("组合包项目更新成功",
|
||||
zap.String("item_id", packageItem.ID),
|
||||
zap.String("package_id", packageItem.PackageID),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeletePackageItem 删除组合包项目
|
||||
func (s *ProductManagementService) DeletePackageItem(ctx context.Context, itemID string) error {
|
||||
if err := s.productRepo.DeletePackageItem(ctx, itemID); err != nil {
|
||||
s.logger.Error("删除组合包项目失败", zap.Error(err))
|
||||
return fmt.Errorf("删除组合包项目失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("组合包项目删除成功", zap.String("item_id", itemID))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdatePackageItemsBatch 批量更新组合包子产品
|
||||
func (s *ProductManagementService) UpdatePackageItemsBatch(ctx context.Context, packageID string, items []commands.PackageItemData) error {
|
||||
// 删除现有的所有子产品
|
||||
if err := s.productRepo.DeletePackageItemsByPackageID(ctx, packageID); err != nil {
|
||||
s.logger.Error("删除现有组合包子产品失败", zap.Error(err))
|
||||
return fmt.Errorf("删除现有组合包子产品失败: %w", err)
|
||||
}
|
||||
|
||||
// 创建新的子产品项目
|
||||
for _, item := range items {
|
||||
packageItem := &entities.ProductPackageItem{
|
||||
PackageID: packageID,
|
||||
ProductID: item.ProductID,
|
||||
SortOrder: item.SortOrder,
|
||||
}
|
||||
|
||||
if err := s.productRepo.CreatePackageItem(ctx, packageItem); err != nil {
|
||||
s.logger.Error("创建组合包子产品失败", zap.Error(err))
|
||||
return fmt.Errorf("创建组合包子产品失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
s.logger.Info("批量更新组合包子产品成功",
|
||||
zap.String("package_id", packageID),
|
||||
zap.Int("item_count", len(items)),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateProduct 更新产品
|
||||
func (s *ProductManagementService) UpdateProduct(ctx context.Context, product *entities.Product) error {
|
||||
// 验证产品信息
|
||||
@@ -152,7 +260,7 @@ func (s *ProductManagementService) ValidateProduct(product *entities.Product) er
|
||||
return errors.New("产品编号不能为空")
|
||||
}
|
||||
|
||||
if product.Price < 0 {
|
||||
if product.Price.IsNegative() {
|
||||
return errors.New("产品价格不能为负数")
|
||||
}
|
||||
|
||||
@@ -182,4 +290,48 @@ func (s *ProductManagementService) ValidateProductCode(code string, excludeID st
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ListProducts 获取产品列表(支持筛选和分页)
|
||||
func (s *ProductManagementService) ListProducts(ctx context.Context, filters map[string]interface{}, options interfaces.ListOptions) ([]*entities.Product, int64, error) {
|
||||
// 构建查询条件
|
||||
query := &queries.ListProductsQuery{
|
||||
Page: options.Page,
|
||||
PageSize: options.PageSize,
|
||||
SortBy: options.Sort,
|
||||
SortOrder: options.Order,
|
||||
}
|
||||
|
||||
// 应用筛选条件
|
||||
if keyword, ok := filters["keyword"].(string); ok && keyword != "" {
|
||||
query.Keyword = keyword
|
||||
}
|
||||
if categoryID, ok := filters["category_id"].(string); ok && categoryID != "" {
|
||||
query.CategoryID = categoryID
|
||||
}
|
||||
if isEnabled, ok := filters["is_enabled"].(bool); ok {
|
||||
query.IsEnabled = &isEnabled
|
||||
}
|
||||
if isVisible, ok := filters["is_visible"].(bool); ok {
|
||||
query.IsVisible = &isVisible
|
||||
}
|
||||
if isPackage, ok := filters["is_package"].(bool); ok {
|
||||
query.IsPackage = &isPackage
|
||||
}
|
||||
|
||||
// 调用仓储层获取产品列表
|
||||
products, total, err := s.productRepo.ListProducts(ctx, query)
|
||||
if err != nil {
|
||||
s.logger.Error("获取产品列表失败", zap.Error(err))
|
||||
return nil, 0, fmt.Errorf("获取产品列表失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("产品列表查询成功",
|
||||
zap.Int("count", len(products)),
|
||||
zap.Int64("total", total),
|
||||
zap.Int("page", options.Page),
|
||||
zap.Int("page_size", options.PageSize),
|
||||
)
|
||||
|
||||
return products, total, nil
|
||||
}
|
||||
@@ -5,10 +5,13 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"tyapi-server/internal/domains/product/entities"
|
||||
"tyapi-server/internal/domains/product/repositories"
|
||||
"tyapi-server/internal/domains/product/repositories/queries"
|
||||
)
|
||||
|
||||
// ProductSubscriptionService 产品订阅领域服务
|
||||
@@ -32,6 +35,31 @@ func NewProductSubscriptionService(
|
||||
}
|
||||
}
|
||||
|
||||
// UserSubscribedProductByCode 查找用户已订阅的产品
|
||||
func (s *ProductSubscriptionService) UserSubscribedProductByCode(ctx context.Context, userID string, productCode string) (*entities.Subscription, error) {
|
||||
product, err := s.productRepo.FindByCode(ctx, productCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
subscription, err := s.subscriptionRepo.FindByUserAndProduct(ctx, userID, product.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return subscription, nil
|
||||
}
|
||||
|
||||
// GetUserSubscribedProduct 查找用户已订阅的产品
|
||||
func (s *ProductSubscriptionService) GetUserSubscribedProduct(ctx context.Context, userID string, productID string) (*entities.Subscription, error) {
|
||||
subscription, err := s.subscriptionRepo.FindByUserAndProduct(ctx, userID, productID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return subscription, nil
|
||||
}
|
||||
|
||||
// CanUserSubscribeProduct 检查用户是否可以订阅产品
|
||||
func (s *ProductSubscriptionService) CanUserSubscribeProduct(ctx context.Context, userID string, productID string) (bool, error) {
|
||||
// 检查产品是否存在且可订阅
|
||||
@@ -92,6 +120,11 @@ func (s *ProductSubscriptionService) CreateSubscription(ctx context.Context, use
|
||||
return &createdSubscription, nil
|
||||
}
|
||||
|
||||
// ListSubscriptions 获取订阅列表
|
||||
func (s *ProductSubscriptionService) ListSubscriptions(ctx context.Context, query *queries.ListSubscriptionsQuery) ([]*entities.Subscription, int64, error) {
|
||||
return s.subscriptionRepo.ListSubscriptions(ctx, query)
|
||||
}
|
||||
|
||||
// GetUserSubscriptions 获取用户订阅列表
|
||||
func (s *ProductSubscriptionService) GetUserSubscriptions(ctx context.Context, userID string) ([]*entities.Subscription, error) {
|
||||
return s.subscriptionRepo.FindByUserID(ctx, userID)
|
||||
@@ -141,4 +174,20 @@ func (s *ProductSubscriptionService) GetProductStats(ctx context.Context) (map[s
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ProductSubscriptionService) SaveSubscription(ctx context.Context, subscription *entities.Subscription) error {
|
||||
exists, err := s.subscriptionRepo.Exists(ctx, subscription.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查订阅是否存在失败: %w", err)
|
||||
}
|
||||
if exists {
|
||||
return s.subscriptionRepo.Update(ctx, *subscription)
|
||||
} else {
|
||||
_, err := s.subscriptionRepo.Create(ctx, *subscription)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建订阅失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user