修改仓储
parent
5db7715e99
commit
2ca7a63053
|
@ -7,27 +7,23 @@ namespace IRaCIS.Application.Services
|
||||||
[ApiExplorerSettings(GroupName = "Reviewer")]
|
[ApiExplorerSettings(GroupName = "Reviewer")]
|
||||||
public class TrialExperienceService : BaseService, ITrialExperienceService
|
public class TrialExperienceService : BaseService, ITrialExperienceService
|
||||||
{
|
{
|
||||||
//private readonly IRepository<TrialExperience> _trialExperienceRepository;
|
private readonly IRepository<TrialExperience> _trialExperienceRepository;
|
||||||
//private readonly IRepository<Doctor> _doctorRepository;
|
private readonly IRepository<Doctor> _doctorRepository;
|
||||||
//private readonly IRepository<Attachment> _attachmentRepository;
|
private readonly IRepository<Attachment> _attachmentRepository;
|
||||||
//private readonly IRepository<TrialExperienceCriteria> _trialExperienceCriteriaRepository;
|
private readonly IRepository<TrialExperienceCriteria> _trialExperienceCriteriaRepository;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//public TrialExperienceService(IRepository<TrialExperience> trialExperienceRepository, IRepository<Doctor> doctorRepository, IRepository<Attachment> attachmentRepository,
|
|
||||||
// IRepository<TrialExperienceCriteria> trialExperienceCriteriaRepository)
|
|
||||||
//{
|
|
||||||
// _trialExperienceRepository = trialExperienceRepository;
|
|
||||||
// _doctorRepository = doctorRepository;
|
|
||||||
// _attachmentRepository = attachmentRepository;
|
|
||||||
// _trialExperienceCriteriaRepository = trialExperienceCriteriaRepository;
|
|
||||||
|
|
||||||
//}
|
|
||||||
|
|
||||||
private IQueryable<Doctor> _doctor => _repository.GetQueryable<Doctor>();
|
public TrialExperienceService(IRepository<TrialExperience> trialExperienceRepository, IRepository<Doctor> doctorRepository, IRepository<Attachment> attachmentRepository,
|
||||||
private IQueryable<Attachment> _attachment => _repository.GetQueryable<Attachment>();
|
IRepository<TrialExperienceCriteria> trialExperienceCriteriaRepository)
|
||||||
private IQueryable<TrialExperience> _trialExperience => _repository.GetQueryable<TrialExperience>();
|
{
|
||||||
private IQueryable<TrialExperienceCriteria> _trialExperienceCriteria => _repository.GetQueryable<TrialExperienceCriteria>();
|
_trialExperienceRepository = trialExperienceRepository;
|
||||||
|
_doctorRepository = doctorRepository;
|
||||||
|
_attachmentRepository = attachmentRepository;
|
||||||
|
_trialExperienceCriteriaRepository = trialExperienceCriteriaRepository;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 根据医生Id,获取临床试验经历 界面所有数据
|
/// 根据医生Id,获取临床试验经历 界面所有数据
|
||||||
|
@ -37,7 +33,7 @@ namespace IRaCIS.Application.Services
|
||||||
{
|
{
|
||||||
var trialExperience = new TrialExperienceModel();
|
var trialExperience = new TrialExperienceModel();
|
||||||
|
|
||||||
var doctor = await _doctor.Where(o => o.Id == doctorId)
|
var doctor = await _doctorRepository.Where(o => o.Id == doctorId)
|
||||||
.ProjectTo<TrialExperienceModel>(_mapper.ConfigurationProvider).FirstOrDefaultAsync();
|
.ProjectTo<TrialExperienceModel>(_mapper.ConfigurationProvider).FirstOrDefaultAsync();
|
||||||
|
|
||||||
trialExperience.ClinicalTrialExperienceList = await GetTrialExperienceList(doctorId);
|
trialExperience.ClinicalTrialExperienceList = await GetTrialExperienceList(doctorId);
|
||||||
|
@ -48,7 +44,7 @@ namespace IRaCIS.Application.Services
|
||||||
trialExperience.Id = doctor.Id;
|
trialExperience.Id = doctor.Id;
|
||||||
trialExperience.OtherClinicalExperience = doctor.OtherClinicalExperience ?? "";
|
trialExperience.OtherClinicalExperience = doctor.OtherClinicalExperience ?? "";
|
||||||
trialExperience.OtherClinicalExperienceCN = doctor.OtherClinicalExperienceCN ?? "";
|
trialExperience.OtherClinicalExperienceCN = doctor.OtherClinicalExperienceCN ?? "";
|
||||||
var attachment = await _attachment.FirstOrDefaultAsync(t => t.Id == doctor.GCPId);
|
var attachment = await _attachmentRepository.FirstOrDefaultAsync(t => t.Id == doctor.GCPId);
|
||||||
if (attachment != null)
|
if (attachment != null)
|
||||||
{
|
{
|
||||||
trialExperience.ExpiryDateStr = attachment.ExpiryDate == null ? "" : attachment.ExpiryDate.Value.ToString("yyyy-MM-dd HH:mm");
|
trialExperience.ExpiryDateStr = attachment.ExpiryDate == null ? "" : attachment.ExpiryDate.Value.ToString("yyyy-MM-dd HH:mm");
|
||||||
|
@ -66,7 +62,7 @@ namespace IRaCIS.Application.Services
|
||||||
|
|
||||||
private async Task<List<TrialExperienceListDTO>> GetTrialExperienceList(Guid doctorId)
|
private async Task<List<TrialExperienceListDTO>> GetTrialExperienceList(Guid doctorId)
|
||||||
{
|
{
|
||||||
var doctorClinicalTrialExperienceList = await _trialExperience.Where(o => o.DoctorId == doctorId).OrderBy(t => t.CreateTime)
|
var doctorClinicalTrialExperienceList = await _trialExperienceRepository.Where(o => o.DoctorId == doctorId).OrderBy(t => t.CreateTime)
|
||||||
.ProjectTo<TrialExperienceListDTO>(_mapper.ConfigurationProvider).ToListAsync();
|
.ProjectTo<TrialExperienceListDTO>(_mapper.ConfigurationProvider).ToListAsync();
|
||||||
|
|
||||||
return doctorClinicalTrialExperienceList;
|
return doctorClinicalTrialExperienceList;
|
||||||
|
@ -101,7 +97,7 @@ namespace IRaCIS.Application.Services
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var needUpdate = await _trialExperience.FirstOrDefaultAsync(t => t.Id == trialExperienceViewModel.Id);
|
var needUpdate = await _trialExperienceRepository.FirstOrDefaultAsync(t => t.Id == trialExperienceViewModel.Id);
|
||||||
|
|
||||||
if (needUpdate == null) return Null404NotFound(needUpdate);
|
if (needUpdate == null) return Null404NotFound(needUpdate);
|
||||||
|
|
||||||
|
|
|
@ -175,7 +175,7 @@ namespace IRaCIS.Core.Application.Contracts
|
||||||
}
|
}
|
||||||
|
|
||||||
var lockedLastSurvey = await _trialSiteSurveyRepository.Where(t => (t.Email == userInfo.ReplaceUserEmailOrPhone || t.Phone == userInfo.ReplaceUserEmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State == TrialSiteSurveyEnum.PMCreatedAndLock == true)
|
var lockedLastSurvey = await _trialSiteSurveyRepository.Where(t => (t.Email == userInfo.ReplaceUserEmailOrPhone || t.Phone == userInfo.ReplaceUserEmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State == TrialSiteSurveyEnum.PMCreatedAndLock == true)
|
||||||
.Include(u => u.TrialSiteEquipmentSurveyList).Include(u => u.TrialSiteUserSurveyList).OrderByDescending(t => t.CreateTime).FirstOrDefaultAsync().IfNullThrowConvertException();
|
.Include(u => u.TrialSiteEquipmentSurveyList).Include(u => u.TrialSiteUserSurveyList).OrderByDescending(t => t.CreateTime).FirstOrDefaultAsync().IfNullThrowException();
|
||||||
|
|
||||||
//Copy 一份 更换邮箱
|
//Copy 一份 更换邮箱
|
||||||
|
|
||||||
|
@ -622,7 +622,7 @@ namespace IRaCIS.Core.Application.Contracts
|
||||||
|
|
||||||
public async Task<IResponseOutput> AbandonSiteSurvey(Guid trialSiteSurveyId)
|
public async Task<IResponseOutput> AbandonSiteSurvey(Guid trialSiteSurveyId)
|
||||||
{
|
{
|
||||||
var survey = (await _trialSiteSurveyRepository.FirstOrDefaultAsync(t => t.Id == trialSiteSurveyId, true)).IfNullThrowConvertException();
|
var survey = (await _trialSiteSurveyRepository.FirstOrDefaultAsync(t => t.Id == trialSiteSurveyId, true)).IfNullThrowException();
|
||||||
|
|
||||||
if (survey.State != TrialSiteSurveyEnum.ToSubmit)
|
if (survey.State != TrialSiteSurveyEnum.ToSubmit)
|
||||||
{
|
{
|
||||||
|
|
|
@ -116,7 +116,7 @@ namespace IRaCIS.Application.Services
|
||||||
[HttpGet("{trialId:guid}")]
|
[HttpGet("{trialId:guid}")]
|
||||||
public async Task<int> GetTrialExpeditedState(Guid trialId)
|
public async Task<int> GetTrialExpeditedState(Guid trialId)
|
||||||
{
|
{
|
||||||
var trial = await _trialRepository.FirstOrDefaultAsync(u => u.Id == trialId).IfNullThrowConvertException();
|
var trial = (await _trialRepository.FirstOrDefaultAsync(u => u.Id == trialId)).IfNullThrowException();
|
||||||
|
|
||||||
return trial.Expedited;
|
return trial.Expedited;
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,6 +87,20 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
|
|
||||||
EntityEntry Entry(TEntity t);
|
EntityEntry Entry(TEntity t);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///不跟踪 查询单个实体,不会出现NUll
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="exp"></param>
|
||||||
|
/// <param name="ignoreQueryFilters"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> exp = null, bool isTracking = false);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///跟踪 查询单个实体,会出现NUll
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="exp"></param>
|
||||||
|
/// <param name="ignoreQueryFilters"></param>
|
||||||
|
/// <returns></returns>
|
||||||
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> exp = null, bool ignoreQueryFilters = false);
|
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> exp = null, bool ignoreQueryFilters = false);
|
||||||
|
|
||||||
Task<TEntity> FirstOrDefaultNoTrackingAsync(Expression<Func<TEntity, bool>> exp = null, bool ignoreQueryFilters = false);
|
Task<TEntity> FirstOrDefaultNoTrackingAsync(Expression<Func<TEntity, bool>> exp = null, bool ignoreQueryFilters = false);
|
||||||
|
@ -97,14 +111,9 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
|
|
||||||
Task<int> CountAsync(Expression<Func<TEntity, bool>> whereLambda = null, bool ignoreQueryFilters = false);
|
Task<int> CountAsync(Expression<Func<TEntity, bool>> whereLambda = null, bool ignoreQueryFilters = false);
|
||||||
|
|
||||||
ValueTask<TEntity> AddAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default, bool isSaveAudit = false);
|
ValueTask<TEntity> AddAsync(TEntity entity, bool autoSave = false);
|
||||||
|
|
||||||
Task<IEnumerable<TEntity>> AddRangeAsync(IEnumerable<TEntity> entities, bool isSaveAudit = false);
|
|
||||||
|
|
||||||
//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<IEnumerable<TEntity>> AddRangeAsync(IEnumerable<TEntity> entities, bool autoSave = false);
|
||||||
|
|
||||||
Task<bool> SaveChangesAsync(CancellationToken cancellationToken = default);
|
Task<bool> SaveChangesAsync(CancellationToken cancellationToken = default);
|
||||||
Task<bool> DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
|
Task<bool> DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
|
||||||
|
|
|
@ -13,19 +13,10 @@ using System.Linq.Expressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using EFCore.BulkExtensions;
|
using EFCore.BulkExtensions;
|
||||||
using IRaCIS.Core.Domain.Share;
|
using IRaCIS.Core.Domain.Share;
|
||||||
using IRaCIS.Core.Infra.EFCore.Common.Dto;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
namespace IRaCIS.Core.Infra.EFCore
|
namespace IRaCIS.Core.Infra.EFCore
|
||||||
{
|
{
|
||||||
public interface IRepository<TEntity> : ICommandRepository<TEntity>, IQueryRepository<TEntity> where TEntity : Entity
|
|
||||||
{
|
|
||||||
IRaCISDBContext _dbContext { get; set; }
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#region 泛型通用版本
|
#region 泛型通用版本
|
||||||
|
|
||||||
|
@ -34,16 +25,12 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
{
|
{
|
||||||
IQueryable<T> GetQueryable<T>(bool isTraking = false) where T : Entity;
|
IQueryable<T> GetQueryable<T>(bool isTraking = false) where T : Entity;
|
||||||
DbSet<T> Set<T>() where T : Entity;
|
DbSet<T> Set<T>() where T : Entity;
|
||||||
|
EntityEntry<T> Entry<T>(T t) where T : Entity;
|
||||||
|
|
||||||
|
Task<bool> SaveChangesAsync();
|
||||||
|
|
||||||
IQueryable<T> WhereIf<T>(bool condition, Expression<Func<T, bool>> filter) where T : Entity;
|
IQueryable<T> WhereIf<T>(bool condition, Expression<Func<T, bool>> filter) where T : Entity;
|
||||||
|
|
||||||
Task<T> InsertOrUpdateAsync<T, TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<T>[] verify) where T : Entity;
|
|
||||||
|
|
||||||
Task<T> InsertFromDTOAsync<T, TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<T>[] verify) where T : Entity;
|
|
||||||
|
|
||||||
Task<T> UpdateFromDTOAsync<T, TFrom>(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp<T>[] verify) where T : Entity;
|
|
||||||
|
|
||||||
EntityEntry<T> Entry<T>(T t) where T : Entity;
|
|
||||||
|
|
||||||
Task<bool> AnyAsync<T>(Expression<Func<T, bool>> filter, bool ignoreQueryFilters = false) where T : Entity;
|
Task<bool> AnyAsync<T>(Expression<Func<T, bool>> filter, bool ignoreQueryFilters = false) where T : Entity;
|
||||||
|
|
||||||
|
@ -54,6 +41,16 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
|
|
||||||
ValueTask<T> FindAsync<T>(Guid id) where T : Entity;
|
ValueTask<T> FindAsync<T>(Guid id) where T : Entity;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Task<T> InsertOrUpdateAsync<T, TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<T>[] verify) where T : Entity;
|
||||||
|
|
||||||
|
Task<T> InsertFromDTOAsync<T, TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<T>[] verify) where T : Entity;
|
||||||
|
|
||||||
|
Task<T> UpdateFromDTOAsync<T, TFrom>(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp<T>[] verify) where T : Entity;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ValueTask<T> AddAsync<T>(T entity, bool autoSave = false) where T : Entity;
|
ValueTask<T> AddAsync<T>(T entity, bool autoSave = false) where T : Entity;
|
||||||
|
|
||||||
Task UpdateRange<T>(IEnumerable<T> entities, bool autoSave = false) where T : Entity;
|
Task UpdateRange<T>(IEnumerable<T> entities, bool autoSave = false) where T : Entity;
|
||||||
|
@ -63,9 +60,6 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
|
|
||||||
Task<bool> DeleteAsync<T>(T entity, bool autoSave = false) where T : Entity;
|
Task<bool> DeleteAsync<T>(T entity, bool autoSave = false) where T : Entity;
|
||||||
|
|
||||||
//Task<bool> DeleteManyAsync<T>(IEnumerable<T> entities, bool autoSave = false) where T : Entity;
|
|
||||||
|
|
||||||
Task<bool> SaveChangesAsync();
|
|
||||||
|
|
||||||
Task<bool> BatchDeleteAsync<T>(Expression<Func<T, bool>> deleteFilter) where T : Entity;
|
Task<bool> BatchDeleteAsync<T>(Expression<Func<T, bool>> deleteFilter) where T : Entity;
|
||||||
|
|
||||||
|
@ -78,10 +72,8 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
|
|
||||||
public class Repository : IRepository
|
public class Repository : IRepository
|
||||||
{
|
{
|
||||||
|
#region 构造 基本
|
||||||
private IRaCISDBContext _dbContext { get; }
|
private IRaCISDBContext _dbContext { get; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public IMapper _mapper { get; set; }
|
public IMapper _mapper { get; set; }
|
||||||
|
|
||||||
public IUserInfo _userInfo { get; set; }
|
public IUserInfo _userInfo { get; set; }
|
||||||
|
@ -99,7 +91,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="isTraking"></param>
|
/// <param name="isTraking"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|
||||||
public IQueryable<T> GetQueryable<T>(bool isTraking = false) where T : Entity
|
public IQueryable<T> GetQueryable<T>(bool isTraking = false) where T : Entity
|
||||||
{
|
{
|
||||||
IQueryable<T> query = _dbContext.Set<T>();
|
IQueryable<T> query = _dbContext.Set<T>();
|
||||||
|
@ -115,12 +107,39 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
return _dbContext.Set<T>();
|
return _dbContext.Set<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EntityEntry<T> Entry<T>(T t) where T : Entity
|
||||||
|
{
|
||||||
|
return _dbContext.Entry<T>(t);
|
||||||
|
}
|
||||||
public IQueryable<T> WhereIf<T>(bool condition, Expression<Func<T, bool>> filter) where T : Entity
|
public IQueryable<T> WhereIf<T>(bool condition, Expression<Func<T, bool>> filter) where T : Entity
|
||||||
{
|
{
|
||||||
IQueryable<T> query = _dbContext.Set<T>().AsNoTracking();
|
IQueryable<T> query = _dbContext.Set<T>().AsNoTracking();
|
||||||
return condition ? query.Where(filter) : query;
|
return condition ? query.Where(filter) : query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<bool> SaveChangesAsync(bool autoSave)
|
||||||
|
{
|
||||||
|
if (autoSave)
|
||||||
|
{
|
||||||
|
return await SaveChangesAsync();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> SaveChangesAsync()
|
||||||
|
{
|
||||||
|
|
||||||
|
return await _dbContext.SaveChangesAsync().ConfigureAwait(false) > 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private async Task EntityVerifyAsync<T>(bool isAdd, EntityVerifyExp<T>[] verify, Guid? entitydId = null) where T : Entity
|
private async Task EntityVerifyAsync<T>(bool isAdd, EntityVerifyExp<T>[] verify, Guid? entitydId = null) where T : Entity
|
||||||
{
|
{
|
||||||
|
@ -129,7 +148,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
{
|
{
|
||||||
foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyUpdate && t.IsVerify))
|
foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyUpdate && t.IsVerify))
|
||||||
{
|
{
|
||||||
if (await _dbContext.Set<T>().IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false))
|
if (await _dbContext.Set<T>().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
|
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
|
||||||
}
|
}
|
||||||
|
@ -141,14 +160,14 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
{
|
{
|
||||||
if (verifyItem.verifyType == VerifyEnum.OnlyUpdate)
|
if (verifyItem.verifyType == VerifyEnum.OnlyUpdate)
|
||||||
{
|
{
|
||||||
if (await _dbContext.Set<T>().IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false))
|
if (await _dbContext.Set<T>().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
|
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (verifyItem.verifyType == VerifyEnum.Both)
|
else if (verifyItem.verifyType == VerifyEnum.Both)
|
||||||
{
|
{
|
||||||
if (await _dbContext.Set<T>().IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp.And(t => t.Id != entitydId)).ConfigureAwait(false))
|
if (await _dbContext.Set<T>().AnyAsync(verifyItem.VerifyExp.And(t => t.Id != entitydId)).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
|
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
|
||||||
}
|
}
|
||||||
|
@ -166,7 +185,6 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
|
|
||||||
await EntityVerifyAsync(true, verify);
|
await EntityVerifyAsync(true, verify);
|
||||||
|
|
||||||
|
|
||||||
entity = await AddAsync(entity, autoSave);
|
entity = await AddAsync(entity, autoSave);
|
||||||
|
|
||||||
|
|
||||||
|
@ -214,18 +232,6 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private async Task<bool> SaveChangesAsync(bool autoSave)
|
|
||||||
{
|
|
||||||
if (autoSave)
|
|
||||||
{
|
|
||||||
return await SaveChangesAsync();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public async Task<T> InsertOrUpdateAsync<T, TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<T>[] verify) where T : Entity
|
public async Task<T> InsertOrUpdateAsync<T, TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<T>[] verify) where T : Entity
|
||||||
{
|
{
|
||||||
|
@ -242,10 +248,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityEntry<T> Entry<T>(T t) where T : Entity
|
|
||||||
{
|
|
||||||
return _dbContext.Entry<T>(t);
|
|
||||||
}
|
|
||||||
public async Task<bool> AnyAsync<T>(Expression<Func<T, bool>> filter, bool ignoreQueryFilters = false) where T : Entity
|
public async Task<bool> AnyAsync<T>(Expression<Func<T, bool>> filter, bool ignoreQueryFilters = false) where T : Entity
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -261,7 +264,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#region 基本查询 异步
|
#region 基本查询 异步
|
||||||
|
|
||||||
|
@ -368,39 +371,22 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
{
|
{
|
||||||
await _dbContext.Set<T>().AddRangeAsync(entities).ConfigureAwait(false);
|
await _dbContext.Set<T>().AddRangeAsync(entities).ConfigureAwait(false);
|
||||||
|
|
||||||
if (autoSave)
|
return await SaveChangesAsync(autoSave);
|
||||||
{
|
|
||||||
return await SaveChangesAsync();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateRange<T>(IEnumerable<T> entities, bool autoSave = false) where T : Entity
|
public async Task UpdateRange<T>(IEnumerable<T> entities, bool autoSave = false) where T : Entity
|
||||||
{
|
{
|
||||||
_dbContext.Set<T>().UpdateRange(entities);
|
_dbContext.Set<T>().UpdateRange(entities);
|
||||||
|
|
||||||
if (autoSave)
|
await SaveChangesAsync(autoSave);
|
||||||
{
|
|
||||||
await SaveChangesAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> UpdateAsync<T>(T entity, bool autoSave = false) where T : Entity
|
public async Task<bool> UpdateAsync<T>(T entity, bool autoSave = false) where T : Entity
|
||||||
{
|
{
|
||||||
_dbContext.Set<T>().Update(entity);
|
_dbContext.Set<T>().Update(entity);
|
||||||
|
|
||||||
if (autoSave)
|
return await SaveChangesAsync(autoSave);
|
||||||
{
|
|
||||||
return await SaveChangesAsync();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -408,36 +394,19 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
{
|
{
|
||||||
_dbContext.Set<T>().Remove(entity);
|
_dbContext.Set<T>().Remove(entity);
|
||||||
|
|
||||||
if (autoSave)
|
return await SaveChangesAsync(autoSave);
|
||||||
{
|
|
||||||
return await SaveChangesAsync();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> DeleteManyAsync<T>(IEnumerable<T> entities, bool autoSave = false) where T : Entity
|
public async Task<bool> DeleteManyAsync<T>(IEnumerable<T> entities, bool autoSave = false) where T : Entity
|
||||||
{
|
{
|
||||||
_dbContext.Set<T>().RemoveRange(entities);
|
_dbContext.Set<T>().RemoveRange(entities);
|
||||||
|
|
||||||
if (autoSave)
|
return await SaveChangesAsync(autoSave);
|
||||||
{
|
|
||||||
return await SaveChangesAsync();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> SaveChangesAsync()
|
|
||||||
{
|
|
||||||
|
|
||||||
return await _dbContext.SaveChangesAsync().ConfigureAwait(false) > 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
|
@ -14,13 +14,15 @@ using EFCore.BulkExtensions;
|
||||||
using IRaCIS.Core.Domain.Share;
|
using IRaCIS.Core.Domain.Share;
|
||||||
using IRaCIS.Core.Infrastructure;
|
using IRaCIS.Core.Infrastructure;
|
||||||
using IRaCIS.Core.Infrastructure.Extention;
|
using IRaCIS.Core.Infrastructure.Extention;
|
||||||
using Microsoft.Data.SqlClient;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
using IRaCIS.Core.Infra.EFCore.Common.Dto;
|
|
||||||
|
|
||||||
namespace IRaCIS.Core.Infra.EFCore
|
namespace IRaCIS.Core.Infra.EFCore
|
||||||
{
|
{
|
||||||
|
public interface IRepository<TEntity> : ICommandRepository<TEntity>, IQueryRepository<TEntity> where TEntity : Entity
|
||||||
|
{
|
||||||
|
IRaCISDBContext _dbContext { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public class Repository<TEntity> : IRepository<TEntity>
|
public class Repository<TEntity> : IRepository<TEntity>
|
||||||
where TEntity : Entity, new()
|
where TEntity : Entity, new()
|
||||||
{
|
{
|
||||||
|
@ -42,29 +44,23 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
|
|
||||||
|
|
||||||
#region 异步 EF 跟踪 添加
|
#region 异步 EF 跟踪 添加
|
||||||
public async Task<IEnumerable<TEntity>> AddRangeAsync(IEnumerable<TEntity> entities, bool isSaveAudit = false)
|
public async Task<IEnumerable<TEntity>> AddRangeAsync(IEnumerable<TEntity> entities, bool autoSave = false)
|
||||||
{
|
{
|
||||||
foreach (var addEntity in entities)
|
|
||||||
{
|
await _dbSet.AddRangeAsync(entities).ConfigureAwait(false);
|
||||||
await AddAsync(addEntity, isSaveAudit);
|
|
||||||
}
|
await SaveChangesAsync(autoSave);
|
||||||
|
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>EF跟踪方式 添加</summary>
|
/// <summary>EF跟踪方式 添加</summary>
|
||||||
public async ValueTask<TEntity> AddAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default, bool isSaveAudit = false)
|
public async ValueTask<TEntity> AddAsync(TEntity entity, bool autoSave = false)
|
||||||
{
|
{
|
||||||
|
|
||||||
await _dbSet.AddAsync(entity).ConfigureAwait(false);
|
await _dbSet.AddAsync(entity).ConfigureAwait(false);
|
||||||
|
|
||||||
// 添加稽查
|
await SaveChangesAsync(autoSave);
|
||||||
//await AddInspectionAsync(entity, isSaveAudit);
|
|
||||||
|
|
||||||
if (autoSave)
|
|
||||||
{
|
|
||||||
await SaveChangesAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
@ -73,10 +69,10 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
|
|
||||||
public async Task<TEntity> InsertFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
|
public async Task<TEntity> InsertFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
|
||||||
{
|
{
|
||||||
var entity = _mapper.Map<TEntity>(from);
|
|
||||||
|
|
||||||
await EntityVerifyAsync(true, verify);
|
await EntityVerifyAsync(true, verify);
|
||||||
|
|
||||||
|
var entity = _mapper.Map<TEntity>(from);
|
||||||
|
|
||||||
entity = await AddAsync(entity, autoSave);
|
entity = await AddAsync(entity, autoSave);
|
||||||
|
|
||||||
|
@ -338,11 +334,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
var query = ignoreQueryFilter ? _dbSet.AsNoTracking().IgnoreQueryFilters() : _dbSet.AsNoTracking();
|
var query = ignoreQueryFilter ? _dbSet.AsNoTracking().IgnoreQueryFilters() : _dbSet.AsNoTracking();
|
||||||
var waitDeleteList = await query.Where(deleteFilter).ToListAsync();
|
var waitDeleteList = await query.Where(deleteFilter).ToListAsync();
|
||||||
|
|
||||||
foreach (var deleteItem in waitDeleteList)
|
_dbSet.RemoveRange(waitDeleteList);
|
||||||
{
|
|
||||||
await DeleteAsync(deleteItem, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
await SaveChangesAsync(autoSave);
|
await SaveChangesAsync(autoSave);
|
||||||
|
|
||||||
return waitDeleteList;
|
return waitDeleteList;
|
||||||
|
@ -352,11 +344,11 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
public async Task<List<TEntity>> SoftDeleteFromQueryAsync(Expression<Func<TEntity, bool>> deleteFilter, bool autoSave = false, bool ignoreQueryFilter = false)
|
public async Task<List<TEntity>> SoftDeleteFromQueryAsync(Expression<Func<TEntity, bool>> deleteFilter, bool autoSave = false, bool ignoreQueryFilter = false)
|
||||||
{
|
{
|
||||||
var query = ignoreQueryFilter ? _dbSet.IgnoreQueryFilters() : _dbSet;
|
var query = ignoreQueryFilter ? _dbSet.IgnoreQueryFilters() : _dbSet;
|
||||||
var waitDeleteList = await query.Where(deleteFilter).ToListAsync();
|
var waitDeleteList = await query.Where(deleteFilter).ToListAsync();
|
||||||
|
|
||||||
foreach (var deleteItem in waitDeleteList)
|
foreach (var deleteItem in waitDeleteList)
|
||||||
{
|
{
|
||||||
if(deleteItem is ISoftDelete softDeleteItem)
|
if (deleteItem is ISoftDelete softDeleteItem)
|
||||||
{
|
{
|
||||||
softDeleteItem.IsDeleted = true;
|
softDeleteItem.IsDeleted = true;
|
||||||
}
|
}
|
||||||
|
@ -618,6 +610,30 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
return await _dbContext.FindAsync<TEntity>(keyValues);
|
return await _dbContext.FindAsync<TEntity>(keyValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> exp = null, bool isTracking = false)
|
||||||
|
{
|
||||||
|
|
||||||
|
var query = _dbSet.AsQueryable();
|
||||||
|
|
||||||
|
if (!isTracking)
|
||||||
|
{
|
||||||
|
query = query.AsNoTracking();
|
||||||
|
}
|
||||||
|
|
||||||
|
var entity = await query.FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
if (entity is null)
|
||||||
|
{
|
||||||
|
throw new QueryBusinessObjectNotExistException($"The query object {typeof(TEntity).Name} does not exist in database, Please check the query parameters");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> exp = null, bool ignoreQueryFilters = false)
|
public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> exp = null, bool ignoreQueryFilters = false)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -764,79 +780,6 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region 待废弃
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 仅仅供字典表插入使用,因为efcore 动态映射列的问题
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TFrom"></typeparam>
|
|
||||||
/// <param name="from"></param>
|
|
||||||
/// <param name="verify"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
/// <exception cref="BusinessValidationFailedException"></exception>
|
|
||||||
public async Task<TEntity> InsertDictionaryAsync<TFrom>(TFrom from, params EntityVerifyExp<TEntity>[] verify)
|
|
||||||
{
|
|
||||||
var entity = _mapper.Map<TEntity>(from);
|
|
||||||
|
|
||||||
|
|
||||||
foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyUpdate && t.IsVerify))
|
|
||||||
{
|
|
||||||
if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false))
|
|
||||||
{
|
|
||||||
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof(TEntity) == typeof(Dictionary))
|
|
||||||
{
|
|
||||||
Type type = typeof(TFrom);
|
|
||||||
|
|
||||||
//以下是不要ID这个字段的 比如自增列ID 就不能像上名那样写
|
|
||||||
var properties = type.GetProperties().Where(t => t.Name != "Id");
|
|
||||||
|
|
||||||
|
|
||||||
string strSqlName = string.Join(",", properties.Select(p => $"[{p.Name}]").ToArray());
|
|
||||||
|
|
||||||
string strSqlValue = string.Join(",", properties.Select(P => $"@{P.Name}").ToArray());
|
|
||||||
|
|
||||||
string strSql = $"insert into {nameof(Dictionary)} ( " + strSqlName + " ) values (" + strSqlValue + ")";
|
|
||||||
|
|
||||||
//para Sql是参数
|
|
||||||
SqlParameter[] para = properties.Select(p => new SqlParameter($"@{p.Name}", p.GetValue(from, null))).ToArray();
|
|
||||||
|
|
||||||
|
|
||||||
_dbContext.Database.ExecuteSqlRaw(strSql, para);
|
|
||||||
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception("仅仅供字典表插入使用,因为efcore 动态映射列的问题");
|
|
||||||
//await _dbSet.BulkInsertAsync(new List<TEntity>() { entity });
|
|
||||||
|
|
||||||
//return entity;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ namespace IRaCIS.Core.Infrastructure.Extention
|
||||||
{
|
{
|
||||||
if(businessObject == null)
|
if(businessObject == null)
|
||||||
{
|
{
|
||||||
throw new QueryBusinessObjectNotExistException($"The query object {typeof(TEntity).Name} does not exist , or was deleted by someone else, or an incorrect parameter query caused");
|
throw new QueryBusinessObjectNotExistException($"The query object {typeof(TEntity).Name} does not exist in database, Please check the query parameters");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -25,7 +25,7 @@ namespace IRaCIS.Core.Infrastructure.Extention
|
||||||
{
|
{
|
||||||
if (businessStruct == null)
|
if (businessStruct == null)
|
||||||
{
|
{
|
||||||
throw new QueryBusinessObjectNotExistException($"The query object {typeof(TEntity).Name} does not exist , or was deleted by someone else, or an incorrect parameter query caused");
|
throw new QueryBusinessObjectNotExistException($"The query object {typeof(TEntity).Name} does not exist in database, Please check the query parameters");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
Loading…
Reference in New Issue