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.DifficultCaseDiscussDoc;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
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 : THR0435
 * @Description : 疑难患者无上级医师(副高及以上)查房记录
 * @Author : 胡敬
 * @Date: 2020-03-25 10:21
 */
@Component
public class THR0435 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        //没有死亡记录并且没有离院记录，直接返回
        if (inputInfo.getDeathRecordDoc() == null && inputInfo.getLeaveHospitalDoc() == null) {
            return;
        }
        //没有疑难病例讨论记录或者查房记录，直接返回
        if (ListUtil.isEmpty(inputInfo.getDifficultCaseDiscussDocs()) || 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<DifficultCaseDiscussDoc> difficultCaseDiscussDocs = inputInfo.getDifficultCaseDiscussDocs(); //疑难病例讨论记录
        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 (DifficultCaseDiscussDoc difficultCaseDiscussDoc : difficultCaseDiscussDocs) {
            Map<String, String> difficultCaseDiscussStructureMap = difficultCaseDiscussDoc.getStructureMap();
            recordTime = difficultCaseDiscussStructureMap.get("讨论时间");
            Date recordDate = StringUtil.parseDateTime(recordTime);
            if (recordDate == null) {
                continue;
            }
            dateRecord.put(recordDate, difficultCaseDiscussStructureMap);
        }
        dateRecord = extractDifficult(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>> extractDifficult(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;
    }
}