Adapt to old_id

This commit is contained in:
2025-08-02 20:44:09 +08:00
parent 719cd14269
commit 14c3a23752
7 changed files with 72 additions and 2 deletions

View File

@@ -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:"产品简介"`

View File

@@ -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,

View File

@@ -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,

View File

@@ -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:"产品简介"`
@@ -95,3 +96,21 @@ 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 != ""
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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