using IRaCIS.Core.Domain.Models;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Reflection;
using EntityFramework.Exceptions.SqlServer;
using IRaCIS.Core.Domain.Share;
using MassTransit;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using UserTypeGroup = IRaCIS.Core.Domain.Models.UserTypeGroup;
using IRaCIS.Core.Infra.EFCore.Common;
using IRaCIS.Core.Infra.EFCore.Common.Dto;
using EntityFramework.Exceptions.Common;
using IRaCIS.Core.Infrastructure;
using System.Data;

namespace IRaCIS.Core.Infra.EFCore
{
    public class IRaCISDBContext : DbContext
    {
        public readonly IUserInfo _userInfo;

        public readonly ILogger<IRaCISDBContext> _logger;

        // 在控制台
        //public static readonly ILoggerFactory MyLoggerFactory    = LoggerFactory.Create(builder => { builder.AddConsole(); });
        // 调试窗口
        public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddDebug(); });

        public IRaCISDBContext(DbContextOptions<IRaCISDBContext> options, IUserInfo userInfo, ILogger<IRaCISDBContext> _Logger

            ) : base(options)
        {
            _userInfo = userInfo;

            _logger = _Logger;

        }


        //比数据库上下文构造函数先执行 不能构造函数注入的方式使用配置文件
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseLoggerFactory(MyLoggerFactory)

                .ReplaceService<IModelCacheKeyFactory, DynamicModelCacheKeyFactoryDesignTimeSupport>();

            optionsBuilder.UseExceptionProcessor();

            //var config = new ConfigurationBuilder()
            //      .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).Build();
            //connectionString = config.GetSection("ConnectionStrings:RemoteNew").Value;
            //optionsBuilder.AddInterceptors(new AuditingInterceptor(connectionString));
        }

        #region IModelCacheKeyFactory


        public class DynamicModelCacheKeyFactoryDesignTimeSupport : IModelCacheKeyFactory
        {
            public object Create(DbContext context, bool designTime)
                => context is IRaCISDBContext dynamicContext
                    ? (context.GetType(), dynamicContext._userInfo.IsEn_Us, designTime)
                    : (object)context.GetType();

            public object Create(DbContext context)
                => Create(context, false);
        }

        #endregion




        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TableList>(builder =>
            {
                builder.HasBaseType((Type)null);
                builder.ToView(null);
                builder.HasNoKey();
            });

            //modelBuilder.HasDbFunction(typeof(DbContext).GetMethod(nameof(GetTableList)));

            //modelBuilder.Entity<User>().HasQueryFilter(u => u.UserTypeEnum != UserTypeEnum.SuperAdmin);

            modelBuilder.Entity<User>().HasMany(t => t.VisitTaskList).WithOne(t => t.DoctorUser).HasForeignKey(t => t.DoctorUserId).IsRequired(false);


            modelBuilder.Entity<TaskMedicalReviewRule>().HasMany(t => t.DoctorTrialVisitTaskList).WithOne(t => t.DoctorTaskMedicalReviewRule).HasForeignKey(t => new { t.DoctorUserId, t.TrialId }).HasPrincipalKey(u => new { u.DoctorUserId, u.TrialId });

            modelBuilder.Entity<TaskMedicalReviewRule>().HasMany(t => t.TaskMedicalReviewList).WithOne(t => t.TaskMedicalReviewRule).HasForeignKey(t => new { t.DoctorUserId, t.TrialId }).HasPrincipalKey(u => new { u.DoctorUserId, u.TrialId });

            //会导致级联删除  
            //modelBuilder.Entity<SubjectUser>().HasMany(t => t.SubjectArmVisitTaskList).WithOne(t => t.SujectArm).HasForeignKey(t => new { t.SubjectId, t.ArmEnum }).HasPrincipalKey(u => new { u.SubjectId, u.ArmEnum })
            //    ./*IsRequired(false)*/OnDelete(DeleteBehavior.NoAction);

            // modelBuilder.Entity<VisitTask>().HasOne(t => t.SujectArm).WithMany(s => s.SubjectArmVisitTaskList).HasForeignKey(t => new { t.SubjectId, t.ArmEnum }).HasPrincipalKey(u => new { u.SubjectId, u.ArmEnum });

            modelBuilder.Entity<VisitTask>().HasMany(t => t.JudgeVisitList).WithOne(t => t.JudgeVisitTask);


            modelBuilder.Entity<VisitTask>().HasMany(t => t.TaskMedicalReviewList).WithOne(t => t.VisitTask).HasForeignKey(t => t.VisitTaskId);

            modelBuilder.Entity<VisitTask>().HasOne(t => t.Subject).WithMany(s => s.SubjectVisitTaskList).HasForeignKey(t => t.SubjectId);

            modelBuilder.Entity<VisitTask>().HasMany(t => t.TaskInfluenceList).WithOne(s => s.OriginalTask).HasForeignKey(t => t.OriginalTaskId);


            modelBuilder.Entity<VisitTask>().HasMany(t => t.GlobalVisitResultList).WithOne(s => s.GlobalVisitTask).HasForeignKey(t => t.GlobalTaskId);


            modelBuilder.Entity<Dictionary>().HasMany(t => t.ChildList).WithOne(t => t.Parent);
            modelBuilder.Entity<SubjectUser>().HasMany(t => t.EarlierSubjectUserList).WithOne(t => t.OrignalSubjectUser);
            if (_userInfo.IsEn_Us)
            {
                modelBuilder.Entity<Dictionary>().Property(t => t.MappedValue).HasColumnName(nameof(Domain.Models.Dictionary.Value));
            }
            else
            {
                modelBuilder.Entity<Dictionary>().Property(t => t.MappedValue).HasColumnName(nameof(Domain.Models.Dictionary.ValueCN));
            }


            //遍历实体模型手动配置
            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes().Where(q => q.GetInterface(typeof(IEntityTypeConfiguration<>).FullName) != null);
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.ApplyConfiguration(configurationInstance);
            }

            base.OnModelCreating(modelBuilder);


            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            {
                // 软删除配置
                if (typeof(ISoftDelete).IsAssignableFrom(entityType.ClrType))
                {
                    entityType.AddSoftDeleteQueryFilter();

                }

                if (typeof(Entity).IsAssignableFrom(entityType.ClrType))
                {
                    modelBuilder.Entity(entityType.ClrType).Property<Guid>(nameof(Entity.Id)).HasValueGenerator<MySequentialGuidValueGenerator>();
                }


            }

        }

        #region 
        public IQueryable<TableList> GetTableList()
        {
            return Set<TableList>().FromSqlRaw("EXEC dbo.procGetTableList");
        }

        public IQueryable<TableList> GetTableColumn(string tableName)
        {
            return Set<TableList>().FromSqlRaw($"EXEC dbo.procGetTableColumn {tableName}");
        }
        #endregion

        #region Doctor
        public virtual DbSet<Dictionary> Dictionary { get; set; }
        public virtual DbSet<Doctor> Doctor { get; set; }

        public virtual DbSet<DoctorCriterionFile> DoctorCriterionFile { get; set; }
        public virtual DbSet<DoctorDictionary> DoctorDictionary { get; set; }
        public virtual DbSet<Postgraduate> Postgraduate { get; set; }
        public virtual DbSet<Education> Education { get; set; }
        public virtual DbSet<ResearchPublication> ResearchPublications { get; set; }
        public virtual DbSet<TrialExperience> TrialExperience { get; set; }

        public virtual DbSet<UserDoctor> UserDoctor { get; set; }
        public virtual DbSet<Vacation> Vacation { get; set; }

        public virtual DbSet<Attachment> Attachment { get; set; }
        public virtual DbSet<TrialExperienceCriteria> TrialExperienceCriteria { get; set; }




        #endregion

        #region Enroll

        public virtual DbSet<Workload> DoctorWorkload { get; set; }
        public virtual DbSet<Enroll> Enroll { get; set; }

        public virtual DbSet<EnrollReadingCategory> EnrollReadingCategory { get; set; }


        public virtual DbSet<EnrollDetail> EnrollDetails { get; set; }




        #endregion


        #region Reading
        public virtual DbSet<TrialCriterionDictionaryCode> TrialCriterionDictionaryCode { get; set; }

        public virtual DbSet<SystemCriterionDictionaryCode> SystemCriterionDictionaryCode { get; set; }
        public virtual DbSet<ReadingTaskRelation> ReadingTaskRelation { get; set; }
        public virtual DbSet<OrganInfo> OrganInfo { get; set; }

        public virtual DbSet<ReadingCriterionDictionary> ReadingCriterionDictionary { get; set; }
        public virtual DbSet<ReadingTableAnswerRowInfo> ReadingTableAnswerRowInfo { get; set; }
        public virtual DbSet<OrganTrialInfo> OrganTrialInfo { get; set; }
        public virtual DbSet<ReadingTableQuestionSystem> ReadingTableQuestionSystem { get; set; }
        public virtual DbSet<ReadingPeriodSet> ReadingPeriodSet { get; set; }

        public virtual DbSet<ReadingTaskQuestionAnswer> ReadingTaskQuestionAnswer { get; set; }
        public virtual DbSet<ReadingPeriodPlan> ReadingPeriodPlan { get; set; }

        public virtual DbSet<ReadingClinicalData> ReadingClinicalData { get; set; }

        public virtual DbSet<ReadingOncologyTaskInfo> ReadingOncologyTaskInfo { get; set; }
        public virtual DbSet<ReadingGlobalTaskInfo> ReadingGlobalTaskInfo { get; set; }
        public virtual DbSet<ReadingQuestionCriterionSystem> ReadingQuestionCriterionSystem { get; set; }

        public virtual DbSet<ReadingQuestionCriterionTrial> ReadingQuestionCriterionTrial { get; set; }

        public virtual DbSet<ReadingQuestionSystem> ReadingQuestionSystem { get; set; }

        public virtual DbSet<ReadingQuestionTrial> ReadingQuestionTrial { get; set; }

        //public virtual DbSet<ReadingClinicalDataView> ReadingClinicalDataView { get; set; }

        public virtual DbSet<ReadingClinicalDataPDF> ReadingClinicalDataPDF { get; set; }

        public virtual DbSet<ReadingJudgeInfo> ReadingJudgeInfo { get; set; }

        public virtual DbSet<ReadModule> ReadModule { get; set; }

        public virtual DbSet<ReadModuleView> ReadModuleView { get; set; }

        public virtual DbSet<ClinicalDataTrialSet> ClinicalDataTrialSet { get; set; }

        public virtual DbSet<ClinicalDataSystemSet> ClinicalDataSystemSet { get; set; }

        public virtual DbSet<ReadingMedicineSystemQuestion> ReadingMedicineSystemQuestion { get; set; }

        public virtual DbSet<ReadingMedicineTrialQuestion> ReadingMedicineTrialQuestion { get; set; }

        public virtual DbSet<ReadingMedicineQuestionAnswer> ReadingMedicineQuestionAnswer { get; set; }

        public virtual DbSet<ReadingMedicalReviewDialog> ReadingMedicalReviewDialog { get; set; }

        public virtual DbSet<CriterionNidus> CriterionNidus { get; set; }

        public virtual DbSet<ReadingTableQuestionTrial> ReadingTableQuestionTrial { get; set; }


        public virtual DbSet<TumorAssessment> TumorAssessment { get; set; }

        public virtual DbSet<TrialClinicalDataSetCriterion> TrialClinicalDataSetCriterion { get; set; }


        //public virtual DbSet<TrialClinicalDataCriterion> TrialClinicalDataCriterion { get; set; }
        //public virtual DbSet<SystemClinicalDataCriterion> SystemClinicalDataCriterion { get; set; }

        #endregion

        #region Subject and Visit and study
        public virtual DbSet<StudyMonitor> StudyMonitor { get; set; }

        public virtual DbSet<Subject> Subject { get; set; }
        public virtual DbSet<VisitStage> VisitPlans { get; set; }
        public virtual DbSet<VisitPlanInfluenceStudy> VisitPlanInfluenceStudy { get; set; }

        public virtual DbSet<VisitPlanInfluenceStat> VisitPlanInfluenceStat { get; set; }

        public virtual DbSet<NoneDicomStudyFile> NoneDicomStudyFile { get; set; }
        public virtual DbSet<NoneDicomStudy> NoneDicomStudy { get; set; }
        public virtual DbSet<PreviousPDF> PreviousPDF { get; set; }
        public virtual DbSet<PreviousSurgery> PreviousSurgery { get; set; }
        public virtual DbSet<PreviousOther> PreviousOther { get; set; }
        public virtual DbSet<PreviousHistory> PreviousHistory { get; set; }

        public virtual DbSet<StudyDTF> StudyDTF { get; set; }
        public virtual DbSet<DicomStudy> DicomStudys { get; set; }
        public virtual DbSet<DicomSeries> DicomSeries { get; set; }
        public virtual DbSet<DicomInstance> DicomInstances { get; set; }
        public virtual DbSet<ImageShare> ImageShare { get; set; }


        #endregion

        #region Management
        public virtual DbSet<VerificationCode> VerificationCodes { get; set; }
        public virtual DbSet<Menu> MenuFunctions { get; set; }
        public virtual DbSet<Role> Roles { get; set; }
        public virtual DbSet<UserTypeMenu> UserTypeMenuFunction { get; set; }
        public virtual DbSet<User> Users { get; set; }
        public virtual DbSet<TrialAudit> TrialAudit { get; set; }
        public virtual DbSet<UserType> UserType { get; set; }

        public virtual DbSet<SaveChangesAudit> SaveChangesAudits { get; set; }



        #endregion

        #region Institution



        public virtual DbSet<Site> ResearchCenter { get; set; }
        public virtual DbSet<Hospital> Hospitals { get; set; }
        public virtual DbSet<CRO> CROCompany { get; set; }
        public virtual DbSet<Sponsor> Sponsor { get; set; }

        #endregion

        #region Trial
        public virtual DbSet<Trial> Trial { get; set; }

       
        public virtual DbSet<TrialDictionary> TrialDictionary { get; set; }
        public virtual DbSet<TrialStatusDetail> TrialDetail { get; set; }
        public virtual DbSet<TrialUser> UserTrial { get; set; }

        public virtual DbSet<TrialDictionary> ProjectDictionary { get; set; }

        public virtual DbSet<TrialSiteUser> UserTrialSite { get; set; }
        public virtual DbSet<TrialSite> TrialSite { get; set; }

        public virtual DbSet<Site> Site { get; set; }

        public virtual DbSet<User> User { get; set; }

        public virtual DbSet<TrialSiteUserSurvey> TrialSiteUserSurvey { get; set; }
        public virtual DbSet<TrialSiteEquipmentSurvey> TrialSiteEquipmentSurvey { get; set; }
        public virtual DbSet<TrialSiteSurvey> TrialSiteSurvey { get; set; }

        public virtual DbSet<StudyStatusDetail> StudyStatusDetails { get; set; }



        #endregion

        #region Financial
        public virtual DbSet<ReviewerPayInformation> ReviewerPayInformation { get; set; }
        public virtual DbSet<RankPrice> RankPrice { get; set; }
        public virtual DbSet<TrialPaymentPrice> TrialPaymentPrice { get; set; }
        public virtual DbSet<VolumeReward> AwardPrice { get; set; }
        public virtual DbSet<Payment> Payment { get; set; }
        public virtual DbSet<PaymentDetail> PaymentDetail { get; set; }
        public virtual DbSet<ExchangeRate> ExchangeRate { get; set; }
        public virtual DbSet<TrialRevenuesPrice> TrialRevenuesPrice { get; set; }
        public virtual DbSet<PaymentAdjustment> PaymentAdjustment { get; set; }
        public virtual DbSet<TrialRevenuesPriceVerification> TrialRevenuesPriceVerification { get; set; }


        #endregion

        #region QC

        public virtual DbSet<TrialQCQuestion> TrialQCQuestionConfigure { get; set; }
        public virtual DbSet<QCQuestion> QCQuestionConfigure { get; set; }
        public virtual DbSet<TrialQCQuestionAnswer> TrialQCQuestionAnswer { get; set; }
        public virtual DbSet<CheckChallengeDialog> CheckChallengeDialog { get; set; }
        #endregion


        #region QA

        public virtual DbSet<QCChallengeDialog> QCChallengeDialog { get; set; }
        //public virtual DbSet<QANotice> QATemplateDictionary { get; set; }
        public virtual DbSet<QCChallenge> QCChallenge { get; set; }
        public virtual DbSet<SubjectVisit> SubjectVisit { get; set; }
        #endregion




        #region Document
        public virtual DbSet<SystemDocument> SystemDocument { get; set; }
        public virtual DbSet<TrialDocument> TrialDocument { get; set; }
        public virtual DbSet<TrialDocNeedConfirmedUserType> TrialDocUserTypeConfirm { get; set; }
        public virtual DbSet<SystemDocConfirmedUser> SystemDocConfirmedUser { get; set; }
        public virtual DbSet<SystemDocNeedConfirmedUserType> SystemDocNeedConfirmedUserType { get; set; }

        public virtual DbSet<TrialDocNeedConfirmedUserType> TrialDocNeedConfirmedUserType { get; set; }

        public virtual DbSet<TrialDocConfirmedUser> TrialDocConfirmedUser { get; set; }
        #endregion

        #region 暂时未用

        #region 工作量分配
        //public virtual DbSet<WorkloadTP> WorkloadTPs { get; set; }
        //public virtual DbSet<WorkloadGlobal> WorkloadGlobals { get; set; }
        //public virtual DbSet<WorkloadAD> WorkloadADs { get; set; }
        //public virtual DbSet<WorkloadDetail> WorkloadDetails { get; set; }
        #endregion
        //public virtual DbSet<Message> SysMessages { get; set; }
        //public virtual DbSet<TrialAttachment> TrialAttachment { get; set; }
        //public virtual DbSet<SystemLog> SystemLogs { get; set; }
        //public virtual DbSet<TU> TU { get; set; }
        //public virtual DbSet<TR> TR { get; set; }
        //public virtual DbSet<RS> RS { get; set; }
        //public virtual DbSet<Report> Reports { get; set; }
        //public virtual DbSet<StudyReviewer> StudyReviewer { get; set; }

        //public virtual DbSet<KeyInstance> KeyInstances { get; set; }
        //public virtual DbSet<GlobalRS> GlobalRS { get; set; }
        //public virtual DbSet<GlobalResult> GlobalResult { get; set; }
        //public virtual DbSet<ImageLabel> ImageLabels { get; set; }

        //public virtual DbSet<QATemplateItem> QaTemplateItem { get; set; }
        //public virtual DbSet<QATemplateItemDictionary> QaTemplateItemDictionary { get; set; }
        //public virtual DbSet<QATemplateTemplateItem> QaTemplateTemplateItem { get; set; }
        //public virtual DbSet<QATrialTemplate> QATrailTemplate { get; set; }
        //public virtual DbSet<QATrialTemplateItem> QATrialTemplateItem { get; set; }
        //public virtual DbSet<QAAboutTrialTemplateItem> QARecordTrialTemplateItem { get; set; }
        //public virtual DbSet<QARecordTemplateItemDetail> QARecordTemplateItemDetail { get; set; }
        //public virtual DbSet<QATemplate> QATemplate { get; set; }
        //public virtual DbSet<QANoticeUser> QANoticeUser { get; set; }
        //public virtual DbSet<QANotice> QANotice { get; set; }
        //public virtual DbSet<TrialUserPreparation> TrialUserPreparation { get; set; }

        #endregion

        public virtual DbSet<ShortcutKey> ShortcutKey { get; set; }
        public virtual DbSet<EmailNoticeConfig> EmailNoticeConfig { get; set; }
        public virtual DbSet<SystemBasicData> SystemBasicData { get; set; }

        public virtual DbSet<TrialSign> TrialSign { get; set; }

        public virtual DbSet<TrialStateChange> TrialStateChange { get; set; }

        public virtual DbSet<SystemAnonymization> SystemAnonymization { get; set; }

        public virtual DbSet<TrialExternalUser> TrialExternalUser { get; set; }

        public virtual DbSet<UserTypeGroup> UserTypeGroup { get; set; }

        public virtual DbSet<DataInspection> DataInspection { get; set; }



        public virtual DbSet<FrontAuditConfig> FrontAuditConfig { get; set; }

        public virtual DbSet<ConsistencyCheckFile> ConsistencyCheckFile { get; set; }

        public virtual DbSet<CommonDocument> CommonDocument { get; set; }

        public virtual DbSet<SystemNotice> SystemNotice { get; set; }

        public virtual DbSet<SystemNoticeUserRead> SystemNoticeUserRead { get; set; }

        public virtual DbSet<SystemNoticeUserType> SystemNoticeUserType { get; set; }

        public virtual DbSet<ReadingTableQuestionAnswer> ReadingTableQuestionAnswer { get; set; }



        #region 废弃

        public override int SaveChanges()
        {

            SetCommonEntityAuditInfo();
            AddAudit().GetAwaiter();
            return base.SaveChanges();
        }
        #endregion
        public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
        {
            //  采用触发器的方式 设置 CreateUserId CreateTime UpdateTime UpdateUserId    稽查实体里面没有这四个字段的值 因为先后顺序的原因
            SetCommonEntityAuditInfo();
            await AddAudit();


            var result = 0;

            try
            {

                result = await base.SaveChangesAsync(cancellationToken);
            }
            catch (UniqueConstraintException ex)
            {
                _logger.LogError(ex.Message);

                throw new DBSaveFailedException("该唯一键已经存在于数据库中。");

            }
            catch (TimeoutException ex)
            {
                _logger.LogError(ex.Message);

                throw new DBSaveFailedException("数据库操作已经超时，请稍后重试。");

            }
            catch (CannotInsertNullException ex)
            {
                _logger.LogError(ex.Message);

                throw new DBSaveFailedException("无法在非空列上插入空值。");
            }
            catch (MaxLengthExceededException ex)
            {
                _logger.LogError(ex.Message);

                throw new DBSaveFailedException("字符串超过了数据库列的最大长度。");

               

            }
            catch (NumericOverflowException ex)
            {
                _logger.LogError(ex.Message);

                throw new DBSaveFailedException("数值超过了数据类型的范围。");
            }
            catch (SyntaxErrorException ex)
            {
                _logger.LogError(ex.Message);

                throw new DBSaveFailedException("SQL 查询中存在语法错误。");
            }
            catch (ReferenceConstraintException ex)
            {
                _logger.LogError(ex.Message);
                throw new DBSaveFailedException("无法进行当前操作，当前数据不符合外键约束。");
            }
            catch (DbUpdateConcurrencyException ex)
            {
                _logger.LogError(ex.Message);

                throw new DBSaveFailedException("SQL 事务失败,请检查环境。");
            }
            return result;
        }


        public async Task AddAudit()
        {

            //触发器里面提交事务 业务方法里面提交事务 会记录两次
            var inspectionGeneralIdList = ChangeTracker.Entries().Where(t => typeof(DataInspection).IsAssignableFrom(t.Entity.GetType())).Select(t => ((DataInspection)t.Entity).GeneralId).ToList();

            var entities = ChangeTracker.Entries().Where(u => (u.State == EntityState.Modified || u.State == EntityState.Deleted || u.State == EntityState.Added))
                .Where(t => !typeof(DataInspection).IsAssignableFrom(t.Entity.GetType()) && !inspectionGeneralIdList.Contains(((Entity)t.Entity).Id))
                .ToList();
            AuditingData auditingData = new AuditingData(this, _userInfo);

            //await auditingData.IncomingEntitys(entities);


            if (entities.Count > 0)
            {
                await auditingData.InsertAddEntitys(entities);

            }
           

        }

        /// <summary>
        /// 重写savechange方式 统一增加审计信息 CreateUserId CreateTime UpdateTime Update UserId  
        /// </summary>
        private void SetCommonEntityAuditInfo()
        {

            ChangeTracker.DetectChanges(); // Important!

            // 获取所有更改，删除，新增的实体，但排除审计实体（避免死循环）
            var entities = ChangeTracker.Entries()
                .Where(u => (u.State == EntityState.Modified || u.State == EntityState.Deleted || u.State == EntityState.Added)).Where(x => !typeof(DataInspection).IsAssignableFrom(x.Entity.GetType())).ToList();

            foreach (var t in entities)
            {
                switch (t.State)
                {

                    case EntityState.Deleted:

                        break;
                    case EntityState.Modified:

                        if (t.Entity is IAuditUpdate updateEntity1)
                        {
                            updateEntity1.UpdateTime = DateTime.Now;
                            updateEntity1.UpdateUserId = _userInfo.Id;
                        }

                        if (t.Entity is ISoftDelete softDelete)
                        {
                            if (softDelete.IsDeleted)
                            {
                                softDelete.DeleteUserId = _userInfo.Id;
                                softDelete.DeletedTime = DateTime.Now;
                            }
                            else
                            {
                                softDelete.DeletedTime = null;
                            }
                        }

                        break;
                    //添加的时候，更新审计字段也赋值
                    case EntityState.Added:


                        if (t.Entity is IAuditAdd addEntity)
                        {
                            if (addEntity.CreateTime == default(DateTime))
                            {
                                addEntity.CreateTime = DateTime.Now;
                            }

                            addEntity.CreateUserId = _userInfo.Id;
                        }

                        if (t.Entity is IAuditUpdate updateEntity)
                        {
                            updateEntity.UpdateTime = DateTime.Now;
                            updateEntity.UpdateUserId = _userInfo.Id;
                        }

                        if (t.Entity is IAuditAddWithUserName addEntity3)
                        {
                            if (addEntity3.CreateTime == default(DateTime))
                            {
                                addEntity3.CreateTime = DateTime.Now;

                            }

                            addEntity3.CreateUserId = _userInfo.Id;
                            addEntity3.CreateUser = _userInfo.RealName;
                        }
                        break;
                }
            }


        }



        #region 更新审计信息  废弃

        /// <summary>
        /// 事件绑定的方式 更新审计信息  废弃
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateAuitUser(object sender, EntityEntryEventArgs e)
        {
            if (e.Entry.Entity is IAuditUpdate updateEntity)
            {
                switch (e.Entry.State)
                {
                    //case EntityState.Deleted:
                    //    entityWithTimestamps.UpdateTime = DateTime.UtcNow;
                    //    Console.WriteLine($"Stamped for delete: {e.Entry.Entity}");
                    //    break;
                    case EntityState.Modified:
                        updateEntity.UpdateTime = DateTime.UtcNow;
                        updateEntity.UpdateUserId = _userInfo.Id;
                        break;
                    //添加的时候，更新审计字段也赋值
                    case EntityState.Added:
                        updateEntity.UpdateTime = DateTime.UtcNow;
                        updateEntity.UpdateUserId = _userInfo.Id;
                        break;
                }
            }
            if (e.Entry.Entity is IAuditAdd addEntity)
            {
                switch (e.Entry.State)
                {
                    case EntityState.Added:
                        addEntity.CreateTime = DateTime.UtcNow;
                        addEntity.CreateUserId = _userInfo.Id;
                        break;
                }
            }

            if (e.Entry.Entity is IAuditAddWithUserName addEntity2)
            {
                switch (e.Entry.State)
                {
                    case EntityState.Added:
                        addEntity2.CreateTime = DateTime.UtcNow;
                        addEntity2.CreateUserId = _userInfo.Id;
                        addEntity2.CreateUser = _userInfo.RealName;
                        break;
                    case EntityState.Deleted:
                        addEntity2.CreateTime = DateTime.UtcNow;
                        addEntity2.CreateUserId = _userInfo.Id;
                        addEntity2.CreateUser = _userInfo.RealName;
                        break;
                }
            }
        }

        #endregion




        public virtual DbSet<TaskAllocationRule> TaskAllocationRule { get; set; }

        public virtual DbSet<VisitTask> VisitTask { get; set; }

        public virtual DbSet<SubjectUser> SubjectUser { get; set; }

        public virtual DbSet<VisitTaskReReading> VisitTaskReReading { get; set; }

        public virtual DbSet<TaskMedicalReview> TaskMedicalReview { get; set; }

        public virtual DbSet<TaskMedicalReviewRule> TaskMedicalReviewRule { get; set; }

        public virtual DbSet<TaskConsistentRule> TaskConsistentRule { get; set; }


        public virtual DbSet<TaskInfluence> TaskInfluence { get; set; }

        public virtual DbSet<SubjectCanceDoctor> SubjectCanceDoctor { get; set; }

        public virtual DbSet<TrialEmailNoticeConfig> TrialEmailNoticeConfig { get; set; }

        public virtual DbSet<TrialEmailNoticeUser> TrialEmailNoticeUser { get; set; }


    }

    public class MySequentialGuidValueGenerator : ValueGenerator<Guid>
    {
        public override Guid Next(EntityEntry entry)
        {
            return NewId.NextGuid();
        }
        public override bool GeneratesTemporaryValues => false;
    }
}