稽查通用缓存添加

Test_IRC_Net8
hang 2025-10-20 17:41:00 +08:00
parent f1fb2068de
commit 7f050a1dd6
5 changed files with 78 additions and 22 deletions

View File

@ -64,7 +64,6 @@
<PackageReference Include="RestSharp" Version="112.1.0" /> <PackageReference Include="RestSharp" Version="112.1.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.11" /> <PackageReference Include="SixLabors.ImageSharp" Version="3.1.11" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="8.0.2" /> <PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="8.0.2" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.4.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -7,6 +7,8 @@ using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using ZiggyCreatures.Caching.Fusion;
using static IRaCIS.Core.Domain.Share.StaticData;
namespace IRaCIS.Core.Infra.EFCore.Common namespace IRaCIS.Core.Infra.EFCore.Common
@ -39,16 +41,20 @@ namespace IRaCIS.Core.Infra.EFCore.Common
/// </summary> /// </summary>
public IUserInfo _userInfo { get; set; } public IUserInfo _userInfo { get; set; }
public IFusionCache _fusionCache { get; set; }
/// <summary> /// <summary>
/// 构造方法 /// 构造方法
/// </summary> /// </summary>
/// <param name="dbContext"></param> /// <param name="dbContext"></param>
/// <param name="userInfo"></param> /// <param name="userInfo"></param>
public AuditingData(IRaCISDBContext dbContext, IUserInfo userInfo) public AuditingData(IRaCISDBContext dbContext, IUserInfo userInfo, IFusionCache fusionCache)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_userInfo = userInfo; _userInfo = userInfo;
_userInfo.BatchId = _userInfo.BatchId == null ? NewId.NextGuid() : _userInfo.BatchId; _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 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<ReadingImportFile>(entity, type, x => new InspectionConvertDTO() await InsertInspection<ReadingImportFile>(entity, type, x => new InspectionConvertDTO()
{ {
@ -3696,7 +3702,7 @@ namespace IRaCIS.Core.Infra.EFCore.Common
string questionName = string.Empty; string questionName = string.Empty;
string questionAnswer = string.Empty; string questionAnswer = string.Empty;
if (thisQuestinonAnswer != null&& question!=null) if (thisQuestinonAnswer != null && question != null)
{ {
questionName = _userInfo.IsEn_Us ? question.QuestionEnName : question.QuestionName; questionName = _userInfo.IsEn_Us ? question.QuestionEnName : question.QuestionName;
questionAnswer = Translationunit(question.Type, question.Unit, question.CustomUnit, unitDataList, thisQuestinonAnswer.Answer); 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.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 #endregion
@ -4174,7 +4180,17 @@ namespace IRaCIS.Core.Infra.EFCore.Common
#region 处理标识 #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.ObjectTypeId = from?.ObjectTypeId;
inspection.OptTypeId = from?.OptTypeId; inspection.OptTypeId = from?.OptTypeId;
//inspection.ChildrenTypeId = from?.ChildrenTypeId; //inspection.ChildrenTypeId = from?.ChildrenTypeId;
@ -4273,14 +4289,20 @@ namespace IRaCIS.Core.Infra.EFCore.Common
}; };
if (inspection.VisitTaskId != null) if (inspection.VisitTaskId != null)
{ {
if (generalData.TrialId == null || generalData.TrialSiteId == null || generalData.SubjectId == null || generalData.TrialReadingCriterionId == 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.TrialReadingCriterionId = info?.TrialReadingCriterionId ?? generalData.TrialReadingCriterionId;
generalData.TrialId = info?.TrialId ?? generalData.TrialId; generalData.TrialId = info?.TrialId ?? generalData.TrialId;
@ -4299,7 +4321,14 @@ namespace IRaCIS.Core.Infra.EFCore.Common
{ {
if (generalData.TrialId == null) 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; 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) 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; generalData.TrialId = info?.TrialId ?? generalData.TrialId;
@ -4332,7 +4370,15 @@ namespace IRaCIS.Core.Infra.EFCore.Common
{ {
if (generalData.TrialId == null || generalData.TrialSiteId == null) 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) if (info == null)
{ {
@ -4348,11 +4394,6 @@ namespace IRaCIS.Core.Infra.EFCore.Common
generalData.TrialSiteId = info?.TrialSiteId ?? generalData.TrialSiteId; generalData.TrialSiteId = info?.TrialSiteId ?? generalData.TrialSiteId;
} }
//以后移除 //以后移除
generalData.SiteCode = info?.TrialSiteCode ?? generalData.SiteCode; generalData.SiteCode = info?.TrialSiteCode ?? generalData.SiteCode;
generalData.SubjectCode = info?.SubjectCode ?? generalData.SubjectCode; generalData.SubjectCode = info?.SubjectCode ?? generalData.SubjectCode;

View File

@ -3,6 +3,20 @@ using IRaCIS.Core.Domain.Share;
namespace IRaCIS.Core.Infra.EFCore.Common 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 class SetInspectionEnumDataDto
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
@ -63,7 +77,7 @@ namespace IRaCIS.Core.Infra.EFCore.Common
public string SelectResult { get; set; } public string SelectResult { get; set; }
public bool? IsReadingReset { get;set; } public bool? IsReadingReset { get; set; }
} }
public class InspectionConvertDTO : DataInspection public class InspectionConvertDTO : DataInspection
{ {

View File

@ -12,6 +12,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic; using Microsoft.VisualBasic;
using System.Collections; using System.Collections;
using System.Data; using System.Data;
using ZiggyCreatures.Caching.Fusion;
namespace IRaCIS.Core.Infra.EFCore; namespace IRaCIS.Core.Infra.EFCore;
@ -23,7 +24,7 @@ namespace IRaCIS.Core.Infra.EFCore;
/// <param name="_mediator"></param> /// <param name="_mediator"></param>
public class AuditEntityInterceptor(IUserInfo _userInfo, public class AuditEntityInterceptor(IUserInfo _userInfo,
ILogger<AuditEntityInterceptor> _logger ILogger<AuditEntityInterceptor> _logger
, IMediator _mediator , IMediator _mediator, IFusionCache _fusionCache
) : SaveChangesInterceptor ) : SaveChangesInterceptor
{ {
@ -157,7 +158,7 @@ public class AuditEntityInterceptor(IUserInfo _userInfo,
.Where(t => !typeof(DataInspection).IsAssignableFrom(t.Entity.GetType()) && typeof(Entity).IsAssignableFrom(t.Entity.GetType()) .Where(t => !typeof(DataInspection).IsAssignableFrom(t.Entity.GetType()) && typeof(Entity).IsAssignableFrom(t.Entity.GetType())
&& !inspectionGeneralIdList.Contains(((Entity)t.Entity).Id)) && !inspectionGeneralIdList.Contains(((Entity)t.Entity).Id))
.ToList(); .ToList();
AuditingData auditingData = new AuditingData((IRaCISDBContext)context, _userInfo); AuditingData auditingData = new AuditingData((IRaCISDBContext)context, _userInfo, _fusionCache);
if (entities.Count > 0) if (entities.Count > 0)

View File

@ -19,6 +19,7 @@
<PackageReference Include="SharpZipLib" Version="1.4.2" /> <PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="BouncyCastle.Cryptography" Version="2.5.1" /> <PackageReference Include="BouncyCastle.Cryptography" Version="2.5.1" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.6.0.2" /> <PackageReference Include="System.Linq.Dynamic.Core" Version="1.6.0.2" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.4.0" />
</ItemGroup> </ItemGroup>