//using IRaCIS.Core.Application.MediatR.CommandAndQueries;
//using MediatR;
//using System.Threading;
//using System.Threading.Tasks;
//using IRaCIS.Core.Domain.Share;
//using IRaCIS.Core.Infra.EFCore;
//using IRaCIS.Core.Domain.Models;
//using System.Linq;
//using System;


//namespace IRaCIS.Core.Application.MediatR.Handlers
//{

//    public class QAMessageHandler : IRequestHandler<QAMessageRequest, bool>
//    {
//        private readonly IRepository<TrialUser> _userTrialRepository;
//        private readonly IRepository<TrialSiteUser> _userTrialSiteRepository;
//        private readonly IRepository<DicomStudy> _studyRepository;
//        private readonly IRepository<QANotice> _qaNoticeRepository;
//        private readonly IUserInfo _userInfo;
//        private readonly IRepository<QCChallenge> _qaRecordRepository;
//        private readonly IRepository<Subject> _subjectRepository;
//        private readonly IRepository<SubjectVisit> _subjectVisitRepository;

//        /// <summary>
//        /// 构造函数注入
//        /// </summary>

//        public QAMessageHandler(IRepository<DicomStudy> studyRepository, IRepository<TrialUser> userTrialRepository, IRepository<TrialSiteUser> userTrialSiteRepository, IRepository<QANotice> qaNoticeRepository, IUserInfo userInfo,
//            IRepository<QCChallenge> qaRecordRepository, IRepository<Subject> subjectRepository, IRepository<SubjectVisit> subjectVisitRepository)
//        {
//            _userTrialSiteRepository = userTrialSiteRepository;
//            _userTrialRepository = userTrialRepository;
//            _studyRepository = studyRepository;
//            _qaNoticeRepository = qaNoticeRepository;
//            _userInfo = userInfo;
//            _qaRecordRepository = qaRecordRepository;
//            _subjectRepository = subjectRepository;
//            _subjectVisitRepository = subjectVisitRepository;
//        }

//        public Task<bool> Handle(QAMessageRequest request, CancellationToken cancellationToken)
//        {
//            var qaRecord = _qaRecordRepository.FirstOrDefault(t => t.Id == request.QCChallengeId);

//            var subjectQuery =
//                   from sv in _subjectVisitRepository.Where(t => t.Id == qaRecord.SubjectVisitId)
//                   join sub in _subjectRepository.AsQueryable() on sv.SubjectId equals sub.Id
//                   select new
//                   {
//                       sv.VisitName,
//                       sv.VisitNum,
//                       SubjectName = sub.LastName + " / " + sub.FirstName,
//                       sub.SiteId
//                   };

//            var subject = subjectQuery.First();



//            //查询项目的参与者   和  负责site下CRC用户
//            var trialUserList = _userTrialRepository.Where(t => t.TrialId == qaRecord.TrialId).ToList();
           

//            var qaList = trialUserList.Where(t => t.User.UserTypeEnum == UserTypeEnum.IQC).ToList();


//            if (request.MessageType == NoticeType.QA_AddQARecord_NoticeCRC)
//            {


//                //找出当前操作的QA   如果是pm 或者admin 代替操作 此时会有问题  所以 谁代替，就以谁的名义执行
//                //var currentQA = qaList.First(t => t.UserId == _userInfo.Id);
//                //var currentQA = trialUserList.First(t => t.UserId == _userInfo.Id);

//                //在项目CRC列表中筛选出  负责该study关联  site的CRC

//                var siteCRCList = _userTrialSiteRepository.Where(t =>
//                t.SiteId == subject.SiteId && t.User.UserTypeEnum == UserTypeEnum.ClinicalResearchCoordinator && t.TrialId == qaRecord.TrialId).ToList();

//                //var siteCRCList = userList.Where(t => t.SiteId == subject.SiteId && t.UserTypeEnum == UserType.ClinicalResearchCoordinator).ToList();


//                var notice = new QANotice()
//                {
//                    TrialId = qaRecord.TrialId,
//                    SubjectVisitId = qaRecord.SubjectVisitId,
                   
//                    FromUser = _userInfo.RealName,
//                    FromUserId = _userInfo.Id,
//                    FromUserType = _userInfo.UserTypeShortName,
//                    NoticeTypeEnum = NoticeType.QA_AddQARecord_NoticeCRC,
//                    NeedDeal = true,
//                    Message = $"QA -> CRC : {_userInfo.RealName} add QA Record",
//                    SendTime = DateTime.Now,

//                    RelationId = request.QCChallengeId
//                };

//                siteCRCList.ForEach(t => notice.QANoticeUserList.Add(new QANoticeUser()
//                {
//                    QANoticeId = notice.Id,
//                    SubjectVisitId = qaRecord.SubjectVisitId,
//                    ToUser = t.User.LastName + " / " + t.User.FirstName,
//                    ToUserId = t.UserId,
//                    ToUserType = t.UserTypeRole.UserTypeShortName
//                }));

//                //添加 发送给CRC的消息  消息和CRC是 一对多
//                _qaNoticeRepository.Add(notice);

//            }

//            else if (request.MessageType == NoticeType.QARecordDialogPost)
//            {
//                #region QA通知处理  新建一条QA通知消息    需要分用户类型  记录关联Id 为了删除操作

//                //CRC  =>QA
//                if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator)
//                {
//                    //PM 或者admin可以代替CRC角色 不能从CRC列表中查询用户
//                    //var currentCRC = trialUserList.First(t => t.UserId == _userInfo.Id);

//                    var notice = new QANotice()
//                    {
//                        TrialId = qaRecord.TrialId,
//                        SubjectVisitId = qaRecord.SubjectVisitId,
//                        //FromUser = currentCRC.UserRealName,
//                        //FromUserId = _userInfo.Id,
//                        //FromUserType = currentCRC.UserType,
//                        FromUser = _userInfo.RealName,
//                        FromUserId = _userInfo.Id,
//                        FromUserType = _userInfo.UserTypeShortName,
//                        NoticeTypeEnum = NoticeType.CRC_QARecordDialogPost_NoticeQA,
//                        NeedDeal = true,
//                        Message = $"CRC -> QA : {_userInfo.RealName} post or reply  message for QA record about SubjectName:{subject.SubjectName } VisitName: {subject.VisitName} VisitNum:{subject.VisitName} !",
//                        SendTime = DateTime.Now,
//                        RelationId = request.QCChallengeReplyId
//                    };

//                    qaList.ForEach(t => notice.QANoticeUserList.Add(new QANoticeUser()
//                    {
//                        QANoticeId = notice.Id,
//                        SubjectVisitId = qaRecord.SubjectVisitId,
//                        ToUser = t.User.LastName + " / " + t.User.FirstName,
//                        ToUserId = t.UserId,
//                        ToUserType = t.User.UserTypeRole.UserTypeShortName
//                    }));

//                    _qaNoticeRepository.Add(notice);

//                    //这里作为 设置QA  设置 Inqa 状态的回复  或者QA和CRC对话的
//                    var needDealNoticeList = _qaNoticeRepository.AsQueryable()
//                        .Where(t => t.SubjectVisitId == qaRecord.SubjectVisitId && t.NeedDeal && (t.NoticeTypeEnum == NoticeType.QA_InQA_NoticeCRC
//    || t.NoticeTypeEnum == NoticeType.QA_QARecordDialogPost_NoticeCRC || t.NoticeTypeEnum == NoticeType.QA_AddQARecord_NoticeCRC)).ToList();

//                    needDealNoticeList.ForEach(t =>
//                    {
//                        t.NeedDeal = false;
//                        t.DealTime = DateTime.Now;
//                        _qaNoticeRepository.Update(t);
//                    });

//                }

//                if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.IQC)
//                {

//                    //找出当前操作的QA   如果是pm 或者admin 代替操作 此时会有问题  所以 谁代替，就以谁的名义执行
//                    //var currentQA = qaList.First(t => t.UserId == _userInfo.Id);
//                    //var currentQA = trialUserList.First(t => t.UserId == _userInfo.Id);

//                    //在项目CRC列表中筛选出  负责该study关联  site的CRC
//                    //var siteCRCList = userList.Where(t => t.SiteId == subject.SiteId && t.UserTypeEnum == UserType.ClinicalResearchCoordinator).ToList();

//                    var siteCRCList = _userTrialSiteRepository.Where(t =>
//               t.SiteId == subject.SiteId && t.User.UserTypeEnum == UserTypeEnum.ClinicalResearchCoordinator && t.TrialId == qaRecord.TrialId).ToList();

//                    var notice = new QANotice()
//                    {
//                        TrialId = qaRecord.TrialId,
//                        SubjectVisitId = qaRecord.SubjectVisitId,
//                        //FromUser = currentQA.UserRealName,
//                        //FromUserId = _userInfo.Id,
//                        //FromUserType = currentQA.UserType,
//                        FromUser = _userInfo.RealName,
//                        FromUserId = _userInfo.Id,
//                        FromUserType = _userInfo.UserTypeShortName,
//                        NoticeTypeEnum = NoticeType.QA_QARecordDialogPost_NoticeCRC,
//                        NeedDeal = true,
//                        Message = $"QA -> CRC : {_userInfo.RealName} post or reply  message for  QA record about SubjectName:{subject.SubjectName } VisitName: {subject.VisitName} VisitNum:{subject.VisitName} !",
//                        SendTime = DateTime.Now,
//                        RelationId = request.QCChallengeReplyId
//                    };

//                    siteCRCList.ForEach(t => notice.QANoticeUserList.Add(new QANoticeUser()
//                    {
//                        QANoticeId = notice.Id,
//                        SubjectVisitId = qaRecord.SubjectVisitId,
//                        ToUser = t.User.LastName + " / " + t.User.FirstName,
//                        ToUserId = t.UserId,
//                        ToUserType = t.UserTypeRole.UserTypeShortName
//                    }));

//                    //添加 发送给CRC的消息  消息和CRC是 一对多
//                    _qaNoticeRepository.Add(notice);

//                    //这里作为  CRC post的对话的回复
//                    var needDealNoticeList = _qaNoticeRepository.AsQueryable()
//                        .Where(t => t.SubjectVisitId == qaRecord.SubjectVisitId && t.NeedDeal && t.NoticeTypeEnum == NoticeType.CRC_QARecordDialogPost_NoticeQA).ToList();

//                    needDealNoticeList.ForEach(t =>
//                    {
//                        t.NeedDeal = false;
//                        t.DealTime = DateTime.Now;
//                        _qaNoticeRepository.Update(t);
//                    });
//                }


//                #endregion
//            }
//            else
//            {

//            }


//            return Task.FromResult(true);

//        }
//    }
//}