package com.lantone.qc.kernel.catalogue.ninghaiyiyi.threelevelward;

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.Content;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.DeathRecordDoc;
import com.lantone.qc.pub.model.doc.SeriouslyIllNoticeDoc;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @ClassName : THR0434
 * @Description : 危重患者无上级医师(副高及以上)查房记录
 * @Author : Mark
 * @Date: 2020-04-05 16:10
 */
@Component
public class THR0434 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        //没有死亡记录并且没有离院记录，直接返回
        if (inputInfo.getDeathRecordDoc() == null && inputInfo.getLeaveHospitalDoc() == null) {
            return;
        }
        //没有病重通知书或者查房记录，直接返回
        if (ListUtil.isEmpty(inputInfo.getSeriouslyIllNoticeDocs()) || ListUtil.isEmpty(inputInfo.getThreeLevelWardDocs())) {
            return;
        }
        //死亡记录不为空
        if (inputInfo.getDeathRecordDoc() != null) {
            DeathRecordDoc deathRecordDoc = inputInfo.getDeathRecordDoc();
            Map<String, String> deathRecordStructureMap = deathRecordDoc.getStructureMap();
            String deathTime = deathRecordStructureMap.get("死亡时间");
            if (StringUtil.isNotBlank(deathTime)) {
                Date deathDate = StringUtil.parseDateTime(deathTime);
                if (deathDate != null) {
                    dealWithRescueRecord(inputInfo, deathDate);
                }
            }
        }
        //离院记录不为空
        if (inputInfo.getLeaveHospitalDoc() != null && inputInfo.getMedicalRecordInfoDoc() != null) {
            Map<String, String> medicalRecordInfoStructureMap = inputInfo.getMedicalRecordInfoDoc().getStructureMap();
            String leaveHospitalTime = medicalRecordInfoStructureMap.get("leaveHospitalDate");
            if (StringUtil.isNotBlank(leaveHospitalTime)) {
                Date leaveHospitalDate = StringUtil.parseDateTime(leaveHospitalTime);
                if (leaveHospitalDate != null) {
                    dealWithRescueRecord(inputInfo, leaveHospitalDate);
                }
            }
        }
    }

    private void dealWithRescueRecord(InputInfo inputInfo, Date timeOfComparison) {
        List<SeriouslyIllNoticeDoc> seriouslyIllNoticeDocs = inputInfo.getSeriouslyIllNoticeDocs(); //病危通知书
        Map<Date, Map<String, String>> dateRecord = new TreeMap<>(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                // 降序排列
                return o1.compareTo(o2);
            }
        });//按时间key排序，存放病危通知书、查房记录
        String recordTime = "", title = "";
        for (SeriouslyIllNoticeDoc seriouslyIllNoticeDoc : seriouslyIllNoticeDocs) {
            Map<String, String> seriouslyIllNoticeDocMap = seriouslyIllNoticeDoc.getStructureMap();
            recordTime = seriouslyIllNoticeDocMap.get("签名时间");
            Date recordDate = StringUtil.parseDateTime(recordTime);
            if (recordDate == null) {
                continue;
            }
            dateRecord.put(recordDate, seriouslyIllNoticeDocMap);
        }
        dateRecord = extractSeriouslyIll(dateRecord);//24小时内记录只取第一条
        List<ThreeLevelWardDoc> allDoctorWradDocs = inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs();//查房记录
        for (ThreeLevelWardDoc threeLevelWardDoc : allDoctorWradDocs) {
            Map<String, String> rescueStructureMap = threeLevelWardDoc.getStructureMap();
            recordTime = rescueStructureMap.get("查房日期");
            title = CatalogueUtil.subTitle(rescueStructureMap.get("查房标题"));
            Date recordDate = StringUtil.parseDateTime(recordTime);
            if (recordDate == null || StringUtil.isBlank(title) || !title.contains(Content.director)) {//只存副高以上查房记录
                continue;
            }
            dateRecord.put(recordDate, rescueStructureMap);
        }

        List<Map<String, String>> dateRecordList = new ArrayList<>(dateRecord.values());
        //如果最后一条是病危通知书，则之后肯定没有查房记录
        if (dateRecordList.get(dateRecordList.size() - 1).containsKey("病程记录名称")) {
            status.set("-1");
            return;
        }
        for (int i = 0; i < dateRecordList.size(); i++) {
            if (i != dateRecordList.size() - 1) {
                //当前为病危通知书
                if (!dateRecordList.get(i).containsKey("签名时间")) {
                    continue;
                }
                //病危通知书下一条不是查房记录则报错
                if (!dateRecordList.get(i + 1).containsKey("查房日期")) {
                    status.set("-1");
                    return;
                }
            }
        }
    }

    /**
     * 抽取24小时内第一条记录
     *
     * @param dateRecord
     */
    private Map<Date, Map<String, String>> extractSeriouslyIll(Map<Date, Map<String, String>> dateRecord) {
        Map<Date, Map<String, String>> record = new TreeMap<>(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                // 降序排列
                return o1.compareTo(o2);
            }
        });
        Date lastDate = null;
        for (Map.Entry<Date, Map<String, String>> dateRecordEntry : dateRecord.entrySet()) {
            Date difficultCaseDiscussDate = dateRecordEntry.getKey();
            if (lastDate == null || CatalogueUtil.compareTime(lastDate, difficultCaseDiscussDate, (long) (24 * 60))) {
                lastDate = difficultCaseDiscussDate;
                record.put(difficultCaseDiscussDate, dateRecordEntry.getValue());
            }
        }
        return record;
    }
}