irc-netcore-api/IRaCIS.Core.Application/Triggers/BeforeSaveTrigger/AddlTrialUserTrigger.cs

56 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using EntityFrameworkCore.Triggered;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;
using Microsoft.Extensions.Localization;
namespace IRaCIS.Core.Application.Triggers
{
// 统一处理 外部用户、中心调研(先添加 再发送邮件)、参与医生加入到项目 ----废弃
public class AddlTrialUserTrigger(
IStringLocalizer _localizer,
IRepository<Trial> _trialRepository,
IRepository<UserRole> _userRoleRepository) : IBeforeSaveTrigger<TrialUserRole>
{
public async Task BeforeSave(ITriggerContext<TrialUserRole> context, CancellationToken cancellationToken)
{
var trialUser = context.Entity;
if (context.ChangeType == ChangeType.Added)
{
//批量添加的时候使用Find 不会多次查询,优先从跟踪的内存中查找
var trialInfo = await _trialRepository.FindAsync(trialUser.TrialId);
// 必须包在同一个事务,有的时候是数据库还没用户,不能直接查询数据库
var user = await _userRoleRepository.FindAsync(trialUser.UserId);
if (trialInfo.TrialType == Domain.Share.TrialType.OfficialTrial || trialInfo.TrialType == Domain.Share.TrialType.Training)
{
if (user.IsTestUser)
{
//---正式类型 、培训类型的项目 不允许加入测试用户
throw new BusinessValidationFailedException(_localizer["AddlTrialUser_NoTestUser"]);
}
}
if (trialInfo.TrialType == TrialType.NoneOfficial)
{
if (user.IsTestUser == false)
{
//---测试项目 不允许加入正式用户
throw new BusinessValidationFailedException(_localizer["AddlTrialUser_NoFormalUser"]);
}
}
}
}
}
}