From 228254db097abee7e38976ab8cb4eba8749e75dc Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 7 Aug 2023 11:47:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E7=A0=94=E8=A1=A8=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SiteSurvey/TrialSiteSurveyService.cs | 588 ++++++------------ .../SiteSurvey/TrialSiteUserSurveyService.cs | 11 +- .../SiteSurvey/TrialSiteUserSurvey.cs | 19 +- 3 files changed, 219 insertions(+), 399 deletions(-) diff --git a/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs b/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs index afdf8c648..3e3cea717 100644 --- a/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs +++ b/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs @@ -15,6 +15,7 @@ using MailKit.Security; using MimeKit; using IRaCIS.Core.Application.Helper; using IRaCIS.Core.Application.Filter; +using IRaCIS.Core.Infrastructure.Extention; namespace IRaCIS.Core.Application.Contracts { @@ -107,7 +108,7 @@ namespace IRaCIS.Core.Application.Contracts } /// - /// 发送验证码 + ///site 调研 发送验证码 /// /// /// @@ -150,225 +151,187 @@ namespace IRaCIS.Core.Application.Contracts public async Task VerifySendCode(LoginDto userInfo, [FromServices] ITokenService _tokenService) { - var isReplaceUser = !string.IsNullOrEmpty(userInfo.ReplaceUserEmailOrPhone); + + #region 历史版本 删除前 + + //var isReplaceUser = !string.IsNullOrEmpty(userInfo.ReplaceUserEmailOrPhone); - if (userInfo.IsUpdate && isReplaceUser && !await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.ReplaceUserEmailOrPhone || t.Phone == userInfo.ReplaceUserEmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId)) - { - //---该中心不存在该交接人的中心调研记录表,不允许选择更新。 - return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_NoRecordToUpdate"]); - } + //if (userInfo.IsUpdate && isReplaceUser && !await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.ReplaceUserEmailOrPhone || t.Phone == userInfo.ReplaceUserEmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId)) + //{ + // //---该中心不存在该交接人的中心调研记录表,不允许选择更新。 + // return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_NoRecordToUpdate"]); + //} - if (userInfo.IsUpdate && await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.EmailOrPhone || t.Phone == userInfo.EmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State != TrialSiteSurveyEnum.PMCreatedAndLock)) - { - //---您的中心调研记录正在审核中,不允许进行更新操作。若需要更新,请在驳回后进行操作。 - return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_RecordUnderReview"]); - } + //if (userInfo.IsUpdate && await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.EmailOrPhone || t.Phone == userInfo.EmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State != TrialSiteSurveyEnum.PMCreatedAndLock)) + //{ + // //---您的中心调研记录正在审核中,不允许进行更新操作。若需要更新,请在驳回后进行操作。 + // return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_RecordUnderReview"]); + //} - //自己的记录锁定了 只能更新自己的,不能更新别人的(但是别人能更新自己锁定的) - if (userInfo.IsUpdate && userInfo.ReplaceUserEmailOrPhone != userInfo.EmailOrPhone && await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.EmailOrPhone || t.Phone == userInfo.EmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State == TrialSiteSurveyEnum.PMCreatedAndLock)) - { - //自己的锁了 想更新别人的 - //---当前中心中,您提交调研记录表已锁定,不允许更新其他人邮箱调研记录。 - return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_LockedByCurrentUser"]); - } + ////自己的记录锁定了 只能更新自己的,不能更新别人的(但是别人能更新自己锁定的) + //if (userInfo.IsUpdate && userInfo.ReplaceUserEmailOrPhone != userInfo.EmailOrPhone && await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.EmailOrPhone || t.Phone == userInfo.EmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State == TrialSiteSurveyEnum.PMCreatedAndLock)) + //{ + // //自己的锁了 想更新别人的 + // //---当前中心中,您提交调研记录表已锁定,不允许更新其他人邮箱调研记录。 + // return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_LockedByCurrentUser"]); + //} - //自己的锁定了 如果有其他未锁定的,也不能更新自己的 - if (userInfo.IsUpdate && userInfo.ReplaceUserEmailOrPhone == userInfo.EmailOrPhone && - await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.EmailOrPhone || t.Phone == userInfo.EmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State == TrialSiteSurveyEnum.PMCreatedAndLock) - && await _trialSiteSurveyRepository.AnyAsync(t => (t.Email != userInfo.EmailOrPhone && t.Phone != userInfo.EmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State != TrialSiteSurveyEnum.PMCreatedAndLock)) - { + ////自己的锁定了 如果有其他未锁定的,也不能更新自己的 + //if (userInfo.IsUpdate && userInfo.ReplaceUserEmailOrPhone == userInfo.EmailOrPhone && + // await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.EmailOrPhone || t.Phone == userInfo.EmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State == TrialSiteSurveyEnum.PMCreatedAndLock) + // && await _trialSiteSurveyRepository.AnyAsync(t => (t.Email != userInfo.EmailOrPhone && t.Phone != userInfo.EmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State != TrialSiteSurveyEnum.PMCreatedAndLock)) + //{ - //---当前中心,您提交的调研记录表已锁定。当前存在其他人员提交的调研记录表未锁定,不允许更新您之前提交的调研记录。 - return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_LockedByOtherUsers"]); - } + // //---当前中心,您提交的调研记录表已锁定。当前存在其他人员提交的调研记录表未锁定,不允许更新您之前提交的调研记录。 + // return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_LockedByOtherUsers"]); + //} - ////存在未锁定的记录,却去更新已锁定的 - if (userInfo.IsUpdate && userInfo.ReplaceUserEmailOrPhone != userInfo.EmailOrPhone && await _trialSiteSurveyRepository.AnyAsync(t => t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State != TrialSiteSurveyEnum.PMCreatedAndLock) - && await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.ReplaceUserEmailOrPhone || t.Phone == userInfo.ReplaceUserEmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State == TrialSiteSurveyEnum.PMCreatedAndLock) - && !await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.ReplaceUserEmailOrPhone || t.Phone == userInfo.ReplaceUserEmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State != TrialSiteSurveyEnum.PMCreatedAndLock) - ) - { - //---当前中心存在未锁定的调研记录,不允许更新已锁定的调研记录。 - return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_UnlockedRecordsExist"]); - } + //////存在未锁定的记录,却去更新已锁定的 + //if (userInfo.IsUpdate && userInfo.ReplaceUserEmailOrPhone != userInfo.EmailOrPhone && await _trialSiteSurveyRepository.AnyAsync(t => t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State != TrialSiteSurveyEnum.PMCreatedAndLock) + //&& await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.ReplaceUserEmailOrPhone || t.Phone == userInfo.ReplaceUserEmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State == TrialSiteSurveyEnum.PMCreatedAndLock) + //&& !await _trialSiteSurveyRepository.AnyAsync(t => (t.Email == userInfo.ReplaceUserEmailOrPhone || t.Phone == userInfo.ReplaceUserEmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State != TrialSiteSurveyEnum.PMCreatedAndLock) + //) + //{ + // //---当前中心存在未锁定的调研记录,不允许更新已锁定的调研记录。 + // return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_UnlockedRecordsExist"]); + //} + #endregion - var verificationRecord = await _repository - .FirstOrDefaultAsync(t => (t.EmailOrPhone == userInfo.EmailOrPhone) && t.Code == userInfo.verificationCode && t.CodeType == userInfo.verificationType); + #region 20230804 修改调研表逻辑 + + var verifyRecord = await _repository.FirstOrDefaultAsync(t => (t.EmailOrPhone == userInfo.EmailOrPhone) && t.Code == userInfo.verificationCode && t.CodeType == userInfo.verificationType); //检查数据库是否存在该验证码 - if (verificationRecord == null) + if (verifyRecord == null) { //---验证码错误。 return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_WrongVerificationCode"]); } + else if (verifyRecord.ExpirationTime < DateTime.Now) + { + return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_WrongVerificationCode"]); + } else { - //检查验证码是否失效 - if (verificationRecord.ExpirationTime < DateTime.Now) - { - //---验证码已经过期。 - return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_ExpiredVerificationCode"]); - } - else //验证码正确 并且 没有超时 - { - - TrialSiteSurvey? dbEntity = null; - - - //替换交接人 - if (isReplaceUser) - { - //该交接人的记录 是否有未锁定的 有就用未锁定的,没有就用 锁定的最后一条 - - var noLockedLastSurvey = await _trialSiteSurveyRepository.Where(t => (t.Email == userInfo.ReplaceUserEmailOrPhone || t.Phone == userInfo.ReplaceUserEmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State == TrialSiteSurveyEnum.PMCreatedAndLock == false, true) - .Include(u => u.TrialSiteEquipmentSurveyList).Include(u => u.TrialSiteUserSurveyList).OrderByDescending(t => t.CreateTime).FirstOrDefaultAsync(); - - //都是锁定的 - if (noLockedLastSurvey == null) - { - - var latestLock = await _trialSiteSurveyRepository.Where(t => t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId).OrderByDescending(t => t.CreateTime).FirstOrDefaultAsync(); - - if (latestLock!.Email != userInfo.ReplaceUserEmailOrPhone) - { - return ResponseOutput.NotOk($"该邮箱{userInfo.ReplaceUserEmailOrPhone}对应的调查表不是最新锁定的记录,不允许更新!"); - } - - var lockedLastSurvey = await _trialSiteSurveyRepository.Where(t => (t.Email == userInfo.ReplaceUserEmailOrPhone || t.Phone == userInfo.ReplaceUserEmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId && t.State == TrialSiteSurveyEnum.PMCreatedAndLock == true) - .Include(u => u.TrialSiteEquipmentSurveyList).Include(u => u.TrialSiteUserSurveyList).OrderByDescending(t => t.CreateTime).FirstOrDefaultAsync().IfNullThrowException(); - - //Copy 一份 更换邮箱 - - var copy = lockedLastSurvey.Clone(); - - copy.State = TrialSiteSurveyEnum.ToSubmit; - - copy.Email = userInfo.EmailOrPhone; - - if (userInfo.ReplaceUserEmailOrPhone != userInfo.EmailOrPhone) - { - copy.UserName = String.Empty; - copy.Phone = String.Empty; - } - - - copy.Id = Guid.Empty; - copy.TrialSiteEquipmentSurveyList.ForEach(t => t.Id = Guid.Empty); - copy.TrialSiteUserSurveyList.ForEach(t => t.Id = Guid.Empty); - - dbEntity = await _repository.AddAsync(copy); - - } - else - { - //有未锁定的 更新下邮箱 - noLockedLastSurvey.Email = userInfo.EmailOrPhone; - noLockedLastSurvey.UserName = String.Empty; - noLockedLastSurvey.Phone = String.Empty; - - dbEntity = noLockedLastSurvey; - } - - - - ////邮箱相同的话 就是同一个人进来 copy一份 - //if (userInfo.EmailOrPhone == userInfo.ReplaceUserEmailOrPhone) - //{ - // dbEntity = await _repository.Where(t => (t.Email == userInfo.EmailOrPhone || t.Phone == userInfo.EmailOrPhone) && t.SiteId == userInfo.SiteId && t.TrialId == userInfo.TrialId).Include(u => u.TrialSiteEquipmentSurveyList).Include(u => u.TrialSiteUserSurveyList).FirstOrDefaultAsync().IfNullThrowConvertException(); - - // var clone = dbEntity.Clone(); - // clone.Id = Guid.Empty; - // clone.TrialSiteEquipmentSurveyList.ForEach(t => t.Id = Guid.Empty); - // clone.TrialSiteUserSurveyList.ForEach(t => t.Id = Guid.Empty); - // clone.State = TrialSiteSurveyEnum.ToSubmit; - - // dbEntity = await _repository.AddAsync(clone); - - //} - - } - else - { - - - var dbEntityList = await _trialSiteSurveyRepository.Where(t => t.TrialId == userInfo.TrialId && t.SiteId == userInfo.SiteId).ToListAsync(); - - - //没有记录 new一份 - if (dbEntityList.Count == 0) - { - - dbEntity = await _repository.AddAsync(_mapper.Map(userInfo)); - - } - else - { - - - - //该site 下不存在该邮箱的记录 - if (!dbEntityList.Any(t => t.Email == userInfo.EmailOrPhone || t.Phone == userInfo.EmailOrPhone)) - { - //---该中心下已经有其他用户已填写的调研表,您不被允许继续填写 - return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_AlreadyFilledByOtherUsers"]); - } - - - //有没有该邮箱 未锁定的 - var nolockEntity = dbEntityList.Where(t => t.Email == userInfo.EmailOrPhone || t.Phone == userInfo.EmailOrPhone).FirstOrDefault(t => t.State != TrialSiteSurveyEnum.PMCreatedAndLock); - - // 未锁定的 为空 - if (nolockEntity == null) - { - //查看最新锁定的 - dbEntity = dbEntityList.Where(t => t.Email == userInfo.EmailOrPhone || t.Phone == userInfo.EmailOrPhone).OrderByDescending(t => t.CreateTime).FirstOrDefault(t => t.State == TrialSiteSurveyEnum.PMCreatedAndLock).IfNullThrowException(); - - } - else //有未锁定的 直接用未锁定的 - { - dbEntity = nolockEntity; - } - - } - //_mapper.Map(userInfo, dbEntity); - - - - - } - - - //删除验证码历史记录 - await _repository.BatchDeleteAsync(t => t.EmailOrPhone == userInfo.EmailOrPhone && t.Code == userInfo.verificationCode && t.CodeType == userInfo.verificationType); - - await _repository.SaveChangesAsync(); - - return ResponseOutput.Ok(new - { - TrialSiteSurveyId = dbEntity!.Id, - Token = _tokenService.GetToken(IRaCISClaims.Create(new UserBasicInfo() - { - Id = Guid.NewGuid(), - IsReviewer = false, - IsAdmin = false, - RealName = "SiteSurvey", - UserName = "SiteSurvey", - Sex = 0, - //UserType = "ShareType", - UserTypeEnum = UserTypeEnum.Undefined, - Code = "SiteSurvey", - })) - }); - - } + //验证码正确 不处理 } + TrialSiteSurvey? currentEntity = null; + + //普通登录 + if (userInfo.IsUpdate == false) + { + + var dbEntityList = await _trialSiteSurveyRepository.Where(t => t.TrialId == userInfo.TrialId && t.SiteId == userInfo.SiteId).ToListAsync(); + + + //没有记录 new一份 + if (dbEntityList.Count == 0) + { + + currentEntity = await _repository.AddAsync(_mapper.Map(userInfo)); + + } + else + { + + //找到当前最新的调研表 + + var currentLatest = dbEntityList.OrderByDescending(t => t.CreateTime).FirstOrDefault(); + + if (currentLatest.Email != userInfo.EmailOrPhone) + { + //---该中心下已经有其他用户已填写的调研表,您不被允许继续填写 + return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_AlreadyFilledByOtherUsers"]); + } + + currentEntity = currentLatest; + + } + + + + } + //更新调研表 + else + { + //找到最新的调研表 + + var currentLatest = await _trialSiteSurveyRepository.Where(t => t.TrialId == userInfo.TrialId && t.SiteId == userInfo.SiteId,true).OrderByDescending(t => t.CreateTime).FirstOrDefaultAsync(); + + if (currentLatest == null) + { + return ResponseOutput.NotOk("当前site没有调研表可以更新,请确认"); + } + + if(currentLatest.Email != userInfo.ReplaceUserEmailOrPhone) + { + //---该中心不存在该交接人的中心调研记录表,不允许选择更新。 + return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_NoRecordToUpdate"]); + } + + //未锁定的状态 就改为废除 + if(currentLatest.State!= TrialSiteSurveyEnum.PMCreatedAndLock) + { + currentLatest.IsDeleted = true; + } + //锁定的 需要改状态么? + else + { + + } + + var copy = currentLatest.Clone(); + + copy.State = TrialSiteSurveyEnum.ToSubmit; + + copy.Email = userInfo.EmailOrPhone; + + if (userInfo.ReplaceUserEmailOrPhone != userInfo.EmailOrPhone) + { + copy.UserName = String.Empty; + copy.Phone = String.Empty; + } + + + copy.Id = Guid.Empty; + copy.TrialSiteEquipmentSurveyList.ForEach(t => t.Id = Guid.Empty); + copy.TrialSiteUserSurveyList.ForEach(t => { t.Id = Guid.Empty; t.IsGenerateSuccess = false; }); + + currentEntity = await _repository.AddAsync(copy); + + } + + + //删除验证码历史记录 + await _repository.BatchDeleteAsync(t => t.EmailOrPhone == userInfo.EmailOrPhone && t.Code == userInfo.verificationCode && t.CodeType == userInfo.verificationType); + + await _repository.SaveChangesAsync(); + + return ResponseOutput.Ok(new + { + TrialSiteSurveyId = currentEntity!.Id, + Token = _tokenService.GetToken(IRaCISClaims.Create(new UserBasicInfo() + { + Id = Guid.NewGuid(), + IsReviewer = false, + IsAdmin = false, + RealName = "SiteSurvey", + UserName = "SiteSurvey", + Sex = 0, + //UserType = "ShareType", + UserTypeEnum = UserTypeEnum.Undefined, + Code = "SiteSurvey", + })) + }); + + #endregion } @@ -487,25 +450,11 @@ namespace IRaCIS.Core.Application.Contracts .WhereIf(queryParam.IsGenerateAccount != null, t => t.IsGenerateAccount == queryParam.IsGenerateAccount) .WhereIf(queryParam.TrialRoleNameId != null, t => t.TrialRoleNameId == queryParam.TrialRoleNameId) .WhereIf(queryParam.State != null && queryParam.State != TrialSiteUserStateEnum.OverTime, t => t.InviteState == queryParam.State) - .WhereIf(queryParam.State != null && queryParam.State == TrialSiteUserStateEnum.OverTime, t => t.InviteState == TrialSiteUserStateEnum.HasSend && t.ExpireTime < DateTime.Now) .WhereIf(!string.IsNullOrEmpty(queryParam.UserName), t => (t.LastName + " / " + t.FirstName).Contains(queryParam.UserName)) .WhereIf(!string.IsNullOrEmpty(queryParam.OrganizationName), t => t.OrganizationName.Contains(queryParam.OrganizationName)) .ProjectTo(_mapper.ConfigurationProvider); - //var query = _trialSiteSurveyRepository.Where(t => t.TrialId == queryParam.TrialId && t.IsAbandon == false) - // .WhereIf(queryParam.SiteId != null, t => t.SiteId == queryParam.SiteId) - // .WhereIf(!string.IsNullOrEmpty(queryParam.FormWriterKeyInfo), t => (t.UserName).Contains(queryParam.FormWriterKeyInfo) || t.Email.Contains(queryParam.FormWriterKeyInfo) || t.Phone.Contains(queryParam.FormWriterKeyInfo)) - // .GroupBy(t => t.SiteId) - // .Select(g => g.OrderByDescending(u => u.CreateTime).FirstOrDefault()) - // .SelectMany(t => t.TrialSiteUserSurveyList) - //.WhereIf(queryParam.UserTypeId != null, t => t.UserTypeId == queryParam.UserTypeId) - //.WhereIf(queryParam.IsGenerateAccount != null, t => t.IsGenerateAccount == queryParam.IsGenerateAccount) - //.WhereIf(queryParam.TrialRoleNameId != null, t => t.TrialRoleNameId == queryParam.TrialRoleNameId) - //.WhereIf(queryParam.State != null && queryParam.State != TrialSiteUserStateEnum.OverTime, t => t.InviteState == queryParam.State) - //.WhereIf(queryParam.State != null && queryParam.State == TrialSiteUserStateEnum.OverTime, t => t.InviteState == TrialSiteUserStateEnum.HasSend && t.ExpireTime < DateTime.Now) - //.WhereIf(!string.IsNullOrEmpty(queryParam.UserKeyInfo), t => (t.LastName + " / " + t.FirstName).Contains(queryParam.UserKeyInfo) || t.Email.Contains(queryParam.UserKeyInfo) || t.Phone.Contains(queryParam.UserKeyInfo)) - //.ProjectTo(_mapper.ConfigurationProvider); return await query.ToPagedListAsync(queryParam.PageIndex, queryParam.PageSize, queryParam.SortField, queryParam.Asc); @@ -556,18 +505,18 @@ namespace IRaCIS.Core.Application.Contracts //---中心调研已锁定,不允许操作。 return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_Locked"]); } - if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager ) + if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager) { - //没有SPM 给填表人发 - messageToSend.To.Add(new MailboxAddress(String.Empty, survey.Email)); + //没有SPM 给填表人发 + messageToSend.To.Add(new MailboxAddress(String.Empty, survey.Email)); - survey.State = TrialSiteSurveyEnum.ToSubmit; + survey.State = TrialSiteSurveyEnum.ToSubmit; - survey.PreliminaryUserId = null; - survey.ReviewerUserId = null; - survey.PreliminaryTime = null; - survey.ReviewerTime = null; + survey.PreliminaryUserId = null; + survey.ReviewerUserId = null; + survey.PreliminaryTime = null; + survey.ReviewerTime = null; } @@ -620,29 +569,8 @@ namespace IRaCIS.Core.Application.Contracts } - /// - /// 驳回 - /// - /// - /// - /// - [HttpPut("{trialId:guid}/{trialSiteSurveyId:guid}")] - [Obsolete] - public async Task SubmissionRejection(Guid trialId, Guid trialSiteSurveyId) - { - if (await _repository.AnyAsync(t => t.State == TrialSiteSurveyEnum.PMCreatedAndLock && t.Id == trialSiteSurveyId)) - { - //---中心调研已锁定,不允许操作。 - return ResponseOutput.NotOk(_localizer["TrialSiteSurvey_Locked"]); - } - await _trialSiteSurveyRepository.BatchUpdateNoTrackingAsync(t => t.Id == trialSiteSurveyId, u => new TrialSiteSurvey() { State = TrialSiteSurveyEnum.ToSubmit }); - - return ResponseOutput.Ok(); - - } - [HttpPut("{trialId:guid}/{trialSiteSurveyId:guid}")] [TypeFilter(typeof(TrialResourceFilter), Arguments = new object[] { "AfterStopCannNotOpt" })] @@ -696,60 +624,40 @@ namespace IRaCIS.Core.Application.Contracts }).ToListAsync(); - - if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.Undefined) { - //是第一次 - if (!siteUserList.Any(t => t.IsGenerateSuccess)) - { - var currentUserList = siteUserList.Where(t => t.TrialSiteSurveyId == trialSiteSurveyId).ToList(); + var currentUserList = siteUserList.Where(t => t.TrialSiteSurveyId == trialSiteSurveyId).ToList(); - if (!currentUserList.Any(t => t.Code == "1") || !currentUserList.Any(t => t.Code == "5")) - { - throw new BusinessValidationFailedException("本次提交,必须有CRC和影像阅片人信息"); - } + + if (!currentUserList.Any(t => t.Code == "1") || !currentUserList.Any(t => t.Code == "5")) + { + throw new BusinessValidationFailedException("本次提交,必须有CRC和影像阅片人信息"); } + + if (!currentUserList.Any(t => t.UserTypeEnum == UserTypeEnum.ClinicalResearchCoordinator) || + !currentUserList.Any(t => t.UserTypeEnum == UserTypeEnum.SR)) + { + throw new BusinessValidationFailedException("本次提交,生成账号必须要有CRC和SR"); + } + + await _trialSiteSurveyRepository.UpdatePartialFromQueryAsync(t => t.Id == trialSiteSurveyId && t.State == TrialSiteSurveyEnum.ToSubmit, u => new TrialSiteSurvey() { State = TrialSiteSurveyEnum.SPMApproved }); } else if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager) { - foreach (var group in (siteUserList.Where(t => t.IsGenerateAccount && t.TrialSiteSurveyId == trialSiteSurveyId) - .GroupBy(t => new { t.Email, t.IsGenerateAccount, t.UserTypeId }))) - { - if (group.Count() > 1) - { - throw new BusinessValidationFailedException("同一邮箱同一用户类型,生成账号的数据只允许存在一条!"); - } - } - - //是第一次 - if (!siteUserList.Any(t => t.IsGenerateSuccess)) - { - var currentUserList = siteUserList.Where(t => t.TrialSiteSurveyId == trialSiteSurveyId).ToList(); - - if (!currentUserList.Any(t => t.UserTypeEnum == UserTypeEnum.ClinicalResearchCoordinator) || - !currentUserList.Any(t => t.UserTypeEnum == UserTypeEnum.SR)) - { - throw new BusinessValidationFailedException("本次提交,生成账号必须要有CRC 和SR"); - } - } - //已生成的不管 管的只需要是 生成失败的并且需要生成账号的 - var needGenerateList = _trialSiteUserSurveyRepository.Where(t => t.TrialSiteSurveyId == trialSiteSurveyId && t.IsGenerateAccount && t.IsJoin != true).ProjectTo(_mapper.ConfigurationProvider).ToList(); - - - //await SendInviteEmail(new InviteEmailCommand() { TrialId = trialId, RouteUrl = siteSurvyeSubmit.RouteUrl, UserList = needGenerateList }); + var needGenerateList = _trialSiteUserSurveyRepository.Where(t => t.TrialSiteSurveyId == trialSiteSurveyId && t.IsGenerateAccount && t.IsGenerateSuccess != true).ProjectTo(_mapper.ConfigurationProvider).ToList(); await GenerateAccountAsync(needGenerateList, trialId); await SendSiteSurveyUserJoinEmail(new TrialSiteUserSurveyJoinCommand() { TrialId = trialId, TrialSiteSurveyId = trialSiteSurveyId, RouteUrl = siteSurvyeSubmit.RouteUrl, BaseUrl = siteSurvyeSubmit.BaseUrl, UserList = needGenerateList }); + await DealSiteUserQuitSiteAsync(trialId, trialSiteSurvey.SiteId, needGenerateList); } await _trialSiteSurveyRepository.SaveChangesAsync(); @@ -811,18 +719,13 @@ namespace IRaCIS.Core.Application.Contracts await _trialSiteUserSurveyRepository.BatchUpdateNoTrackingAsync(t => t.Id == item.Id, u => new TrialSiteUserSurvey() { IsGenerateSuccess = true, SystemUserId = sysUserInfo.Id }); - - } await _trialSiteUserSurveyRepository.SaveChangesAsync(); } - - - [TypeFilter(typeof(TrialResourceFilter), Arguments = new object[] { "AfterStopCannNotOpt" })] - public async Task SendSiteSurveyUserJoinEmail(TrialSiteUserSurveyJoinCommand joinCommand) + private async Task SendSiteSurveyUserJoinEmail(TrialSiteUserSurveyJoinCommand joinCommand) { var trialSiteSurvey = await _trialSiteSurveyRepository.FirstAsync(t => t.Id == joinCommand.TrialSiteSurveyId); @@ -846,17 +749,20 @@ namespace IRaCIS.Core.Application.Contracts if (!await _trialUserRepository.AnyAsync(t => t.TrialId == trialId && t.UserId == userId, true)) { await _repository.AddAsync(new TrialUser() { TrialId = trialId, UserId = userId, JoinTime = DateTime.Now }); + + await _mailVerificationService.SiteSurveyUserJoinEmail(trialId, userId, joinCommand.BaseUrl, joinCommand.RouteUrl); + } if (!await _repository.AnyAsync(t => t.TrialId == trialId && t.UserId == userId && t.SiteId == siteId, true)) { await _repository.AddAsync(new TrialSiteUser() { TrialId = trialId, SiteId = siteId, UserId = userId }); + + await _mailVerificationService.SiteSurveyUserJoinEmail(trialId, userId, joinCommand.BaseUrl, joinCommand.RouteUrl); } await _userRepository.BatchUpdateNoTrackingAsync(t => t.Id == userId, u => new User() { Status = UserStateEnum.Enable }); - await _trialSiteUserSurveyRepository.UpdatePartialFromQueryAsync(t => t.Id == userInfo.Id, u => new TrialSiteUserSurvey() { IsJoin = true }); - await _mailVerificationService.SiteSurveyUserJoinEmail(trialId, userId, joinCommand.BaseUrl, joinCommand.RouteUrl); } @@ -869,123 +775,25 @@ namespace IRaCIS.Core.Application.Contracts } - - - - #region 废弃 - //Site 调研邀请 - public async Task SendInviteEmail(InviteEmailCommand inviteEmailCommand) + private async Task DealSiteUserQuitSiteAsync(Guid trialId, Guid siteId, List list) { + //跟踪查询该site下的所有的用户 + var siteUserList = await _repository.Where(t => t.TrialId == trialId && t.SiteId == siteId, true).ToListAsync(); - var trialInfo = await _repository.FirstOrDefaultAsync(t => t.Id == inviteEmailCommand.TrialId); - - - foreach (var item in inviteEmailCommand.UserList) + foreach (var siteUser in siteUserList) { - - var messageToSend = new MimeMessage(); - //发件地址 - messageToSend.From.Add(new MailboxAddress("GRR", "iracis_grr@163.com")); - //收件地址 - messageToSend.To.Add(new MailboxAddress(String.Empty, item.Email)); - //主题 - //$"[来自展影IRC] [{trialInfo.ResearchProgramNo}] 邀请信"; - messageToSend.Subject = _localizer["TrialSiteSurvey_IRCInvitation", trialInfo.ResearchProgramNo]; - - var builder = new BodyBuilder(); - - //找下系统中是否存在该用户类型的 并且邮箱 或者手机的账户 - var sysUserInfo = await _userRepository.Where(t => t.UserTypeId == item.UserTypeId && t.EMail == item.Email).Include(t => t.UserTypeRole).FirstOrDefaultAsync(); - - //int verificationCode = new Random().Next(100000, 1000000); - - //var baseApiUrl = baseUrl.Remove(baseUrl.IndexOf("#")) + "api"; - - - if (sysUserInfo == null) + //当前中心用户 不在调研表存在,就将该人退出 + if (!list.Any(t => t.SystemUserId == siteUser.UserId)) { - - lock (lockObj) - { - var saveItem = _mapper.Map(item); - - saveItem.Code = _userRepository.Select(t => t.Code).DefaultIfEmpty().Max() + 1; - - saveItem.UserCode = AppSettings.GetCodeStr(saveItem.Code, nameof(User)); ; - - saveItem.UserName = saveItem.UserCode; - - saveItem.UserTypeEnum = _repository.Where(t => t.Id == saveItem.UserTypeId).Select(t => t.UserTypeEnum).First(); - - //saveItem.Password = MD5Helper.Md5(verificationCode.ToString()); - - _ = _repository.AddAsync(saveItem).Result; - - _ = _repository.SaveChangesAsync().Result; - - - sysUserInfo = saveItem; - } - - } - - - - builder.HtmlBody = @$" -
-
-
- {sysUserInfo.LastName + "/" + sysUserInfo.FirstName}: -
-
- {_localizer["TrialSiteSurvey_IRCInvitationContent", trialInfo.ResearchProgramNo]} -
- - - 查看并确认 - -
-
- "; - - - - messageToSend.Body = builder.ToMessageBody(); - - using (var smtp = new MailKit.Net.Smtp.SmtpClient()) - { - - smtp.ServerCertificateValidationCallback = (s, c, h, e) => true; - - smtp.MessageSent += (sender, args) => - { - - _ = _trialSiteUserSurveyRepository.BatchUpdateNoTrackingAsync(t => t.Id == item.Id, u => new TrialSiteUserSurvey() { IsGenerateSuccess = true, InviteState = TrialSiteUserStateEnum.HasSend, ConfirmTime = null, RejectReason = String.Empty, SystemUserId = sysUserInfo.Id, ExpireTime = DateTime.Now.AddDays(7) }).Result; - - }; - - - await smtp.ConnectAsync("smtp.163.com", 465, SecureSocketOptions.StartTls); - - - await smtp.AuthenticateAsync("iracis_grr@163.com", "XLWVQKZAEKLDWOAH"); - - - await smtp.SendAsync(messageToSend); - - - await smtp.DisconnectAsync(true); + siteUser.IsDeleted = true; + siteUser.DeletedTime = DateTime.Now; } } - - return ResponseOutput.Ok(); + await _repository.SaveChangesAsync(); } - #endregion - - } } diff --git a/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteUserSurveyService.cs b/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteUserSurveyService.cs index 78a25359e..248db3de0 100644 --- a/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteUserSurveyService.cs +++ b/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteUserSurveyService.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc; using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Application.Filter; using IRaCIS.Core.Infrastructure; +using IRaCIS.Application.Contracts; namespace IRaCIS.Core.Application.Contracts { @@ -105,7 +106,15 @@ namespace IRaCIS.Core.Application.Contracts } - var entity = await _trialSiteUserSurveyRepository.InsertOrUpdateAsync(addOrEditTrialSiteUserSurvey, true); + var verifyExp1 = new EntityVerifyExp() + { + VerifyExp = u => u.UserTypeId == addOrEditTrialSiteUserSurvey.UserTypeId && u.Email == addOrEditTrialSiteUserSurvey.Email, + VerifyMsg = "同一邮箱同一用户类型,生成账号的数据只允许存在一条!", + IsVerify=addOrEditTrialSiteUserSurvey.IsGenerateAccount + + }; + + var entity = await _trialSiteUserSurveyRepository.InsertOrUpdateAsync(addOrEditTrialSiteUserSurvey, true, verifyExp1); return ResponseOutput.Ok(entity.Id.ToString()); diff --git a/IRaCIS.Core.Domain/SiteSurvey/TrialSiteUserSurvey.cs b/IRaCIS.Core.Domain/SiteSurvey/TrialSiteUserSurvey.cs index 69563657c..9600b3c91 100644 --- a/IRaCIS.Core.Domain/SiteSurvey/TrialSiteUserSurvey.cs +++ b/IRaCIS.Core.Domain/SiteSurvey/TrialSiteUserSurvey.cs @@ -85,13 +85,6 @@ namespace IRaCIS.Core.Domain.Models public Guid? SystemUserId { get; set; } - public DateTime? ExpireTime { get; set; } - - public bool IsJoin { get; set; } - - public DateTime? ConfirmTime { get; set; } - - public string RejectReason { get; set; } = string.Empty; /// /// IsGenerateAccount @@ -104,7 +97,17 @@ namespace IRaCIS.Core.Domain.Models public TrialSiteUserStateEnum InviteState { get; set; } = TrialSiteUserStateEnum.WaitSent; - + + #region 废弃 + public DateTime? ExpireTime { get; set; } + + public bool IsJoin { get; set; } + + public DateTime? ConfirmTime { get; set; } + + public string RejectReason { get; set; } = string.Empty; + #endregion + }