using AutoMapper;
using AutoMapper.EquivalencyExpression;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Contracts.DTO;
using IRaCIS.Core.Application.MediatR.CommandAndQueries;
using IRaCIS.Core.Domain.Share;
using static IRaCIS.Core.Application.Contracts.SubjectProgressDto;

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

            #region 导出列表


            var isEn_Us = false;

            CreateMap<Trial, ExcelExportInfo>();
            CreateMap<SubjectVisit, CRCVisitExportDTO>()
          .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
          .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.Subject.Code))
          .ForMember(d => d.SubmitUserName, u => u.MapFrom(s => s.SubmitUser.FullName))

           .ForMember(d => d.IsHaveClinicalData, u => u.MapFrom(t => t.IsBaseLine ? t.PreviousHistoryList.Any() || t.PreviousOtherList.Any()
           || t.ReadingClinicalDataList.Any(x => x.ClinicalDataTrialSet.UploadRole == Domain.Share.UploadRole.CRC && x.ReadingClinicalDataPDFList.Count() > 0)
           || t.PreviousSurgeryList.Any() : false));



            CreateMap<QCChallenge, QCChanllengeExportDto>()

              .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.SubjectVisit.Subject.Code))
              .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.SubjectVisit.TrialSite.TrialSiteCode))
              .ForMember(d => d.VisitName, u => u.MapFrom(s => s.SubjectVisit.VisitName))

              .ForMember(d => d.CreateUserName, u => u.MapFrom(s => s.CreateUser.UserName))
              .ForMember(d => d.LatestReplyUserName, u => u.MapFrom(t => t.LatestReplyUser.UserName))

              .ForMember(d => d.DialogStr, u => u.MapFrom(t => string.Join(" | ", t.DialogList.OrderBy(t => t.CreateTime).Select(c => c.CreateUser.UserName + " " + c.CreateTime.ToString("yyyy-mm-dd hh:mm:ss") + " :" + c.TalkContent))))

              .ForMember(d => d.SubjectState, u => u.MapFrom(s => s.SubjectVisit.Subject.Status));


            CreateMap<Subject, SubjectExportDTO>()
               .ForMember(d => d.SiteName, u => u.MapFrom(s => s.TrialSite.Site.SiteName))
               .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
               .ForMember(d => d.LatestBlindName, u => u.MapFrom(s => s.LatestSubjectVisit.BlindName))
               .ForMember(d => d.LatestVisitName, u => u.MapFrom(s => s.LatestSubjectVisit.VisitName))
               .ForMember(d => d.FinalSubjectVisitName, u => u.MapFrom(s => s.FinalSubjectVisit.VisitName))
               .ForMember(d => d.InPlanVisitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.InPlan)))
               .ForMember(d => d.OutPlanVisitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.InPlan == false)))
               //执行不一定上传了   可能是失访  实际执行过了
               .ForMember(d => d.MissingSubmmitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.VisitNum < s.LatestSubjectVisit.VisitNum && t.SubmitState != SubmitStateEnum.Submitted && t.IsLostVisit == false)))
               .ForMember(d => d.InPlanVisitSubmmitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.SubmitState == SubmitStateEnum.Submitted && t.InPlan == true)))
               .ForMember(d => d.LostVisitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.IsLostVisit)))
               .ForMember(d => d.OutPlanVisitSubmmitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.SubmitState == SubmitStateEnum.Submitted && t.InPlan == false)))

               .ForMember(d => d.IsPDProgress, u => u.MapFrom(s => s.SubjectVisitList.Any(t => t.IsEnrollmentConfirm == true)))
               .ForMember(d => d.IsEnrollmentConfirmed, u => u.MapFrom(s => s.SubjectVisitList.Any(t => t.PDState == PDStateEnum.PDProgress)))

               .ForMember(d => d.RadiologyClinicalDataCount, u => u.MapFrom(s => s.ClinicalDataList.Count(t => t.ClinicalDataTrialSet.ClinicalDataLevel == ClinicalLevel.ImageRead)))
               .ForMember(d => d.OncologyClinicalDataCount, u => u.MapFrom(s => s.ClinicalDataList.Count(t => t.ClinicalDataTrialSet.ClinicalDataLevel == ClinicalLevel.OncologyRead)))

                 .ForMember(d => d.ChallengeWaitReplyCount, u => u.MapFrom(s => s.SubjectVisitList.SelectMany(c => c.QCChallengeList).Count(t => t.IsClosed == false && !t.DialogList.Any(t => t.CreateUser.UserTypeEnum == UserTypeEnum.ClinicalResearchCoordinator))))
               .ForMember(d => d.CheckWaitReplyCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.CheckState == CheckStateEnum.CVIng && t.CheckChallengeState != CheckChanllengeTypeEnum.Closed && !t.CheckChallengeDialogList.Any(t => t.CreateUser.UserTypeEnum == UserTypeEnum.ClinicalResearchCoordinator))))
               ;

            CreateMap<Subject, SubjectProgressDto>()
                 .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
                 .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.Code))
                 .ForMember(d => d.SubjectStatus, u => u.MapFrom(s => s.Status))
                 .ForMember(d => d.VisitList, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.VisitExecuted != VisitExecutedEnum.Unavailable)
                 ));

            //CreateMap<SubjectVisit, SubjectProgressDto.MouduleProgress> ()
            //    .ForMember(d => d.TaskName, u => u.MapFrom(s => s.VisitName))
            //     .ForMember(d => d.VisitTaskNum, u => u.MapFrom(s => s.VisitNum))
            //     .ForMember(d => d.Inplan, u => u.MapFrom(s => s.InPlan))
            //      .ForMember(d => d.ReadingStatus, u => u.MapFrom(s => s.VisitTaskList
            //      .Count(t=>t.TaskState==TaskState.Effect && t.IsAnalysisCreate==false && t.ReadingCategory==ReadingCategory.Visit && t.ReadingTaskState==ReadingTaskState.HaveSigned)==s. s.ReadingStatus))

                 




            CreateMap<ReadModule, ReadPeriodExportDto>()
               .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.Subject.TrialSite.TrialSiteCode))
               .ForMember(d => d.DeadlineVisitName, u => u.MapFrom(s => s.SubjectVisit.VisitName))
               .ForMember(d => d.EarliestScanDate, u => u.MapFrom(s => s.SubjectVisit.EarliestScanDate))
                .ForMember(d => d.LatestScanDate, u => u.MapFrom(s => s.SubjectVisit.LatestScanDate))
                 .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.Subject.Code))
                .ForMember(d => d.SubjectStatus, u => u.MapFrom(s => s.Subject.Status))
                   .ForMember(d => d.FirstGiveMedicineTime, u => u.MapFrom(s => s.Subject.FirstGiveMedicineTime))
                .ForMember(d => d.TrialReadingCriterionName, u => u.MapFrom(s => s.TrialReadingCriterion.CriterionName));


            CreateMap<SubjectVisit, PMKCheckEXportDTO>()
             .ForMember(d => d.TalkContent, u => u.MapFrom(s => s.CheckChallengeDialogList.OrderByDescending(y => y.CreateTime).Select(x => x.TalkContent).FirstOrDefault()))
             .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.Subject.Code))
             .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))

             .ForMember(d => d.CheckDialogStr, u => u.MapFrom(t => string.Join(" | ", t.CheckChallengeDialogList.OrderBy(t => t.CreateTime).Select(c => c.CreateUser.UserName + " " + c.CreateTime.ToString("yyyy-mm-dd hh:mm:ss") + " :" + c.TalkContent))))
               .ForMember(d => d.ModalityList, c => c.MapFrom(s =>
                (s.NoneDicomStudyList.Select(t => t.Modality)
                .Union(s.StudyList.Select(k => k.ModalityForEdit))).Distinct()))
             ;

            CreateMap<VisitTask, ReadingTaskExportDto>()
                 .ForMember(o => o.TrialReadingCriterionName, t => t.MapFrom(u => u.TrialReadingCriterion.CriterionName))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.IsSelfAnalysis == true ? u.BlindTrialSiteCode : u.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.IsSelfAnalysis == 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.SubjectStatus, t => t.MapFrom(u => u.Subject.Status));



            CreateMap<VisitTaskReReading, ReReadingTaskExportDto>().IncludeMembers(t => t.OriginalReReadingTask)
                .ForMember(o => o.ReReadingNewTaskCode, t => t.MapFrom(u => u.NewReReadingTask.TaskCode))
                /*.ForMember(o => o.ApplyTask, t => t.MapFrom(u => u.OriginalReReadingTask))*/;

            CreateMap<VisitTask, ReReadingTaskExportDto>()
                .ForMember(o => o.TrialReadingCriterionName, t => t.MapFrom(u => u.TrialReadingCriterion.CriterionName))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.IsSelfAnalysis == true ? u.BlindTrialSiteCode : u.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.IsSelfAnalysis == 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.SubjectStatus, t => t.MapFrom(u => u.Subject.Status));

            CreateMap<TaskMedicalReview, TaskMedicalReviewExportDto>().IncludeMembers(t => t.VisitTask)

               .ForMember(o => o.TrialReadingCriterionName, t => t.MapFrom(u => u.VisitTask.TrialReadingCriterion.CriterionName))
               .ForMember(o => o.MedicalNo, t => t.MapFrom(u => u.VisitTask.Subject.MedicalNo))
               //.ForMember(o => o.DoctorUserName, t => t.MapFrom(u => u.VisitTask.DoctorUser.UserName))
               .ForMember(o => o.MedicalManagerUserName, t => t.MapFrom(u => u.MedicalManagerUser.UserName));

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


            CreateMap<VisitTask, AnalysisExortCommon>()
                .ForMember(o => o.TrialReadingCriterionName, t => t.MapFrom(u => u.TrialReadingCriterion.CriterionName))
                .ForMember(o => o.CriterionType, t => t.MapFrom(u => u.TrialReadingCriterion.CriterionType))

                .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.Subject.TrialSite.TrialSiteCode))
                .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Subject.Code))
                .ForMember(o => o.IsBaseline, t => t.MapFrom(u => u.SourceSubjectVisit.IsBaseLine))
                    .ForMember(o => o.EvaluateResult, t => t.MapFrom(u =>
                    u.SourceSubjectVisit.IsBaseLine? u.ReadingTaskQuestionAnswerList.Where(c => c.ReadingQuestionTrial.QuestionType == QuestionType.ExistDisease).FirstOrDefault().Answer
                                                     : u.ReadingTaskQuestionAnswerList.Where(c => c.ReadingQuestionTrial.QuestionType == QuestionType.Tumor).FirstOrDefault().Answer  ))

                .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName));

            CreateMap<VisitTask, GroupAnalysisExport>().IncludeBase<VisitTask, AnalysisExortCommon>();
            CreateMap<VisitTask, SelftAnalysisExport>().IncludeBase<VisitTask, AnalysisExortCommon>();



            CriterionType? criterionType = null;
            CreateMap<VisitTask, OverallTumorEvaluationExport>()
                // .ForMember(o => o.TrialReadingCriterionName, t => t.MapFrom(u => u.TrialReadingCriterion.CriterionName))
                .ForMember(o => o.IsBaseline, t => t.MapFrom(u => u.SourceSubjectVisit.IsBaseLine))

                 .ForMember(o => o.JudgeArmEnum, t => t.MapFrom(u => u.JudgeResultTask.ArmEnum))

                .ForMember(o => o.OverallTumorEvaluationResult, t => t.MapFrom(u =>
               criterionType == CriterionType.RECIST1Point1 ?( u.SourceSubjectVisit.IsBaseLine==true ? u.ReadingTaskQuestionAnswerList.Where(c => c.ReadingQuestionTrial.QuestionType == QuestionType.ExistDisease).FirstOrDefault().Answer:
               u.ReadingTaskQuestionAnswerList.Where(c => c.ReadingQuestionTrial.QuestionType == QuestionType.Tumor).FirstOrDefault().Answer)
                : criterionType == CriterionType.PCWG3 ? u.ReadingTaskQuestionAnswerList.Where(c => c.ReadingQuestionTrial.QuestionType == QuestionType.SiteVisitForTumorEvaluation).FirstOrDefault().Answer : String.Empty
                ))

                .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.Subject.TrialSite.TrialSiteCode))
                .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Subject.Code))
                .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName));

            CreateMap<VisitTask, RECIST1Point1EvaluationOfTumorEfficacyExport>().IncludeBase<VisitTask, OverallTumorEvaluationExport>()
                 .ForMember(o => o.TargetlesionEvaluationResult, t => t.MapFrom(u => u.ReadingTaskQuestionAnswerList.Where(c => c.ReadingQuestionTrial.QuestionType == QuestionType.TargetLesion).FirstOrDefault().Answer))
                 .ForMember(o => o.NoneTargetlesionEvaluationResult, t => t.MapFrom(u => u.ReadingTaskQuestionAnswerList.Where(c => c.ReadingQuestionTrial.QuestionType == QuestionType.NoTargetLesion).FirstOrDefault().Answer))
                 .ForMember(o => o.IsExistNewlesionEvaluationResult, t => t.MapFrom(u => u.ReadingTaskQuestionAnswerList.Where(c => c.ReadingQuestionTrial.QuestionType == QuestionType.NewLesions).FirstOrDefault().Answer))

                //.ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.Subject.TrialSite.TrialSiteCode))
                //.ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Subject.Code))
                //.ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                ;


            CreateMap<VisitTask, RECIST1Point1DetailedOfEvaluatedLesionExport>().IncludeBase<VisitTask, RECIST1Point1EvaluationOfTumorEfficacyExport>()
                 .ForMember(o => o.LesionList, t => t.MapFrom(u => u.LesionList));

            CreateMap<ReadingTableAnswerRowInfo, RECIST1Point1LessionInfo>()
                  .ForMember(o => o.LessionCode, t => t.MapFrom(u => u.RowMark))
                   .ForMember(o => o.LessionType, t => t.MapFrom(u =>(int?) u.ReadingQuestionTrial.LesionType))
                    .ForMember(o => o.IsLymph, t => t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.IsLymph).FirstOrDefault().Answer))

                    //位置可能是自己填写的
                    .ForMember(o => o.LessionLocation, t =>
                    //t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.Location).FirstOrDefault().Answer)
                    t.MapFrom(u => u.OrganInfo.IsCanEditPosition?
                    u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.Location).FirstOrDefault().Answer: isEn_Us ? u.OrganInfo.TULATEN : u.OrganInfo.TULAT)
                    )
                    .ForMember(o => o.LessionOrgan, t =>
                    //t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.Organ).FirstOrDefault().Answer)
                    t.MapFrom(u => isEn_Us ? u.OrganInfo.TULOCEN : u.OrganInfo.TULOC)
                    )
                    .ForMember(o => o.BodyPartDescription, t =>
                    //t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.Part).FirstOrDefault().Answer)
                     t.MapFrom(u => isEn_Us? u.OrganInfo.PartEN : u.OrganInfo.Part)
                    
                    )
                   
                    //.ForMember(o => o.MeasurementResult, t => t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.Location).FirstOrDefault().Answer))
                    .ForMember(o => o.LongDiameter, t => t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.MajorAxis).FirstOrDefault().Answer))
                    .ForMember(o => o.ShortDiameter, t => t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.ShortAxis).FirstOrDefault().Answer))
                    .ForMember(o => o.LessionState, t => t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.State).FirstOrDefault().Answer));


            CreateMap<VisitTask, PCWG3DetailedOfEvaluatedLesionExport>().IncludeBase<VisitTask, OverallTumorEvaluationExport>()
                 .ForMember(o => o.LesionList, t => t.MapFrom(u => u.LesionList));


            CreateMap<ReadingTableAnswerRowInfo, PCWG3LessionInfo>()
                 .ForMember(o => o.LessionCode, t => t.MapFrom(u => u.RowMark))
                  .ForMember(o => o.LessionType, t => t.MapFrom(u => (int?)u.ReadingQuestionTrial.LesionType))

                   //位置可能是自己填写的
                   .ForMember(o => o.LessionLocation, t =>
                   //t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.Location).FirstOrDefault().Answer)
                   t.MapFrom(u => u.OrganInfo.IsCanEditPosition ?
                   u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.Location).FirstOrDefault().Answer : isEn_Us ? u.OrganInfo.TULATEN : u.OrganInfo.TULAT)
                   )

                        .ForMember(o => o.LessionOrgan, t =>
                    t.MapFrom(u => isEn_Us ? u.OrganInfo.TULOCEN : u.OrganInfo.TULOC)
                    )

                   .ForMember(o => o.BodyPartDescription, t =>
                    //t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.Part).FirstOrDefault().Answer)
                    t.MapFrom(u => isEn_Us ? u.OrganInfo.PartEN : u.OrganInfo.Part)
                   )              
                   .ForMember(o => o.LessionState, t => t.MapFrom(u => u.LesionAnswerList.Where(c => c.ReadingTableQuestionTrial.QuestionMark == QuestionMark.State).FirstOrDefault().Answer));

            #endregion


          

			string token = string.Empty;
            //一致性核查
            CreateMap<CheckDBModel, CheckViewModel>();

            CreateMap<QCChallengeCommand, QCChallenge>();
            CreateMap<QADialogCommand, QCChallengeDialog>();


            #region QA 废弃
            //CreateMap<QaTemplateCommand, QATemplate>();

            //CreateMap<QaTemplateItemCommand, QATemplateItem>();


            //CreateMap<QARecordTrialTemplateItemCommand, QARecordTemplateItemDetail>();


            //CreateMap<QaTemplateConfigCommand, QANotice>();


            //CreateMap<TrialQATemplateAddCommand, QATrialTemplate>();

            //CreateMap<UpdateQATemplateItemCommand, QATrialTemplateItem>();
            //CreateMap<AddQATemplateItemCommand, QATrialTemplateItem>();


            //CreateMap<TrialQATemplateUpdateCommand, QATrialTemplate>();

            //CreateMap<QARecordTrialTemplateItemCommand, QAAboutTrialTemplateItem>();

            //CreateMap<QATemplate, QaTemplateCommand>();
            //CreateMap<QATemplate, QaTemplateDto>();

            //CreateMap<QARecordTemplateItemDetail, TemplateItemProblem>();






            #endregion
            CreateMap<QCChallenge, QCChallengeCommand>();

            CreateMap<QCQuestionAddOrEdit, QCQuestion>();

            CreateMap<QCQuestion, TrialQCQuestion>();
            CreateMap<TrialQCQuestionAddOrEdit, TrialQCQuestion>();

            CreateMap<TrialQCQuestionConfigureBatchAdd, TrialQCQuestion>();

            CreateMap<TrialQCQuestion, TrialQCQuestionSelect>();

            CreateMap<TrialQCQuestion, QCQuestionView>(); 
            CreateMap<QCQuestion, QCQuestionView>(); 


            CreateMap<QCQuestion, QCQuestionConfigureView>()
                 .ForMember(d => d.ParentShowOrder, u => u.MapFrom(s => s.ParentQuestion.ShowOrder))
                     .ForMember(d => d.ParentQuestionName, u => u.MapFrom(s => s.ParentQuestion.QuestionName));

            CreateMap<QCQuestion, QCQuestionAddOrEdit>();

            CreateMap<TrialQCQuestion, TrialQCQuestionAddOrEdit>();

            CreateMap<TrialQCQuestion, TrialQCQuestionConfigureView>()
               .ForMember(d => d.ParentShowOrder, u => u.MapFrom(s => s.ParentQCQuestion.ShowOrder));

            CreateMap<TrialQCQuestionSelect, TrialQCQuestionConfigureView>();

            CreateMap<CheckChallengeDialogCommand, CheckChallengeDialog>();

            CreateMap<PreviousPDFAddOrEdit, PreviousPDF>().ReverseMap();
            //受试者临床数据 添加编辑
            CreateMap<PreviousHistoryAddOrEdit, PreviousHistory>().ReverseMap();

            CreateMap<PreviousOtherAddOrEdit, PreviousOther>().ReverseMap();

            CreateMap<PreviousSurgeryAddOrEdit, PreviousSurgery>().ReverseMap();

            // 受试者临床数据 视图映射
            Guid subjectVisitId = Guid.Empty;
            CreateMap<SubjectVisit, SubjectClinicalDataDto>()
                 .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.Subject.Code))
                 .ForMember(d => d.SubjectVisitId, u => u.MapFrom(s => s.Id))
                 .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
                 .ForMember(d => d.PreviousHistoryList, u => u.MapFrom(s => s.PreviousHistoryList.Where(t => t.SubjectVisitId == subjectVisitId || t.IsSubjectLevel)))
                 .ForMember(d => d.PreviousOtherList, u => u.MapFrom(s => s.PreviousOtherList.Where(t => t.SubjectVisitId == subjectVisitId || t.IsSubjectLevel)))
                 .ForMember(d => d.PreviousSurgeryList, u => u.MapFrom(s => s.PreviousSurgeryList.Where(t => t.SubjectVisitId == subjectVisitId || t.IsSubjectLevel)));

            CreateMap<SubjectVisit, SubjectVisitDTO>();
            CreateMap<SubjectVisit, SubjectVisitSelectItem>()
                 .ForMember(d => d.IsUploadedImage, u => u.MapFrom(s => s.StudyList.Count() > 0 || s.NoneDicomStudyList.SelectMany(u => u.NoneDicomFileList).Count() > 0));

            CreateMap<PreviousPDF, PreviousPDFView>()
                .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path + "?access_token=" + token));


            //影像质控
            CreateMap<SubjectVisit, QCVisitViewModel>()
                .ForMember(d => d.ChallengeCount, u => u.MapFrom(s => s.QCChallengeList.Count()))

                  //.ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.Subject.Code))
                  //.ForMember(d => d.IsEnrollmentConfirm, u => u.MapFrom(s => s.Subject.IsEnrollmentConfirm))
                  //.ForMember(d => d.FistGiveMedicineTime, u => u.MapFrom(s => s.Subject.FistGiveMedicineTime))
                  //.ForMember(d => d.SubjectVisitId, u => u.MapFrom(s => s.Id))
                  .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
                  .ForMember(d => d.QCProcessEnum, u => u.MapFrom(s => s.Trial.QCProcessEnum))
                  .ForMember(d => d.SubjectStatus, u => u.MapFrom(s => s.Subject.Status))
                  .ForMember(d => d.StudyCount, u => u.MapFrom(s => s.StudyList.Count()))
                  .ForMember(d => d.CurrentActionUserName, u => u.MapFrom(s => s.CurrentActionUser.UserName))
                  .ForMember(d => d.PreliminaryAuditUserName, u => u.MapFrom(s => s.PreliminaryAuditUser.UserName))
                  .ForMember(d => d.ReviewAuditUserName, u => u.MapFrom(s => s.ReviewAuditUser.UserName))
                    .ForMember(d => d.IsHaveClinicalData, u => u.MapFrom(t => t.PreviousHistoryList.Any() || t.PreviousOtherList.Any()
                 || t.Subject.ClinicalFormList.Any(x => x.ClinicalDataTrialSet.UploadRole == UploadRole.CRC && x.ReadingId == t.Id)
                 || t.ReadingClinicalDataList.Any(x => x.ClinicalDataTrialSet.UploadRole == Domain.Share.UploadRole.CRC && x.ReadingClinicalDataPDFList.Count() > 0)
                 || t.PreviousSurgeryList.Any()))
                  .ForMember(d => d.DicomStudyCount, u => u.MapFrom(t => t.StudyList.Count()))
                  .ForMember(d => d.NoneDicomStudyCount, u => u.MapFrom(t => t.NoneDicomStudyList.Count(t => t.NoneDicomFileList.Any())));

            //CRC 上传列表
            CreateMap<SubjectVisit, QCCRCVisitViewModel>()/*.IncludeMembers(t=>t.Subject)*/
                 //.ForMember(d => d.SubjectStatus, u => u.MapFrom(s => s.Subject.Status))
                 //.ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.Subject.Code))
                 // .ForMember(d => d.SubejctIsEnrollmentConfirm, u => u.MapFrom(t => t.Subject.IsEnrollmentConfirm))
                 //  .ForMember(d => d.SubejctFistGiveMedicineTime, u => u.MapFrom(t => t.Subject.FistGiveMedicineTime))
                 .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
                 .ForMember(d => d.QCProcessEnum, u => u.MapFrom(s => s.Trial.QCProcessEnum))
                 .ForMember(d => d.SubjectId, u => u.MapFrom(t => t.Subject.Id))
                 .ForMember(d => d.MedicalNo, u => u.MapFrom(s => s.Subject.MedicalNo))
                 .ForMember(d => d.Sex, u => u.MapFrom(s => s.Subject.Sex))
                 .ForMember(d => d.Age, u => u.MapFrom(t => t.Subject.Age))
                 .ForMember(d => d.IsHaveClinicalData, u => u.MapFrom(t => t.PreviousHistoryList.Any() || t.PreviousOtherList.Any()
                 || t.Subject.ClinicalFormList.Any(x=>x.ClinicalDataTrialSet.UploadRole==UploadRole.CRC&& x.ReadingId==t.Id)
                 || t.ReadingClinicalDataList.Any(x => x.ClinicalDataTrialSet.UploadRole == Domain.Share.UploadRole.CRC && x.ReadingClinicalDataPDFList.Count() > 0)
                 || t.PreviousSurgeryList.Any()))

                  //.ForMember(d => d.VisitName, u => u.MapFrom(t =>t.InPlan? t.VisitStage.VisitName : t.VisitName))
                  //.ForMember(d => d.VisitNum, u => u.MapFrom(t => t.InPlan ? t.VisitStage.VisitNum : t.VisitNum))
                  //.ForMember(d => d.VisitDay, u => u.MapFrom(t => t.InPlan ? t.VisitStage.VisitDay : t.VisitDay))
                  .ForMember(d => d.DicomStudyCount, u => u.MapFrom(t => t.StudyList.Count()))
                  .ForMember(d => d.NoneDicomStudyCount, u => u.MapFrom(t => t.NoneDicomStudyList.Count(t => t.NoneDicomFileList.Any())));
            //.ForMember(d => d.StudyCount, u => u.MapFrom(s => s.StudyList.Count()));
            CreateMap<Subject, QCCRCVisitViewModel>();

            //一致性核查
            CreateMap<SubjectVisit, QCCheckViewModel>()
               .ForMember(d => d.TalkContent, u => u.MapFrom(s => s.CheckChallengeDialogList.OrderByDescending(y => y.CreateTime).Select(x => x.TalkContent).FirstOrDefault()))
               .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.Subject.Code))
               .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode));

            CreateMap<SubjectVisit, QCCheckWithModalityView>().IncludeBase<SubjectVisit, QCCheckViewModel>()
                .ForMember(d => d.ModalityList, c => c.MapFrom(s =>
                (s.NoneDicomStudyList.Select(t => t.Modality)
                .Union(s.StudyList.Select(k => k.ModalityForEdit))).Distinct()));
            

            //一致性核查 质疑对话
            CreateMap<CheckChallengeDialog, CheckChanllengeDialogDTO>()
                 .ForMember(d => d.CreateUserName, u => u.MapFrom(t => t.CreateUser.UserName))
                 .ForMember(d => d.CreateUserFullName, u => u.MapFrom(t => t.CreateUser.FullName));

            CreateMap<SubjectVisit, CheckDialogDTO>()
               .ForMember(d => d.SubjectVisitCheck, u => u.MapFrom(t => t))
                .ForMember(d => d.DialogList, u => u.MapFrom(t => t.CheckChallengeDialogList.OrderBy(t => t.CreateTime)));


            CreateMap<SubjectVisit, ForWardViewModel>()
             .ForMember(d => d.ForwardUserName, u => u.MapFrom(s => s.ForwardUser.UserName))
            .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.Subject.Code))
            .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode));

            //QC 界面 受试者 site 基本信息  展平的属性   比如 SubjectAge => Subject.Age
            CreateMap<SubjectVisit, QARelationInfo>().IncludeMembers(t => t.Subject)
               .ForMember(d => d.SubjectVisitId, u => u.MapFrom(s => s.Id))
               .ForMember(d => d.SubjectName, u => u.MapFrom(s => s.Subject.ShortName))
               .ForMember(d => d.IsHaveFirstGiveMedicineDate, u => u.MapFrom(s => s.Trial.IsHaveFirstGiveMedicineDate))
              //.ForMember(d => d.ChangeDefalutDays, u => u.MapFrom(s => s.Trial.ChangeDefalutDays))
              .ForMember(d => d.SubjectFirstGiveMedicineTime, u => u.MapFrom(s => s.Subject.FirstGiveMedicineTime))
              .ForMember(d => d.SiteName, u => u.MapFrom(s => s.Site.SiteName))
              .ForMember(d => d.TotalChallengeCount, u => u.MapFrom(s => s.QCChallengeList.Count()))
              .ForMember(d => d.NotClosedChallengeCount, u => u.MapFrom(s => s.QCChallengeList.Count(c => c.IsClosed == false)));


            CreateMap<Subject, QARelationInfo>(MemberList.None);



            // 临床数据上传  路径拼接返回

            CreateMap<PreviousHistory, PreviousHistoryView>()
                .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path + "?access_token=" + token));
            CreateMap<PreviousOther, PreviousOtherView>()
             .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path + "?access_token=" + token));
            CreateMap<PreviousSurgery, PreviousSurgeryView>()
                .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path + "?access_token=" + token));





            //QC 质疑对话
            var currentUserId = Guid.Empty;
            CreateMap<QCChallengeDialog, QCChanllengeDialogDTO>()
                .ForMember(d => d.CreateUserName, u => u.MapFrom(t => t.CreateUser.UserName))
                 .ForMember(d => d.CreateUserFullName, u => u.MapFrom(t => t.CreateUser.FullName))

                .ForMember(d => d.IsCurrentUser, u => u.MapFrom(s => s.CreateUserId == currentUserId));
            //质疑编号
            CreateMap<QCChallenge, QCChallengeWithUser>()
                 .ForMember(d => d.LatestReplyUserName, u => u.MapFrom(t => t.LatestReplyUser.UserName))
                 .ForMember(d => d.CreateUserName, u => u.MapFrom(t => t.CreateUser.UserName))
                 .ForMember(d => d.CurrentActionUserId, u => u.MapFrom(t => t.SubjectVisit.CurrentActionUserId))
                 .ForMember(d => d.CurrentActionUserName, u => u.MapFrom(t => t.SubjectVisit.CurrentActionUser.UserName))

                 .ForMember(d => d.SubjectId, u => u.MapFrom(t => t.SubjectVisit.SubjectId))
                .ForMember(d => d.ChallengeCode, u => u.MapFrom(s => s.ChallengeCode))

                   .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.SubjectVisit.Subject.Code))
                   .ForMember(d => d.VisitName, u => u.MapFrom(s => s.SubjectVisit.VisitName))
                   .ForMember(d => d.BlindName, u => u.MapFrom(s => s.SubjectVisit.BlindName));


            CreateMap<QCChallenge, ChallengeAndDialog>()
                .ForMember(d => d.LatestReplyUserName, u => u.MapFrom(t => t.LatestReplyUser.UserName))
                 .ForMember(d => d.CreateUserName, u => u.MapFrom(t => t.CreateUser.UserName))
                 .ForMember(d => d.CurrentActionUserId, u => u.MapFrom(t => t.SubjectVisit.CurrentActionUserId))
                 .ForMember(d => d.CurrentActionUserName, u => u.MapFrom(t => t.SubjectVisit.CurrentActionUser.UserName))

                 .ForMember(d => d.SubjectId, u => u.MapFrom(t => t.SubjectVisit.SubjectId))
                 .ForMember(d => d.DialogList, u => u.MapFrom(t => t.DialogList.OrderBy(t => t.CreateTime)))
                 .ForMember(d => d.ChallengeCode, u => u.MapFrom(s => s.ChallengeCode))

                   .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.SubjectVisit.Subject.Code))
                   .ForMember(d => d.VisitName, u => u.MapFrom(s => s.SubjectVisit.VisitName))
                   .ForMember(d => d.BlindName, u => u.MapFrom(s => s.SubjectVisit.BlindName))

                 ;

            // 一致性核查文件
            CreateMap<UserUploadFile, GetUserUploadFileDto>()
             .ForMember(d => d.CreateUserName, u => u.MapFrom(t => t.User.FirstName + "/" + t.User.LastName));


            //CRC 质疑列表
            CreateMap<QCChallenge, QCCRCChallengeViewModel>()
               .ForMember(d => d.PreliminaryAuditUserName, u => u.MapFrom(s => s.SubjectVisit.PreliminaryAuditUser.UserName))
               .ForMember(d => d.PreliminaryAuditUserId, u => u.MapFrom(s => s.SubjectVisit.PreliminaryAuditUserId))
               .ForMember(d => d.CurrentActionUserId, u => u.MapFrom(s => s.SubjectVisit.CurrentActionUserId))
               .ForMember(d => d.CurrentActionUserName, u => u.MapFrom(s => s.SubjectVisit.CurrentActionUser.UserName))
                .ForMember(d => d.SubmitState, u => u.MapFrom(s => s.SubjectVisit.SubmitState))


              .ForMember(d => d.SiteId, u => u.MapFrom(s => s.SubjectVisit.SiteId))
              .ForMember(d => d.AuditState, u => u.MapFrom(s => s.SubjectVisit.AuditState))
              .ForMember(d => d.IsUrgent, u => u.MapFrom(s => s.SubjectVisit.IsUrgent))
              .ForMember(d => d.IsBaseLine, u => u.MapFrom(s => s.SubjectVisit.IsBaseLine))
              .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.SubjectVisit.Subject.Code))
              .ForMember(d => d.SubjectId, u => u.MapFrom(t => t.SubjectVisit.SubjectId))
              .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.SubjectVisit.TrialSite.TrialSiteCode))
              .ForMember(d => d.VisitName, u => u.MapFrom(s => s.SubjectVisit.VisitName))
              .ForMember(d => d.RequestBackState, u => u.MapFrom(s => s.SubjectVisit.RequestBackState))
              .ForMember(d => d.VisitNum, u => u.MapFrom(s => s.SubjectVisit.VisitNum))
              .ForMember(d => d.BlindName, u => u.MapFrom(s => s.SubjectVisit.BlindName))
              //.ForMember(d => d.ClosedUserUserName, u => u.MapFrom(s => s.ClosedUser.UserName))
              .ForMember(d => d.IsQCConfirmedReupload, u => u.MapFrom(s => s.SubjectVisit.IsQCConfirmedReupload))
              .ForMember(d => d.CreateUserName, u => u.MapFrom(s => s.CreateUser.UserName))
              .ForMember(d => d.LatestReplyUserName, u => u.MapFrom(t => t.LatestReplyUser.UserName))
              .ForMember(d => d.ChallengeCode, u => u.MapFrom(s => s.ChallengeCode)); //排序的时候有坑 把这个带到sql 中去了
                                                                                      //.AfterMap((src, dest) => dest.ChallengeCode = "Q" + src.ChallengeCode.ToString("D5"));//实测没有效果




            //质疑问题答案 
            CreateMap<QCQuestionAnswerCommand, TrialQCQuestionAnswer>().EqualityComparison((odto, o) => odto.Id == o.Id)
                .ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));
            //.ForMember(d => d.TrialQCQuestionConfigureId, opt => opt.Ignore())//前端更新的时候不会传递这个参数，但是添加的时候会传递
            //.ForMember(d => d.SubjectVisitId, opt => opt.Ignore());
            //更新的时候，因为前端没有传递TrialQCQuestionConfigureId 导致映射后的数据变为 Guid.Empty,明明配置了 如果source 为null 就不映射 但是没生效 临时解决
            //.BeforeMap((src, dest) => src.TrialQCQuestionConfigureId = dest.TrialQCQuestionConfigureId == System.Guid.Empty ? src.TrialQCQuestionConfigureId : dest.TrialQCQuestionConfigureId);


            CreateMap<TrialQCQuestionAnswer, QCQuestionAnswerItemDto>().IncludeMembers(t => t.TrialQCQuestionConfigure)
            .ForMember(d => d.ParentShowOrder, u => u.MapFrom(s => s.TrialQCQuestionConfigure.ParentQCQuestion.ShowOrder));

            CreateMap<TrialQCQuestion, QCQuestionAnswerItemDto>()
                .ForMember(d => d.Id, u => u.Ignore())
                .ForMember(d => d.ParentShowOrder, u => u.MapFrom(s => s.ParentQCQuestion.ShowOrder))
                .ForMember(d => d.TrialQCQuestionConfigureId, u => u.MapFrom(s => s.Id));


            CreateMap<NoneDicomStudyAddOrEdit, NoneDicomStudy>().ReverseMap();


            CreateMap<NoneDicomStudyFile, NoneDicomStudyFileView>()
                .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path + "?access_token=" + token));

            CreateMap<NoneDicomStudy, NoneDicomStudyView>()
                //.ForMember(d => d.FileCount, u => u.MapFrom(s => s.NoneDicomFileList.Count))
                .ForMember(d => d.NoneDicomStudyFileList, u => u.MapFrom(s => s.NoneDicomFileList))
                .ForMember(d => d.CodeView, u => u.MapFrom(s => s.StudyCode));


        }
    }

}
