package com.lantone.qc.trans.ninghaiyiyi;

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.pub.util.StringUtil;
import com.lantone.qc.trans.DocTrans;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import lombok.extern.slf4j.Slf4j;

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

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

    @Override
    protected InputInfo extract(QueryVo queryVo) {
        InputInfo inputInfo = new InputInfo();
        for (MedrecVo i : queryVo.getMedrec()) {
            if (i.getTitle() != null) {
                if (i.getTitle().equals("会诊")) {
                    NingHaiYiYiConsultationDocTrans consultationDocTrans = new NingHaiYiYiConsultationDocTrans();
                    inputInfo.setConsultationDocs(consultationDocTrans.extract(i));
                }
                if (i.getTitle().equals("手术")) {
                    NingHaiYiYiOperationDocTrans operationDocTrans = new NingHaiYiYiOperationDocTrans();
                    inputInfo.setOperationDocs(operationDocTrans.extract(i));
                }
                if (i.getTitle().equals("转科")) {
                    NingHaiYiYiTransferRecordDocTrans transferRecordDocTrans = new NingHaiYiYiTransferRecordDocTrans();
                    inputInfo.setTransferRecordDocs(transferRecordDocTrans.extract(i));
                }
                if (i.getTitle().equals("入院记录")) {
                    NingHaiYiYiBeHospitalizedDocTrans beHospitalizedDocTrans = new NingHaiYiYiBeHospitalizedDocTrans();
                    inputInfo.setBeHospitalizedDoc(beHospitalizedDocTrans.extract(i));
                }
                if (i.getTitle().equals("输血/血制品病程记录")) {
                    NingHaiYiYiClinicalBloodDocTrans clinicalBloodDocTrans = new NingHaiYiYiClinicalBloodDocTrans();
                    inputInfo.setClinicalBloodDocs(clinicalBloodDocTrans.extract(i));
                }
                if (i.getTitle().equals("输血后效果评价")) {
                    NingHaiYiYiClinicBloodEffectDocTrans clinicBloodEffectDocTrans = new NingHaiYiYiClinicBloodEffectDocTrans();
                    inputInfo.setClinicBloodEffectDocs(clinicBloodEffectDocTrans.extract(i));
                }
                if (i.getTitle().equals("危急值记录")) {
                    NingHaiYiYiCrisisValueReportDocTrans crisisValueReportDocTrans = new NingHaiYiYiCrisisValueReportDocTrans();
                    inputInfo.setCrisisValueReportDocs(crisisValueReportDocTrans.extract(i));
                }
                if (i.getTitle().equals("危急值")) {
                    NingHaiYiYiCrisisInfoDocTrans crisisInfoDocTrans = new NingHaiYiYiCrisisInfoDocTrans();
                    inputInfo.setCrisisInfoDocs(crisisInfoDocTrans.extract(i));
                }
                if (i.getTitle().equals("病危通知书")) {
                    NingHaiYiYiCriticallyIllNoticeDocTrans criticallyIllNoticeDocTrans = new NingHaiYiYiCriticallyIllNoticeDocTrans();
                    inputInfo.setCriticallyIllNoticeDocs(criticallyIllNoticeDocTrans.extract(i));
                }
                if (i.getTitle().equals("死亡病例讨论记录")) {
                    NingHaiYiYiDeathCaseDiscussDocTrans deathCaseDiscussDocTrans = new NingHaiYiYiDeathCaseDiscussDocTrans();
                    inputInfo.setDeathCaseDiscussDoc(deathCaseDiscussDocTrans.extract(i));
                }
                if (i.getTitle().equals("死亡记录")) {
                    NingHaiYiYiDeathRecordDocTrans deathRecordDocTrans = new NingHaiYiYiDeathRecordDocTrans();
                    inputInfo.setDeathRecordDoc(deathRecordDocTrans.extract(i));
                }
                if (i.getTitle().equals("疑难病例讨论记录")) {
                    NingHaiYiYiDifficultCaseDiscussDocTrans difficultCaseDiscussDocTrans = new NingHaiYiYiDifficultCaseDiscussDocTrans();
                    inputInfo.setDifficultCaseDiscussDocs(difficultCaseDiscussDocTrans.extract(i));
                }
                if (i.getTitle().equals("医嘱信息")) {
                    NingHaiYiYiDoctorAdviceDocTrans doctorAdviceDocTrans = new NingHaiYiYiDoctorAdviceDocTrans();
                    inputInfo.setDoctorAdviceDocs(doctorAdviceDocTrans.extract(i));
                }
                if (i.getTitle().equals("值班交接制度")) {
                    NingHaiYiYiDutyShiftSystemDocTrans dutyShiftSystemDocTrans = new NingHaiYiYiDutyShiftSystemDocTrans();
                    inputInfo.setDutyShiftSystemDocs(dutyShiftSystemDocTrans.extract(i));
                }
                if (i.getTitle().equals("首次病程录")) {
                    NingHaiYiYiFirstCourseRecordDocTrans firstCourseRecordDocTrans = new NingHaiYiYiFirstCourseRecordDocTrans();
                    inputInfo.setFirstCourseRecordDoc(firstCourseRecordDocTrans.extract(i));
                }
                if (i.getTitle().equals("病案首页")) {
                    NingHaiYiYiFirstPageRecordDocTrans firstPageRecordDocTrans = new NingHaiYiYiFirstPageRecordDocTrans();
                    inputInfo.setFirstPageRecordDoc(firstPageRecordDocTrans.extract(i));
                }
                if (i.getTitle().equals("出院小结")) {
                    NingHaiYiYiLeaveHospitalDocTrans leaveHospitalDocTrans = new NingHaiYiYiLeaveHospitalDocTrans();
                    inputInfo.setLeaveHospitalDoc(leaveHospitalDocTrans.extract(i));
                }
                if (i.getTitle().equals("抢救记录")) {
                    NingHaiYiYiRescueDocTrans rescueDocTrans = new NingHaiYiYiRescueDocTrans();
                    inputInfo.setRescueDocs(rescueDocTrans.extract(i));
                }
                if (i.getTitle().equals("阶段小结")) {
                    NingHaiYiYiStagesSummaryDocTrans stagesSummaryDocTrans = new NingHaiYiYiStagesSummaryDocTrans();
                    inputInfo.setStagesSummaryDocs(stagesSummaryDocTrans.extract(i));
                }
                if (i.getTitle().equals("查房记录")) {
                    NingHaiYiYiThreeLevelWardDocTrans threeLevelWardDocTrans = new NingHaiYiYiThreeLevelWardDocTrans();
                    threeLevelWardDocTrans.setOperationDocs(inputInfo.getOperationDocs());
                    inputInfo.setThreeLevelWardDocs(threeLevelWardDocTrans.extract(i));
                }
                if (i.getTitle().equals("住院病历信息")) {
                    NingHaiYiYiMedicalRecordInfoDocTrans medicalRecordInfoDocTrans = new NingHaiYiYiMedicalRecordInfoDocTrans();
                    inputInfo.setMedicalRecordInfoDoc(medicalRecordInfoDocTrans.extract(i));
                }
                if (i.getTitle().equals("化验")) {
                    NingHaiYiYiLisDocTrans lisDocTrans = new NingHaiYiYiLisDocTrans();
                    inputInfo.setLisDocs(lisDocTrans.extract(i));
                }
                if (i.getTitle().equals("门诊")) {
                    NingHaiYiYiOutDepDocTrans outDepDocTrans = new NingHaiYiYiOutDepDocTrans();
                    inputInfo.setBeHospitalizedDoc(outDepDocTrans.extract(i));
                }
            }
        }
        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.getLeaveHospitalDoc() != null) {
            inputInfo.getPageData().put("出院小结", Lists.newArrayList(inputInfo.getLeaveHospitalDoc().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()));
            inputInfo.setClinicalBloodDocs(inputInfo.getClinicalBloodDocs().stream().filter(i -> StringUtil.isNotBlank(i.getText()) && !i.getText().contains("输白蛋白")).collect(Collectors.toList()));
        }
        if (ListUtil.isNotEmpty(inputInfo.getClinicBloodEffectDocs())) {
            inputInfo.getPageData().put(
                    "输血后效果评价",
                    inputInfo.getClinicBloodEffectDocs()
                            .stream()
                            .map(i -> i.getPageData())
                            .sorted((map1, map2) -> OrdinaryAssistant.pageDataTimeSort(map1, map2, "记录时间", "yyyy-MM-dd HH:mm"))
                            .collect(Collectors.toList()));
        }
        if (ListUtil.isNotEmpty(inputInfo.getCrisisValueReportDocs())) {
            inputInfo.getPageData().put("危急值记录", inputInfo.getCrisisValueReportDocs().stream().map(i -> i.getPageData()).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.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()).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())) {
            inputInfo.getPageData().put("查房记录", inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs().stream().map(i -> i.getPageData()).collect(Collectors.toList()));
        }
        if (ListUtil.isNotEmpty(inputInfo.getConsultationDocs())) {
            List<Map<String, Object>> crPd = inputInfo.getConsultationDocs()
                    .stream()
                    .filter(consultationDoc -> consultationDoc != null && consultationDoc.getConsultationRecordDoc() != null)
                    .map(consultationDoc -> consultationDoc.getConsultationRecordDoc().getPageData())
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(crPd)) {
                inputInfo.getPageData().put("会诊记录", crPd);
            }

            List<Map<String, Object>> caPd = inputInfo.getConsultationDocs()
                    .stream()
                    .filter(consultationDoc -> consultationDoc != null && consultationDoc.getConsultationApplicationDoc() != null)
                    .map(consultationDoc -> consultationDoc.getConsultationApplicationDoc().getPageData())
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(caPd)) {
                inputInfo.getPageData().put("会诊申请单", caPd);
            }

            List<Map<String, Object>> ctPd = inputInfo.getConsultationDocs()
                    .stream()
                    .filter(consultationDoc -> consultationDoc != null && consultationDoc.getConsultationResultsDoc() != null)
                    .map(consultationDoc -> consultationDoc.getConsultationResultsDoc().getPageData())
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(ctPd)) {
                inputInfo.getPageData().put("会诊结果单", ctPd);
            }
        }
        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())
                    .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())
                    .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())
                    .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(pdPageDataList)) {
                inputInfo.getPageData().put("术前讨论、术前小结", pdPageDataList);
            }
        }
        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())
                );
            }
        }
    }
}