using EntityFrameworkCore.Triggered;
using IRaCIS.Core.Application.Service.Reading.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IRaCIS.Core.Application.Triggers
{
	public class AddCRCCliniaclDataTrigger : IAfterSaveTrigger<SubjectVisit>
	{
		private readonly IRepository<SubjectVisit> _subjectVisitRepository;
		private readonly IRepository _repository;
		private readonly IClinicalAnswerService _iClinicalAnswerService;

		public AddCRCCliniaclDataTrigger(IRepository<SubjectVisit> subjectVisitRepository, IRepository repository, IClinicalAnswerService iClinicalAnswerService)
		{
			_subjectVisitRepository = subjectVisitRepository;

			_repository = repository;
			this._iClinicalAnswerService = iClinicalAnswerService;
		}

		//注意删除不能用扩展方法，必须用EF跟踪的实体  否则不能取到  SubjectVisitId
		public async Task AfterSave(ITriggerContext<SubjectVisit> context, CancellationToken cancellationToken)
		{
			var subjectVisit = context.Entity;

			await _iClinicalAnswerService.AutoAddCRCClinical(new Service.Reading.Dto.AutoAddClinicalInDto()
			{
				TrialId = subjectVisit.TrialId,
				SubjectId = subjectVisit.SubjectId,

			});
		}

	}
}
