From 7f050a1dd65703ce06e02b5572bd1eb592d9b348 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 20 Oct 2025 17:41:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A8=BD=E6=9F=A5=E9=80=9A=E7=94=A8=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IRaCIS.Core.Application.csproj | 1 - .../Common/AuditingData.cs | 77 ++++++++++++++----- .../Common/AuditingDto.cs | 16 +++- .../Interceptor/AuditEntityInterceptor.cs | 5 +- .../IRaCIS.Core.Infrastructure.csproj | 1 + 5 files changed, 78 insertions(+), 22 deletions(-) diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj b/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj index 790c4f8ed..fba269f0f 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj @@ -64,7 +64,6 @@ - diff --git a/IRaCIS.Core.Infra.EFCore/Common/AuditingData.cs b/IRaCIS.Core.Infra.EFCore/Common/AuditingData.cs index 0534ce3a8..b391b2dcc 100644 --- a/IRaCIS.Core.Infra.EFCore/Common/AuditingData.cs +++ b/IRaCIS.Core.Infra.EFCore/Common/AuditingData.cs @@ -7,6 +7,8 @@ using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using ZiggyCreatures.Caching.Fusion; +using static IRaCIS.Core.Domain.Share.StaticData; namespace IRaCIS.Core.Infra.EFCore.Common @@ -39,16 +41,20 @@ namespace IRaCIS.Core.Infra.EFCore.Common /// public IUserInfo _userInfo { get; set; } + public IFusionCache _fusionCache { get; set; } + + /// /// 构造方法 /// /// /// - public AuditingData(IRaCISDBContext dbContext, IUserInfo userInfo) + public AuditingData(IRaCISDBContext dbContext, IUserInfo userInfo, IFusionCache fusionCache) { _dbContext = dbContext; _userInfo = userInfo; _userInfo.BatchId = _userInfo.BatchId == null ? NewId.NextGuid() : _userInfo.BatchId; + _fusionCache = fusionCache; } @@ -262,7 +268,7 @@ namespace IRaCIS.Core.Infra.EFCore.Common var entity = item.Entity as ReadingImportFile; - var visitTaskInfo = await _dbContext.VisitTask.Where(x => x.Id == entity.VisitTaskId).Include(x=>x.DoctorUser).FirstOrDefaultAsync(); + var visitTaskInfo = await _dbContext.VisitTask.Where(x => x.Id == entity.VisitTaskId).Include(x => x.DoctorUser).FirstOrDefaultAsync(); await InsertInspection(entity, type, x => new InspectionConvertDTO() { @@ -3696,7 +3702,7 @@ namespace IRaCIS.Core.Infra.EFCore.Common string questionName = string.Empty; string questionAnswer = string.Empty; - if (thisQuestinonAnswer != null&& question!=null) + if (thisQuestinonAnswer != null && question != null) { questionName = _userInfo.IsEn_Us ? question.QuestionEnName : question.QuestionName; questionAnswer = Translationunit(question.Type, question.Unit, question.CustomUnit, unitDataList, thisQuestinonAnswer.Answer); @@ -3857,7 +3863,7 @@ namespace IRaCIS.Core.Infra.EFCore.Common entity.OrderMark = await _dbContext.ReadingQuestionTrial.Where(x => x.Id == entity.QuestionId).Select(x => x.OrderMark).FirstOrDefaultAsync(); - entity.RowMark = entity.OrderMark+ entity.RowIndex.GetLesionMark(); + entity.RowMark = entity.OrderMark + entity.RowIndex.GetLesionMark(); @@ -4123,7 +4129,7 @@ namespace IRaCIS.Core.Infra.EFCore.Common #endregion - + @@ -4174,7 +4180,17 @@ namespace IRaCIS.Core.Infra.EFCore.Common #region 处理标识 - var from = await _dbContext.FrontAuditConfig.FirstOrDefaultAsync(x => x.Identification == inspection.Identification); + //批量记录instance 这种稽查的时候,减少数据库查询交互 --稽查缓存 + var from = await _fusionCache.GetOrSetAsync(InspectionCacheKeys.FrontAuditConfig(inspection.Identification), async _ => + { + return await _dbContext.FrontAuditConfig + .FirstOrDefaultAsync(x => x.Identification == inspection.Identification); + }, + TimeSpan.FromMinutes(10) // 缓存 10 分钟 + ); + + //var from = await _dbContext.FrontAuditConfig.FirstOrDefaultAsync(x => x.Identification == inspection.Identification); + inspection.ObjectTypeId = from?.ObjectTypeId; inspection.OptTypeId = from?.OptTypeId; //inspection.ChildrenTypeId = from?.ChildrenTypeId; @@ -4273,14 +4289,20 @@ namespace IRaCIS.Core.Infra.EFCore.Common }; - - if (inspection.VisitTaskId != null) { if (generalData.TrialId == null || generalData.TrialSiteId == null || generalData.SubjectId == null || generalData.TrialReadingCriterionId == null) { - var info = await _dbContext.VisitTask.Where(x => x.Id == inspection.VisitTaskId).Select(x => new { SubjectCode = x.Subject.Code, TrialSiteCode = x.Subject.TrialSite.TrialSiteCode, SubjectId = x.SubjectId, x.Subject.TrialSiteId, x.TrialId, x.SourceSubjectVisitId, ReadModuleSubjectVisitId = (Guid?)x.ReadModule.SubjectVisitId, x.TrialReadingCriterionId }).FirstOrDefaultAsync(); + //var info = await _dbContext.VisitTask.Where(x => x.Id == inspection.VisitTaskId).Select(x => new { SubjectCode = x.Subject.Code, TrialSiteCode = x.Subject.TrialSite.TrialSiteCode, SubjectId = x.SubjectId, x.Subject.TrialSiteId, x.TrialId, x.SourceSubjectVisitId, ReadModuleSubjectVisitId = (Guid?)x.ReadModule.SubjectVisitId, x.TrialReadingCriterionId }).FirstOrDefaultAsync(); + + var info = await _fusionCache.GetOrSetAsync(InspectionCacheKeys.VisitTask(inspection.VisitTaskId), async _ => + { + return await _dbContext.VisitTask.Where(x => x.Id == inspection.VisitTaskId).Select(x => new { SubjectCode = x.Subject.Code, TrialSiteCode = x.Subject.TrialSite.TrialSiteCode, SubjectId = x.SubjectId, x.Subject.TrialSiteId, x.TrialId, x.SourceSubjectVisitId, ReadModuleSubjectVisitId = (Guid?)x.ReadModule.SubjectVisitId, x.TrialReadingCriterionId }).FirstOrDefaultAsync(); + + }, + TimeSpan.FromDays(1) // 缓存 1天 + ); generalData.TrialReadingCriterionId = info?.TrialReadingCriterionId ?? generalData.TrialReadingCriterionId; generalData.TrialId = info?.TrialId ?? generalData.TrialId; @@ -4299,7 +4321,14 @@ namespace IRaCIS.Core.Infra.EFCore.Common { if (generalData.TrialId == null) { - var info = await _dbContext.ReadingQuestionCriterionTrial.Where(x => x.Id == inspection.TrialReadingCriterionId).Select(x => new { x.TrialId }).FirstOrDefaultAsync(); + var info = await _fusionCache.GetOrSetAsync(InspectionCacheKeys.ReadingQuestionCriterionTrial(inspection.TrialReadingCriterionId), async _ => + { + return await _dbContext.ReadingQuestionCriterionTrial.Where(x => x.Id == inspection.TrialReadingCriterionId).Select(x => new { x.TrialId }).FirstOrDefaultAsync(); + }, + TimeSpan.FromDays(1) // 缓存 1天 + ); + + //var info = await _dbContext.ReadingQuestionCriterionTrial.Where(x => x.Id == inspection.TrialReadingCriterionId).Select(x => new { x.TrialId }).FirstOrDefaultAsync(); generalData.TrialId = info?.TrialId ?? generalData.TrialId; } @@ -4311,8 +4340,17 @@ namespace IRaCIS.Core.Infra.EFCore.Common { if (generalData.TrialId == null || generalData.TrialSiteId == null || generalData.SubjectId == null) { - var info = await _dbContext.SubjectVisit.Where(x => x.Id == generalData.SubjectVisitId).Select(x => - new { x.SubjectId, x.TrialSiteId, x.TrialId, SubjectCode = x.Subject.Code, TrialSiteCode = x.Subject.TrialSite.TrialSiteCode }).FirstOrDefaultAsync(); + + var info = await _fusionCache.GetOrSetAsync(InspectionCacheKeys.SubjectVisit(inspection.SubjectVisitId), async _ => + { + return await _dbContext.SubjectVisit.Where(x => x.Id == generalData.SubjectVisitId).Select(x => + new { x.SubjectId, x.TrialSiteId, x.TrialId, SubjectCode = x.Subject.Code, TrialSiteCode = x.Subject.TrialSite.TrialSiteCode }).FirstOrDefaultAsync(); + }, + TimeSpan.FromDays(1) // 缓存 1天 + ); + + // var info = await _dbContext.SubjectVisit.Where(x => x.Id == generalData.SubjectVisitId).Select(x => + //new { x.SubjectId, x.TrialSiteId, x.TrialId, SubjectCode = x.Subject.Code, TrialSiteCode = x.Subject.TrialSite.TrialSiteCode }).FirstOrDefaultAsync(); generalData.TrialId = info?.TrialId ?? generalData.TrialId; @@ -4332,7 +4370,15 @@ namespace IRaCIS.Core.Infra.EFCore.Common { if (generalData.TrialId == null || generalData.TrialSiteId == null) { - var info = await _dbContext.Subject.Where(x => x.Id == generalData.SubjectId).Select(x => new { SubjectId = x.Id, x.TrialSiteId, x.TrialId, SubjectCode = x.Code, TrialSiteCode = x.TrialSite.TrialSiteCode }).FirstOrDefaultAsync(); + //var info = await _dbContext.Subject.Where(x => x.Id == generalData.SubjectId).Select(x => new { SubjectId = x.Id, x.TrialSiteId, x.TrialId, SubjectCode = x.Code, TrialSiteCode = x.TrialSite.TrialSiteCode }).FirstOrDefaultAsync(); + + var info = await _fusionCache.GetOrSetAsync(InspectionCacheKeys.SubjecSiteInfo(inspection.SubjectId), async _ => + { + return await _dbContext.Subject.Where(x => x.Id == generalData.SubjectId).Select(x => new { SubjectId = x.Id, x.TrialSiteId, x.TrialId, SubjectCode = x.Code, TrialSiteCode = x.TrialSite.TrialSiteCode }).FirstOrDefaultAsync(); + }, + TimeSpan.FromDays(1) // 缓存 1天 + ); + if (info == null) { @@ -4348,11 +4394,6 @@ namespace IRaCIS.Core.Infra.EFCore.Common generalData.TrialSiteId = info?.TrialSiteId ?? generalData.TrialSiteId; } - - - - - //以后移除 generalData.SiteCode = info?.TrialSiteCode ?? generalData.SiteCode; generalData.SubjectCode = info?.SubjectCode ?? generalData.SubjectCode; diff --git a/IRaCIS.Core.Infra.EFCore/Common/AuditingDto.cs b/IRaCIS.Core.Infra.EFCore/Common/AuditingDto.cs index f093bc9a4..5fb1a9ff3 100644 --- a/IRaCIS.Core.Infra.EFCore/Common/AuditingDto.cs +++ b/IRaCIS.Core.Infra.EFCore/Common/AuditingDto.cs @@ -3,6 +3,20 @@ using IRaCIS.Core.Domain.Share; namespace IRaCIS.Core.Infra.EFCore.Common { + public static class InspectionCacheKeys + { + public static string FrontAuditConfig(string identification) => $"FrontAuditConfig:{identification}"; + + public static string ReadingQuestionCriterionTrial(Guid? trialReadingCriterionId) => $"ReadingQuestionCriterionTrial:{trialReadingCriterionId}"; + + public static string SubjectVisit(Guid? subjectVisitId) => $"SubjectVisit:{subjectVisitId}"; + + public static string SubjecSiteInfo(Guid? subjectId) => $"Subject:{subjectId}"; + + public static string VisitTask(Guid? visitTaskId) => $"VisitTask:{visitTaskId}"; + } + + public class SetInspectionEnumDataDto { public Guid Id { get; set; } @@ -63,7 +77,7 @@ namespace IRaCIS.Core.Infra.EFCore.Common public string SelectResult { get; set; } - public bool? IsReadingReset { get;set; } + public bool? IsReadingReset { get; set; } } public class InspectionConvertDTO : DataInspection { diff --git a/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs b/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs index ad1ba2867..5be1469b6 100644 --- a/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs +++ b/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs @@ -12,6 +12,7 @@ using Microsoft.Extensions.Logging; using Microsoft.VisualBasic; using System.Collections; using System.Data; +using ZiggyCreatures.Caching.Fusion; namespace IRaCIS.Core.Infra.EFCore; @@ -23,7 +24,7 @@ namespace IRaCIS.Core.Infra.EFCore; /// public class AuditEntityInterceptor(IUserInfo _userInfo, ILogger _logger - , IMediator _mediator + , IMediator _mediator, IFusionCache _fusionCache ) : SaveChangesInterceptor { @@ -157,7 +158,7 @@ public class AuditEntityInterceptor(IUserInfo _userInfo, .Where(t => !typeof(DataInspection).IsAssignableFrom(t.Entity.GetType()) && typeof(Entity).IsAssignableFrom(t.Entity.GetType()) && !inspectionGeneralIdList.Contains(((Entity)t.Entity).Id)) .ToList(); - AuditingData auditingData = new AuditingData((IRaCISDBContext)context, _userInfo); + AuditingData auditingData = new AuditingData((IRaCISDBContext)context, _userInfo, _fusionCache); if (entities.Count > 0) diff --git a/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj b/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj index 2ee7eae59..e0ee437d6 100644 --- a/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj +++ b/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj @@ -19,6 +19,7 @@ +