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.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.DoctorAdviceDoc;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.util.DateUtil;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : THR03024
 * @Description : 病危没有每天或随时记录
 * @Author : zhoutg
 * @Date: 2020-03-28 14:22
 */
@Component
public class THR03024 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        /**
         * 1、有病危医嘱、查房记录。
         * 2、第一个危重医嘱的开始时间当天有查房记录，结束时间有查房记录。
         * 3、每天有查房记录。
         * 4、如果医嘱结束时间大于出院时间，那以出院时间为结束时间
         * 5、首程也算第一次查房
         */
        status.set("0");
        if (ListUtil.isEmpty(inputInfo.getDoctorAdviceDocs()) ||
                ListUtil.isEmpty(inputInfo.getThreeLevelWardDocs())) {
            return;
        }
        String leaveDateString = "";
        Date leaveDate = null;
        if (inputInfo.getLeaveHospitalDoc() != null) {
            leaveDateString = inputInfo.getLeaveHospitalDoc().getStructureMap().get("出院时间");
        } else if (inputInfo.getDeathRecordDoc() != null) {
            leaveDateString = inputInfo.getDeathRecordDoc().getStructureMap().get("死亡时间");
        }
        if (StringUtils.isNotEmpty(leaveDateString)) {
            leaveDate = StringUtil.parseDateTime(leaveDateString);
        }

        List<DoctorAdviceDoc> doctorAdviceDocList = inputInfo.getDoctorAdviceDocs();
        List<List<Date>> doctorAdviceDate = new ArrayList<>();

        // 目前只判断第一个病重医嘱
        for (DoctorAdviceDoc bean : doctorAdviceDocList) {
            Map<String, String> structureMap = bean.getStructureMap();
            String name = structureMap.get("医嘱项目名称");
            if (StringUtil.isNotBlank(name) && name.contains("病危")) {
                String startDateStr = structureMap.get("医嘱开始时间");
                String endDateStr = structureMap.get("医嘱结束时间");
                if (StringUtil.isNotBlank(startDateStr) && StringUtil.isNotBlank(endDateStr)) {
                    Date startDate = StringUtil.parseDateTime(startDateStr);
                    Date endDate = StringUtil.parseDateTime(endDateStr);
                    //如果医嘱结束时间大于出院时间，那以出院时间为结束时间
                    if (leaveDate != null && leaveDate.before(endDate)) {
                        endDate = leaveDate;
                    }
                    if (startDate.before(endDate)) {
                        List<Date> listDate = new ArrayList<>();
                        listDate.add(DateUtil.dateZeroClear(startDate));
                        listDate.add(DateUtil.dateZeroClear(endDate));
                        doctorAdviceDate.add(listDate);
                        break;
                    }
                }
            }
        }

        if (ListUtil.isEmpty(doctorAdviceDate)) {
            return ;
        }
        List<Date> dateRecordDay = new ArrayList<>();

        List<ThreeLevelWardDoc> allDoctorWradDocs = inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs();//查房记录
        for (ThreeLevelWardDoc threeLevelWardDoc : allDoctorWradDocs) {
            Map<String, String> rescueStructureMap = threeLevelWardDoc.getStructureMap();
            String recordTime = rescueStructureMap.get("查房日期");
            if (StringUtils.isNotEmpty(recordTime)) {
                Date recordDate = StringUtil.parseDateTime(recordTime);
                dateRecordDay.add(DateUtil.dateZeroClear(recordDate));
            }
        }
        //首程也算第一次查房
        if (inputInfo.getFirstCourseRecordDoc() != null) {
            String recordTime = inputInfo.getFirstCourseRecordDoc().getStructureMap().get("病历日期");
            if (StringUtils.isNotEmpty(recordTime)) {
                Date recordDate = StringUtil.parseDateTime(recordTime);
                dateRecordDay.add(DateUtil.dateZeroClear(recordDate));
            }
        }

        if (ListUtil.isEmpty(dateRecordDay)) {
            return ;
        }

        // 时间正序排列
        Collections.sort(dateRecordDay, new Comparator<Date>() {
            public int compare(Date o1, Date o2) {
                int flag = o1.compareTo(o2);
                return flag;
            }
        });

        // 医嘱开始时间和结束时间当天必须要有查房记录
        Date dateStart = dateRecordDay.get(0); // 查房第一个时间
        Date dateEnd = dateRecordDay.get(dateRecordDay.size() - 1);// 查房最后一个时间
        Date doctorDateStart = doctorAdviceDate.get(0).get(0); // 医嘱开始时间
        Date doctorDateEnd = doctorAdviceDate.get(0).get(1); // 医嘱结束时间

        // 医嘱开始时间当天有查房记录
        if (!dateRecordDay.contains(doctorDateStart)) {
            info.set(DateUtil.formatDate(doctorDateStart));
            status.set("-1");
            return ;
        }
        // 医嘱结束时间当天有查房记录
        if (!dateRecordDay.contains(doctorDateEnd)) {
            info.set(DateUtil.formatDate(doctorDateEnd));
            status.set("-1");
            return ;
        }
        Date nextDate = doctorDateStart;
        int returnFlag = 0; // 防止出现死循环系统崩溃
        String infoStr = "";
        while(returnFlag <= 1000) {
            if (!dateRecordDay.contains(nextDate)) {
                status.set("-1");
                infoStr = CatalogueUtil.concatInfo(infoStr, DateUtil.formatDate(nextDate));
            }
            nextDate = DateUtil.addDate(nextDate, 1);
            if (nextDate.after(doctorDateEnd)) {
                return ;
            }
            returnFlag++; // 防止出现死循环系统崩溃
        }
        if (StringUtils.isNotEmpty(infoStr)) {
            info.set(infoStr);
        }
    }
}