using AutoMapper;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Domain.Share;

namespace IRaCIS.Core.Application.Service
{
    public class AllocationConfig : Profile
    {
        public AllocationConfig()
        {


            CreateMap<TaskAllocationRule, TaskAllocationRuleView>()
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.DoctorUser.UserTypeRole.UserTypeShortName));

            List<Guid> subjectIdList = new List<Guid>();
            bool isJudgeDoctor = false;


            CreateMap<TaskAllocationRule, DoctorVisitTaskStatView>()
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.DoctorUser.UserTypeRole.UserTypeShortName))
                 //.ForMember(o => o.ArmList, t => t.MapFrom(u => u.DoctorVisitTaskList.Where(c => c.TrialId == u.TrialId).Select(t => t.ArmEnum).Distinct()))
                 .ForMember(o => o.TotalTaskCount, t => t.MapFrom(u => u.Trial.VisitTaskList.Count()))
                 .ForMember(o => o.SelfUndoTaskCount, t => t.MapFrom(u => u.Trial.VisitTaskList.Count(t => t.DoctorUserId == u.DoctorUserId && t.ReadingTaskState != ReadingTaskState.HaveSigned)))
                 .ForMember(o => o.TotalSubjectCount, t => t.MapFrom(u => u.Trial.SubjectList.Count()))
                 .ForMember(o => o.SelfApplyedTaskCount, t => t.MapFrom(u => u.Trial.VisitTaskList.Count(t => t.DoctorUserId == u.DoctorUserId)))
                 .ForMember(o => o.ApplyedTotalTaskCount, t => t.MapFrom(u => u.Trial.VisitTaskList.Count(t => t.DoctorUserId != null)))

                 //.ForMember(o => o.SelfApplyedSubjectCount, t => t.MapFrom(u => u.Trial.SubjectDoctorUserList.Where(t => t.DoctorUserId == u.DoctorUserId && t.SubjectArmVisitTaskList.Any(c => c.DoctorUserId != null)).Count()))
                 .ForMember(o => o.SelfSubjectCount, t => t.MapFrom(u => u.Trial.SubjectDoctorUserList.Where(t => t.DoctorUserId == u.DoctorUserId).Count()))

                 .ForMember(o => o.ApplyedTotalSubjectCount, t => t.MapFrom(u => u.Trial.SubjectList.Count(c => c.SubjectVisitTaskList.Any(d => d.DoctorUserId != null))))



                 //该医生未应用Subject 数量
                 //.ForMember(o => o.WaitApplySelfSubjectCount, t => t.MapFrom(u =>
                 // subjectIdList.Count == 0 ? u.Trial.SubjectDoctorUserList.Where(t => t.DoctorUserId == u.DoctorUserId && t.SubjectArmVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Any(c => c.DoctorUserId == null)).Count()
                 // : u.Trial.SubjectDoctorUserList.Where(t => t.DoctorUserId == u.DoctorUserId && subjectIdList.Contains(t.SubjectId) && t.SubjectArmVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Any(c => c.DoctorUserId == null)).Count()
                 // ))


                 .ForMember(o => o.WaitApplyTotalSubjectCount, t => t.MapFrom(u =>
                  subjectIdList.Count == 0 ? u.Trial.SubjectList.Where(t => t.SubjectVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Any(c => c.DoctorUserId == null)).Count()
                  : u.Trial.SubjectList.Where(t => subjectIdList.Contains(t.Id) && t.SubjectVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Any(c => c.DoctorUserId == null)).Count()
                 ))

                 //.ForMember(o => o.WaitApplySelfTaskCount, t => t.MapFrom(u =>
                 //subjectIdList.Count == 0 ? u.Trial.SubjectDoctorUserList.Where(d => d.DoctorUserId == u.DoctorUserId).SelectMany(t => t.SubjectArmVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Where(t => t.DoctorUserId == null)).Count()
                 //: u.Trial.SubjectDoctorUserList.Where(d => d.DoctorUserId == u.DoctorUserId && subjectIdList.Contains(d.SubjectId)).SelectMany(t => t.SubjectArmVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Where(t => t.DoctorUserId == null)).Count()
                 //                                               ))

                  .ForMember(o => o.WaitApplyTotalTaskCount, t => t.MapFrom(u =>
                         subjectIdList.Count == 0 ? u.Trial.VisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Where(t => t.DoctorUserId == null).Count()
                 : u.Trial.VisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Where(t => t.DoctorUserId == null && subjectIdList.Contains(t.SubjectId)).Count()))

                 ;

            var trialReadingCriterionId = Guid.Empty;
            CreateMap<TaskAllocationRule, TaskAllocationRuleDTO>()
            .ForMember(o => o.DoctorUser, t => t.MapFrom(u => u.Enroll.DoctorUser))
            .ForMember(o => o.CriterionReadingCategoryList, t => t.MapFrom(u => u.Enroll.EnrollReadingCategoryList.Select(t => new TrialCriterionReadingCategory() { EnrollId = t.EnrollId, ReadingCategory = t.ReadingCategory, TrialReadingCriterionId = t.TrialReadingCriterionId })))
            .ForMember(o => o.TrialReadingCriterionList, t => t.MapFrom(u => u.Trial.ReadingQuestionCriterionTrialList.Where(t => t.IsConfirm)))
            .ForMember(o => o.ReadingCategoryList, t => t.MapFrom(u => u.Enroll.EnrollReadingCategoryList.Where(t=>t.TrialReadingCriterionId== trialReadingCriterionId).OrderBy(t => t.ReadingCategory).Select(t => t.ReadingCategory).ToList()))
            ;

            CreateMap<ReadingQuestionCriterionTrial, TrialReadingCriterionDto>()
                .ForMember(t => t.TrialReadingCriterionId, u => u.MapFrom(c => c.Id))
                .ForMember(t => t.TrialReadingCriterionName, u => u.MapFrom(c => c.CriterionName));

            CreateMap<TaskAllocationRule, AssignDoctorStatView>().IncludeBase<TaskAllocationRule, TaskAllocationRuleDTO>()
                 .ForMember(o => o.AssignedSubjectCount, t => t.MapFrom(u => u.DoctorUser.VisitTaskList.Where(t => t.TrialId == u.TrialId).Select(t => t.SubjectId).Distinct().Count()))
                 .ForMember(o => o.WaitDealTrialTaskCount, t => t.MapFrom(u => u.DoctorUser.VisitTaskList.Where(t => t.TrialId == u.TrialId).Where(t => t.ReadingTaskState != ReadingTaskState.HaveSigned && t.TaskState == TaskState.Effect).Count()))
                 .ForMember(o => o.WaitDealAllTaskCount, t => t.MapFrom(u => u.DoctorUser.VisitTaskList.Where(t => t.ReadingTaskState != ReadingTaskState.HaveSigned && t.TaskState == TaskState.Effect).Count()));
          
            
            CreateMap<Subject, SubjectAssignStat>()

                 .ForMember(o => o.SubjectId, t => t.MapFrom(u => u.Id))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Code))
                 .ForMember(o => o.VisitTaskTypeCount, t => t.MapFrom(u => u.SubjectVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Visit && t.TrialReadingCriterionId==trialReadingCriterionId && t.TaskState==TaskState.Effect).Select(t => t.VisitTaskNum).Distinct().Count()))
                 .ForMember(o => o.GlobalTaskTypeCount, t => t.MapFrom(u => u.SubjectVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Global && t.TrialReadingCriterionId == trialReadingCriterionId && t.TaskState == TaskState.Effect).Select(t => t.VisitTaskNum).Distinct().Count()))
                 .ForMember(o => o.OncologyTaskTypeCount, t => t.MapFrom(u => u.SubjectVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Oncology && t.TrialReadingCriterionId == trialReadingCriterionId && t.TaskState == TaskState.Effect).Select(t => t.VisitTaskNum).Distinct().Count()))
                 .ForMember(o => o.JudgeTaskTypeCount, t => t.MapFrom(u => u.SubjectVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Judge && t.TrialReadingCriterionId == trialReadingCriterionId && t.TaskState == TaskState.Effect).Select(t => t.VisitTaskNum).Distinct().Count()))
                 .ForMember(o => o.DoctorUserList, t => t.MapFrom(u => u.SubjectDoctorList.Where(t=>t.TrialReadingCriterionId== trialReadingCriterionId)));

            CreateMap<SubjectUser, SubjectUserView>()
                 .ForMember(o => o.DoctorUser, t => t.MapFrom(u => u.DoctorUser));


            CreateMap<SubjectUser, SubjectUserDTO>().IncludeBase<SubjectUser, SubjectUserView>()
             .ForMember(o => o.IsHaveReading, t => t.MapFrom(u => u.Subject.SubjectVisitTaskList.Any(t => t.ReadingTaskState != ReadingTaskState.WaitReading && t.TrialReadingCriterionId==u.TrialReadingCriterionId && t.DoctorUserId==u.DoctorUserId)));


            CreateMap<SubjectVisit, VisitGenerataTaskDTO>();


            CreateMap<VisitTask, VisitTaskViewBasic>()
                 .ForMember(o => o.TrialReadingCriterionName, t => t.MapFrom(u => u.TrialReadingCriterion.CriterionName))
                  .ForMember(o => o.ReadingTool, t => t.MapFrom(u => u.TrialReadingCriterion.ReadingTool))
                   .ForMember(o => o.IsReadingTaskViewInOrder, t => t.MapFrom(u => u.TrialReadingCriterion.IsReadingTaskViewInOrder))
                    .ForMember(o => o.IsReadingShowSubjectInfo, t => t.MapFrom(u => u.TrialReadingCriterion.IsReadingShowSubjectInfo))
                     .ForMember(o => o.IsReadingShowPreviousResults, t => t.MapFrom(u => u.TrialReadingCriterion.IsReadingShowPreviousResults))
                      .ForMember(o => o.DigitPlaces, t => t.MapFrom(u => u.TrialReadingCriterion.DigitPlaces))
                       .ForMember(o => o.IseCRFShowInDicomReading, t => t.MapFrom(u => u.TrialReadingCriterion.IseCRFShowInDicomReading))
                       .ForMember(o => o.CriterionType, t => t.MapFrom(u => u.TrialReadingCriterion.CriterionType));

            CreateMap<VisitTask, VisitTaskView>().IncludeBase<VisitTask, VisitTaskViewBasic>()


                 .ForMember(o => o.SiteId, t => t.MapFrom(u => u.Subject.SiteId))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.IsAnalysisCreate == true ? u.BlindTrialSiteCode : u.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.IsAnalysisCreate == true ? u.BlindSubjectCode : u.Subject.Code))
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.DoctorUser.UserTypeRole.UserTypeShortName))
                 //.ForMember(o => o.IsClinicalDataSigned, t => t.MapFrom(u => u.Subject.ClinicalDataList.Any(c => c.IsSign && (c.ReadingId == u.SouceReadModuleId || c.ReadingId == u.SourceSubjectVisitId))))
                                  ;




            CreateMap<VisitTask, JudgeVisitTaskView>().IncludeBase<VisitTask, VisitTaskView>()


                 .ForMember(o => o.HistoryReadingDoctorUserList, t => t.MapFrom(u => u.JudgeVisitList));

            CreateMap<VisitTask, ReadingTaskView>().IncludeBase<VisitTask, VisitTaskView>()

                 ;

            CreateMap<VisitTask, AnalysisTaskView>().IncludeBase<VisitTask, VisitTaskView>()
                 .ForMember(o => o.IsReReadingOrBackInfluenceAnalysis, t => t.MapFrom(u => u.Subject.IsReReadingOrBackInfluenceAnalysis));

            

           CreateMap<CancelDoctorCommand, SubjectCanceDoctor>();

            CreateMap<SubjectCanceDoctor, SubjectCancelDoctorView>();


            

            CreateMap<VisitTaskReReading, ReReadingTaskView>()
                 .ForMember(o => o.ReReadingNewTaskCode, t => t.MapFrom(u => u.NewReReadingTask.TaskCode))
                 .ForMember(o => o.OriginalReReadingTask, t => t.MapFrom(u => u.OriginalReReadingTask))
                 ;




            CreateMap<VisitTask, IRHaveReadView>().IncludeBase<VisitTask, VisitTaskViewBasic>()
                  .ForMember(o => o.SiteId, t => t.MapFrom(u => u.Subject.SiteId))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.IsAnalysisCreate == true ? u.BlindTrialSiteCode : u.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.IsAnalysisCreate == true ? u.BlindSubjectCode : u.Subject.Code));




            CreateMap<VisitTask, HistoryReadingDoctorUser>()
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.JudgeTaskCode, t => t.MapFrom(u => u.TaskCode))
                 .ForMember(o => o.DoctorUserId, t => t.MapFrom(u => u.DoctorUser.Id));


            CreateMap<Subject, SubjectAssignView>()
                 .ForMember(o => o.SubjectId, t => t.MapFrom(u => u.Id))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Code))
                 .ForMember(o => o.IsJudge, t => t.MapFrom(u => isJudgeDoctor))
                 .ForMember(o => o.IsAssignedDoctorUser, t => t.MapFrom(u => u.SubjectDoctorList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Any()))


                 .ForMember(o => o.TotalDoctorUserList, t => t.MapFrom(u => u.SubjectDoctorList.Where(t => isJudgeDoctor ? true : t.ArmEnum != Arm.JudgeArm).OrderBy(t => t.ArmEnum)));

            CreateMap<SubjectUser, AssignDoctorView>()
                //.ForMember(o => o.AssignTime, t => t.MapFrom(u => u.AssignTime))
                .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.DoctorUser.UserTypeRole.UserTypeShortName));



            CreateMap<TaskAllocationRuleAddOrEdit, TaskAllocationRule>();


            CreateMap<User, TrialDoctorUserSelectView>()
                 .ForMember(o => o.DoctorUserId, t => t.MapFrom(u => u.Id))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.FullName));

            CreateMap<TaskMedicalReview, TaskMedicalReviewView>().IncludeMembers(t => t.VisitTask)
                 .ForMember(o => o.TrialReadingCriterionId, t => t.MapFrom(u => u.VisitTask.TrialReadingCriterion.Id))
                 .ForMember(o => o.TrialReadingCriterionName, t => t.MapFrom(u => u.VisitTask.TrialReadingCriterion.CriterionName))
                  .ForMember(o => o.ReadingTool, t => t.MapFrom(u => u.VisitTask.TrialReadingCriterion.ReadingTool))
                  .ForMember(o => o.IsReadingTaskViewInOrder, t => t.MapFrom(u => u.VisitTask.TrialReadingCriterion.IsReadingTaskViewInOrder))
                  .ForMember(o => o.IsReadingShowSubjectInfo, t => t.MapFrom(u => u.VisitTask.TrialReadingCriterion.IsReadingShowSubjectInfo))
                  .ForMember(o => o.IsReadingShowPreviousResults, t => t.MapFrom(u => u.VisitTask.TrialReadingCriterion.IsReadingShowPreviousResults))
                  .ForMember(o => o.DigitPlaces, t => t.MapFrom(u => u.VisitTask.TrialReadingCriterion.DigitPlaces))
                  .ForMember(o => o.IseCRFShowInDicomReading, t => t.MapFrom(u => u.VisitTask.TrialReadingCriterion.IseCRFShowInDicomReading))
                  .ForMember(o => o.CriterionType, t => t.MapFrom(u => u.VisitTask.TrialReadingCriterion.CriterionType))

                 .ForMember(o => o.Id, t => t.MapFrom(u => u.Id))
                 .ForMember(o => o.MedicalNo, t => t.MapFrom(u => u.VisitTask.Subject.MedicalNo))
                 .ForMember(o => o.DoctorUser, t => t.MapFrom(u => u.VisitTask.DoctorUser))
                 .ForMember(o => o.MedicalManagerUser, t => t.MapFrom(u => u.MedicalManagerUser));

            CreateMap<VisitTask, TaskMedicalReviewView>()
                .ForMember(o => o.TrialReadingCriterionName, t => t.MapFrom(u => u.TrialReadingCriterion.CriterionName))
                .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.IsAnalysisCreate == true ? u.BlindTrialSiteCode : u.Subject.TrialSite.TrialSiteCode))
                .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.IsAnalysisCreate == true ? u.BlindSubjectCode : u.Subject.Code));

            CreateMap<VisitTask, GenerateMedicalReviewTaskView>()
                 .ForMember(o => o.TrialReadingCriterionName, t => t.MapFrom(u => u.TrialReadingCriterion.CriterionName))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.IsAnalysisCreate == true ? u.BlindTrialSiteCode : u.Subject.TrialSite.TrialSiteCode))
                .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.IsAnalysisCreate == true ? u.BlindSubjectCode : u.Subject.Code))
                .ForMember(o => o.GeneratedMedicalReviewCount, t => t.MapFrom(u => u.TaskMedicalReviewList.Count()))
                .ForMember(o => o.MedicalNo, t => t.MapFrom(u => u.Subject.MedicalNo))
                .ForMember(o => o.IsGeneratedJudge, t => t.MapFrom(u => u.JudgeVisitTaskId != null))
                .ForMember(o => o.ReadingDurationTimeSpan, t => t.MapFrom(u => u.SignTime - u.FirstReadingTime))

                  
              ;

            CreateMap<VisitTask, VisitTaskGroupSimpleDTO>();
            CreateMap<VisitTask, VisitTaskSimpleDTO>();



            CreateMap<User, UserSimpleInfo>()

                  .ForMember(o => o.UserId, t => t.MapFrom(u => u.Id))
             .ForMember(o => o.UserCode, t => t.MapFrom(u => u.UserCode))
              .ForMember(o => o.UserName, t => t.MapFrom(u => u.UserName))
              .ForMember(o => o.FullName, t => t.MapFrom(u => u.FullName));
            //.ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.UserTypeRole.UserTypeShortName));



            CreateMap<TaskMedicalReviewRule, TaskMedicalReviewRuleView>()
              .ForMember(o => o.DoctorUser, t => t.MapFrom(u => u.DoctorUser))
              .ForMember(o => o.ActualVisitTaskList, t => t.MapFrom(u => u.DoctorTrialVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Visit && t.IsAnalysisCreate == false && t.TaskState == TaskState.Effect
               && t.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.Id, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))

              .ForMember(o => o.ActualJudgeTaskList, t => t.MapFrom(u => u.DoctorTrialVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Judge && t.IsAnalysisCreate == false && t.TaskState == TaskState.Effect &&
              t.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.Id, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))

              .ForMember(o => o.ActualGlobalTaskList, t => t.MapFrom(u => u.DoctorTrialVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Global && t.IsAnalysisCreate == false && t.TaskState == TaskState.Effect &&
              t.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.Id, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))

              .ForMember(o => o.ActualTumorTaskList, t => t.MapFrom(u => u.DoctorTrialVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Oncology && t.IsAnalysisCreate == false && t.TaskState == TaskState.Effect &&
              t.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.Id, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))

              .ForMember(o => o.GeneratedGlobalTaskList, t => t.MapFrom(u => u.TaskMedicalReviewList.Where(t => t.VisitTask.ReadingCategory == ReadingCategory.Global && t.VisitTask.IsAnalysisCreate == false && t.VisitTask.TaskState == TaskState.Effect && t.VisitTask.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.VisitTask.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.VisitTaskId, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))

              .ForMember(o => o.GeneratedJudgeTaskList, t => t.MapFrom(u => u.TaskMedicalReviewList.Where(t => t.VisitTask.ReadingCategory == ReadingCategory.Judge && t.VisitTask.IsAnalysisCreate == false && t.VisitTask.TaskState == TaskState.Effect && t.VisitTask.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.VisitTask.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.VisitTaskId, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))

              .ForMember(o => o.GeneratedTumorTaskList, t => t.MapFrom(u => u.TaskMedicalReviewList.Where(t => t.VisitTask.ReadingCategory == ReadingCategory.Oncology && t.VisitTask.IsAnalysisCreate == false && t.VisitTask.TaskState == TaskState.Effect && t.VisitTask.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.VisitTask.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.VisitTaskId, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))

              .ForMember(o => o.GeneratedVisitTaskList, t => t.MapFrom(u => u.TaskMedicalReviewList.Where(t => t.VisitTask.ReadingCategory == ReadingCategory.Visit && t.VisitTask.IsAnalysisCreate == false && t.VisitTask.TaskState == TaskState.Effect && t.VisitTask.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.VisitTask.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.VisitTaskId, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })));

            CreateMap<TaskMedicalReviewRuleAddOrEdit, TaskMedicalReviewRule>();


            //CreateMap<Enroll, TaskConsistentRuleView>().IncludeMembers(t => t.TaskConsistentRule)
            //    .ForMember(o => o.AnalysisDoctorUser, t => t.MapFrom(u => u.do));


            //CreateMap<TaskConsistentRule, TaskConsistentRuleView>()
            //     .ForMember(o => o.GeneratedSubjectCount, t => t.MapFrom(u => u.DoctorVisitTaskList.Where(t => t.IsAnalysisCreate && t.TaskConsistentRuleId == u.Id).Select(t => t.SubjectId).Distinct().Count())) ;

            CreateMap<TaskConsistentRule, TaskConsistentRuleBasic>();

            CreateMap<TaskConsistentRuleAddOrEdit, TaskConsistentRule>();

            CreateMap<TaskConsistentRuleBatchAddOrEdit, TaskConsistentRule>().ForMember(t => t.Id, u => u.Ignore()).ReverseMap();


            CreateMap<VisitTask, VisitTaskSimpleView>()
                //.ForMember(o => o.DoctorUser, t => t.MapFrom(u => u.DoctorUser))
                .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.Subject.TrialSite.TrialSiteCode))
                .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Subject.Code));

            CreateMap<VisitTask, InfluenceTaskInfo>()
               .ForMember(o => o.TrialReadingCriterionName, t => t.MapFrom(u => u.TrialReadingCriterion.CriterionName))
              .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.IsAnalysisCreate == true ? u.BlindTrialSiteCode : u.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.IsAnalysisCreate == true ? u.BlindSubjectCode : u.Subject.Code))
                .ForMember(o => o.OptType, t => t.Ignore());


            CreateMap<TaskInfluence, InfluenceTaskInfo>().IncludeMembers(c => c.InfluenceTask)
                 .ForMember(o => o.OptType, t => t.MapFrom(u => u.OptType))
                 .ForMember(o => o.Id, t => t.MapFrom(u => u.InfluenceTask.Id));




        }
    }

}
