diff --git a/IRaCIS.Core.Infra.EFCore/Common/AuditingData.cs b/IRaCIS.Core.Infra.EFCore/Common/AuditingData.cs index 79838ca0b..a8e573af1 100644 --- a/IRaCIS.Core.Infra.EFCore/Common/AuditingData.cs +++ b/IRaCIS.Core.Infra.EFCore/Common/AuditingData.cs @@ -6,6 +6,7 @@ using IRaCIS.Core.Infrastructure; using IRaCIS.Core.Infrastructure.Extention; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; +using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -184,41 +185,74 @@ namespace IRaCIS.Core.Infra.EFCore.Common } // 非Dicom文件 - foreach (var item in entitys.Where(x => x.Entity.GetType() == typeof(NoneDicomStudyFile))) + //var noneDicomFile= + + if (entitys.Any(x => x.Entity.GetType() == typeof(NoneDicomStudyFile))) { - var entity = item.Entity as NoneDicomStudyFile; - var noneDicomStudy = entity.NoneDicomStudy; - if (noneDicomStudy == null) + var noneDicomStudyfile = entitys.Where(x => x.Entity.GetType() == typeof(NoneDicomStudyFile)).Select(x => x.Entity).FirstOrDefault() as NoneDicomStudyFile; + var noneDicomStudy = await _dbContext.NoneDicomStudy.Where(x => x.Id == noneDicomStudyfile.NoneDicomStudyId).FirstOrDefaultAsync(); + if (noneDicomStudy != null) { - noneDicomStudy = await _dbContext.NoneDicomStudy.FirstOrDefaultAsync(x => x.Id == entity.NoneDicomStudyId); + var filecount = await _dbContext.NoneDicomStudyFile.Where(x => x.NoneDicomStudyId == noneDicomStudyfile.NoneDicomStudyId).CountAsync(); + var count = entitys.Where(x => x.Entity.GetType() == typeof(NoneDicomStudyFile)).ToList().Count; + if (type == "Deleted") + { + filecount = filecount - count; + } + else + { + filecount = filecount + count; + } + + await InsertInspection(null, type, x => new DataInspection() + { + GeneralId = x.Id, + }, new + { + FileCount = filecount, + }, noneDicomStudy); + } - noneDicomStudy = noneDicomStudy ?? new NoneDicomStudy(); - var fileCount = 0; - if (type == "Add") - { - fileCount = _dbContext.NoneDicomStudyFile.Where(x => x.NoneDicomStudyId == entity.NoneDicomStudyId).Count() + 1; - } else if (type == "Deleted") - { - fileCount = _dbContext.NoneDicomStudyFile.Where(x => x.NoneDicomStudyId == entity.NoneDicomStudyId).Count() - 1; - } - await InsertInspection(item, type, x => new DataInspection() - { - GeneralId = x.Id, - TrialId = noneDicomStudy.TrialId, - SubjectId = noneDicomStudy.SubjectId, - SiteId= noneDicomStudy.SiteId, - SubjectVisitId = noneDicomStudy.SubjectVisitId, - - },new { - StudyCode = noneDicomStudy.StudyCode, - Modality= noneDicomStudy.Modality, - BodyPart= noneDicomStudy.BodyPart, - FileCount= fileCount, - ImageDate= noneDicomStudy.ImageDate, - UpNum= 1,// 每次上传或下载一个 - }); } + ////非Dicom文件 + //foreach (var item in entitys.Where(x => x.Entity.GetType() == typeof(NoneDicomStudyFile))) + //{ + // var entity = item.Entity as NoneDicomStudyFile; + // var noneDicomStudy = entity.NoneDicomStudy; + // if (noneDicomStudy == null) + // { + // noneDicomStudy = await _dbContext.NoneDicomStudy.FirstOrDefaultAsync(x => x.Id == entity.NoneDicomStudyId); + // } + // noneDicomStudy = noneDicomStudy ?? new NoneDicomStudy(); + // var fileCount = 0; + // if (type == "Add") + // { + // fileCount = _dbContext.NoneDicomStudyFile.Where(x => x.NoneDicomStudyId == entity.NoneDicomStudyId).Count() + 1; + // } + // else if (type == "Deleted") + // { + // fileCount = _dbContext.NoneDicomStudyFile.Where(x => x.NoneDicomStudyId == entity.NoneDicomStudyId).Count() - 1; + // } + // await InsertInspection(item, type, x => new DataInspection() + // { + // GeneralId = x.Id, + // TrialId = noneDicomStudy.TrialId, + // SubjectId = noneDicomStudy.SubjectId, + // SiteId = noneDicomStudy.SiteId, + // SubjectVisitId = noneDicomStudy.SubjectVisitId, + + // }, new + // { + // StudyCode = noneDicomStudy.StudyCode, + // Modality = noneDicomStudy.Modality, + // BodyPart = noneDicomStudy.BodyPart, + // FileCount = fileCount, + // ImageDate = noneDicomStudy.ImageDate, + // UpNum = 1,// 每次上传或下载一个 + // }); + //} + // 访视 foreach (var item in entitys.Where(x => x.Entity.GetType() == typeof(SubjectVisit))) { @@ -357,8 +391,15 @@ namespace IRaCIS.Core.Infra.EFCore.Common //Qc 问题答案 foreach (var item in entitys.Where(x => x.Entity.GetType() == typeof(TrialQCQuestionAnswer))) { + var entity = item.Entity as TrialQCQuestionAnswer; + var subjectvisit = await _dbContext.SubjectVisit.Where(x => x.Id == entity.SubjectVisitId).FirstOrDefaultAsync(); + subjectvisit = subjectvisit ?? new SubjectVisit(); + type = type == "Update" ? "Add" : type; await InsertInspection(item, type, x => new DataInspection() { + SiteId= subjectvisit.SiteId, + SubjectId= subjectvisit.SubjectId, + SubjectVisitName= subjectvisit.VisitName, GeneralId = x.Id, }); } @@ -373,25 +414,30 @@ namespace IRaCIS.Core.Infra.EFCore.Common /// 数据 /// 类型 /// 表达式 + /// 实体对象 /// - public async Task InsertInspection(EntityEntry data, string type, Expression> expression = null,object otherItem=null) where T:class + public async Task InsertInspection(EntityEntry data, string type, Expression> expression = null,object otherItem=null,T entityData=null) where T:class { - + object entityobj = entityData == null ? data.Entity : entityData; DataInspection inspection = new DataInspection(); if (expression != null) { var f = expression.Compile(); - var entity = data.Entity as T; + var entity = entityobj as T; inspection = f(entity); } - inspection.Identification = $"{_userInfo.RequestUrl}/{ data.Entity.GetType().Name}/{type}"; - var originaldata = data.OriginalValues.ToObject(); - if (originaldata != null) + inspection.Identification = $"{_userInfo.RequestUrl}/{ entityobj.GetType().Name}/{type}"; + if (data != null) { - inspection.LastJsonDetail = originaldata.ToJcJson(); + var originaldata = data.OriginalValues.ToObject(); + if (originaldata != null) + { + inspection.LastJsonDetail = originaldata.ToJcJson(); + } } + - await AddInspectionRecordAsync(inspection, data.Entity, otherItem); + await AddInspectionRecordAsync(inspection, entityobj, otherItem); }