diff --git a/internal/application/product/product_application_service.go b/internal/application/product/product_application_service.go index e03b756..cc63c2e 100644 --- a/internal/application/product/product_application_service.go +++ b/internal/application/product/product_application_service.go @@ -11,7 +11,7 @@ import ( // ProductApplicationService 产品应用服务接口 type ProductApplicationService interface { // 产品管理 - CreateProduct(ctx context.Context, cmd *commands.CreateProductCommand) error + 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 @@ -46,6 +46,4 @@ type ProductApplicationService interface { CreateProductApiConfig(ctx context.Context, productID string, config *responses.ProductApiConfigResponse) error UpdateProductApiConfig(ctx context.Context, configID string, config *responses.ProductApiConfigResponse) error DeleteProductApiConfig(ctx context.Context, configID string) error - - } diff --git a/internal/application/product/product_application_service_impl.go b/internal/application/product/product_application_service_impl.go index c57528e..d33b6ea 100644 --- a/internal/application/product/product_application_service_impl.go +++ b/internal/application/product/product_application_service_impl.go @@ -51,7 +51,7 @@ func NewProductApplicationService( // CreateProduct 创建产品 // 业务流程�?. 构建产品实体 2. 创建产品 -func (s *ProductApplicationServiceImpl) CreateProduct(ctx context.Context, cmd *commands.CreateProductCommand) error { +func (s *ProductApplicationServiceImpl) CreateProduct(ctx context.Context, cmd *commands.CreateProductCommand) (*responses.ProductAdminInfoResponse, error) { // 1. 构建产品实体 product := &entities.Product{ Name: cmd.Name, @@ -71,8 +71,13 @@ func (s *ProductApplicationServiceImpl) CreateProduct(ctx context.Context, cmd * } // 2. 创建产品 - _, err := s.productManagementService.CreateProduct(ctx, product) - return err + createdProduct, err := s.productManagementService.CreateProduct(ctx, product) + if err != nil { + return nil, err + } + + // 3. 转换为响应对象 + return s.convertToProductAdminInfoResponse(createdProduct), nil } // UpdateProduct 更新产品 diff --git a/internal/infrastructure/database/repositories/product/gorm_product_repository.go b/internal/infrastructure/database/repositories/product/gorm_product_repository.go index 685299d..960d59a 100644 --- a/internal/infrastructure/database/repositories/product/gorm_product_repository.go +++ b/internal/infrastructure/database/repositories/product/gorm_product_repository.go @@ -34,7 +34,13 @@ func NewGormProductRepository(db *gorm.DB, logger *zap.Logger) repositories.Prod } func (r *GormProductRepository) Create(ctx context.Context, entity entities.Product) (entities.Product, error) { - err := r.CreateEntity(ctx, &entity) + // 使用 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 return entity, err } diff --git a/internal/infrastructure/http/handlers/product_admin_handler.go b/internal/infrastructure/http/handlers/product_admin_handler.go index 91bf16b..89c7a65 100644 --- a/internal/infrastructure/http/handlers/product_admin_handler.go +++ b/internal/infrastructure/http/handlers/product_admin_handler.go @@ -73,13 +73,14 @@ func (h *ProductAdminHandler) CreateProduct(c *gin.Context) { return } - if err := h.productAppService.CreateProduct(c.Request.Context(), &cmd); err != nil { + result, err := h.productAppService.CreateProduct(c.Request.Context(), &cmd) + if err != nil { h.logger.Error("创建产品失败", zap.Error(err)) h.responseBuilder.BadRequest(c, err.Error()) return } - h.responseBuilder.Created(c, nil, "产品创建成功") + h.responseBuilder.Created(c, result, "产品创建成功") } // UpdateProduct 更新产品