This commit is contained in:
zpc 2025-08-21 16:27:30 +08:00
parent 913ce41ca5
commit d5018a36ba

View File

@ -443,6 +443,135 @@ namespace ZR.Admin.WebApi.Controllers
return SUCCESS(1); return SUCCESS(1);
} }
/// <summary>
/// 删除工作记录
/// </summary>
/// <param name="id">工作记录ID</param>
/// <returns></returns>
[HttpPost]
[Route("/deleteworkrecord/{id}")]
[ActionPermissionFilter(Permission = "camworkrecord:delete")]
public async Task<IActionResult> 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);
}
/// <summary>
/// 清理空文件夹
/// </summary>
/// <param name="directoryPath">目录路径</param>
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] [HttpGet]
[Route("/getDownloadZip")] [Route("/getDownloadZip")]
[AllowAnonymous] [AllowAnonymous]