From f55f498626bac774a3d891ffd42e0cb9c097006e Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Tue, 30 Sep 2025 10:01:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0gpt=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/Document/AuditDocumentService.cs | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/IRaCIS.Core.Application/Service/Document/AuditDocumentService.cs b/IRaCIS.Core.Application/Service/Document/AuditDocumentService.cs index d45d8895b..f04b52e8a 100644 --- a/IRaCIS.Core.Application/Service/Document/AuditDocumentService.cs +++ b/IRaCIS.Core.Application/Service/Document/AuditDocumentService.cs @@ -23,6 +23,7 @@ using IRaCIS.Core.Application.Contracts; using Microsoft.EntityFrameworkCore; using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource; using NPOI.SS.Formula.Functions; +using EFCore.BulkExtensions; namespace IRaCIS.Core.Application.Service; @@ -234,6 +235,60 @@ public class AuditDocumentService(IRepository _auditDocumentRepos #region 闭包修改 + /// + /// GPT 移动子树代码--适合一次提交事务 + /// + /// + /// + /// + private async Task MoveSubtreeAsync(Guid subtreeRootId, Guid? newParentId) + { + + // 1. 子树所有闭包关系(后代包含自身) + var subtreeClosures = await _auditDocumentClosureRepository + .Where(c => c.AncestorId == subtreeRootId) + .ToListAsync(); + + var descendantIdsQuery = _auditDocumentClosureRepository + .Where(c => c.AncestorId == subtreeRootId).Select(c => c.DescendantId); + + // 2. 删除原有祖先关系,保留自反关系和子树内部关系 + await _auditDocumentClosureRepository.BatchDeleteNoTrackingAsync(c => + descendantIdsQuery.Contains(c.DescendantId) && + c.AncestorId != c.DescendantId && // 保留自反关系 + !descendantIdsQuery.Contains(c.AncestorId) // 保留子树内部关系 + ); + + // 3. 查新父节点祖先 + var newParentClosures = newParentId.HasValue + ? await _auditDocumentClosureRepository + .Where(c => c.DescendantId == newParentId.Value) + .ToListAsync() + : new List(); + + // 4. 生成新关系 + var newClosures = new List(); + + foreach (var pc in newParentClosures) + { + foreach (var sc in subtreeClosures) + { + newClosures.Add(new AuditDocumentClosure + { + AncestorId = pc.AncestorId, + DescendantId = sc.DescendantId, + Depth = pc.Depth + sc.Depth + 1 //深度公式:新深度 = 父祖先深度 + 子树原深度 + 1 + }); + } + } + + // 5. 插入新关系 + await _auditDocumentClosureRepository.AddRangeAsync(newClosures); + await _auditDocumentClosureRepository.SaveChangesAsync(); + + } + + /// /// 插入闭包表关系 /// @@ -600,6 +655,17 @@ public class AuditDocumentService(IRepository _auditDocumentRepos throw new BusinessValidationFailedException(_localizer["AuditDocument_CanNotMove"]); } + #region 不考虑文件名重复 移动子树快速方法 + //foreach (var item in inDto.Ids) + //{ + // var subtreeRoot = await _auditDocumentRepository.FirstOrDefaultAsync(t => t.Id == item); + + // subtreeRoot.ParentId = inDto.ParentId; + + // await MoveSubtreeAsync(item, inDto.ParentId); + //} + #endregion + await CopyFileOrFolder(inDto); await DeleteAuditDocument(new DeleteAuditDocumentInDto()