From 746366a1c27805d1807e8489bbe937c8c2306936 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 18 Apr 2022 13:31:40 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessFilter/ProjectExceptionFilter.cs | 3 +- .../Service/Management/UserService.cs | 3 - IRaCIS.Core.Application/TestService.cs | 5 +- .../Repository/ICommandRepository.cs | 6 +- .../Repository/Repository.cs | 82 +++++++++++++------ 5 files changed, 68 insertions(+), 31 deletions(-) diff --git a/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs b/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs index 419a40885..0a599c8f7 100644 --- a/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs +++ b/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs @@ -35,7 +35,8 @@ namespace IRaCIS.Core.Application.Filter } else { - context.Result = new JsonResult(ResponseOutput.NotOk(" Program exception, please contact the developer! " + (context.Exception.InnerException is null ? (context.Exception.Message + context.Exception.StackTrace) : (context.Exception.InnerException?.Message + context.Exception.InnerException?.StackTrace)) )); + context.Result = new JsonResult(ResponseOutput.NotOk(" Program exception, please contact the developer! " + (context.Exception.InnerException is null ? (context.Exception.Message /*+ context.Exception.StackTrace*/) + : (context.Exception.InnerException?.Message /*+ context.Exception.InnerException?.StackTrace*/)) )); } diff --git a/IRaCIS.Core.Application/Service/Management/UserService.cs b/IRaCIS.Core.Application/Service/Management/UserService.cs index 8d7eb6c87..ea499bd59 100644 --- a/IRaCIS.Core.Application/Service/Management/UserService.cs +++ b/IRaCIS.Core.Application/Service/Management/UserService.cs @@ -295,9 +295,6 @@ namespace IRaCIS.Application.Services public async Task ModifyPassword(EditPasswordCommand editPwModel) { - - - //验证旧密码OK var dbUser = await _userRepository.FirstOrDefaultAsync(t => t.Id == _userInfo.Id && t.Password == editPwModel.OldPassWord); diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 69d204b38..c60c6adc2 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -25,8 +25,11 @@ namespace IRaCIS.Application.Services var c = _dicRepository.Where(t => t.ParentId != null).Select(t => t.MappedValue).First(); CultureInfo culture = CultureInfo.CurrentUICulture; + var dd= _dicRepository.UpdatePartialFields(Guid.Parse("8a90c96e-0776-4f7b-82a6-18933d339584"), + u => new Dictionary() { ParentId = null, Code = "test" }, true).Result; - var d= _dicRepository.UpdateFromDTOAsync(new AddOrEditBasicDic() { Id =Guid.Parse("60d86683-c33b-4349-b672-08da1e91b622"), ParentId = null,ChildGroup = null,Code = null},true,true).Result; + + var d = _dicRepository.UpdateFromDTOAsync(new AddOrEditBasicDic() { Id = Guid.Parse("60d86683-c33b-4349-b672-08da1e91b622"), ParentId = null, ChildGroup = null, Code = null }, true, true).Result; var a = 123; var b = _localizer["test{0}", "测试"]; diff --git a/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs b/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs index 1c99bbfe6..ea895984a 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs @@ -18,10 +18,10 @@ namespace IRaCIS.Core.Infra.EFCore Task UpdateFromDTOAsync(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp[] verify); - TEntity UpdatePartialFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp[] verify); + Task UpdatePartialFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp[] verify); + + Task UpdatePartialFields(Guid id, Expression> updateFactory, bool autoSave = false, params EntityVerifyExp[] verify); - //TEntity UpdatePartialFields(Guid id, Expression> updateFactory, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp[] verify); - } public interface ICommandRepository where TEntity : class diff --git a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs index 6366550ad..3adf611a2 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs @@ -20,7 +20,7 @@ using Z.EntityFramework.Plus; namespace IRaCIS.Core.Infra.EFCore { public class Repository : IRepository - where TEntity : Entity + where TEntity : Entity, new() { public IMapper _mapper { get; set; } public IRaCISDBContext _dbContext { get; set; } @@ -136,6 +136,7 @@ namespace IRaCIS.Core.Infra.EFCore } } } + } public async Task InsertFromDTOAsync(TFrom from, bool autoSave = false, params EntityVerifyExp[] verify) @@ -146,11 +147,8 @@ namespace IRaCIS.Core.Infra.EFCore await _dbSet.AddAsync(entity).ConfigureAwait(false); - if (autoSave) - { - await SaveChangesAsync(); + await SaveChangesAsync(autoSave); - } return entity; } @@ -190,29 +188,67 @@ namespace IRaCIS.Core.Infra.EFCore } } - if (autoSave) - { - await SaveChangesAsync(); - } + await SaveChangesAsync(autoSave); return dbBeforEntity; } - //public TEntity UpdatePartialFields(Guid id, Expression> updateFactory, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp[] verify) - //{ - // EntityVerifyAsync(false, verify, id).RunSynchronously(); + /// + /// 通过主键id 和表达式树 更新部分字段 例如 Guid.Parse("8a90c96e-0776-4f7b-82a6-18933d339584"),u => new Dictionary() { ParentId = null, Code = "test" } 默认会去处理更新更新人 更新时间 + /// + /// + /// + /// + /// + /// + public async Task UpdatePartialFields(Guid id, Expression> updateFactory, bool autoSave = false, params EntityVerifyExp[] verify) + { + await EntityVerifyAsync(false, verify, id); - // var entity = updateFactory.Compile().Invoke(new TEntity() { Id = id }); + var entity = new TEntity() { Id = id }; - // var entityEntry = _dbContext.Entry(entity); - // entityEntry.State = EntityState.Detached; + var entityEntry = _dbContext.Entry(entity); + entityEntry.State = EntityState.Detached; - //} + + Func func = updateFactory.Compile(); + + List list = ((MemberInitExpression)updateFactory.Body).Bindings.Select((Func)(_param1 => _param1.Member.Name)).Select((Func)(_param1 => (PropertyInfo)typeof(TEntity).GetProperty(_param1, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))).ToList(); + + + 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; + } + + + await SaveChangesAsync(autoSave); + + return entityEntry.Entity; + } + + + private async Task SaveChangesAsync(bool autoSave) + { + if (autoSave) + { + return await SaveChangesAsync(); + } + else + { + return false; + } + } /// - /// 部分字段更新 (只更新传递的字段名) + /// 部分字段更新 (只更新传递的字段名 new[] {nameof(User.Name), nameof(User.Age)) /// /// /// 更新的字段数组 @@ -220,10 +256,10 @@ namespace IRaCIS.Core.Infra.EFCore /// /// /// - public TEntity UpdatePartialFields(TEntity entity, string[] propertyNames, + public async Task UpdatePartialFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp[] verify) { - EntityVerifyAsync(false, verify, entity.Id).RunSynchronously(); + await EntityVerifyAsync(false, verify, entity.Id); var entityEntry = _dbContext.Entry(entity); entityEntry.State = EntityState.Detached; @@ -241,7 +277,7 @@ namespace IRaCIS.Core.Infra.EFCore } /// - /// 更新 排除某些字段的更新 + /// 更新 排除某些字段的更新 排除方式: new[] {nameof(User.Name), nameof(User.Age) /// /// /// @@ -249,15 +285,15 @@ namespace IRaCIS.Core.Infra.EFCore /// /// /// - public TEntity UpdateExcludeFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp[] verify) + public async Task UpdateExcludeFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp[] verify) { - EntityVerifyAsync(false, verify, entity.Id).RunSynchronously(); + await EntityVerifyAsync(false, verify, entity.Id); var entityEntry = _dbContext.Entry(entity); entityEntry.State = EntityState.Modified; - + foreach (var propertyName in propertyNames) { _dbContext.Entry(entity).Property(propertyName).IsModified = false;