

using AutoMapper;
using IRaCIS.Core.Domain;
using MassTransit;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace IRaCIS.Core.Application.MassTransit.Consumer;




/// <summary>
/// 添加Subject 触发添加访视 不能代替 Trigger,稽查BatchId 不一致 
/// 因为消费者这里的数据库上下文 和消息发送者上下文不是同一个，相当于两个独立的事务
/// </summary>
/// <param name="_subjectVisitRepository"></param>
/// <param name="_visitStageRepository"></param>
/// <param name="_trialRepository"></param>
/// <param name="_mapper"></param>
public class AddSubjectTriggerConsumer(IRepository<SubjectVisit> _subjectVisitRepository,

         IRepository<VisitStage> _visitStageRepository,
         IRepository<Trial> _trialRepository,
         IMapper _mapper) : IConsumer<AddSubjectTriggerCommand>
{
    public async Task Consume(ConsumeContext<AddSubjectTriggerCommand> context)
    {
        var addSubjectEvent = context.Message;


        {
            Console.WriteLine(_visitStageRepository._dbContext.GetHashCode());

            Console.WriteLine("两个 DbContext 不是同一个实例");
        }


        //添加受试者的时候，获取访视计划列表，添加到受试者访视表。
        var visitPlanList = await _visitStageRepository.Where(t => t.TrialId == addSubjectEvent.TrialId && t.IsConfirmed).ToListAsync();

        var svList = _mapper.Map<List<SubjectVisit>>(visitPlanList);

        var IsEnrollementQualificationConfirm = await _trialRepository.Where(t => t.Id == addSubjectEvent.TrialId).Select(u => u.IsEnrollementQualificationConfirm).FirstOrDefaultAsync();

        svList.ForEach(t =>
        {
            t.SubjectId = addSubjectEvent.SubjectId;
            t.TrialId = addSubjectEvent.TrialId;
            t.TrialSiteId = addSubjectEvent.TrialSiteId;
            t.IsEnrollmentConfirm = t.IsBaseLine ? IsEnrollementQualificationConfirm : false;
            t.Id = NewId.NextGuid();

        });

        await _subjectVisitRepository.AddRangeAsync(svList);
    }
}

public class AddSubjectTriggerConsumer2(IRepository<SubjectVisit> _subjectVisitRepository,

         IRepository<VisitStage> _visitStageRepository,
         IRepository<Trial> _trialRepository,
         IMapper _mapper) : IConsumer<AddSubjectTriggerCommand2>
{
    public async Task Consume(ConsumeContext<AddSubjectTriggerCommand2> context)
    {
        var addSubjectEvent = context.Message;


        {
            Console.WriteLine(_visitStageRepository._dbContext.GetHashCode());

            Console.WriteLine("两个 DbContext 不是同一个实例");
        }


        //添加受试者的时候，获取访视计划列表，添加到受试者访视表。
        var visitPlanList = await _visitStageRepository.Where(t => t.TrialId == addSubjectEvent.TrialId && t.IsConfirmed).ToListAsync();

        var svList = _mapper.Map<List<SubjectVisit>>(visitPlanList);

        var IsEnrollementQualificationConfirm = await _trialRepository.Where(t => t.Id == addSubjectEvent.TrialId).Select(u => u.IsEnrollementQualificationConfirm).FirstOrDefaultAsync();

        svList.ForEach(t =>
        {
            t.SubjectId = addSubjectEvent.SubjectId;
            t.TrialId = addSubjectEvent.TrialId;
            t.TrialSiteId = addSubjectEvent.TrialSiteId;
            t.IsEnrollmentConfirm = t.IsBaseLine ? IsEnrollementQualificationConfirm : false;
            t.Id = NewId.NextGuid();

        });

        await _subjectVisitRepository.AddRangeAsync(svList);
    }
}


