From d5018a36ba752cb0bd437779ca019655cdc470fa Mon Sep 17 00:00:00 2001 From: zpc Date: Thu, 21 Aug 2025 16:27:30 +0800 Subject: [PATCH] 333 --- .../Controllers/CommonController.cs | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/ZR.Admin.WebApi/Controllers/CommonController.cs b/ZR.Admin.WebApi/Controllers/CommonController.cs index daede30..f1ff621 100644 --- a/ZR.Admin.WebApi/Controllers/CommonController.cs +++ b/ZR.Admin.WebApi/Controllers/CommonController.cs @@ -443,6 +443,135 @@ namespace ZR.Admin.WebApi.Controllers return SUCCESS(1); } + /// + /// 删除工作记录 + /// + /// 工作记录ID + /// + [HttpPost] + [Route("/deleteworkrecord/{id}")] + [ActionPermissionFilter(Permission = "camworkrecord:delete")] + public async Task DeleteCamWorkRecord(long id) + { + // 查询工作记录 + var workRecord = await _CamWorkrecordService.AsQueryable().Where(it => it.Id == id).FirstAsync(); + if (workRecord == null) + { + return ToResponse(ResultCode.PARAM_ERROR, "未找到该记录"); + } + + // 查询相关的工作人员 + var workers = await _CamWorkerService.AsQueryable().Where(it => it.WorkrecordId == id).ToListAsync(); + + // 删除图片文件 + if (!string.IsNullOrEmpty(workRecord.ImageUrl)) + { + // 从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)) + { + IOFile.Delete(fullImagePath); + } + + // 删除参与人员照片 + var participantsPath = Path.Combine(WebHostEnvironment.WebRootPath, "workfiles", workRecord.RecordTime?.ToString("yyyyMM/yyyyMMdd"), "参与人员"); + if (Directory.Exists(participantsPath)) + { + foreach (var worker in workers) + { + 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"))); + } + + // 删除工作人员记录 + await _CamWorkerService.DeleteAsync(it => it.WorkrecordId == id); + + // 删除工作记录 + await _CamWorkrecordService.DeleteAsync(it => it.Id == id); + + return SUCCESS(1); + } + + /// + /// 清理空文件夹 + /// + /// 目录路径 + private void CleanEmptyDirectories(string directoryPath) + { + if (!Directory.Exists(directoryPath)) + return; + + try + { + // 递归清理子目录 + foreach (var subDir in Directory.GetDirectories(directoryPath)) + { + CleanEmptyDirectories(subDir); + } + + // 如果当前目录为空,则删除 + if (!Directory.EnumerateFiles(directoryPath).Any() && !Directory.EnumerateDirectories(directoryPath).Any()) + { + Directory.Delete(directoryPath); + } + } + catch (Exception ex) + { + // 记录日志但不抛出异常,避免影响主流程 + logger.Error($"清理空文件夹失败: {directoryPath}, 错误: {ex.Message}"); + } + } + [HttpGet] [Route("/getDownloadZip")] [AllowAnonymous]