From 75543f9bcf153c6f125d1f1c7d94fc63cdd75c12 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Tue, 4 Jul 2023 11:30:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=A7=92=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Management/DTO/UserTypeRoleModel.cs | 3 ++ .../Service/Management/UserTypeService.cs | 34 +++++++++++------ .../Repository/IRaCISContextExtension.cs | 37 +++++++++++++++++++ 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/IRaCIS.Core.Application/Service/Management/DTO/UserTypeRoleModel.cs b/IRaCIS.Core.Application/Service/Management/DTO/UserTypeRoleModel.cs index f29a43272..3a059e4bd 100644 --- a/IRaCIS.Core.Application/Service/Management/DTO/UserTypeRoleModel.cs +++ b/IRaCIS.Core.Application/Service/Management/DTO/UserTypeRoleModel.cs @@ -38,6 +38,8 @@ namespace IRaCIS.Core.Application.Contracts public class UserTypeMenuAddOrEdit: UserTypeRoleAddOrEdit { + public UserTypeEnum UserTypeEnum { get; set; } + public List MenuIds { get; set; } = new List(); } @@ -53,6 +55,7 @@ namespace IRaCIS.Core.Application.Contracts public int Order { get; set; } + public List UserTypeGroupIdList { get; set; } = new List(); diff --git a/IRaCIS.Core.Application/Service/Management/UserTypeService.cs b/IRaCIS.Core.Application/Service/Management/UserTypeService.cs index 009323dfc..1e55c1dd3 100644 --- a/IRaCIS.Core.Application/Service/Management/UserTypeService.cs +++ b/IRaCIS.Core.Application/Service/Management/UserTypeService.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc; using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Infra.EFCore; +using IRaCIS.Application.Contracts; namespace IRaCIS.Core.Application.Contracts { @@ -15,18 +16,18 @@ namespace IRaCIS.Core.Application.Contracts [ApiExplorerSettings(GroupName = "Management")] public class UserTypeRoleService : BaseService, IUserTypeService { - private readonly IRepository userTypeServiceRepository; + private readonly IRepository _userTypeRepository; - public UserTypeRoleService(IRepository userTypeServiceRepository) + public UserTypeRoleService(IRepository userTypeRepository) { - this.userTypeServiceRepository = userTypeServiceRepository; + _userTypeRepository = userTypeRepository; } [HttpPost] public async Task> GetUserTypeRoleList(UserTypeQuery userTypeQuery) { - var userTypeRoleQueryable = userTypeServiceRepository + var userTypeRoleQueryable = _userTypeRepository .WhereIf(!string.IsNullOrWhiteSpace(userTypeQuery.SearchFilter), t => t.Description.Contains(userTypeQuery.SearchFilter!) || t.UserTypeName.Contains(userTypeQuery.SearchFilter!) || t.UserTypeShortName.Contains(userTypeQuery.SearchFilter!)) .WhereIf(userTypeQuery.GroupId!=null,t=>t.UserTypeGroupList.Any(t=>t.DictionaryId== userTypeQuery.GroupId)) @@ -40,16 +41,22 @@ namespace IRaCIS.Core.Application.Contracts public async Task AddOrUpdateUserTypeRole(UserTypeMenuAddOrEdit addOrEditUserTypeRole) { + var verifyExp1 = new EntityVerifyExp() + { + VerifyExp = u => u.UserTypeEnum == addOrEditUserTypeRole.UserTypeEnum , + + VerifyMsg = "已存在该用户类型的角色。" + }; + var entity = new UserType(); if (addOrEditUserTypeRole.Id == null) { entity = _mapper.Map(addOrEditUserTypeRole); - entity.UserTypeEnum = userTypeServiceRepository.Select(t => t.UserTypeEnum).DefaultIfEmpty().Max() + 1; - - await _repository.AddAsync(entity); + //entity.UserTypeEnum = userTypeServiceRepository.Select(t => t.UserTypeEnum).DefaultIfEmpty().Max() + 1; + await _userTypeRepository.InsertFromDTOAsync(addOrEditUserTypeRole,false, verifyExp1); } else @@ -57,14 +64,17 @@ namespace IRaCIS.Core.Application.Contracts if (addOrEditUserTypeRole.MenuIds.Count > 0) { - entity = userTypeServiceRepository.Where(t => t.Id == addOrEditUserTypeRole.Id, true).Include(t => t.UserTypeMenuList).Include(t=>t.UserTypeGroupList).FirstOrDefault().IfNullThrowException(); + entity = _userTypeRepository.Where(t => t.Id == addOrEditUserTypeRole.Id, true).Include(t => t.UserTypeMenuList).Include(t=>t.UserTypeGroupList).FirstOrDefault().IfNullThrowException(); } else { - entity = userTypeServiceRepository.Where(t => t.Id == addOrEditUserTypeRole.Id, true).FirstOrDefault().IfNullThrowException(); + entity = _userTypeRepository.Where(t => t.Id == addOrEditUserTypeRole.Id, true).FirstOrDefault().IfNullThrowException(); } _mapper.Map(addOrEditUserTypeRole, entity); + + + await _userTypeRepository.EntityVerifyAsync(entity.Id, verifyExp1); } var success = await _repository.SaveChangesAsync(); @@ -83,7 +93,7 @@ namespace IRaCIS.Core.Application.Contracts return ResponseOutput.NotOk(_localizer["UserType_InUse"]); } - var success = await userTypeServiceRepository.BatchDeleteNoTrackingAsync(t => t.Id == userTypeId); + var success = await _userTypeRepository.BatchDeleteNoTrackingAsync(t => t.Id == userTypeId); return ResponseOutput.Result(success); } @@ -139,7 +149,7 @@ namespace IRaCIS.Core.Application.Contracts - var query = userTypeServiceRepository.Where(x => x.UserTypeEnum != UserTypeEnum.SuperAdmin) + var query = _userTypeRepository.Where(x => x.UserTypeEnum != UserTypeEnum.SuperAdmin) .WhereIf(userTypeSelectEnum != UserTypeSelectEnum.None, t => userTypeEnums.Contains(t.UserTypeEnum)) .OrderBy(t => t.Order).ProjectTo(_mapper.ConfigurationProvider); @@ -156,7 +166,7 @@ namespace IRaCIS.Core.Application.Contracts /// public async Task> GetTrialUserTypeList() { - var query = userTypeServiceRepository.Where(x => x.UserTypeEnum != UserTypeEnum.SuperAdmin) + var query = _userTypeRepository.Where(x => x.UserTypeEnum != UserTypeEnum.SuperAdmin) .Where(t => !t.UserTypeGroupList.Any(t=> t.Group.Code=="3")) .OrderBy(t => t.Order).ProjectTo(_mapper.ConfigurationProvider); diff --git a/IRaCIS.Core.Infra.EFCore/Repository/IRaCISContextExtension.cs b/IRaCIS.Core.Infra.EFCore/Repository/IRaCISContextExtension.cs index 80bb27dae..35dac7b38 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/IRaCISContextExtension.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/IRaCISContextExtension.cs @@ -83,6 +83,43 @@ namespace IRaCIS.Core.Infra.EFCore } } + public static async Task EntityVerifyAsync(this IRepository _entityRepository, Guid? entitydId = null,params EntityVerifyExp[] verify) where T : Entity + { + + if (entitydId!=null) + { + foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyUpdate && t.IsVerify)) + { + if (await _entityRepository.AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false)) + { + throw new BusinessValidationFailedException(verifyItem.VerifyMsg); + } + } + } + else + { + foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyAdd && t.IsVerify)) + { + if (verifyItem.verifyType == VerifyEnum.OnlyUpdate) + { + if (await _entityRepository.AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false)) + { + throw new BusinessValidationFailedException(verifyItem.VerifyMsg); + } + } + else if (verifyItem.verifyType == VerifyEnum.Both) + { + if (await _entityRepository.AnyAsync(verifyItem.VerifyExp.And(t => t.Id != entitydId)).ConfigureAwait(false)) + { + throw new BusinessValidationFailedException(verifyItem.VerifyMsg); + } + } + } + } + } + + + ///注意 模型标注了 ConcurrencyCheck的属性,这样的实体,不适合用部分字段更新,ef生成的更新sql会自动带上ConcurrencyCheck的属性条件 /// EntityState.Detached的实体 修改 部分字段 public static void EntityModifyPartialFiled(this IRaCISDBContext _dbContext,T waitModifyEntity, Expression> updateFactory) where T : Entity