diff --git a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs index 66ee3ef08..55e0af46b 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs @@ -224,39 +224,46 @@ namespace IRaCIS.Core.Infra.EFCore } - /// EF跟踪方式 先查询出来,再更新部分字段 稽查的时候需要完整的实体信息 - public async Task UpdatePartialAsync(TEntity entity, Expression> updateFactory, bool autoSave = false, CancellationToken cancellationToken = default) + + + + /// EF跟踪方式 外层先有查询好的完成实体,再更新部分字段 稽查的时候需要完整的实体信息 + public async Task UpdatePartialAsync(TEntity waitModifyEntity, Expression> updateFactory, bool autoSave = false, CancellationToken cancellationToken = default) { - var entityEntry = _dbContext.Entry(entity); - - + var entityEntry = _dbContext.Entry(waitModifyEntity); entityEntry.State = EntityState.Detached; - List list = ((MemberInitExpression)updateFactory.Body).Bindings.Select(mb => mb.Member.Name) - .Select(propName => typeof(TEntity).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)).ToList(); - - Func func = updateFactory.Compile(); - - TEntity applyObj = func(entity); - - foreach (PropertyInfo prop in list) - { - object value = prop.GetValue((object)applyObj); - prop.SetValue((object)entity, value); - - _dbContext.Entry(entity).Property(prop.Name).IsModified = true; - } + ModifyPartialFiled(waitModifyEntity, updateFactory); return await SaveChangesAsync(autoSave); + } + + public async Task UpdatePartialSearchFirstAsync(Expression> updateFilter, + Expression> updateFactory, + bool autoSave = false, CancellationToken cancellationToken = default) + { + if (updateFilter == null) + { + throw new ArgumentException("更新过滤条件不允许为空", nameof(updateFilter)); + } + + var searchEntityList = await _dbSet.AsNoTracking().Where(updateFilter).ToListAsync(); + + foreach (var needUpdateEntity in searchEntityList) + { + await UpdatePartialAsync(needUpdateEntity, updateFactory, autoSave); + } } + /// EF跟踪方式 先查询出来,再更新部分字段 稽查的时候需要完整的实体信息 public async Task UpdatePartialSearchFirstAsync(Guid id, Expression> updateFactory, bool autoSave = false, CancellationToken cancellationToken = default) { + //不跟踪 查询出来的实体就是Detached var searchEntity = await _dbSet.AsNoTracking().FirstOrDefaultAsync(t => t.Id == id); if (searchEntity == null) @@ -266,35 +273,39 @@ namespace IRaCIS.Core.Infra.EFCore } - List list = ((MemberInitExpression)updateFactory.Body).Bindings.Select(mb => mb.Member.Name) - .Select(propName => typeof(TEntity).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)).ToList(); - - Func func = updateFactory.Compile(); - - TEntity applyObj = func(searchEntity); - - foreach (PropertyInfo prop in list) - { - object value = prop.GetValue((object)applyObj); - prop.SetValue((object)searchEntity, value); - - _dbContext.Entry(searchEntity).Property(prop.Name).IsModified = true; - } - - + ModifyPartialFiled(searchEntity, updateFactory); + await SaveChangesAsync(autoSave); return searchEntity; } + private void ModifyPartialFiled(TEntity waitModifyEntity, Expression> updateFactory) + { + List list = ((MemberInitExpression)updateFactory.Body).Bindings.Select(mb => mb.Member.Name) + .Select(propName => typeof(TEntity).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)).ToList(); + + Func func = updateFactory.Compile(); + + TEntity applyObj = func(waitModifyEntity); + + foreach (PropertyInfo prop in list) + { + object value = prop.GetValue(applyObj); + prop.SetValue(waitModifyEntity, value); + + _dbContext.Entry(waitModifyEntity).Property(prop.Name).IsModified = true; + } + } + /// EF跟踪方式 删除 public async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) { _dbSet.Remove(entity); - + if (autoSave) @@ -537,17 +548,17 @@ namespace IRaCIS.Core.Infra.EFCore /// public async Task SetEnum(Guid trilaid, string Identification, string json) { - var list =await (from u in _dbContext.FrontAuditConfig.Where(x=>x.Identification== Identification) - join p in _dbContext.FrontAuditConfig.Where(x=>x.Code== "AuditState"||(x.DictionaryCode!=null&& x.DictionaryCode != string.Empty&&x.DictionaryType != null&&x.DictionaryType != string.Empty)) on u.Id equals p.ParentId - select new - { - Key= p.Code, - Code= p.DictionaryCode, - Type= p.DictionaryType - }).ToListAsync(); + var list = await (from u in _dbContext.FrontAuditConfig.Where(x => x.Identification == Identification) + join p in _dbContext.FrontAuditConfig.Where(x => x.Code == "AuditState" || (x.DictionaryCode != null && x.DictionaryCode != string.Empty && x.DictionaryType != null && x.DictionaryType != string.Empty)) on u.Id equals p.ParentId + select new + { + Key = p.Code, + Code = p.DictionaryCode, + Type = p.DictionaryType + }).ToListAsync(); // 添加单双审 - var trialtype = await _dbContext.Trial.AsQueryable().Where(x => x.Id == trilaid).Select(x => x.QCProcessEnum).FirstOrDefaultAsync(); + var trialtype = await _dbContext.Trial.AsQueryable().Where(x => x.Id == trilaid).Select(x => x.QCProcessEnum).FirstOrDefaultAsync(); list.Add(new { @@ -709,7 +720,7 @@ namespace IRaCIS.Core.Infra.EFCore else if (typeof(TEntity) == typeof(DicomStudy)) { DicomStudy data = entity as DicomStudy; - await UpdateSubjectVisit(data.SubjectVisitId, SubmitStateEnum.ToSubmit,"上传Dicom影像"); + await UpdateSubjectVisit(data.SubjectVisitId, SubmitStateEnum.ToSubmit, "上传Dicom影像"); } // 非Dicom影像 else if (typeof(TEntity) == typeof(NoneDicomStudy)) @@ -719,7 +730,7 @@ namespace IRaCIS.Core.Infra.EFCore } // 修改访视状态记录稽查 - async Task UpdateSubjectVisit(Guid subvisitVisit, SubmitStateEnum submit,string reason) + async Task UpdateSubjectVisit(Guid subvisitVisit, SubmitStateEnum submit, string reason) { var subjectvisit = await _dbContext.SubjectVisit.AsNoTracking().AsQueryable().Where(x => x.Id == subvisitVisit).FirstOrDefaultAsync(); if (subjectvisit.SubmitState != submit) @@ -730,13 +741,13 @@ namespace IRaCIS.Core.Infra.EFCore List inspections = new List(); inspections.Add(new DataInspection() { - Identification= "Edit|Visit|Status|Visit-Image Upload|Add Image", - TrialId= subjectvisit.TrialId, - SiteId=subjectvisit.SiteId, - SubjectId=subjectvisit.SubjectId, - SubjectVisitId=subjectvisit.Id, - Reason= reason, - JsonDetail= subjectvisit.ToJcJson(), + Identification = "Edit|Visit|Status|Visit-Image Upload|Add Image", + TrialId = subjectvisit.TrialId, + SiteId = subjectvisit.SiteId, + SubjectId = subjectvisit.SubjectId, + SubjectVisitId = subjectvisit.Id, + Reason = reason, + JsonDetail = subjectvisit.ToJcJson(), }); } @@ -774,7 +785,7 @@ namespace IRaCIS.Core.Infra.EFCore { add.CreateTime = DateTime.Now; } - add.JsonDetail = await SetEnum(add.TrialId,add.Identification, add.JsonDetail); + add.JsonDetail = await SetEnum(add.TrialId, add.Identification, add.JsonDetail); await SetDataInspectionDateType(add); } @@ -782,7 +793,7 @@ namespace IRaCIS.Core.Infra.EFCore } - + /// /// IsNullOrEmpty