From 14c3a237527153aa629a570fa6783447434110ed Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Sat, 2 Aug 2025 20:44:09 +0800 Subject: [PATCH] Adapt to old_id --- .../dto/responses/product_responses.go | 3 +++ .../product_application_service_impl.go | 11 ++++++-- .../subscription_application_service_impl.go | 1 + internal/domains/product/entities/product.go | 19 ++++++++++++++ .../product_repository_interface.go | 1 + .../services/product_management_service.go | 26 +++++++++++++++++++ .../product/gorm_product_repository.go | 13 ++++++++++ 7 files changed, 72 insertions(+), 2 deletions(-) diff --git a/internal/application/product/dto/responses/product_responses.go b/internal/application/product/dto/responses/product_responses.go index 963fb2d..47b4702 100644 --- a/internal/application/product/dto/responses/product_responses.go +++ b/internal/application/product/dto/responses/product_responses.go @@ -15,6 +15,7 @@ type PackageItemResponse struct { // ProductInfoResponse 产品详情响应 type ProductInfoResponse struct { ID string `json:"id" comment:"产品ID"` + OldID *string `json:"old_id,omitempty" comment:"旧产品ID"` Name string `json:"name" comment:"产品名称"` Code string `json:"code" comment:"产品编号"` Description string `json:"description" comment:"产品简介"` @@ -59,6 +60,7 @@ type ProductSearchResponse struct { // ProductSimpleResponse 产品简单信息响应 type ProductSimpleResponse struct { ID string `json:"id" comment:"产品ID"` + OldID *string `json:"old_id,omitempty" comment:"旧产品ID"` Name string `json:"name" comment:"产品名称"` Code string `json:"code" comment:"产品编号"` Description string `json:"description" comment:"产品简介"` @@ -79,6 +81,7 @@ type ProductStatsResponse struct { // ProductAdminInfoResponse 管理员产品详情响应 type ProductAdminInfoResponse struct { ID string `json:"id" comment:"产品ID"` + OldID *string `json:"old_id,omitempty" comment:"旧产品ID"` Name string `json:"name" comment:"产品名称"` Code string `json:"code" comment:"产品编号"` Description string `json:"description" comment:"产品简介"` diff --git a/internal/application/product/product_application_service_impl.go b/internal/application/product/product_application_service_impl.go index 4ebccdf..4836a6a 100644 --- a/internal/application/product/product_application_service_impl.go +++ b/internal/application/product/product_application_service_impl.go @@ -436,9 +436,14 @@ func (s *ProductApplicationServiceImpl) GetProductByIDForAdmin(ctx context.Conte // GetProductByIDForUser 根据ID获取产品(用户端专用) // 业务流程:1. 获取产品信息 2. 验证产品可见性 3. 构建用户响应数据 func (s *ProductApplicationServiceImpl) GetProductByIDForUser(ctx context.Context, query *appQueries.GetProductDetailQuery) (*responses.ProductInfoWithDocumentResponse, error) { + // 首先尝试通过新ID查找产品 product, err := s.productManagementService.GetProductWithCategory(ctx, query.ID) if err != nil { - return nil, err + // 如果通过新ID找不到,尝试通过旧ID查找 + product, err = s.productManagementService.GetProductByOldIDWithCategory(ctx, query.ID) + if err != nil { + return nil, err + } } // 用户端只能查看可见的产品 @@ -452,7 +457,7 @@ func (s *ProductApplicationServiceImpl) GetProductByIDForUser(ctx context.Contex // 如果需要包含文档信息 if query.WithDocument != nil && *query.WithDocument { - doc, err := s.documentationAppService.GetDocumentationByProductID(ctx, query.ID) + doc, err := s.documentationAppService.GetDocumentationByProductID(ctx, product.ID) if err == nil && doc != nil { response.Documentation = doc } @@ -465,6 +470,7 @@ func (s *ProductApplicationServiceImpl) GetProductByIDForUser(ctx context.Contex func (s *ProductApplicationServiceImpl) convertToProductInfoResponse(product *entities.Product) *responses.ProductInfoResponse { response := &responses.ProductInfoResponse{ ID: product.ID, + OldID: product.OldID, Name: product.Name, Code: product.Code, Description: product.Description, @@ -507,6 +513,7 @@ func (s *ProductApplicationServiceImpl) convertToProductInfoResponse(product *en func (s *ProductApplicationServiceImpl) convertToProductAdminInfoResponse(product *entities.Product) *responses.ProductAdminInfoResponse { response := &responses.ProductAdminInfoResponse{ ID: product.ID, + OldID: product.OldID, Name: product.Name, Code: product.Code, Description: product.Description, diff --git a/internal/application/product/subscription_application_service_impl.go b/internal/application/product/subscription_application_service_impl.go index 6c35850..a54b9d8 100644 --- a/internal/application/product/subscription_application_service_impl.go +++ b/internal/application/product/subscription_application_service_impl.go @@ -231,6 +231,7 @@ func (s *SubscriptionApplicationServiceImpl) convertToSubscriptionInfoResponse(s func (s *SubscriptionApplicationServiceImpl) convertToProductSimpleResponse(product *entities.Product) *responses.ProductSimpleResponse { return &responses.ProductSimpleResponse{ ID: product.ID, + OldID: product.OldID, Name: product.Name, Code: product.Code, Description: product.Description, diff --git a/internal/domains/product/entities/product.go b/internal/domains/product/entities/product.go index 646f7bd..b6b523b 100644 --- a/internal/domains/product/entities/product.go +++ b/internal/domains/product/entities/product.go @@ -11,6 +11,7 @@ import ( // Product 产品实体 type Product struct { ID string `gorm:"primaryKey;type:varchar(36)" comment:"产品ID"` + OldID *string `gorm:"type:varchar(36);index" comment:"旧产品ID,用于兼容"` Name string `gorm:"type:varchar(100);not null" comment:"产品名称"` Code string `gorm:"type:varchar(50);uniqueIndex;not null" comment:"产品编号"` Description string `gorm:"type:text" comment:"产品简介"` @@ -94,4 +95,22 @@ func (p *Product) SetAsPackage() { func (p *Product) IsCombo() bool { return p.IsPackage +} + +// SetOldID 设置旧ID +func (p *Product) SetOldID(oldID string) { + p.OldID = &oldID +} + +// GetOldID 获取旧ID +func (p *Product) GetOldID() string { + if p.OldID != nil { + return *p.OldID + } + return "" +} + +// 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/repositories/product_repository_interface.go b/internal/domains/product/repositories/product_repository_interface.go index 96c6be8..49f814b 100644 --- a/internal/domains/product/repositories/product_repository_interface.go +++ b/internal/domains/product/repositories/product_repository_interface.go @@ -13,6 +13,7 @@ type ProductRepository interface { // 基础查询方法 FindByCode(ctx context.Context, code string) (*entities.Product, error) + FindByOldID(ctx context.Context, oldID string) (*entities.Product, error) FindByCategoryID(ctx context.Context, categoryID string) ([]*entities.Product, error) FindVisible(ctx context.Context) ([]*entities.Product, error) FindEnabled(ctx context.Context) ([]*entities.Product, error) diff --git a/internal/domains/product/services/product_management_service.go b/internal/domains/product/services/product_management_service.go index fb8bdd8..5254216 100644 --- a/internal/domains/product/services/product_management_service.go +++ b/internal/domains/product/services/product_management_service.go @@ -105,6 +105,32 @@ func (s *ProductManagementService) GetProductWithCategory(ctx context.Context, p return &product, nil } +// GetProductByOldIDWithCategory 根据旧ID获取产品及其分类信息 +func (s *ProductManagementService) GetProductByOldIDWithCategory(ctx context.Context, oldID string) (*entities.Product, error) { + product, err := s.productRepo.FindByOldID(ctx, oldID) + 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 + } + } + + // 如果是组合包,加载子产品信息 + if product.IsPackage { + packageItems, err := s.productRepo.GetPackageItems(ctx, product.ID) + 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) diff --git a/internal/infrastructure/database/repositories/product/gorm_product_repository.go b/internal/infrastructure/database/repositories/product/gorm_product_repository.go index 9db40b7..13bed3f 100644 --- a/internal/infrastructure/database/repositories/product/gorm_product_repository.go +++ b/internal/infrastructure/database/repositories/product/gorm_product_repository.go @@ -66,6 +66,19 @@ func (r *GormProductRepository) FindByCode(ctx context.Context, code string) (*e return &entity, nil } +// FindByOldID 根据旧ID查找产品 +func (r *GormProductRepository) FindByOldID(ctx context.Context, oldID string) (*entities.Product, error) { + var entity entities.Product + err := r.GetDB(ctx).Where("old_id = ?", oldID).First(&entity).Error + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, gorm.ErrRecordNotFound + } + return nil, err + } + return &entity, nil +} + // FindByCategoryID 根据分类ID查找产品 func (r *GormProductRepository) FindByCategoryID(ctx context.Context, categoryID string) ([]*entities.Product, error) { var productEntities []entities.Product