From 34e2c1bc4132b85d9bd2a97d9deb75dabb860317 Mon Sep 17 00:00:00 2001 From: 18278715334 <18278715334@163.com> Date: Tue, 23 Dec 2025 17:17:41 +0800 Subject: [PATCH] fixdele --- .../ui_component_application_service.go | 37 ++++++++- .../product/ui_component_file_service.go | 53 ++++++++++++- .../product/gorm_ui_component_repository.go | 3 +- .../http/handlers/ui_component_handler.go | 3 +- test_delete_component.go | 78 +++++++++++++++++++ 5 files changed, 165 insertions(+), 9 deletions(-) create mode 100644 test_delete_component.go diff --git a/internal/application/product/ui_component_application_service.go b/internal/application/product/ui_component_application_service.go index 8925cc1..f3203a1 100644 --- a/internal/application/product/ui_component_application_service.go +++ b/internal/application/product/ui_component_application_service.go @@ -459,27 +459,56 @@ func (s *UIComponentApplicationServiceImpl) UpdateUIComponent(ctx context.Contex // DeleteUIComponent 删除UI组件 func (s *UIComponentApplicationServiceImpl) DeleteUIComponent(ctx context.Context, id string) error { + // 获取组件信息 component, err := s.uiComponentRepo.GetByID(ctx, id) if err != nil { - return err + s.logger.Error("获取UI组件失败", zap.Error(err), zap.String("id", id)) + return fmt.Errorf("获取UI组件失败: %w", err) } if component == nil { + s.logger.Warn("UI组件不存在", zap.String("id", id)) return ErrComponentNotFound } + // 记录组件信息 + s.logger.Info("开始删除UI组件", + zap.String("id", id), + zap.String("componentCode", component.ComponentCode), + zap.String("componentName", component.ComponentName), + zap.Bool("isExtracted", component.IsExtracted), + zap.Any("filePath", component.FilePath), + zap.Any("folderPath", component.FolderPath)) + // 使用智能删除方法,根据组件编码和上传时间删除相关文件 if err := s.fileService.DeleteFilesByComponentCode(component.ComponentCode, component.FileUploadTime); err != nil { // 记录错误但不阻止删除数据库记录 - s.logger.Error("删除组件文件失败", zap.Error(err), zap.String("componentCode", component.ComponentCode)) + s.logger.Error("删除组件文件失败", + zap.Error(err), + zap.String("componentCode", component.ComponentCode), + zap.Any("fileUploadTime", component.FileUploadTime)) } // 删除关联的文件(FilePath指向的文件) if component.FilePath != nil { - _ = s.fileStorageService.DeleteFile(ctx, *component.FilePath) + if err := s.fileStorageService.DeleteFile(ctx, *component.FilePath); err != nil { + s.logger.Error("删除文件失败", + zap.Error(err), + zap.String("filePath", *component.FilePath)) + } } // 删除数据库记录 - return s.uiComponentRepo.Delete(ctx, id) + if err := s.uiComponentRepo.Delete(ctx, id); err != nil { + s.logger.Error("删除UI组件数据库记录失败", + zap.Error(err), + zap.String("id", id)) + return fmt.Errorf("删除UI组件数据库记录失败: %w", err) + } + + s.logger.Info("UI组件删除成功", + zap.String("id", id), + zap.String("componentCode", component.ComponentCode)) + return nil } // ListUIComponents 获取UI组件列表 diff --git a/internal/application/product/ui_component_file_service.go b/internal/application/product/ui_component_file_service.go index 9dd61db..1e8af4c 100644 --- a/internal/application/product/ui_component_file_service.go +++ b/internal/application/product/ui_component_file_service.go @@ -225,11 +225,34 @@ func (s *UIComponentFileServiceImpl) CreateFolderByCode(componentCode string) (s // DeleteFolder 删除组件文件夹 func (s *UIComponentFileServiceImpl) DeleteFolder(folderPath string) error { + // 记录尝试删除的文件夹路径 + s.logger.Info("尝试删除文件夹", zap.String("folderPath", folderPath)) + + // 获取文件夹信息,用于调试 + if info, err := os.Stat(folderPath); err == nil { + s.logger.Info("文件夹信息", + zap.String("folderPath", folderPath), + zap.Bool("isDir", info.IsDir()), + zap.Int64("size", info.Size()), + zap.Time("modTime", info.ModTime())) + } else { + s.logger.Error("获取文件夹信息失败", + zap.Error(err), + zap.String("folderPath", folderPath)) + } + + // 检查文件夹是否存在 if !s.FolderExists(folderPath) { + s.logger.Info("文件夹不存在", zap.String("folderPath", folderPath)) return nil // 文件夹不存在,不视为错误 } + // 尝试删除文件夹 + s.logger.Info("开始删除文件夹", zap.String("folderPath", folderPath)) if err := os.RemoveAll(folderPath); err != nil { + s.logger.Error("删除文件夹失败", + zap.Error(err), + zap.String("folderPath", folderPath)) return fmt.Errorf("删除文件夹失败: %w", err) } @@ -345,28 +368,52 @@ func (s *UIComponentFileServiceImpl) extractZipFile(zipPath, destPath string) er // DeleteFilesByComponentCode 根据组件编码和上传时间智能删除组件相关文件 func (s *UIComponentFileServiceImpl) DeleteFilesByComponentCode(componentCode string, uploadTime *time.Time) error { + // 记录基础路径和组件编码 + s.logger.Info("开始删除组件文件", + zap.String("basePath", s.basePath), + zap.String("componentCode", componentCode), + zap.Any("uploadTime", uploadTime)) + // 1. 查找名为组件编码的文件夹 componentDir := filepath.Join(s.basePath, componentCode) + s.logger.Info("检查组件文件夹", zap.String("componentDir", componentDir)) + if s.FolderExists(componentDir) { + s.logger.Info("找到组件文件夹,开始删除", zap.String("componentDir", componentDir)) if err := s.DeleteFolder(componentDir); err != nil { - s.logger.Error("删除组件文件夹失败", zap.Error(err), zap.String("componentCode", componentCode)) + s.logger.Error("删除组件文件夹失败", + zap.Error(err), + zap.String("componentCode", componentCode), + zap.String("componentDir", componentDir)) return fmt.Errorf("删除组件文件夹失败: %w", err) } s.logger.Info("成功删除组件文件夹", zap.String("componentCode", componentCode)) return nil + } else { + s.logger.Info("组件文件夹不存在", zap.String("componentDir", componentDir)) } // 2. 查找文件名包含组件编码的文件 - files, err := filepath.Glob(filepath.Join(s.basePath, "*"+componentCode+"*")) + pattern := filepath.Join(s.basePath, "*"+componentCode+"*") + s.logger.Info("查找匹配文件", zap.String("pattern", pattern)) + + files, err := filepath.Glob(pattern) if err != nil { + s.logger.Error("查找组件文件失败", + zap.Error(err), + zap.String("pattern", pattern)) return fmt.Errorf("查找组件文件失败: %w", err) } + + s.logger.Info("找到匹配文件", + zap.Strings("files", files), + zap.Int("count", len(files))) // 3. 如果没有上传时间,删除所有匹配的文件 if uploadTime == nil { for _, file := range files { if err := os.Remove(file); err != nil { - s.logger.Warn("删除文件失败", zap.String("file", file), zap.Error(err)) + s.logger.Error("删除文件失败", zap.String("file", file), zap.Error(err)) } else { s.logger.Info("成功删除文件", zap.String("file", file)) } diff --git a/internal/infrastructure/database/repositories/product/gorm_ui_component_repository.go b/internal/infrastructure/database/repositories/product/gorm_ui_component_repository.go index c383477..2b60ca8 100644 --- a/internal/infrastructure/database/repositories/product/gorm_ui_component_repository.go +++ b/internal/infrastructure/database/repositories/product/gorm_ui_component_repository.go @@ -108,7 +108,8 @@ func (r *GormUIComponentRepository) Update(ctx context.Context, component entiti // Delete 删除UI组件 func (r *GormUIComponentRepository) Delete(ctx context.Context, id string) error { - if err := r.db.WithContext(ctx).Delete(&entities.UIComponent{}, id).Error; err != nil { + // 记录删除操作的详细信息 + if err := r.db.WithContext(ctx).Where("id = ?", id).Delete(&entities.UIComponent{}).Error; err != nil { return fmt.Errorf("删除UI组件失败: %w", err) } return nil diff --git a/internal/infrastructure/http/handlers/ui_component_handler.go b/internal/infrastructure/http/handlers/ui_component_handler.go index c43a71b..af970cf 100644 --- a/internal/infrastructure/http/handlers/ui_component_handler.go +++ b/internal/infrastructure/http/handlers/ui_component_handler.go @@ -282,7 +282,8 @@ func (h *UIComponentHandler) DeleteUIComponent(c *gin.Context) { h.responseBuilder.NotFound(c, "UI组件不存在") return } - h.responseBuilder.InternalError(c, "删除UI组件失败") + // 提供更详细的错误信息 + h.responseBuilder.InternalError(c, fmt.Sprintf("删除UI组件失败: %v", err)) return } diff --git a/test_delete_component.go b/test_delete_component.go new file mode 100644 index 0000000..4bd6400 --- /dev/null +++ b/test_delete_component.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" +) + +func main() { + // 测试文件路径 + basePath := "resources/Pure_Component/src/ui" + + // 检查基础路径是否存在 + if _, err := os.Stat(basePath); os.IsNotExist(err) { + fmt.Printf("基础路径不存在: %s\n", basePath) + return + } + + // 获取基础路径信息 + info, err := os.Stat(basePath) + if err != nil { + fmt.Printf("获取基础路径信息失败: %v\n", err) + return + } + + fmt.Printf("基础路径信息:\n") + fmt.Printf(" 路径: %s\n", basePath) + fmt.Printf(" 是否为目录: %t\n", info.IsDir()) + fmt.Printf(" 大小: %d 字节\n", info.Size()) + fmt.Printf(" 修改时间: %s\n", info.ModTime()) + + // 检查权限 + if info.IsDir() { + // 尝试创建测试文件 + testFile := filepath.Join(basePath, "test_delete.txt") + if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil { + fmt.Printf("无法在基础路径中创建文件,可能是权限问题: %v\n", err) + } else { + fmt.Printf("成功创建测试文件: %s\n", testFile) + + // 尝试删除测试文件 + if err := os.Remove(testFile); err != nil { + fmt.Printf("无法删除测试文件,可能是权限问题: %v\n", err) + } else { + fmt.Printf("成功删除测试文件: %s\n", testFile) + } + } + } + + // 列出基础路径下的所有文件和文件夹 + fmt.Printf("\n基础路径下的内容:\n") + err = filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + relPath, err := filepath.Rel(basePath, path) + if err != nil { + return err + } + + if relPath == "." { + return nil + } + + if info.IsDir() { + fmt.Printf(" [目录] %s\n", relPath) + } else { + fmt.Printf(" [文件] %s (大小: %d 字节)\n", relPath, info.Size()) + } + + return nil + }) + + if err != nil { + fmt.Printf("遍历目录失败: %v\n", err) + } +}