using AutoMapper;
using EasyCaching.Core;
using EntityFrameworkCore.Triggered;
using IRaCIS.Core.Application.Service;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;

namespace IRaCIS.Core.Application.Triggers
{
    /// <summary>
    ///   因为可能先一致性核查通过，生成其他标准的任务了，新签名的标准也需要产生任务
    /// </summary>
    public class TrialCriterionSignTrigger : IBeforeSaveTrigger<ReadingQuestionCriterionTrial>
    {
      
        private readonly IVisitTaskHelpeService _visitTaskHelpeService;


        public TrialCriterionSignTrigger(
          
            IVisitTaskHelpeService visitTaskHelpeService)
        {
            
        
            _visitTaskHelpeService = visitTaskHelpeService;
        }

        public async Task BeforeSave(ITriggerContext<ReadingQuestionCriterionTrial> context, CancellationToken cancellationToken)
        {
            var trialCriterion = context.Entity;


            if (context.ChangeType == ChangeType.Modified)
            {

                // 一致性核查通过  生成读片任务
                if (context.UnmodifiedEntity?.ReadingInfoSignTime != trialCriterion.ReadingInfoSignTime && trialCriterion.ReadingInfoSignTime != null)
                {


                    await _visitTaskHelpeService.BaseCritrionGenerateVisitTask(trialCriterion.TrialId, trialCriterion.Id);
                }

            }
        }
    }
}