事件存储准备
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
|
||||
|
||||
using IRaCIS.Core.Domain;
|
||||
using IRaCIS.Core.Domain.BaseModel;
|
||||
using IRaCIS.Core.Domain.Models;
|
||||
using IRaCIS.Core.Domain.Share;
|
||||
using IRaCIS.Core.Infrastructure.Extention;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace IRaCIS.Core.Infra.EFCore;
|
||||
|
||||
@@ -12,17 +16,77 @@ public static class DBContext_Ext
|
||||
{
|
||||
public static void AddDomainEvents(this DbContext context)
|
||||
{
|
||||
var dbContext = (IRaCISDBContext)context;
|
||||
|
||||
var changeTracker = context.ChangeTracker;
|
||||
|
||||
|
||||
//记录要保存到数据库的事件
|
||||
var eventStoreList = new List<EventStoreRecord>();
|
||||
|
||||
#region 中心调研邮件通知
|
||||
|
||||
foreach (var entry in changeTracker.Entries<TrialSiteSurvey>())
|
||||
{
|
||||
var trialSiteSurvey = entry.Entity;
|
||||
|
||||
var originState = entry.Property(p => p.State).OriginalValue;
|
||||
|
||||
//状态从待提交 变为CRC提交 (驳回也会变为已提交,所以必须设置前置状态是待提交)
|
||||
if (trialSiteSurvey.State == TrialSiteSurveyEnum.CRCSubmitted && originState == TrialSiteSurveyEnum.ToSubmit)
|
||||
{
|
||||
trialSiteSurvey.AddDomainEvent(new UserSiteSurveySubmitedEvent() { TrialSiteSurveyId = trialSiteSurvey.Id });
|
||||
}
|
||||
|
||||
//SPM CPM 提交的时候
|
||||
else if (trialSiteSurvey.State == TrialSiteSurveyEnum.SPMApproved && originState == TrialSiteSurveyEnum.CRCSubmitted)
|
||||
{
|
||||
trialSiteSurvey.AddDomainEvent(new SiteSurveySPMSubmitedEvent() { TrialSiteSurveyId = trialSiteSurvey.Id });
|
||||
}
|
||||
|
||||
//PM 驳回 (在消费者中具体判断 是驳回给谁发邮件)
|
||||
else if ((trialSiteSurvey.State == TrialSiteSurveyEnum.CRCSubmitted || trialSiteSurvey.State == TrialSiteSurveyEnum.ToSubmit) && originState == TrialSiteSurveyEnum.SPMApproved)
|
||||
{
|
||||
trialSiteSurvey.AddDomainEvent(new SiteSurverRejectedEvent() { TrialSiteSurveyId = trialSiteSurvey.Id });
|
||||
}
|
||||
|
||||
//添加进记录
|
||||
eventStoreList.AddRange(trialSiteSurvey.DomainEvents.Select(t => new EventStoreRecord() { EventType = t.GetType().Name, EventData = t.ToJsonStr() }));
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region PD 入组发送邮件(不侵入之前的代码,有些判断过于复杂的,在代码里面有的,就手动 在跟踪的实体添加领域事件)
|
||||
|
||||
foreach (var entry in changeTracker.Entries<SubjectVisit>())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
//跟随事务一起保存数据库
|
||||
dbContext.EventStoreRecord.AddRangeAsync(eventStoreList);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region 暂时废弃
|
||||
/// <summary>
|
||||
/// 暂时废弃,没有场景使用
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public static void AddDomainCommands(this DbContext context)
|
||||
{
|
||||
var changeTracker = context.ChangeTracker;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//遍历 ChangeTracker 中的实体
|
||||
//foreach (var entry in changeTracker.Entries<UserLog>())
|
||||
//{
|
||||
@@ -51,5 +115,8 @@ public static class DBContext_Ext
|
||||
//}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user