diff --git a/internal/application/product/product_application_service.go b/internal/application/product/product_application_service.go index cc63c2e..6b3c54d 100644 --- a/internal/application/product/product_application_service.go +++ b/internal/application/product/product_application_service.go @@ -12,6 +12,7 @@ import ( type ProductApplicationService interface { // 产品管理 CreateProduct(ctx context.Context, cmd *commands.CreateProductCommand) (*responses.ProductAdminInfoResponse, error) + UpdateProduct(ctx context.Context, cmd *commands.UpdateProductCommand) error DeleteProduct(ctx context.Context, cmd *commands.DeleteProductCommand) error diff --git a/internal/domains/product/entities/product.go b/internal/domains/product/entities/product.go index d452e64..4ec1ab5 100644 --- a/internal/domains/product/entities/product.go +++ b/internal/domains/product/entities/product.go @@ -20,8 +20,8 @@ type Product struct { Price decimal.Decimal `gorm:"type:decimal(10,2);not null;default:0" comment:"产品价格"` CostPrice decimal.Decimal `gorm:"type:decimal(10,2);default:0" comment:"成本价"` Remark string `gorm:"type:text" comment:"备注"` - IsEnabled bool `gorm:"default:true" comment:"是否启用"` - IsVisible bool `gorm:"default:true" comment:"是否展示"` + IsEnabled bool `gorm:"default:false" comment:"是否启用"` + IsVisible bool `gorm:"default:false" comment:"是否展示"` IsPackage bool `gorm:"default:false" comment:"是否组合包"` // 组合包相关关联 PackageItems []*ProductPackageItem `gorm:"foreignKey:PackageID" comment:"组合包项目列表"` @@ -62,7 +62,6 @@ func (p *Product) CanBeSubscribed() bool { return p.IsValid() } - // UpdateSEO 更新SEO信息 func (p *Product) UpdateSEO(title, description, keywords string) { p.SEOTitle = title @@ -115,4 +114,4 @@ func (p *Product) GetOldID() string { // HasOldID 检查是否有旧ID func (p *Product) HasOldID() bool { return p.OldID != nil && *p.OldID != "" -} \ No newline at end of file +} diff --git a/internal/domains/product/services/product_management_service.go b/internal/domains/product/services/product_management_service.go index f198a56..9468f04 100644 --- a/internal/domains/product/services/product_management_service.go +++ b/internal/domains/product/services/product_management_service.go @@ -18,9 +18,9 @@ import ( // ProductManagementService 产品管理领域服务 // 负责产品的基本管理操作,包括创建、查询、更新等 type ProductManagementService struct { - productRepo repositories.ProductRepository - categoryRepo repositories.ProductCategoryRepository - logger *zap.Logger + productRepo repositories.ProductRepository + categoryRepo repositories.ProductCategoryRepository + logger *zap.Logger } // NewProductManagementService 创建产品管理领域服务 @@ -79,6 +79,7 @@ func (s *ProductManagementService) GetProductByCode(ctx context.Context, product } return product, nil } + // GetProductWithCategory 获取产品及其分类信息 func (s *ProductManagementService) GetProductWithCategory(ctx context.Context, productID string) (*entities.Product, error) { product, err := s.productRepo.GetByID(ctx, productID) @@ -326,9 +327,9 @@ func (s *ProductManagementService) ValidateProductCode(code string, excludeID st 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, + Page: options.Page, + PageSize: options.PageSize, + SortBy: options.Sort, SortOrder: options.Order, } @@ -370,9 +371,9 @@ func (s *ProductManagementService) ListProducts(ctx context.Context, filters map func (s *ProductManagementService) ListProductsWithSubscriptionStatus(ctx context.Context, filters map[string]interface{}, options interfaces.ListOptions) ([]*entities.Product, map[string]bool, int64, error) { // 构建查询条件 query := &queries.ListProductsQuery{ - Page: options.Page, - PageSize: options.PageSize, - SortBy: options.Sort, + Page: options.Page, + PageSize: options.PageSize, + SortBy: options.Sort, SortOrder: options.Order, } @@ -415,4 +416,4 @@ func (s *ProductManagementService) ListProductsWithSubscriptionStatus(ctx contex ) return products, subscriptionStatusMap, total, nil -} \ No newline at end of file +} diff --git a/internal/infrastructure/database/repositories/product/gorm_product_repository.go b/internal/infrastructure/database/repositories/product/gorm_product_repository.go index 960d59a..685299d 100644 --- a/internal/infrastructure/database/repositories/product/gorm_product_repository.go +++ b/internal/infrastructure/database/repositories/product/gorm_product_repository.go @@ -34,13 +34,7 @@ func NewGormProductRepository(db *gorm.DB, logger *zap.Logger) repositories.Prod } func (r *GormProductRepository) Create(ctx context.Context, entity entities.Product) (entities.Product, error) { - // 使用 Select 明确指定所有字段,确保 false 值也能正确保存 - // 这样可以避免 GORM 的 default:true 标签在字段为 false 时使用数据库默认值 - err := r.GetDB(ctx).Select( - "id", "old_id", "name", "code", "description", "content", "category_id", - "price", "cost_price", "remark", "is_enabled", "is_visible", "is_package", - "seo_title", "seo_description", "seo_keywords", - ).Create(&entity).Error + err := r.CreateEntity(ctx, &entity) return entity, err }