分布式锁 sqlserver 05 项目迁移修改

This commit is contained in:
2023-10-12 09:34:09 +08:00
parent 5d7beb71e6
commit a565282a94
10 changed files with 181 additions and 161 deletions
@@ -16,6 +16,7 @@ using Panda.DynamicWebApi.Attributes;
using IRaCIS.Core.Application.Auth;
using IRaCIS.Application.Services;
using IRaCIS.Core.Application.Filter;
using Medallion.Threading;
namespace IRaCIS.Core.Application.Service
{
@@ -31,10 +32,11 @@ namespace IRaCIS.Core.Application.Service
private readonly IRepository<TrialSiteUserSurvey> _trialSiteSurveyUserRepository;
private readonly IRepository<TrialSiteUser> _trialSiteUserRepository;
private readonly IMailVerificationService _mailVerificationService;
private readonly IDistributedLockProvider _distributedLockProvider;
public TrialExternalUserService(IRepository<TrialExternalUser> trialExternalUseRepository, IRepository<User> userRepository, IRepository<TrialUser> trialUserRepository,
IRepository<TrialSiteUserSurvey> trialSiteSurveyUserRepository, IRepository<TrialSiteUser> trialSiteUserRepository,
IMailVerificationService mailVerificationService)
IMailVerificationService mailVerificationService, IDistributedLockProvider distributedLockProvider)
{
_trialExternalUseRepository = trialExternalUseRepository;
_userRepository = userRepository;
@@ -43,6 +45,7 @@ namespace IRaCIS.Core.Application.Service
_trialSiteUserRepository = trialSiteUserRepository;
_mailVerificationService = mailVerificationService;
_distributedLockProvider = distributedLockProvider;
}
@@ -75,121 +78,125 @@ namespace IRaCIS.Core.Application.Service
if (addOrEditTrialExternalUser.Id == null)
{
var existSysUser = await _userRepository.FirstOrDefaultAsync(t => t.EMail == addOrEditTrialExternalUser.Email && t.UserTypeId == addOrEditTrialExternalUser.UserTypeId);
var @lock = _distributedLockProvider.CreateLock($"UserCode");
if (existSysUser != null)
using (await @lock.AcquireAsync())
{
if (existSysUser.LastName != addOrEditTrialExternalUser.LastName || existSysUser.FirstName != addOrEditTrialExternalUser.FirstName)
var existSysUser = await _userRepository.FirstOrDefaultAsync(t => t.EMail == addOrEditTrialExternalUser.Email && t.UserTypeId == addOrEditTrialExternalUser.UserTypeId);
if (existSysUser != null)
{
//$"该用户在系统中的用户名为:{existSysUser.LastName + " / " + existSysUser.FirstName} 电话:{existSysUser.Phone},与填写信息存在不一致项, 请将界面信息修改为与系统一致,再进行保存",
return ResponseOutput.NotOk(_localizer["TrialExternalUser_Inconsistency", existSysUser.LastName + " / " + existSysUser.FirstName, existSysUser.Phone], new { existSysUser.LastName, existSysUser.FirstName, existSysUser.Phone, existSysUser.IsZhiZhun, existSysUser.IsTestUser }, ApiResponseCodeEnum.NeedTips);
if (existSysUser.LastName != addOrEditTrialExternalUser.LastName || existSysUser.FirstName != addOrEditTrialExternalUser.FirstName)
{
//$"该用户在系统中的用户名为:{existSysUser.LastName + " / " + existSysUser.FirstName} 电话:{existSysUser.Phone},与填写信息存在不一致项, 请将界面信息修改为与系统一致,再进行保存",
return ResponseOutput.NotOk(_localizer["TrialExternalUser_Inconsistency", existSysUser.LastName + " / " + existSysUser.FirstName, existSysUser.Phone], new { existSysUser.LastName, existSysUser.FirstName, existSysUser.Phone, existSysUser.IsZhiZhun, existSysUser.IsTestUser }, ApiResponseCodeEnum.NeedTips);
}
}
}
//处理 生成账户
//处理 生成账户
if (await _trialExternalUseRepository.AnyAsync(t =>
t.Email == addOrEditTrialExternalUser.Email &&
t.UserTypeId == addOrEditTrialExternalUser.UserTypeId && t.TrialId == addOrEditTrialExternalUser.TrialId))
{
//---系统已经存在与列表中填写的邮箱和用户类型相同的账户,请确认。
return ResponseOutput.NotOk(_localizer["TrialExternalUser_EmailTypeDuplicate"]);
}
if (await _trialExternalUseRepository.AnyAsync(t =>
t.Email == addOrEditTrialExternalUser.Email &&
t.UserTypeId == addOrEditTrialExternalUser.UserTypeId && t.TrialId == addOrEditTrialExternalUser.TrialId))
{
//---系统已经存在与列表中填写的邮箱和用户类型相同的账户,请确认。
return ResponseOutput.NotOk(_localizer["TrialExternalUser_EmailTypeDuplicate"]);
}
var addEntity = _mapper.Map<TrialExternalUser>(addOrEditTrialExternalUser);
var addEntity = _mapper.Map<TrialExternalUser>(addOrEditTrialExternalUser);
await _trialExternalUseRepository.AddAsync(addEntity);
await _trialExternalUseRepository.AddAsync(addEntity);
var existUser = await _userRepository.FirstOrDefaultAsync(t => t.EMail == addOrEditTrialExternalUser.Email && t.UserTypeId == addOrEditTrialExternalUser.UserTypeId);
var trialType = await _repository.Where<Trial>(t => t.Id == addOrEditTrialExternalUser.TrialId).Select(t => t.TrialType).FirstOrDefaultAsync();
var existUser = await _userRepository.FirstOrDefaultAsync(t => t.EMail == addOrEditTrialExternalUser.Email && t.UserTypeId == addOrEditTrialExternalUser.UserTypeId);
var trialType = await _repository.Where<Trial>(t => t.Id == addOrEditTrialExternalUser.TrialId).Select(t => t.TrialType).FirstOrDefaultAsync();
if (existUser != null)
{
addEntity.IsSystemUser = true;
addEntity.SystemUserId = existUser.Id;
if (existUser != null)
{
addEntity.IsSystemUser = true;
addEntity.SystemUserId = existUser.Id;
}
else
{
}
else
{
//生成账户 并插入
//生成账户 并插入
var generateUser = _mapper.Map<User>(addOrEditTrialExternalUser);
var generateUser = _mapper.Map<User>(addOrEditTrialExternalUser);
if (trialType == TrialType.NoneOfficial)
{
generateUser.IsTestUser = true;
}
// 外部人员生成账号 都是外部的
generateUser.IsZhiZhun = false;
generateUser.Code = _userRepository.Select(t => t.Code).DefaultIfEmpty().Max() + 1;
generateUser.UserCode = AppSettings.GetCodeStr(generateUser.Code, nameof(User));
generateUser.UserName = generateUser.UserCode;
generateUser.UserTypeEnum = _repository.Where<UserType>(t => t.Id == generateUser.UserTypeId).Select(t => t.UserTypeEnum).First();
generateUser.Password = MD5Helper.Md5("123456");
generateUser.Status = UserStateEnum.Disable;
var newAddUser = await _repository.AddAsync(generateUser);
addEntity.IsSystemUser = false;
addEntity.SystemUserId = newAddUser.Id;
existUser = newAddUser;
}
#region
if (trialType == TrialType.OfficialTrial || trialType == TrialType.Training)
{
if (existUser.IsTestUser)
{
//---正式类型 、培训类型的项目 不允许加入测试用户
throw new BusinessValidationFailedException(_localizer["TrialExternalUser_TestUserNotAllowed"]);
}
}
if (trialType == TrialType.NoneOfficial)
{
generateUser.IsTestUser = true;
if (existUser.IsTestUser == false)
{
//---测试项目 不允许加入正式用户
throw new BusinessValidationFailedException(_localizer["TrialExternalUser_FormalUserNotAllowed"]);
}
}
// 外部人员生成账号 都是外部的
generateUser.IsZhiZhun = false;
generateUser.Code = _userRepository.Select(t => t.Code).DefaultIfEmpty().Max() + 1;
#endregion
generateUser.UserCode = AppSettings.GetCodeStr(generateUser.Code, nameof(User));
generateUser.UserName = generateUser.UserCode;
generateUser.UserTypeEnum = _repository.Where<UserType>(t => t.Id == generateUser.UserTypeId).Select(t => t.UserTypeEnum).First();
generateUser.Password = MD5Helper.Md5("123456");
generateUser.Status = UserStateEnum.Disable;
var newAddUser = await _repository.AddAsync(generateUser);
await _trialExternalUseRepository.SaveChangesAsync();
addEntity.IsSystemUser = false;
addEntity.SystemUserId = newAddUser.Id;
existUser = newAddUser;
}
#region
if (trialType == TrialType.OfficialTrial || trialType == TrialType.Training)
{
if (existUser.IsTestUser)
//添加的时候就发邮件 现在省略
if (addOrEditTrialExternalUser.IsSendEmail)
{
//---正式类型 、培训类型的项目 不允许加入测试用户
throw new BusinessValidationFailedException(_localizer["TrialExternalUser_TestUserNotAllowed"]);
}
}
if (trialType == TrialType.NoneOfficial)
{
if (existUser.IsTestUser == false )
{
//---测试项目 不允许加入正式用户
throw new BusinessValidationFailedException(_localizer["TrialExternalUser_FormalUserNotAllowed"]);
}
}
#endregion
await _trialExternalUseRepository.SaveChangesAsync();
//添加的时候就发邮件 现在省略
if (addOrEditTrialExternalUser.IsSendEmail)
{
await SendExternalUserJoinEmail(new TrialExternalUserSendEmail()
{
BaseUrl = addOrEditTrialExternalUser.BaseUrl,
RouteUrl = addOrEditTrialExternalUser.RouteUrl,
TrialId = addOrEditTrialExternalUser.TrialId,
SendUsers = new List<UserEmail>()
await SendExternalUserJoinEmail(new TrialExternalUserSendEmail()
{
BaseUrl = addOrEditTrialExternalUser.BaseUrl,
RouteUrl = addOrEditTrialExternalUser.RouteUrl,
TrialId = addOrEditTrialExternalUser.TrialId,
SendUsers = new List<UserEmail>()
{
new UserEmail()
{
@@ -199,12 +206,13 @@ namespace IRaCIS.Core.Application.Service
SystemUserId=addEntity.SystemUserId
}
}
});
});
}
return ResponseOutput.Ok(addEntity.Id.ToString());
}
return ResponseOutput.Ok(addEntity.Id.ToString());
}
else
{