From 77b55e40d7d2292bd0cb91064fc81a527f14d723 Mon Sep 17 00:00:00 2001 From: zpc Date: Tue, 6 Jan 2026 00:50:43 +0800 Subject: [PATCH] 333 --- client/WorkCameraExport/Forms/HomeForm.cs | 8 +++- .../Forms/WorkRecordEditForm.cs | 2 +- .../WorkCameraExport/Forms/WorkRecordForm.cs | 45 +++++++++++++------ .../Models/WorkRecordModels.cs | 13 ++++-- .../WorkCameraExport/Services/ExcelService.cs | 29 ++++++++++-- .../Services/ExportService.cs | 38 ++++++++++++++-- 6 files changed, 110 insertions(+), 25 deletions(-) diff --git a/client/WorkCameraExport/Forms/HomeForm.cs b/client/WorkCameraExport/Forms/HomeForm.cs index ae4209b..15a8626 100644 --- a/client/WorkCameraExport/Forms/HomeForm.cs +++ b/client/WorkCameraExport/Forms/HomeForm.cs @@ -146,7 +146,13 @@ namespace WorkCameraExport.Forms public void NavigateToWorkRecord() { _logService?.Info("导航到工作记录管理界面"); - using var form = new WorkRecordForm(_apiService, _logService); + + // 创建导出服务 + var configService = new ConfigService(); + var imageService = new ImageService(_logService); + var exportService = new ExportService(_apiService, imageService, configService, _logService); + + using var form = new WorkRecordForm(_apiService, _logService, exportService); form.ShowDialog(this); } diff --git a/client/WorkCameraExport/Forms/WorkRecordEditForm.cs b/client/WorkCameraExport/Forms/WorkRecordEditForm.cs index 4676036..de99a4e 100644 --- a/client/WorkCameraExport/Forms/WorkRecordEditForm.cs +++ b/client/WorkCameraExport/Forms/WorkRecordEditForm.cs @@ -795,7 +795,7 @@ namespace WorkCameraExport.Forms /// public class ImageItem { - public int? Id { get; set; } + public long? Id { get; set; } public string? Url { get; set; } public string? LocalPath { get; set; } public bool IsExisting { get; set; } diff --git a/client/WorkCameraExport/Forms/WorkRecordForm.cs b/client/WorkCameraExport/Forms/WorkRecordForm.cs index 7d016af..68f93a9 100644 --- a/client/WorkCameraExport/Forms/WorkRecordForm.cs +++ b/client/WorkCameraExport/Forms/WorkRecordForm.cs @@ -727,43 +727,60 @@ namespace WorkCameraExport.Forms try { + _logService?.Info($"[WorkRecordForm] 开始执行导出,输出路径: {outputPath}"); + _logService?.Info($"[WorkRecordForm] ExportService 是否为空: {_exportService == null}"); + var progress = new Progress(UpdateExportProgress); if (_exportService != null) { if (selectedIds != null) { + _logService?.Info($"[WorkRecordForm] 导出选中记录,共 {selectedIds.Count} 条"); await _exportService.ExportWorkRecordsByIdsAsync( selectedIds, outputPath, progress, _exportCts.Token); } else { var query = BuildQuery(); + _logService?.Info($"[WorkRecordForm] 导出全部记录,查询条件: PageNum={query.PageNum}, PageSize={query.PageSize}"); await _exportService.ExportWorkRecordsAsync( query, outputPath, progress, _exportCts.Token); } } else { + _logService?.Warn("[WorkRecordForm] ExportService 为空,使用简化导出逻辑"); // 如果没有注入 ExportService,使用简化的导出逻辑 await DoSimpleExportAsync(outputPath, selectedIds, progress, _exportCts.Token); } if (!_exportCts.Token.IsCancellationRequested) { - lblStatusBar.Text = "导出完成"; - lblStatusBar.ForeColor = Color.Green; - _logService?.Info($"导出完成: {outputPath}"); - - var result = MessageBox.Show( - $"导出完成!\n文件已保存到:{outputPath}\n\n是否打开文件所在目录?", - "提示", - MessageBoxButtons.YesNo, - MessageBoxIcon.Question); - - if (result == DialogResult.Yes) + // 验证文件是否存在 + if (File.Exists(outputPath)) { - System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{outputPath}\""); + var fileInfo = new FileInfo(outputPath); + _logService?.Info($"[WorkRecordForm] 导出完成,文件存在: {outputPath}, 大小: {fileInfo.Length} 字节"); + + lblStatusBar.Text = "导出完成"; + lblStatusBar.ForeColor = Color.Green; + + var result = MessageBox.Show( + $"导出完成!\n文件已保存到:{outputPath}\n\n是否打开文件所在目录?", + "提示", + MessageBoxButtons.YesNo, + MessageBoxIcon.Question); + + if (result == DialogResult.Yes) + { + System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{outputPath}\""); + } + } + else + { + _logService?.Error($"[WorkRecordForm] 导出完成但文件不存在: {outputPath}"); + ShowError("导出失败:文件未生成"); } } } @@ -771,12 +788,12 @@ namespace WorkCameraExport.Forms { lblStatusBar.Text = "导出已取消"; lblStatusBar.ForeColor = Color.Orange; - _logService?.Info("导出已取消"); + _logService?.Info("[WorkRecordForm] 导出已取消"); } catch (Exception ex) { ShowError($"导出失败: {ex.Message}"); - _logService?.Error($"导出失败: {ex.Message}", ex); + _logService?.Error($"[WorkRecordForm] 导出失败: {ex.Message}", ex); } finally { diff --git a/client/WorkCameraExport/Models/WorkRecordModels.cs b/client/WorkCameraExport/Models/WorkRecordModels.cs index 80428a1..8ca9d18 100644 --- a/client/WorkCameraExport/Models/WorkRecordModels.cs +++ b/client/WorkCameraExport/Models/WorkRecordModels.cs @@ -35,13 +35,20 @@ namespace WorkCameraExport.Models } /// - /// 图片 DTO + /// 图片 DTO - 匹配服务端 CamWorkrecordImageDto /// public class ImageDto { - public int Id { get; set; } - public string Url { get; set; } = ""; + public long Id { get; set; } + public int WorkrecordId { get; set; } + public string ImageUrl { get; set; } = ""; public int SortOrder { get; set; } + public DateTime? CreateTime { get; set; } + + /// + /// 兼容属性,返回 ImageUrl + /// + public string Url => ImageUrl; } /// diff --git a/client/WorkCameraExport/Services/ExcelService.cs b/client/WorkCameraExport/Services/ExcelService.cs index d6f84d2..6a0a861 100644 --- a/client/WorkCameraExport/Services/ExcelService.cs +++ b/client/WorkCameraExport/Services/ExcelService.cs @@ -39,6 +39,16 @@ namespace WorkCameraExport.Services { try { + _logService?.Info($"[Excel] 开始生成 Excel,共 {records.Count} 条记录"); + + // 确保输出目录存在 + var outputDir = Path.GetDirectoryName(outputPath); + if (!string.IsNullOrEmpty(outputDir) && !Directory.Exists(outputDir)) + { + Directory.CreateDirectory(outputDir); + _logService?.Info($"[Excel] 创建输出目录: {outputDir}"); + } + using var package = new ExcelPackage(); var worksheet = package.Workbook.Worksheets.Add("工作记录"); @@ -59,18 +69,31 @@ namespace WorkCameraExport.Services // 保存文件 var fileInfo = new FileInfo(outputPath); + _logService?.Info($"[Excel] 准备保存文件: {fileInfo.FullName}"); + await package.SaveAsAsync(fileInfo, cancellationToken); - _logService?.Info($"Excel 导出成功: {outputPath}, 共 {records.Count} 条记录"); + // 验证文件 + if (fileInfo.Exists) + { + fileInfo.Refresh(); + _logService?.Info($"[Excel] 文件保存成功: {fileInfo.FullName}, 大小: {fileInfo.Length} 字节"); + } + else + { + _logService?.Error($"[Excel] 文件保存后不存在: {fileInfo.FullName}"); + } + + _logService?.Info($"[Excel] Excel 导出成功: {outputPath}, 共 {records.Count} 条记录"); } catch (OperationCanceledException) { - _logService?.Info("Excel 导出已取消"); + _logService?.Info("[Excel] Excel 导出已取消"); throw; } catch (Exception ex) { - _logService?.Error($"Excel 导出失败: {ex.Message}", ex); + _logService?.Error($"[Excel] Excel 导出失败: {ex.Message}", ex); throw; } } diff --git a/client/WorkCameraExport/Services/ExportService.cs b/client/WorkCameraExport/Services/ExportService.cs index 083c953..8a8a4a2 100644 --- a/client/WorkCameraExport/Services/ExportService.cs +++ b/client/WorkCameraExport/Services/ExportService.cs @@ -47,7 +47,16 @@ namespace WorkCameraExport.Services var tempDir = CreateTempDirectory(); try { - _logService?.Info($"开始导出工作记录: {outputPath}"); + _logService?.Info($"[导出] 开始导出工作记录: {outputPath}"); + _logService?.Info($"[导出] 临时目录: {tempDir}"); + + // 确保输出目录存在 + var outputDir = Path.GetDirectoryName(outputPath); + if (!string.IsNullOrEmpty(outputDir) && !Directory.Exists(outputDir)) + { + Directory.CreateDirectory(outputDir); + _logService?.Info($"[导出] 创建输出目录: {outputDir}"); + } // 获取配置 var config = _configService.LoadConfig(); @@ -55,10 +64,13 @@ namespace WorkCameraExport.Services var imageQuality = config?.ImageCompressQuality ?? DefaultImageQuality; // 1. 分页获取所有数据 + _logService?.Info("[导出] 开始获取数据..."); var allRecords = await FetchAllRecordsAsync(query, progress, cancellationToken); + _logService?.Info($"[导出] 获取到 {allRecords.Count} 条记录"); + if (allRecords.Count == 0) { - _logService?.Warn("没有找到符合条件的工作记录"); + _logService?.Warn("[导出] 没有找到符合条件的工作记录"); throw new InvalidOperationException("没有找到符合条件的工作记录"); } @@ -68,6 +80,7 @@ namespace WorkCameraExport.Services .Where(url => !string.IsNullOrEmpty(url)) .Distinct() .ToList(); + _logService?.Info($"[导出] 共 {allImageUrls.Count} 张图片需要下载"); ReportProgress(progress, allRecords.Count, 0, allImageUrls.Count, 0, "正在下载图片..."); @@ -76,17 +89,36 @@ namespace WorkCameraExport.Services allImageUrls, tempDir, concurrency, imageQuality, downloaded => ReportProgress(progress, allRecords.Count, 0, allImageUrls.Count, downloaded, "正在下载图片..."), cancellationToken); + _logService?.Info($"[导出] 图片下载完成,成功 {imagePathMap.Count(x => x.Value != null)} 张"); // 4. 构建导出数据 var exportItems = BuildExportItems(allRecords, imagePathMap); + _logService?.Info($"[导出] 构建导出数据完成,共 {exportItems.Count} 条"); ReportProgress(progress, allRecords.Count, allRecords.Count, allImageUrls.Count, allImageUrls.Count, "正在生成 Excel..."); // 5. 生成 Excel + _logService?.Info($"[导出] 开始生成 Excel: {outputPath}"); await _excelService.ExportWorkRecordsToExcelAsync(exportItems, outputPath, cancellationToken); + // 验证文件是否生成 + if (File.Exists(outputPath)) + { + var fileInfo = new FileInfo(outputPath); + _logService?.Info($"[导出] Excel 文件已生成: {outputPath}, 大小: {fileInfo.Length} 字节"); + } + else + { + _logService?.Error($"[导出] Excel 文件未生成: {outputPath}"); + } + ReportProgress(progress, allRecords.Count, allRecords.Count, allImageUrls.Count, allImageUrls.Count, "导出完成"); - _logService?.Info($"工作记录导出完成: {allRecords.Count} 条记录, {allImageUrls.Count} 张图片"); + _logService?.Info($"[导出] 工作记录导出完成: {allRecords.Count} 条记录, {allImageUrls.Count} 张图片"); + } + catch (Exception ex) + { + _logService?.Error($"[导出] 导出异常: {ex.Message}", ex); + throw; } finally {