package com.lantone.qc.trans.shaoyf;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.model.vo.QueryVo;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.trans.DocTrans;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.shaoyf.ShaoyfBeHospitalizedDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfClinicBloodEffectDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfClinicalBloodDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfConsultationDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfCrisisValueReportDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfCriticallyIllNoticeDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfDeathCaseDiscussDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfDeathRecordDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfDifficultCaseDiscussDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfDoctorAdviceDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfDutyShiftSystemDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfFirstCourseRecordDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfFirstPageRecordDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfLeaveHospitalDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfMedicalRecordInfoDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfNursingSystemDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfOperationDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfPathologyShipDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfRescueDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfSeriouslyIllNoticeDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfStagesSummaryDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfThreeLevelWardDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfTransferRecordDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfInformedConsentDocTrans;
import com.lantone.qc.trans.shaoyf.ShaoyfNoticeOfConversationDocTrans;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName : ShaoyfDocTrans
 * @Description :
 * @Author : 楼辉荣
 * @Date: 2020-03-03 19:47
 */
public class ShaoyfDocTrans extends DocTrans {

    @Override
    protected InputInfo extract(QueryVo queryVo) {
        InputInfo inputInfo = new InputInfo();
        for (MedrecVo i : queryVo.getMedrec()) {
            switch (i.getTitle()) {
                case "会诊":
                    ShaoyfConsultationDocTrans consultationDocTrans = new ShaoyfConsultationDocTrans();
                    inputInfo.setConsultationDocs(consultationDocTrans.extract(i));
                    break;
                case "手术":
                    ShaoyfOperationDocTrans operationDocTrans = new ShaoyfOperationDocTrans();
                    inputInfo.setOperationDocs(operationDocTrans.extract(i));
                    break;
                case "转科":
                    ShaoyfTransferRecordDocTrans transferRecordDocTrans = new ShaoyfTransferRecordDocTrans();
                    inputInfo.setTransferRecordDocs(transferRecordDocTrans.extract(i));
                    break;
                case "入院记录":
                    ShaoyfBeHospitalizedDocTrans beHospitalizedDocTrans = new ShaoyfBeHospitalizedDocTrans();
                    inputInfo.setBeHospitalizedDoc(beHospitalizedDocTrans.extract(i));
                    break;
                case "输血/血制品病程记录":
                    ShaoyfClinicalBloodDocTrans clinicalBloodDocTrans = new ShaoyfClinicalBloodDocTrans();
                    inputInfo.setClinicalBloodDocs(clinicalBloodDocTrans.extract(i));
                    break;
                case "危急值记录":
                    ShaoyfCrisisValueReportDocTrans crisisValueReportDocTrans = new ShaoyfCrisisValueReportDocTrans();
                    inputInfo.setCrisisValueReportDocs(crisisValueReportDocTrans.extract(i));
                    break;
                case "危急值":
                    ShaoyfCrisisInfoDocTrans shaoyfCrisisInfoDocTrans = new ShaoyfCrisisInfoDocTrans();
                    inputInfo.setCrisisInfoDocs(shaoyfCrisisInfoDocTrans.extract(i));
                    break;
                case "病危通知书":
                    ShaoyfCriticallyIllNoticeDocTrans criticallyIllNoticeDocTrans = new ShaoyfCriticallyIllNoticeDocTrans();
                    inputInfo.setCriticallyIllNoticeDocs(criticallyIllNoticeDocTrans.extract(i));
                    break;
                case "死亡病例讨论记录":
                    ShaoyfDeathCaseDiscussDocTrans deathCaseDiscussDocTrans = new ShaoyfDeathCaseDiscussDocTrans();
                    inputInfo.setDeathCaseDiscussDoc(deathCaseDiscussDocTrans.extract(i));
                    break;
                case "死亡记录":
                    ShaoyfDeathRecordDocTrans deathRecordDocTrans = new ShaoyfDeathRecordDocTrans();
                    inputInfo.setDeathRecordDoc(deathRecordDocTrans.extract(i));
                    break;
                case "疑难病例讨论记录":
                    ShaoyfDifficultCaseDiscussDocTrans difficultCaseDiscussDocTrans = new ShaoyfDifficultCaseDiscussDocTrans();
                    inputInfo.setDifficultCaseDiscussDocs(difficultCaseDiscussDocTrans.extract(i));
                    break;
                case "医嘱信息":
                    ShaoyfDoctorAdviceDocTrans doctorAdviceDocTrans = new ShaoyfDoctorAdviceDocTrans();
                    inputInfo.setDoctorAdviceDocs(doctorAdviceDocTrans.extract(i));
                    break;
                case "值班交接制度":
                    ShaoyfDutyShiftSystemDocTrans dutyShiftSystemDocTrans = new ShaoyfDutyShiftSystemDocTrans();
                    /*inputInfo.setDutyShiftSystemDocs(dutyShiftSystemDocTrans.extract(i));*/
                    break;
                case "首次病程录":
                    ShaoyfFirstCourseRecordDocTrans firstCourseRecordDocTrans = new ShaoyfFirstCourseRecordDocTrans();
                    inputInfo.setFirstCourseRecordDoc(firstCourseRecordDocTrans.extract(i));
                    break;
                case "病案首页":
                    ShaoyfFirstPageRecordDocTrans firstPageRecordDocTrans = new ShaoyfFirstPageRecordDocTrans();
                    if (inputInfo.getFirstPageRecordDoc() == null) {
                        inputInfo.setFirstPageRecordDoc(firstPageRecordDocTrans.extract(i));
                    }
                    break;
                case "出院小结":
                    ShaoyfLeaveHospitalDocTrans leaveHospitalDocTrans = new ShaoyfLeaveHospitalDocTrans();
                    inputInfo.setLeaveHospitalDoc(leaveHospitalDocTrans.extract(i));
                    break;
                case "分级护理制度":
                    ShaoyfNursingSystemDocTrans nursingSystemDocTrans = new ShaoyfNursingSystemDocTrans();
                    /*inputInfo.setNursingSystemDoc(nursingSystemDocTrans.extract(i));*/
                    break;
                case "抢救记录":
                    ShaoyfRescueDocTrans rescueDocTrans = new ShaoyfRescueDocTrans();
                    inputInfo.setRescueDocs(rescueDocTrans.extract(i));
                    break;
                case "病重通知书":
                    ShaoyfSeriouslyIllNoticeDocTrans seriouslyIllNoticeDocTrans = new ShaoyfSeriouslyIllNoticeDocTrans();
                    inputInfo.setSeriouslyIllNoticeDocs(seriouslyIllNoticeDocTrans.extract(i));
                    break;
                case "阶段小结":
                    ShaoyfStagesSummaryDocTrans stagesSummaryDocTrans = new ShaoyfStagesSummaryDocTrans();
                    inputInfo.setStagesSummaryDocs(stagesSummaryDocTrans.extract(i));
                    break;
                case "查房记录":
                    ShaoyfThreeLevelWardDocTrans threeLevelWardDocTrans = new ShaoyfThreeLevelWardDocTrans();
                    threeLevelWardDocTrans.setOperationDocs(inputInfo.getOperationDocs());
                    inputInfo.setThreeLevelWardDocs(threeLevelWardDocTrans.extract(i));
                    break;
                case "输血后效果评价":
                    ShaoyfClinicBloodEffectDocTrans clinicBloodEffectDocTrans = new ShaoyfClinicBloodEffectDocTrans();
                    inputInfo.setClinicBloodEffectDocs(clinicBloodEffectDocTrans.extract(i));
                    break;
                case "病理检验送检单":
                    ShaoyfPathologyShipDocTrans pathologyShipDocTrans = new ShaoyfPathologyShipDocTrans();
                    inputInfo.setPathologyShipDocs(pathologyShipDocTrans.extract(i));
                    break;
                case "知情同意书":
                    ShaoyfInformedConsentDocTrans informedConsentDocTrans = new ShaoyfInformedConsentDocTrans();
                    inputInfo.setInformedConsentDoc(informedConsentDocTrans.extract(i));
                    break;
                case "谈话告知书":
                    ShaoyfNoticeOfConversationDocTrans shaoyfNoticeOfConversationDocTrans = new ShaoyfNoticeOfConversationDocTrans();
                    inputInfo.setNoticeOfConversationDoc(shaoyfNoticeOfConversationDocTrans.extract(i));
                    break;
                case "住院病历信息":
                    ShaoyfMedicalRecordInfoDocTrans medicalRecordInfoDocTrans = new ShaoyfMedicalRecordInfoDocTrans();
                    inputInfo.setMedicalRecordInfoDoc(medicalRecordInfoDocTrans.extract(i));
                    break;
                case "化验":
                    ShaoyfLisDocTrans shaoyfLisDocTrans = new ShaoyfLisDocTrans();
                    inputInfo.setLisDocs(shaoyfLisDocTrans.extract(i));
                    break;
                case "辅检":
                    ShaoyfPacsDocTrans shaoyfPacsDocTrans = new ShaoyfPacsDocTrans();
                    inputInfo.setPacsDocs(shaoyfPacsDocTrans.extract(i));
                    break;
                case  "门诊":
                    ShaoyfOutDepDocTrans shaoyfOutDepDocTrans = new ShaoyfOutDepDocTrans();
                    inputInfo.setBeHospitalizedDoc(shaoyfOutDepDocTrans.extract(i));
                default:
                    break;
            }
        }
        pageDataHandle(inputInfo);
        return inputInfo;
    }

    private void pageDataHandle(InputInfo inputInfo) {
        if (inputInfo.getBeHospitalizedDoc() != null) {
            inputInfo.getPageData().put("入院记录", Lists.newArrayList(inputInfo.getBeHospitalizedDoc().getPageData()));
        }
        if (inputInfo.getDeathCaseDiscussDoc() != null) {
            inputInfo.getPageData().put("死亡病例讨论记录", Lists.newArrayList(inputInfo.getDeathCaseDiscussDoc().getPageData()));
        }
        if (inputInfo.getDeathRecordDoc() != null) {
            inputInfo.getPageData().put("死亡记录", Lists.newArrayList(inputInfo.getDeathRecordDoc().getPageData()));
        }
        if (inputInfo.getFirstCourseRecordDoc() != null) {
            inputInfo.getPageData().put("首次病程录", Lists.newArrayList(inputInfo.getFirstCourseRecordDoc().getPageData()));
        }
        if (inputInfo.getDeathRecordDoc() == null && inputInfo.getLeaveHospitalDoc() != null) {
            inputInfo.getPageData().put("出院小结", Lists.newArrayList(inputInfo.getLeaveHospitalDoc().getPageData()));
        }
        //        if (inputInfo.getNursingSystemDoc() != null) {
        //            inputInfo.getPageData().put("分级护理制度", Lists.newArrayList(inputInfo.getNursingSystemDoc().getPageData()));
        //        }
        if (inputInfo.getFirstPageRecordDoc() != null) {
            inputInfo.getPageData().put("病案首页", Lists.newArrayList(inputInfo.getFirstPageRecordDoc().getPageData()));
        }

        if (ListUtil.isNotEmpty(inputInfo.getClinicalBloodDocs())) {
            inputInfo.getPageData().put("输血/血制品病程记录", inputInfo.getClinicalBloodDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList()));
        }
        if (ListUtil.isNotEmpty(inputInfo.getClinicBloodEffectDocs())) {
            inputInfo.getPageData().put("输血后效果评价", inputInfo.getClinicBloodEffectDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList()));
        }
        if (ListUtil.isNotEmpty(inputInfo.getCrisisValueReportDocs())) {
            inputInfo.getPageData().put(
                    "危急值记录",
                    inputInfo.getCrisisValueReportDocs()
                            .stream()
                            .map(i -> i.getPageData())
                            .sorted((map1, map2) -> -OrdinaryAssistant.pageDataTimeSort(map1, map2, "病历日期", "yyyy-MM-dd HH:mm"))
                            .collect(Collectors.toList())
            );
        }
        if (ListUtil.isNotEmpty(inputInfo.getCriticallyIllNoticeDocs())) {
            inputInfo.getPageData().put("病危通知书", inputInfo.getCriticallyIllNoticeDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList()));
        }
        if (ListUtil.isNotEmpty(inputInfo.getDifficultCaseDiscussDocs())) {
            inputInfo.getPageData().put("疑难病例讨论记录", inputInfo.getDifficultCaseDiscussDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList()));
        }
        //        if (ListUtil.isNotEmpty(inputInfo.getDoctorAdviceDocs())) {
        //            inputInfo.getPageData().put("医嘱信息", inputInfo.getDoctorAdviceDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList()));
        //        }
        //        if (ListUtil.isNotEmpty(inputInfo.getDutyShiftSystemDocs())) {
        //            inputInfo.getPageData().put("值班交接制度", inputInfo.getDutyShiftSystemDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList()));
        //        }
        if (ListUtil.isNotEmpty(inputInfo.getRescueDocs())) {
            inputInfo.getPageData().put(
                    "抢救记录",
                    inputInfo.getRescueDocs()
                            .stream()
                            .map(i -> i.getPageData())
                            .sorted((map1, map2) -> OrdinaryAssistant.pageDataTimeSort(map1, map2, "事件日期", "yyyy/MM/dd HH:mm"))
                            .collect(Collectors.toList())
            );
        }
        //        if (ListUtil.isNotEmpty(inputInfo.getSeriouslyIllNoticeDocs())) {
        //            inputInfo.getPageData().put("病重通知书", inputInfo.getSeriouslyIllNoticeDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList()));
        //        }
        if (ListUtil.isNotEmpty(inputInfo.getStagesSummaryDocs())) {
            inputInfo.getPageData().put("阶段小结", inputInfo.getStagesSummaryDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList()));
        }
        if (ListUtil.isNotEmpty(inputInfo.getThreeLevelWardDocs())) {
            List<Map<String, Object>> pageDatas = inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList());
            inputInfo.getPageData().put("查房记录", pageDatas);
        }
        if (ListUtil.isNotEmpty(inputInfo.getPathologyShipDocs())) {
            inputInfo.getPageData().put("病理检验送检单", inputInfo.getPathologyShipDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList()));
        }

        if (ListUtil.isNotEmpty(inputInfo.getConsultationDocs())) {
            List<Map<String, Object>> crePageDataList = inputInfo.getConsultationDocs()
                    .stream()
                    .filter(consultationDoc -> consultationDoc != null && consultationDoc.getConsultationRecordDoc() != null)
                    .map(consultationDoc -> consultationDoc.getConsultationRecordDoc().getPageData())
                    .sorted((map1, map2) -> -OrdinaryAssistant.pageDataTimeSort(map1, map2, "记录时间", "yyyy/MM/dd HH:mm"))
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(crePageDataList)) {
                inputInfo.getPageData().put("会诊记录", crePageDataList);
            }

            List<Map<String, Object>> caPageDataList = inputInfo.getConsultationDocs()
                    .stream()
                    .filter(consultationDoc -> consultationDoc != null && consultationDoc.getConsultationApplicationDoc() != null)
                    .map(consultationDoc -> consultationDoc.getConsultationApplicationDoc().getPageData())
                    .sorted((map1, map2) -> -OrdinaryAssistant.pageDataTimeSort(map1, map2, "申请日期", "yyyy/MM/dd HH:mm"))
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(caPageDataList)) {
                inputInfo.getPageData().put("会诊申请单", caPageDataList);
            }

            List<Map<String, Object>> crsPageDataList = inputInfo.getConsultationDocs()
                    .stream()
                    .filter(consultationDoc -> consultationDoc != null && consultationDoc.getConsultationResultsDoc() != null)
                    .map(consultationDoc -> consultationDoc.getConsultationResultsDoc().getPageData())
                    .sorted((map1, map2) -> -OrdinaryAssistant.pageDataTimeSort(map1, map2, "申请日期", "yyyy/MM/dd HH:mm"))
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(crsPageDataList)) {
                inputInfo.getPageData().put("会诊结果单", crsPageDataList);
            }
        }
        if (ListUtil.isNotEmpty(inputInfo.getOperationDocs())) {
            List<Map<String, Object>> odPageDataList = inputInfo.getOperationDocs()
                    .stream()
                    .filter(operationDoc -> operationDoc != null && operationDoc.getOperationDiscussionDoc() != null)
                    .map(operationDoc -> operationDoc.getOperationDiscussionDoc().getPageData())
                    .sorted((map1, map2) -> OrdinaryAssistant.pageDataTimeSort(map1, map2, "手术日期", "yyyy/MM/dd"))
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(odPageDataList)) {
                inputInfo.getPageData().put("术后首次病程及谈话记录", odPageDataList);
            }

            List<Map<String, Object>> orPageDataList = inputInfo.getOperationDocs()
                    .stream()
                    .filter(operationDoc -> operationDoc != null && operationDoc.getOperationRecordDoc() != null)
                    .map(operationDoc -> operationDoc.getOperationRecordDoc().getPageData())
                    .sorted((map1, map2) -> OrdinaryAssistant.pageDataTimeSort(map1, map2, "开始时间", "yyyy/MM/dd HH:mm"))
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(orPageDataList)) {
                inputInfo.getPageData().put("手术记录", orPageDataList);
            }

            List<Map<String, Object>> pdPageDataList = inputInfo.getOperationDocs()
                    .stream()
                    .filter(operationDoc -> operationDoc != null && operationDoc.getPreoperativeDiscussionDoc() != null)
                    .map(operationDoc -> operationDoc.getPreoperativeDiscussionDoc().getPageData())
                    .sorted((map1, map2) -> OrdinaryAssistant.pageDataTimeSort(map1, map2, "讨论时间", "yyyy年MM月dd日 HH时mm分"))
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(pdPageDataList)) {
                inputInfo.getPageData().put("术前讨论、术前小结", pdPageDataList);
            }

            /*List<Map<String, Object>> oicPageDataList = inputInfo.getOperationDocs()
                    .stream()
                    .filter(operationDoc -> operationDoc != null && operationDoc.getOperationInformedConsentDoc() != null)
                    .map(operationDoc -> operationDoc.getOperationInformedConsentDoc().getPageData())
                    .sorted((map1, map2) -> OrdinaryAssistant.pageDataTimeSort(map1, map2, "谈话日期", "yyyy/MM/dd HH:mm"))
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(oicPageDataList)) {
                inputInfo.getPageData().put("手术知情同意书", oicPageDataList);
            }*/

            List<Map<String, Object>> oscPageDataList = inputInfo.getOperationDocs()
                    .stream()
                    .filter(operationDoc -> operationDoc != null && operationDoc.getOperationSafetyChecklistDoc() != null)
                    .map(operationDoc -> operationDoc.getOperationSafetyChecklistDoc().getPageData())
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(oscPageDataList)) {
                inputInfo.getPageData().put("手术安全核查表", oscPageDataList);
            }
        }
        if (inputInfo.getTransferRecordDocs() != null) {
            if (ListUtil.isNotEmpty(inputInfo.getTransferRecordDocs().getTransferIntoDocs())) {
                inputInfo.getPageData().put(
                        "转入记录",
                        inputInfo.getTransferRecordDocs().getTransferIntoDocs()
                                .stream()
                                .filter(i -> i != null)
                                .map(i -> i.getPageData())
                                .collect(Collectors.toList())
                );
            }
            if (ListUtil.isNotEmpty(inputInfo.getTransferRecordDocs().getTransferOutDocs())) {
                inputInfo.getPageData().put(
                        "转出记录",
                        inputInfo.getTransferRecordDocs().getTransferOutDocs()
                                .stream()
                                .filter(i -> i != null)
                                .map(i -> i.getPageData())
                                .collect(Collectors.toList())
                );
            }
        }
    }

}