diff --git a/IRaCIS.Core.Application/Service/Common/DictionaryService.cs b/IRaCIS.Core.Application/Service/Common/DictionaryService.cs index 4a7a8be9c..af6ccffb7 100644 --- a/IRaCIS.Core.Application/Service/Common/DictionaryService.cs +++ b/IRaCIS.Core.Application/Service/Common/DictionaryService.cs @@ -59,10 +59,19 @@ namespace IRaCIS.Application.Services [HttpPost] public async Task AddOrUpdateBasicDic(AddOrEditBasicDic addOrEditBasic) { + if (addOrEditBasic.Id == null) + { + var entity = await _dicRepository.InsertDictionaryAsync(addOrEditBasic); + return ResponseOutput.Ok(entity.Id.ToString()); + } + else + { + var entity = await _dicRepository.InsertOrUpdateAsync(addOrEditBasic, true); + return ResponseOutput.Ok(entity.Id.ToString()); + } + //var entity = await _dicRepository.InsertOrUpdateAsync(addOrEditBasic, true); - var entity = await _repository.InsertOrUpdateAsync(addOrEditBasic, true); - - return ResponseOutput.Ok(entity.Id.ToString()); + //return ResponseOutput.Ok(entity.Id.ToString()); } diff --git a/IRaCIS.Core.Application/Service/TrialSiteUser/PersonalWorkstation.cs b/IRaCIS.Core.Application/Service/TrialSiteUser/PersonalWorkstation.cs index f9a734d0d..4e6418c07 100644 --- a/IRaCIS.Core.Application/Service/TrialSiteUser/PersonalWorkstation.cs +++ b/IRaCIS.Core.Application/Service/TrialSiteUser/PersonalWorkstation.cs @@ -91,17 +91,26 @@ namespace IRaCIS.Core.Application [HttpPost] public async Task> GetSiteSurveyApprovalList(TrialSiteSurveyStatQuery query) { - if (_userInfo.IsAdmin) + + + if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager || + _userInfo.UserTypeEnumInt == (int)UserTypeEnum.APM || + _userInfo.UserTypeEnumInt == (int)UserTypeEnum.CPM || + _userInfo.UserTypeEnumInt == (int)UserTypeEnum.SPM) { - return new PageOutput(query.PageIndex, query.PageSize, 0, new List()); + return await _trialRepository + .Where(t => t.TrialUserList.Any(t => t.UserId == _userInfo.Id)) + .WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.APM, c => c.TrialSiteSurveyList.Where(t => t.State == TrialSiteSurveyEnum.SPMApproved).Count() > 0) + .WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.CPM || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.SPM, c => c.TrialSiteSurveyList.Where(t => t.State == TrialSiteSurveyEnum.CRCSubmitted).Count() > 0) + .ProjectTo(_mapper.ConfigurationProvider, new { userTypeEnumInt = _userInfo.UserTypeEnumInt, userId = _userInfo.Id }) + .OrderByDescending(t => t.ApprovalRequiredCount).ToPagedListAsync(query.PageIndex, query.PageSize, query.SortField, query.Asc); } - return await _trialRepository - .Where(t => t.TrialUserList.Any(t => t.UserId == _userInfo.Id)) - .WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.APM,c=> c.TrialSiteSurveyList.Where(t => t.State == TrialSiteSurveyEnum.SPMApproved).Count()>0) - .WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.CPM || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.SPM, c => c.TrialSiteSurveyList.Where(t => t.State == TrialSiteSurveyEnum.CRCSubmitted).Count() > 0) - .ProjectTo(_mapper.ConfigurationProvider, new { userTypeEnumInt = _userInfo.UserTypeEnumInt, userId = _userInfo.Id }) - .OrderByDescending(t=>t.ApprovalRequiredCount).ToPagedListAsync(query.PageIndex, query.PageSize, query.SortField, query.Asc); + else + { + return new PageOutput(query.PageIndex, query.PageSize, 0, new List()); + + } } @@ -121,7 +130,7 @@ namespace IRaCIS.Core.Application else { var trialDocStat = await _trialRepository - .WhereIf(_userInfo.UserTypeEnumInt != (int)UserTypeEnum.SuperAdmin, t => t.TrialUserList.Any(t => t.UserId == _userInfo.Id)) + .Where(t => t.TrialUserList.Any(t => t.UserId == _userInfo.Id)) .WhereIf(!_userInfo.IsAdmin, c => c.TrialDocumentList.Where(t => t.NeedConfirmedUserTypeList.Any(t => t.NeedConfirmUserTypeId == _userInfo.UserTypeId) && !t.TrialDocConfirmedUserList.Any(t => t.ConfirmUserId == _userInfo.Id)).Count() > 0) .ProjectTo(_mapper.ConfigurationProvider, new { userTypeEnumInt = _userInfo.UserTypeEnumInt, userId = _userInfo.Id, userTypeId = _userInfo.UserTypeId }) .OrderByDescending(t => t.WaitSignCount) diff --git a/IRaCIS.Core.Application/Service/Visit/VisitPlanService.cs b/IRaCIS.Core.Application/Service/Visit/VisitPlanService.cs index 1227e424b..c7f7ef3df 100644 --- a/IRaCIS.Core.Application/Service/Visit/VisitPlanService.cs +++ b/IRaCIS.Core.Application/Service/Visit/VisitPlanService.cs @@ -56,7 +56,7 @@ namespace IRaCIS.Application.Services [HttpGet("{trialId:guid}")] public async Task GetVisitStageList(Guid trialId) { - var query = _visitStageRepository.Where(u => u.TrialId == trialId) + var query = _visitStageRepository.AsQueryable(true).Where(u => u.TrialId == trialId) .ProjectTo(_mapper.ConfigurationProvider).OrderBy(t => t.VisitNum); var list = await query.ToListAsync(); var trial = await _repository.FirstOrDefaultAsync(t => t.Id == trialId).IfNullThrowException(); diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 85fe3ac58..8768cdb99 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -7,12 +7,12 @@ using System.ComponentModel.DataAnnotations; namespace IRaCIS.Application.Services { - [ ApiExplorerSettings(GroupName = "Institution")] + [ApiExplorerSettings(GroupName = "Institution")] public class TestService : BaseService { private readonly IRepository _dicRepository; - public TestService(IRepository dicRepository) + public TestService(IRepository dicRepository) { _dicRepository = dicRepository; } @@ -21,13 +21,14 @@ namespace IRaCIS.Application.Services public string Get(testModel testModel) { - var c = _dicRepository.Where(t=>t.ParentId!=null).Select(t=>t.MappedValue).First(); + var d = _repository.Where().Select(t => t.FullName).FirstOrDefault(); + var c = _dicRepository.Where(t => t.ParentId != null).Select(t => t.MappedValue).First(); CultureInfo culture = CultureInfo.CurrentUICulture; var a = 123; var b = _localizer["test{0}", "测试"]; - return _localizer["test{0}","测试"]; + return _localizer["test{0}", "测试"]; } } diff --git a/IRaCIS.Core.Domain/Common/Dictionary.cs b/IRaCIS.Core.Domain/Common/Dictionary.cs index b41de14d5..a9a03f71f 100644 --- a/IRaCIS.Core.Domain/Common/Dictionary.cs +++ b/IRaCIS.Core.Domain/Common/Dictionary.cs @@ -58,8 +58,7 @@ namespace IRaCIS.Core.Domain.Models public string KeyName { get; set; } = string.Empty; - // - [NotMapped] + [NotMapped] public string MappedValue { get; set; } diff --git a/IRaCIS.Core.Domain/Management/User.cs b/IRaCIS.Core.Domain/Management/User.cs index 17c65816f..41fb43432 100644 --- a/IRaCIS.Core.Domain/Management/User.cs +++ b/IRaCIS.Core.Domain/Management/User.cs @@ -68,8 +68,7 @@ namespace IRaCIS.Core.Domain.Models public bool IsFirstAdd { get; set; } = true; - - - + [NotMapped] + public string FullName { get; set; } } } diff --git a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs index 812eeefb4..5b482b7e3 100644 --- a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs +++ b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs @@ -74,16 +74,19 @@ namespace IRaCIS.Core.Infra.EFCore //modelBuilder.Entity().Property(t => t.MappedValue).HasComputedColumnSql(_userInfo.IsEn_Us? "[Value]":"[ValueCN]"); //modelBuilder.Entity()// .HasQueryFilter(t => t.IsDeleted == false); - if (_userInfo.IsEn_Us) { - modelBuilder.Entity().Property(t => t.MappedValue).HasColumnName( "Value" ); + modelBuilder.Entity().Property(t => t.MappedValue).HasColumnName("Value"); } else { - modelBuilder.Entity().Property(t => t.MappedValue).HasColumnName( "ValueCN"); + modelBuilder.Entity().Property(t => t.MappedValue).HasColumnName("ValueCN"); } + + modelBuilder.Entity().Property(t => t.FullName) .HasComputedColumnSql("[LastName] + ' / ' + [FirstName]",false); + modelBuilder.Entity().Ignore(t => t.FullName); + //遍历实体模型手动配置 var typesToRegister = Assembly.GetExecutingAssembly().GetTypes().Where(q => q.GetInterface(typeof(IEntityTypeConfiguration<>).FullName) != null); foreach (var type in typesToRegister) diff --git a/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs b/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs index 04a309726..6268b9dca 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs @@ -11,7 +11,7 @@ namespace IRaCIS.Core.Infra.EFCore { public interface ICommandRepository: ICommandRepository where TEntity : Entity { - + Task InsertDictionaryAsync(TFrom from, params EntityVerifyExp[] verify); Task InsertOrUpdateAsync(TFrom from, bool autoSave = false, params EntityVerifyExp[] verify); } diff --git a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs index b30848e43..915f39d07 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs @@ -1,7 +1,10 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Linq.Expressions; +using System.Reflection; using System.Threading; using System.Threading.Tasks; using AutoMapper; @@ -11,6 +14,8 @@ using Microsoft.EntityFrameworkCore.ChangeTracking; using AutoMapper.QueryableExtensions; using IRaCIS.Core.Infrastructure; using IRaCIS.Core.Infrastructure.Extention; +using Microsoft.Data.SqlClient; +using Z.EntityFramework.Plus; namespace IRaCIS.Core.Infra.EFCore { @@ -20,7 +25,7 @@ namespace IRaCIS.Core.Infra.EFCore public IMapper _mapper { get; set; } public IRaCISDBContext _dbContext { get; set; } - private DbSet _dbSet => _dbContext.Set(); + public DbSet _dbSet => _dbContext.Set(); public Repository(IRaCISDBContext dbContext, IMapper mapper) @@ -35,7 +40,64 @@ namespace IRaCIS.Core.Infra.EFCore #region 异步部分 - + /// + /// 仅仅供字典表插入使用,因为efcore 动态映射列的问题 + /// + /// + /// + /// + /// + /// + public async Task InsertDictionaryAsync(TFrom from, params EntityVerifyExp[] verify) + { + var entity = _mapper.Map(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"); + //.Where(t => !t.GetCustomAttributes().Any(c => c.GetType() == typeof(NotMappedAttribute))) + //.Where(t => !t.PropertyType.IsGenericType); + + string[] strSqlNames = properties + .Select(p => $"[{p.Name}]").ToArray(); + + string strSqlName = string.Join(",", strSqlNames); + string[] strSqlValues = properties.Select(P => $"@{P.Name}").ToArray(); + string strSqlValue = string.Join(",", strSqlValues); + + // strSql得到的 sql语句为insert into testModel ( [ID],[Name],[QQ],[Email] ) values (@ID,@Name,@QQ,@Email) + 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); + + //测试还是有问题 NotMapped 会失效 + //await quey.BulkInsertAsync(new List() { entity as Dictionary }); + return entity; + } + else + { + throw new Exception("仅仅供字典表插入使用,因为efcore 动态映射列的问题"); + //await _dbSet.BulkInsertAsync(new List() { entity }); + + //return entity; + } + } public async Task InsertOrUpdateAsync(TFrom from, bool autoSave = false, params EntityVerifyExp[] verify) { @@ -127,7 +189,7 @@ namespace IRaCIS.Core.Infra.EFCore { query = query.IgnoreQueryFilters(); } - if (exp == null) + if (exp == null) return await query.FirstOrDefaultAsync().ConfigureAwait(false); return await query.FirstOrDefaultAsync(exp).ConfigureAwait(false); } @@ -290,7 +352,7 @@ namespace IRaCIS.Core.Infra.EFCore } - public IQueryable AsQueryable( bool ignoreQueryFilters = false) + public IQueryable AsQueryable(bool ignoreQueryFilters = false) { var query = _dbSet.AsQueryable(); @@ -312,7 +374,7 @@ namespace IRaCIS.Core.Infra.EFCore return condition ? _dbSet.AsNoTracking().Where(filter) : _dbSet.AsNoTracking(); } - public IQueryable Where(Expression> exp = null, bool isTraking = false,bool ignoreQueryFilters = false) + public IQueryable Where(Expression> exp = null, bool isTraking = false, bool ignoreQueryFilters = false) { IQueryable query = _dbSet;