package com.lantone.qc.kernel.catalogue.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.LeaveHospitalDoc;
import com.lantone.qc.pub.model.doc.RescueDoc;
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 : THR0436
 * @Description : 抢救患者无上级医师(副高及以上)查房记录
 * @Author : 胡敬
 * @Date: 2020-03-24 19:35
 */
@Component
public class THR0436 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        //没有死亡记录并且没有离院记录，直接返回
        if (inputInfo.getDeathRecordDoc() == null && inputInfo.getLeaveHospitalDoc() == null) {
            return;
        }
        //没有抢救记录或者查房记录，直接返回
        if (ListUtil.isEmpty(inputInfo.getRescueDocs()) || 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) {
            LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
            Map<String, String> leaveHospitalStructureMap = leaveHospitalDoc.getStructureMap();
            String leaveHospitalTime = leaveHospitalStructureMap.get("出院时间");
            if (StringUtil.isNotBlank(leaveHospitalTime)) {
                Date leaveHospitalDate = StringUtil.parseDateTime(leaveHospitalTime);
                if (leaveHospitalDate != null) {
                    dealWithRescueRecord(inputInfo, leaveHospitalDate);
                }
            }
        }
    }

    private void dealWithRescueRecord(InputInfo inputInfo, Date timeOfComparison) {
        List<RescueDoc> rescueDocs = inputInfo.getRescueDocs(); //抢救记录
        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 (RescueDoc rescueDoc : rescueDocs) {
            Map<String, String> rescueStructureMap = rescueDoc.getStructureMap();
            recordTime = rescueStructureMap.get("抢救结束时间");
            if (StringUtil.isBlank(recordTime)) {
                continue;
            }
            Date recordDate = StringUtil.parseDateTime(recordTime);
            if (recordDate == null || CatalogueUtil.equalsDate(recordDate, timeOfComparison, "yyyy-MM-dd")) {
                continue;
            }
            dateRecord.put(recordDate, rescueStructureMap);
        }
        dateRecord = extractRescue(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("查房标题"));
            if (StringUtil.isBlank(recordTime)) {
                continue;
            }
            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>> extractRescue(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 rescueDate = dateRecordEntry.getKey();
            if (lastDate == null || CatalogueUtil.compareTime(lastDate, rescueDate, (long) (24 * 60))) {
                lastDate = rescueDate;
                record.put(rescueDate, dateRecordEntry.getValue());
            }
        }
        return record;
    }
}