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;
using System.Linq.Expressions;

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

        private readonly IRepository<VisitTask> _visitTaskRepository;


        public VisitTaskIsFrontTaskNeedSignButNotSignTrigger(

            IRepository<VisitTask> visitTaskRepository)
        {


            _visitTaskRepository = visitTaskRepository;
        }


        //添加任务的时候  如果需要签名 并且已经签名了  那么要维护该标准 该Subject   IsFrontTaskNeedSignButNotSign字段
        public async Task AfterSave(ITriggerContext<VisitTask> context, CancellationToken cancellationToken)
        {
            var visitTask = context.Entity;


            Expression<Func<VisitTask, bool>> visitTaskLambda = x => x.TrialId == visitTask.TrialId && x.SubjectId == visitTask.SubjectId && x.TrialReadingCriterionId == visitTask.TrialReadingCriterionId;
            if (context.ChangeType == ChangeType.Added)
            {

                if (visitTask.IsNeedClinicalDataSign )
                {

                    var visitTaskIdQueryable = _visitTaskRepository.Where(visitTaskLambda) //该Subject  该标准的任务
                                                                                           //小于自己任务号的任务  存在需要签名  但是没签名
                      .Where(t => t.Subject.SubjectVisitTaskList.AsQueryable().Where(visitTaskLambda).Any(c => c.IsNeedClinicalDataSign == true && c.IsClinicalDataSign == false && c.VisitTaskNum < t.VisitTaskNum)
                          && t.IsFrontTaskNeedSignButNotSign == false)
                      .Select(t => t.Id);

                    await _visitTaskRepository.BatchUpdateNoTrackingAsync(t => visitTaskIdQueryable.Contains(t.Id), u => new VisitTask()
                    {
                        IsFrontTaskNeedSignButNotSign = true
                    });



                    var visitTaskIdQueryable2 = _visitTaskRepository.Where(visitTaskLambda) //该Subject  该标准的任务
                                                                                            //小于自己任务号的任务  存在需要签名  但是没签名
                              .Where(t => !t.Subject.SubjectVisitTaskList.AsQueryable().Where(visitTaskLambda).Any(c => c.IsNeedClinicalDataSign == true && c.IsClinicalDataSign == false && c.VisitTaskNum < t.VisitTaskNum)
                              && t.IsFrontTaskNeedSignButNotSign == true)
                              .Select(t => t.Id);

                    await _visitTaskRepository.BatchUpdateNoTrackingAsync(t => visitTaskIdQueryable2.Contains(t.Id), u => new VisitTask()
                    {
                        IsFrontTaskNeedSignButNotSign = false
                    });
                }

            }
        }


    }
}