117 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package services
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"errors"
 | |
| 	"fmt"
 | |
| 
 | |
| 	"tyapi-server/internal/domains/product/entities"
 | |
| 	"tyapi-server/internal/domains/product/repositories"
 | |
| )
 | |
| 
 | |
| // ProductDocumentationService 产品文档服务
 | |
| type ProductDocumentationService struct {
 | |
| 	docRepo     repositories.ProductDocumentationRepository
 | |
| 	productRepo repositories.ProductRepository
 | |
| }
 | |
| 
 | |
| // NewProductDocumentationService 创建文档服务实例
 | |
| func NewProductDocumentationService(
 | |
| 	docRepo repositories.ProductDocumentationRepository,
 | |
| 	productRepo repositories.ProductRepository,
 | |
| ) *ProductDocumentationService {
 | |
| 	return &ProductDocumentationService{
 | |
| 		docRepo:     docRepo,
 | |
| 		productRepo: productRepo,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // CreateDocumentation 创建文档
 | |
| func (s *ProductDocumentationService) CreateDocumentation(ctx context.Context, productID string, doc *entities.ProductDocumentation) error {
 | |
| 	// 验证产品是否存在
 | |
| 	product, err := s.productRepo.GetByID(ctx, productID)
 | |
| 	if err != nil {
 | |
| 		return fmt.Errorf("产品不存在: %w", err)
 | |
| 	}
 | |
| 	if !product.IsValid() {
 | |
| 		return errors.New("产品已禁用或删除")
 | |
| 	}
 | |
| 
 | |
| 	// 检查是否已存在文档
 | |
| 	existingDoc, err := s.docRepo.FindByProductID(ctx, productID)
 | |
| 	if err == nil && existingDoc != nil {
 | |
| 		return errors.New("该产品已存在文档")
 | |
| 	}
 | |
| 
 | |
| 	// 设置产品ID
 | |
| 	doc.ProductID = productID
 | |
| 
 | |
| 	// 验证文档完整性
 | |
| 	if err := doc.Validate(); err != nil {
 | |
| 		return fmt.Errorf("文档验证失败: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	// 创建文档
 | |
| 	return s.docRepo.Create(ctx, doc)
 | |
| }
 | |
| 
 | |
| // UpdateDocumentation 更新文档
 | |
| func (s *ProductDocumentationService) UpdateDocumentation(ctx context.Context, id string, requestURL, requestMethod, basicInfo, requestParams, responseFields, responseExample, errorCodes string) error {
 | |
| 	// 查找现有文档
 | |
| 	doc, err := s.docRepo.FindByID(ctx, id)
 | |
| 	if err != nil {
 | |
| 		return fmt.Errorf("文档不存在: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	// 使用实体的更新方法
 | |
| 	err = doc.UpdateDocumentation(requestURL, requestMethod, basicInfo, requestParams, responseFields, responseExample, errorCodes)
 | |
| 	if err != nil {
 | |
| 		return fmt.Errorf("文档更新失败: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	// 保存更新
 | |
| 	return s.docRepo.Update(ctx, doc)
 | |
| }
 | |
| 
 | |
| // GetDocumentation 获取文档
 | |
| func (s *ProductDocumentationService) GetDocumentation(ctx context.Context, id string) (*entities.ProductDocumentation, error) {
 | |
| 	return s.docRepo.FindByID(ctx, id)
 | |
| }
 | |
| 
 | |
| // GetDocumentationByProductID 通过产品ID获取文档
 | |
| func (s *ProductDocumentationService) GetDocumentationByProductID(ctx context.Context, productID string) (*entities.ProductDocumentation, error) {
 | |
| 	return s.docRepo.FindByProductID(ctx, productID)
 | |
| }
 | |
| 
 | |
| // DeleteDocumentation 删除文档
 | |
| func (s *ProductDocumentationService) DeleteDocumentation(ctx context.Context, id string) error {
 | |
| 	_, err := s.docRepo.FindByID(ctx, id)
 | |
| 	if err != nil {
 | |
| 		return fmt.Errorf("文档不存在: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	return s.docRepo.Delete(ctx, id)
 | |
| }
 | |
| 
 | |
| // GetDocumentationWithProduct 获取文档及其关联的产品信息
 | |
| func (s *ProductDocumentationService) GetDocumentationWithProduct(ctx context.Context, id string) (*entities.ProductDocumentation, error) {
 | |
| 	doc, err := s.docRepo.FindByID(ctx, id)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	// 加载产品信息
 | |
| 	product, err := s.productRepo.GetByID(ctx, doc.ProductID)
 | |
| 	if err != nil {
 | |
| 		return nil, fmt.Errorf("获取产品信息失败: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	doc.Product = &product
 | |
| 	return doc, nil
 | |
| }
 | |
| 
 | |
| // GetDocumentationsByProductIDs 批量获取文档
 | |
| func (s *ProductDocumentationService) GetDocumentationsByProductIDs(ctx context.Context, productIDs []string) ([]*entities.ProductDocumentation, error) {
 | |
| 	return s.docRepo.FindByProductIDs(ctx, productIDs)
 | |
| }
 |