diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index 3950e6f79..b8034d5ff 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -12949,7 +12949,7 @@ 项目外部人员 录入流程相关 - + 项目外部人员 录入流程相关 @@ -15587,7 +15587,7 @@ TrialSiteSurveyService - + TrialSiteSurveyService diff --git a/IRaCIS.Core.Application/Service/Common/MailService.cs b/IRaCIS.Core.Application/Service/Common/MailService.cs index c195e4511..dc21c1622 100644 --- a/IRaCIS.Core.Application/Service/Common/MailService.cs +++ b/IRaCIS.Core.Application/Service/Common/MailService.cs @@ -7,6 +7,7 @@ using IRaCIS.Core.Infrastructure; using MailKit; using Medallion.Threading; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Options; using MimeKit; @@ -485,14 +486,6 @@ namespace IRaCIS.Core.Application.Service await SendEmailHelper.SendEmailAsync(messageToSend, _systemEmailConfig, null); - //创建账号 和创建角色 一条,更新的时候才记录更新角色 - if (!sysUserInfo.IsFirstAdd) - { - await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, ActionIdentityUserId = _userInfo.IdentityUserId, ActionUserName = _userInfo.UserName, TargetIdentityUserId = userId, OptType = UserOptType.UpdateUserRole }, true); - - } - - } //Site调研 用户加入项目 @@ -553,13 +546,6 @@ namespace IRaCIS.Core.Application.Service await SendEmailHelper.SendEmailAsync(messageToSend, _systemEmailConfig); - //创建账号 和创建角色 一条,更新的时候才记录更新角色 - if (!sysUserInfo.IsFirstAdd) - { - await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, ActionIdentityUserId = _userInfo.IdentityUserId, ActionUserName = _userInfo.UserName, TargetIdentityUserId = userId, OptType = UserOptType.UpdateUserRole }, true); - - } - } @@ -574,6 +560,9 @@ namespace IRaCIS.Core.Application.Service var userType = await _userTypeRepository.FirstAsync(t => t.UserTypeEnum == UserTypeEnum.IndependentReviewer); + //需要创建新的账户 + var isNeedCreateNewUser = false; + var @lock = _distributedLockProvider.CreateLock($"UserCode"); using (await @lock.AcquireAsync()) @@ -583,6 +572,7 @@ namespace IRaCIS.Core.Application.Service if (!isDoctorHaveAccount) { + isNeedCreateNewUser = true; var saveItem = new IdentityUser() { FirstName = doctor.FirstName, LastName = doctor.LastName, EMail = doctor.EMail }; @@ -685,10 +675,9 @@ namespace IRaCIS.Core.Application.Service await SendEmailHelper.SendEmailAsync(messageToSend, _systemEmailConfig, null); //创建账号 和创建角色 一条,更新的时候才记录更新角色 - if (!sysUserInfo.IsFirstAdd) + if (isNeedCreateNewUser == false) { await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, ActionIdentityUserId = _userInfo.IdentityUserId, ActionUserName = _userInfo.UserName, TargetIdentityUserId = sysUserInfo.Id, OptType = UserOptType.UpdateUserRole }, true); - } diff --git a/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs b/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs index fe1dd8fde..7fb3fcec1 100644 --- a/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs +++ b/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs @@ -54,6 +54,7 @@ namespace IRaCIS.Core.Application.Contracts ITokenService _tokenService, IRepository _userTypeRepository, IMailVerificationService _mailVerificationService, + IRepository _userLogRepository, IOptionsMonitor systemEmailConfig, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, ITrialSiteSurveyService { private readonly SystemEmailSendConfig _systemEmailConfig = systemEmailConfig.CurrentValue; @@ -672,7 +673,7 @@ namespace IRaCIS.Core.Application.Contracts TrialSiteSurveyId = currentEntity!.Id, Token = _tokenService.GetToken(new UserTokenInfo() { - IdentityUserId=Guid.NewGuid(), + IdentityUserId = Guid.NewGuid(), UserRoleId = Guid.NewGuid(), UserName = "SiteSurvey", UserTypeEnum = UserTypeEnum.Undefined, @@ -871,6 +872,8 @@ namespace IRaCIS.Core.Application.Contracts #region 人员生成 + //需要创建新的账户 + var isNeedCreateNewUser = existSysUser == null; if (existSysUser != null) { @@ -949,11 +952,12 @@ namespace IRaCIS.Core.Application.Contracts } + + #endregion await _identityUserRepository.SaveChangesAsync(); - var identityUserId = existSysUser.Id; #region 项目加入 @@ -1048,12 +1052,18 @@ namespace IRaCIS.Core.Application.Contracts if (isNeedSendEmail) { + //创建账号 和创建角色 一条,更新的时候才记录更新角色 + if (isNeedCreateNewUser == false) + { + await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, ActionIdentityUserId = _userInfo.IdentityUserId, ActionUserName = _userInfo.UserName, TargetIdentityUserId = identityUserId, OptType = UserOptType.UpdateUserRole }); + } var dbUserType = _userTypeRepository.Where(t => userTypeIdList.Contains(t.Id)).ToList(); var usertyps = string.Join(',', dbUserType.Select(t => t.UserTypeName)); await _mailVerificationService.SiteSurveyUserJoinEmail(trialId, identityUserId, usertyps, baseUrl, routeUrl); + } var userJoinIdList = item.Select(t => t.Id).ToList(); @@ -1104,6 +1114,9 @@ namespace IRaCIS.Core.Application.Contracts var existSysUser = await _identityUserRepository.Where(t => t.EMail == userEmail, true).Include(t => t.UserRoleList).FirstOrDefaultAsync(); + //需要创建新的账户 + var isNeedCreateNewUser = existSysUser == null; + if (existSysUser != null) { @@ -1288,9 +1301,17 @@ namespace IRaCIS.Core.Application.Contracts if (isNeedSendEmail) { + //创建账号 和创建角色 一条,更新的时候才记录更新角色 + if (isNeedCreateNewUser == false) + { + await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, ActionIdentityUserId = _userInfo.IdentityUserId, ActionUserName = _userInfo.UserName, TargetIdentityUserId = identityUserId, OptType = UserOptType.UpdateUserRole }); + } + var dbUserType = _userTypeRepository.Where(t => userTypeIdList.Contains(t.Id)).ToList(); var usertyps = string.Join(',', dbUserType.Select(t => t.UserTypeName)); await _mailVerificationService.SiteSurveyUserJoinEmail(trialId, identityUserId, usertyps, baseUrl, routeUrl); + + } await _trialSiteUserRoleRepository.SaveChangesAsync(); diff --git a/IRaCIS.Core.Application/Service/TrialSiteUser/TrialExternalUserService.cs b/IRaCIS.Core.Application/Service/TrialSiteUser/TrialExternalUserService.cs index 31d253d81..922c7d4fc 100644 --- a/IRaCIS.Core.Application/Service/TrialSiteUser/TrialExternalUserService.cs +++ b/IRaCIS.Core.Application/Service/TrialSiteUser/TrialExternalUserService.cs @@ -31,6 +31,7 @@ namespace IRaCIS.Core.Application.Service IRepository _trialIdentityUserRepository, IRepository _trialRepository, IRepository _userTypeRepository, + IRepository _userLogRepository, IMailVerificationService _mailVerificationService, IDistributedLockProvider _distributedLockProvider, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, ITrialExternalUserService { @@ -215,8 +216,13 @@ namespace IRaCIS.Core.Application.Service //var identityUserId = item.Key; + + var existSysUser = await _identityUserRepository.Where(t => t.EMail == userEmail, true).Include(t => t.UserRoleList).FirstOrDefaultAsync(); + //需要创建新的账户 + var isNeedCreateNewUser = existSysUser == null; + if (existSysUser != null) { @@ -294,7 +300,7 @@ namespace IRaCIS.Core.Application.Service var newAddUser = await _identityUserRepository.AddAsync(generateUser); - + existSysUser = newAddUser; @@ -390,6 +396,12 @@ namespace IRaCIS.Core.Application.Service await _trialExternalUseRepository.BatchUpdateNoTrackingAsync(t => externalIdList.Contains(t.Id), u => new TrialExternalUser() { IsJoin = true }); + //创建账号 和创建角色 一条,更新的时候才记录更新角色 + if (isNeedCreateNewUser == false) + { + await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, ActionIdentityUserId = _userInfo.IdentityUserId, ActionUserName = _userInfo.UserName, TargetIdentityUserId = identityUserId, OptType = UserOptType.UpdateUserRole }, true); + } + }