This commit is contained in:
zpc 2026-01-06 00:50:43 +08:00
parent e443805972
commit 77b55e40d7
6 changed files with 110 additions and 25 deletions

View File

@ -146,7 +146,13 @@ namespace WorkCameraExport.Forms
public void NavigateToWorkRecord() public void NavigateToWorkRecord()
{ {
_logService?.Info("导航到工作记录管理界面"); _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); form.ShowDialog(this);
} }

View File

@ -795,7 +795,7 @@ namespace WorkCameraExport.Forms
/// </summary> /// </summary>
public class ImageItem public class ImageItem
{ {
public int? Id { get; set; } public long? Id { get; set; }
public string? Url { get; set; } public string? Url { get; set; }
public string? LocalPath { get; set; } public string? LocalPath { get; set; }
public bool IsExisting { get; set; } public bool IsExisting { get; set; }

View File

@ -727,43 +727,60 @@ namespace WorkCameraExport.Forms
try try
{ {
_logService?.Info($"[WorkRecordForm] 开始执行导出,输出路径: {outputPath}");
_logService?.Info($"[WorkRecordForm] ExportService 是否为空: {_exportService == null}");
var progress = new Progress<ExportProgress>(UpdateExportProgress); var progress = new Progress<ExportProgress>(UpdateExportProgress);
if (_exportService != null) if (_exportService != null)
{ {
if (selectedIds != null) if (selectedIds != null)
{ {
_logService?.Info($"[WorkRecordForm] 导出选中记录,共 {selectedIds.Count} 条");
await _exportService.ExportWorkRecordsByIdsAsync( await _exportService.ExportWorkRecordsByIdsAsync(
selectedIds, outputPath, progress, _exportCts.Token); selectedIds, outputPath, progress, _exportCts.Token);
} }
else else
{ {
var query = BuildQuery(); var query = BuildQuery();
_logService?.Info($"[WorkRecordForm] 导出全部记录,查询条件: PageNum={query.PageNum}, PageSize={query.PageSize}");
await _exportService.ExportWorkRecordsAsync( await _exportService.ExportWorkRecordsAsync(
query, outputPath, progress, _exportCts.Token); query, outputPath, progress, _exportCts.Token);
} }
} }
else else
{ {
_logService?.Warn("[WorkRecordForm] ExportService 为空,使用简化导出逻辑");
// 如果没有注入 ExportService使用简化的导出逻辑 // 如果没有注入 ExportService使用简化的导出逻辑
await DoSimpleExportAsync(outputPath, selectedIds, progress, _exportCts.Token); await DoSimpleExportAsync(outputPath, selectedIds, progress, _exportCts.Token);
} }
if (!_exportCts.Token.IsCancellationRequested) if (!_exportCts.Token.IsCancellationRequested)
{ {
lblStatusBar.Text = "导出完成"; // 验证文件是否存在
lblStatusBar.ForeColor = Color.Green; if (File.Exists(outputPath))
_logService?.Info($"导出完成: {outputPath}");
var result = MessageBox.Show(
$"导出完成!\n文件已保存到{outputPath}\n\n是否打开文件所在目录",
"提示",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{ {
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.Text = "导出已取消";
lblStatusBar.ForeColor = Color.Orange; lblStatusBar.ForeColor = Color.Orange;
_logService?.Info("导出已取消"); _logService?.Info("[WorkRecordForm] 导出已取消");
} }
catch (Exception ex) catch (Exception ex)
{ {
ShowError($"导出失败: {ex.Message}"); ShowError($"导出失败: {ex.Message}");
_logService?.Error($"导出失败: {ex.Message}", ex); _logService?.Error($"[WorkRecordForm] 导出失败: {ex.Message}", ex);
} }
finally finally
{ {

View File

@ -35,13 +35,20 @@ namespace WorkCameraExport.Models
} }
/// <summary> /// <summary>
/// 图片 DTO /// 图片 DTO - 匹配服务端 CamWorkrecordImageDto
/// </summary> /// </summary>
public class ImageDto public class ImageDto
{ {
public int Id { get; set; } public long Id { get; set; }
public string Url { get; set; } = ""; public int WorkrecordId { get; set; }
public string ImageUrl { get; set; } = "";
public int SortOrder { get; set; } public int SortOrder { get; set; }
public DateTime? CreateTime { get; set; }
/// <summary>
/// 兼容属性,返回 ImageUrl
/// </summary>
public string Url => ImageUrl;
} }
/// <summary> /// <summary>

View File

@ -39,6 +39,16 @@ namespace WorkCameraExport.Services
{ {
try 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(); using var package = new ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("工作记录"); var worksheet = package.Workbook.Worksheets.Add("工作记录");
@ -59,18 +69,31 @@ namespace WorkCameraExport.Services
// 保存文件 // 保存文件
var fileInfo = new FileInfo(outputPath); var fileInfo = new FileInfo(outputPath);
_logService?.Info($"[Excel] 准备保存文件: {fileInfo.FullName}");
await package.SaveAsAsync(fileInfo, cancellationToken); 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) catch (OperationCanceledException)
{ {
_logService?.Info("Excel 导出已取消"); _logService?.Info("[Excel] Excel 导出已取消");
throw; throw;
} }
catch (Exception ex) catch (Exception ex)
{ {
_logService?.Error($"Excel 导出失败: {ex.Message}", ex); _logService?.Error($"[Excel] Excel 导出失败: {ex.Message}", ex);
throw; throw;
} }
} }

View File

@ -47,7 +47,16 @@ namespace WorkCameraExport.Services
var tempDir = CreateTempDirectory(); var tempDir = CreateTempDirectory();
try 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(); var config = _configService.LoadConfig();
@ -55,10 +64,13 @@ namespace WorkCameraExport.Services
var imageQuality = config?.ImageCompressQuality ?? DefaultImageQuality; var imageQuality = config?.ImageCompressQuality ?? DefaultImageQuality;
// 1. 分页获取所有数据 // 1. 分页获取所有数据
_logService?.Info("[导出] 开始获取数据...");
var allRecords = await FetchAllRecordsAsync(query, progress, cancellationToken); var allRecords = await FetchAllRecordsAsync(query, progress, cancellationToken);
_logService?.Info($"[导出] 获取到 {allRecords.Count} 条记录");
if (allRecords.Count == 0) if (allRecords.Count == 0)
{ {
_logService?.Warn("没有找到符合条件的工作记录"); _logService?.Warn("[导出] 没有找到符合条件的工作记录");
throw new InvalidOperationException("没有找到符合条件的工作记录"); throw new InvalidOperationException("没有找到符合条件的工作记录");
} }
@ -68,6 +80,7 @@ namespace WorkCameraExport.Services
.Where(url => !string.IsNullOrEmpty(url)) .Where(url => !string.IsNullOrEmpty(url))
.Distinct() .Distinct()
.ToList(); .ToList();
_logService?.Info($"[导出] 共 {allImageUrls.Count} 张图片需要下载");
ReportProgress(progress, allRecords.Count, 0, allImageUrls.Count, 0, "正在下载图片..."); ReportProgress(progress, allRecords.Count, 0, allImageUrls.Count, 0, "正在下载图片...");
@ -76,17 +89,36 @@ namespace WorkCameraExport.Services
allImageUrls, tempDir, concurrency, imageQuality, allImageUrls, tempDir, concurrency, imageQuality,
downloaded => ReportProgress(progress, allRecords.Count, 0, allImageUrls.Count, downloaded, "正在下载图片..."), downloaded => ReportProgress(progress, allRecords.Count, 0, allImageUrls.Count, downloaded, "正在下载图片..."),
cancellationToken); cancellationToken);
_logService?.Info($"[导出] 图片下载完成,成功 {imagePathMap.Count(x => x.Value != null)} 张");
// 4. 构建导出数据 // 4. 构建导出数据
var exportItems = BuildExportItems(allRecords, imagePathMap); var exportItems = BuildExportItems(allRecords, imagePathMap);
_logService?.Info($"[导出] 构建导出数据完成,共 {exportItems.Count} 条");
ReportProgress(progress, allRecords.Count, allRecords.Count, allImageUrls.Count, allImageUrls.Count, "正在生成 Excel..."); ReportProgress(progress, allRecords.Count, allRecords.Count, allImageUrls.Count, allImageUrls.Count, "正在生成 Excel...");
// 5. 生成 Excel // 5. 生成 Excel
_logService?.Info($"[导出] 开始生成 Excel: {outputPath}");
await _excelService.ExportWorkRecordsToExcelAsync(exportItems, outputPath, cancellationToken); 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, "导出完成"); 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 finally
{ {