修改仓储实现
parent
c968308905
commit
9e484d2089
|
@ -16,6 +16,7 @@ namespace IRaCIS.Core.Application.Triggers
|
|||
public async Task AfterSave(ITriggerContext<NoneDicomStudy> context, CancellationToken cancellationToken)
|
||||
{
|
||||
var subjectVisitId = context.Entity.SubjectVisitId;
|
||||
|
||||
if (context.ChangeType == ChangeType.Added || context.ChangeType == ChangeType.Modified)
|
||||
{
|
||||
await UpdateSubjectVisitImageDateAsync(context.Entity.SubjectVisitId);
|
||||
|
|
|
@ -33,6 +33,10 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
/// <summary>批量更新,相当于原生sql, 没用EF跟踪方式(所有查询出来,再更新 浪费性能)</summary>
|
||||
Task<bool> BatchUpdateAsync(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TEntity>> updateFactory);
|
||||
|
||||
|
||||
/// <summary>批量删除,EF跟踪方式(所有查询出来,再删除 浪费性能,但是稽查 或者触发某些操作时,需要知道数据库实体信息 不可避免用这种)</summary>
|
||||
Task TrackingBatchDeleteAsync(Expression<Func<TEntity, bool>> deleteFilter);
|
||||
|
||||
}
|
||||
|
||||
public interface ICommandRepository<TEntity, TKey> where TEntity : class
|
||||
|
@ -53,7 +57,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
|
||||
Task<IEnumerable<TEntity>> AddRangeAsync(IEnumerable<TEntity> entities);
|
||||
|
||||
Task<bool> AddRangeAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default);
|
||||
//Task<bool> AddRangeAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default);
|
||||
// 不建议使用,使用跟踪,然后save 部分字段更新,此种方式是更新所有字段
|
||||
Task<bool> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
|
||||
Task<bool> SaveChangesAsync(CancellationToken cancellationToken = default);
|
||||
|
|
|
@ -42,7 +42,50 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
|
||||
#region 异步部分
|
||||
|
||||
/// <summary>EF跟踪方式 添加</summary>
|
||||
public async ValueTask<TEntity> AddAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
await _dbSet.AddAsync(entity).ConfigureAwait(false);
|
||||
|
||||
await AddInspectionAsync(entity);
|
||||
|
||||
if (autoSave)
|
||||
{
|
||||
await SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
||||
private async Task AddInspectionAsync(TEntity entity)
|
||||
{
|
||||
List<DataInspection> datas = new List<DataInspection>();
|
||||
|
||||
var createtime = DateTime.Now.AddSeconds(1);
|
||||
|
||||
// 受试者
|
||||
if (typeof(TEntity) == typeof(Subject))
|
||||
{
|
||||
Subject data = entity as Subject;
|
||||
datas.Add(new DataInspection()
|
||||
{
|
||||
TrialId = data.TrialId,
|
||||
SiteId = data.SiteId,
|
||||
SubjectId = data.Id,
|
||||
SubjectCode = data.Code,
|
||||
IsSign = false,
|
||||
CreateTime = createtime,
|
||||
Identification = "Init|Subject|Status|Subject",
|
||||
JsonDetail = JsonConvert.SerializeObject(new
|
||||
{
|
||||
Status = "新增",
|
||||
})
|
||||
});
|
||||
}
|
||||
await AddListInspectionRecordAsync(datas);
|
||||
}
|
||||
|
||||
public async Task<TEntity> InsertOrUpdateAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
|
||||
{
|
||||
|
@ -98,46 +141,21 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
|
||||
public async Task<TEntity> InsertFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
|
||||
{
|
||||
List<DataInspection> datas = new List<DataInspection>();
|
||||
var entity = _mapper.Map<TEntity>(from);
|
||||
|
||||
await EntityVerifyAsync(true, verify);
|
||||
|
||||
await _dbSet.AddAsync(entity).ConfigureAwait(false);
|
||||
|
||||
await SaveChangesAsync(autoSave);
|
||||
entity= await AddAsync(entity);
|
||||
|
||||
|
||||
|
||||
var createtime = DateTime.Now.AddSeconds(1);
|
||||
|
||||
// 受试者
|
||||
if (typeof(TEntity) == typeof(Subject))
|
||||
{
|
||||
Subject data = entity as Subject;
|
||||
datas.Add(new DataInspection()
|
||||
{
|
||||
TrialId = data.TrialId,
|
||||
SiteId = data.SiteId,
|
||||
SubjectId = data.Id,
|
||||
SubjectCode = data.Code,
|
||||
IsSign = false,
|
||||
CreateTime = createtime,
|
||||
Identification = "Init|Subject|Status|Subject",
|
||||
JsonDetail = JsonConvert.SerializeObject(new
|
||||
{
|
||||
Status = "新增",
|
||||
})
|
||||
});
|
||||
}
|
||||
await AddListInspectionRecordAsync(datas);
|
||||
|
||||
await SaveChangesAsync(autoSave);
|
||||
return entity;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#region 稽查
|
||||
/// <summary>
|
||||
/// 添加稽查记录
|
||||
|
@ -221,8 +239,8 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
join child in _dbContext.FrontAuditConfig.AsQueryable().Where(x => x.DateType != null && x.DateType != string.Empty) on parent.Id equals child.ParentId
|
||||
select new DateDto()
|
||||
{
|
||||
Code= child.Code,
|
||||
DateType= child.DateType,
|
||||
Code = child.Code,
|
||||
DateType = child.DateType,
|
||||
}).ToListAsync();
|
||||
|
||||
var JsonData = JsonConvert.DeserializeObject<IDictionary<string, object>>(Data.JsonDetail);
|
||||
|
@ -230,7 +248,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
foreach (var item in JsonData.Keys)
|
||||
{
|
||||
var datefirst = list.FirstOrDefault(x => x.Code.ToLower() == item.ToLower());
|
||||
if (datefirst != null&& !IsNullOrEmpty(JsonData[item]))
|
||||
if (datefirst != null && !IsNullOrEmpty(JsonData[item]))
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -270,7 +288,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
{
|
||||
#region 项目名称
|
||||
|
||||
var trialdata = await _dbContext.Trial.Select(x=>new { x.Id, x.ResearchProgramNo, x.ExperimentName, }).FirstOrDefaultAsync(x => x.Id == Data.TrialId);
|
||||
var trialdata = await _dbContext.Trial.Select(x => new { x.Id, x.ResearchProgramNo, x.ExperimentName, }).FirstOrDefaultAsync(x => x.Id == Data.TrialId);
|
||||
Data.ResearchProgramNo = trialdata?.ResearchProgramNo;
|
||||
if (IsNullOrEmpty(Data.TrialName))
|
||||
{
|
||||
|
@ -285,9 +303,9 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
|
||||
|
||||
|
||||
if (IsNullOrEmpty(Data.SiteName)&& Data.SiteId!=null)
|
||||
if (IsNullOrEmpty(Data.SiteName) && Data.SiteId != null)
|
||||
{
|
||||
var sitedata = await _dbContext.Site.Where(x => x.Id == Data.SiteId).Select(x=>new { x.SiteName}).FirstOrDefaultAsync();
|
||||
var sitedata = await _dbContext.Site.Where(x => x.Id == Data.SiteId).Select(x => new { x.SiteName }).FirstOrDefaultAsync();
|
||||
Data.SiteName = sitedata?.SiteName;
|
||||
}
|
||||
#endregion
|
||||
|
@ -298,14 +316,14 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
if (IsNullOrEmpty(Data.SubjectCode) && Data.SubjectId != null)
|
||||
{
|
||||
|
||||
Data.SubjectCode = (await _dbContext.Subject.Where(x => x.Id == Data.SubjectId).Select(x=>new {x.Code }).FirstOrDefaultAsync())?.Code;
|
||||
Data.SubjectCode = (await _dbContext.Subject.Where(x => x.Id == Data.SubjectId).Select(x => new { x.Code }).FirstOrDefaultAsync())?.Code;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 访视
|
||||
if (IsNullOrEmpty(Data.SubjectVisitName))
|
||||
{
|
||||
Data.SubjectVisitName = (await _dbContext.SubjectVisit.Where(x => x.Id == Data.SubjectVisitId).Select(x=>new { x.VisitName}).FirstOrDefaultAsync())?.VisitName;
|
||||
Data.SubjectVisitName = (await _dbContext.SubjectVisit.Where(x => x.Id == Data.SubjectVisitId).Select(x => new { x.VisitName }).FirstOrDefaultAsync())?.VisitName;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
@ -629,42 +647,19 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
return await query.FirstOrDefaultAsync(exp).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async ValueTask<TEntity> AddAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
await _dbSet.AddAsync(entity).ConfigureAwait(false);
|
||||
|
||||
if (autoSave)
|
||||
{
|
||||
await SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
||||
public async Task<IEnumerable<TEntity>> AddRangeAsync(IEnumerable<TEntity> entities)
|
||||
{
|
||||
await _dbSet.AddRangeAsync(entities).ConfigureAwait(false);
|
||||
foreach (var addEntity in entities)
|
||||
{
|
||||
await AddAsync(addEntity);
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> AddRangeAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _dbSet.AddRangeAsync(entities).ConfigureAwait(false);
|
||||
|
||||
if (autoSave)
|
||||
{
|
||||
return await SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -699,7 +694,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
return whereLambda == null ? await query.AsNoTracking().CountAsync() : await query.AsNoTracking().CountAsync(whereLambda);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>EF跟踪方式 更新,全字段更新</summary>
|
||||
public async Task<bool> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_dbSet.Update(entity);
|
||||
|
@ -715,14 +710,11 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>EF跟踪方式 删除</summary>
|
||||
public async Task<bool> DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_dbSet.Remove(entity);
|
||||
|
||||
//var entry = _dbSet.Attach(entity);
|
||||
//entry.State = EntityState.Deleted;
|
||||
|
||||
if (autoSave)
|
||||
{
|
||||
return await SaveChangesAsync(cancellationToken);
|
||||
|
@ -734,13 +726,27 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
}
|
||||
|
||||
|
||||
/// <summary>批量删除,EF跟踪方式(所有查询出来,再删除 浪费性能,但是稽查 或者触发某些操作时,需要知道数据库实体信息 不可避免用这种)</summary>
|
||||
public async Task TrackingBatchDeleteAsync(Expression<Func<TEntity, bool>> deleteFilter)
|
||||
{
|
||||
var waitDeleteList = await _dbSet.IgnoreQueryFilters().Where(deleteFilter).ToListAsync();
|
||||
|
||||
foreach (var deleteItem in waitDeleteList)
|
||||
{
|
||||
await DeleteAsync(deleteItem, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>批量删除,相当于原生sql, 没用EF跟踪方式(所有查询出来,再删除 浪费性能)</summary>
|
||||
public async Task<bool> BatchDeleteAsync(Expression<Func<TEntity, bool>> deleteFilter)
|
||||
{
|
||||
return await _dbSet.IgnoreQueryFilters().Where(deleteFilter).BatchDeleteAsync() > 0;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>批量更新,相当于原生sql, 没用EF跟踪方式(所有查询出来,再更新 浪费性能)</summary>
|
||||
public async Task<bool> BatchUpdateAsync(Expression<Func<TEntity, bool>> where,
|
||||
Expression<Func<TEntity, TEntity>> updateFactory)
|
||||
{
|
||||
|
@ -757,7 +763,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
|
||||
}
|
||||
|
||||
if (!hasPropNameList.Contains( nameof(IAuditUpdate.UpdateUserId)))
|
||||
if (!hasPropNameList.Contains(nameof(IAuditUpdate.UpdateUserId)))
|
||||
{
|
||||
bindings.Add(Expression.Bind(typeof(TEntity).GetMember(nameof(IAuditUpdate.UpdateUserId))[0], Expression.Constant(_userInfo.Id)));
|
||||
|
||||
|
@ -932,6 +938,20 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
}
|
||||
}
|
||||
|
||||
public async Task<bool> AddRangeAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _dbSet.AddRangeAsync(entities).ConfigureAwait(false);
|
||||
|
||||
if (autoSave)
|
||||
{
|
||||
return await SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue