diff --git a/ZR.Admin.WebApi/Controllers/CommonController.cs b/ZR.Admin.WebApi/Controllers/CommonController.cs index af9982b..5cd07a4 100644 --- a/ZR.Admin.WebApi/Controllers/CommonController.cs +++ b/ZR.Admin.WebApi/Controllers/CommonController.cs @@ -463,76 +463,83 @@ namespace ZR.Admin.WebApi.Controllers // 查询相关的工作人员 var workers = await _CamWorkerService.AsQueryable().Where(it => it.WorkrecordId == id).ToListAsync(); - // 删除图片文件 - if (!string.IsNullOrEmpty(workRecord.ImageUrl)) + try { - // 从URL中提取文件路径 - var imagePath = workRecord.ImageUrl.Substring(workRecord.ImageUrl.IndexOf("workfiles")); - var fullImagePath = Path.Combine(WebHostEnvironment.WebRootPath, imagePath); - - // 获取文件名 - var fileName = Path.GetFileName(fullImagePath); - var directoryPath = Path.GetDirectoryName(fullImagePath); - - // 删除当日照片 - if (IOFile.Exists(fullImagePath)) + // 删除图片文件 + if (!string.IsNullOrEmpty(workRecord.ImageUrl)) { - IOFile.Delete(fullImagePath); - } + // 从URL中提取文件路径 + var imagePath = workRecord.ImageUrl.Substring(workRecord.ImageUrl.IndexOf("workfiles")); + var fullImagePath = Path.Combine(WebHostEnvironment.WebRootPath, imagePath); - // 删除参与人员照片 - var participantsPath = Path.Combine(WebHostEnvironment.WebRootPath, "workfiles", workRecord.RecordTime?.ToString("yyyyMM/yyyyMMdd"), "参与人员"); - if (Directory.Exists(participantsPath)) - { - foreach (var worker in workers) + // 获取文件名 + var fileName = Path.GetFileName(fullImagePath); + var directoryPath = Path.GetDirectoryName(fullImagePath); + + // 删除当日照片 + if (IOFile.Exists(fullImagePath)) { - var workerImagePath = Path.Combine(participantsPath, worker.WorkerName, fileName); - if (IOFile.Exists(workerImagePath)) - { - IOFile.Delete(workerImagePath); - } + IOFile.Delete(fullImagePath); + } - // 如果该工作人员的文件夹为空,则删除文件夹 - var workerDir = Path.GetDirectoryName(workerImagePath); - if (Directory.Exists(workerDir) && !Directory.EnumerateFiles(workerDir).Any()) + // 删除参与人员照片 + var participantsPath = Path.Combine(WebHostEnvironment.WebRootPath, "workfiles", workRecord.RecordTime?.ToString("yyyyMM/yyyyMMdd"), "参与人员"); + if (Directory.Exists(participantsPath)) + { + foreach (var worker in workers) { - Directory.Delete(workerDir); + var workerImagePath = Path.Combine(participantsPath, worker.WorkerName, fileName); + if (IOFile.Exists(workerImagePath)) + { + IOFile.Delete(workerImagePath); + } + + // 如果该工作人员的文件夹为空,则删除文件夹 + var workerDir = Path.GetDirectoryName(workerImagePath); + if (Directory.Exists(workerDir) && !Directory.EnumerateFiles(workerDir).Any()) + { + Directory.Delete(workerDir); + } } } + + // 删除工作内容照片 + var jobContentPath = Path.Combine(WebHostEnvironment.WebRootPath, "workfiles", workRecord.RecordTime?.ToString("yyyyMM/yyyyMMdd"), "工作内容", workRecord.Content); + var jobContentImagePath = Path.Combine(jobContentPath, fileName); + if (IOFile.Exists(jobContentImagePath)) + { + IOFile.Delete(jobContentImagePath); + } + + // 如果工作内容文件夹为空,则删除文件夹 + if (Directory.Exists(jobContentPath) && !Directory.EnumerateFiles(jobContentPath).Any()) + { + Directory.Delete(jobContentPath); + } + + // 删除部门照片 + var departmentPath = Path.Combine(WebHostEnvironment.WebRootPath, "workfiles", workRecord.RecordTime?.ToString("yyyyMM/yyyyMMdd"), "部门", workRecord.DeptName); + var departmentImagePath = Path.Combine(departmentPath, fileName); + if (IOFile.Exists(departmentImagePath)) + { + IOFile.Delete(departmentImagePath); + } + + // 如果部门文件夹为空,则删除文件夹 + if (Directory.Exists(departmentPath) && !Directory.EnumerateFiles(departmentPath).Any()) + { + Directory.Delete(departmentPath); + } + + // 清理空文件夹 + CleanEmptyDirectories(Path.Combine(WebHostEnvironment.WebRootPath, "workfiles", workRecord.RecordTime?.ToString("yyyyMM/yyyyMMdd"))); } - // 删除工作内容照片 - var jobContentPath = Path.Combine(WebHostEnvironment.WebRootPath, "workfiles", workRecord.RecordTime?.ToString("yyyyMM/yyyyMMdd"), "工作内容", workRecord.Content); - var jobContentImagePath = Path.Combine(jobContentPath, fileName); - if (IOFile.Exists(jobContentImagePath)) - { - IOFile.Delete(jobContentImagePath); - } - - // 如果工作内容文件夹为空,则删除文件夹 - if (Directory.Exists(jobContentPath) && !Directory.EnumerateFiles(jobContentPath).Any()) - { - Directory.Delete(jobContentPath); - } - - // 删除部门照片 - var departmentPath = Path.Combine(WebHostEnvironment.WebRootPath, "workfiles", workRecord.RecordTime?.ToString("yyyyMM/yyyyMMdd"), "部门", workRecord.DeptName); - var departmentImagePath = Path.Combine(departmentPath, fileName); - if (IOFile.Exists(departmentImagePath)) - { - IOFile.Delete(departmentImagePath); - } - - // 如果部门文件夹为空,则删除文件夹 - if (Directory.Exists(departmentPath) && !Directory.EnumerateFiles(departmentPath).Any()) - { - Directory.Delete(departmentPath); - } - - // 清理空文件夹 - CleanEmptyDirectories(Path.Combine(WebHostEnvironment.WebRootPath, "workfiles", workRecord.RecordTime?.ToString("yyyyMM/yyyyMMdd"))); } - + catch (Exception ex) + { + logger.Error($"删除文件失败,错误: {ex.Message}"); + } // 删除工作人员记录 await _CamWorkerService.DeleteAsync(it => it.WorkrecordId == id); @@ -577,7 +584,7 @@ namespace ZR.Admin.WebApi.Controllers [AllowAnonymous] public async Task DownloadZip([FromQuery] string? yearMoney) { - if (string.IsNullOrEmpty(yearMoney)|| yearMoney=="null") + if (string.IsNullOrEmpty(yearMoney) || yearMoney == "null") { yearMoney = DateTime.Now.ToString("yyyyMM"); } @@ -633,7 +640,7 @@ namespace ZR.Admin.WebApi.Controllers try { var workfilesPath = Path.Combine(WebHostEnvironment.WebRootPath, "workfiles"); - + if (!Directory.Exists(workfilesPath)) { return ToResponse(ResultCode.FAIL, "workfiles文件夹不存在"); @@ -702,7 +709,7 @@ namespace ZR.Admin.WebApi.Controllers private List GetDailyFoldersSize(string workfilesPath) { var dailyFolders = new List(); - + try { var directory = new DirectoryInfo(workfilesPath); @@ -732,7 +739,7 @@ namespace ZR.Admin.WebApi.Controllers return dailyFolders; } - - + + } }