package com.lantone.qc.kernel.catalogue.consultation;

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.consultation.ConsultationApplicationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationResultsDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.*;

/**
 * @ClassName : CON0526
 * @Description : 急会诊未在10分钟内到达
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class CON0526 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
        status.set("0");
        if (inputInfo.getConsultationDocs().size() == 0) {
            return;
        }
        //会诊单按日会诊申请日期排序
        List<ConsultationDoc> consultationDocs = inputInfo.getConsultationDocs();
        //医嘱按照医嘱开始时间排序
        List<DoctorAdviceDoc> doctorAdviceDocs = inputInfo.getDoctorAdviceDocs();
        Collections.sort(doctorAdviceDocs, new Comparator<DoctorAdviceDoc>() {
            public int compare(DoctorAdviceDoc o1, DoctorAdviceDoc o2) {
                return o1.getStructureMap().get("医嘱开始时间")
                        .compareTo(o2.getStructureMap().get("医嘱开始时间"));
            }
        });
        for (ConsultationDoc consultationDoc : consultationDocs) {
            ConsultationResultsDoc consultationResultsDoc = consultationDoc.getConsultationResultsDoc();
            if (consultationResultsDoc == null) {
                continue;
            }
            Map<String, String> conStructureMap = consultationResultsDoc.getStructureMap();
            if (conStructureMap.get("会诊类别") == null || !conStructureMap.get("会诊类别").contains("急会诊")) {
                continue;
            }
            for (DoctorAdviceDoc doctorAdviceDoc : doctorAdviceDocs) {//循环取最近一条医嘱和会诊申请单中的急会诊比较
                Map<String, String> docStructureMap = doctorAdviceDoc.getStructureMap();
                if (docStructureMap.get("医嘱项目名称") != null && docStructureMap.get("医嘱项目名称").contains("会诊")) {
                    String applicationDateStr = docStructureMap.get("医嘱开始时间");
                    String arrivalDateStr = conStructureMap.get("会诊时间");
                    if (StringUtil.isBlank(applicationDateStr) || StringUtil.isBlank(arrivalDateStr)) {
                        continue;
                    }
                    Date applicationDate = StringUtil.parseDateTime(applicationDateStr);//医嘱开始时间
                    Date arrivalDate = StringUtil.parseDateTime(arrivalDateStr);//会诊时间
                    if (applicationDate == null || arrivalDate == null) {
                        continue;
                    }
                    if (CatalogueUtil.compareTime(applicationDate, arrivalDate, 10L)) {
                        status.set("-1");
                        return;
                    }
                }
            }
        }




        

        /*status.set("0");
        List<ConsultationDoc> consultationDocs = inputInfo.getConsultationDocs();
        if (consultationDocs.size() > 0) {
            for (ConsultationDoc consulation : consultationDocs) {
                ConsultationApplicationDoc consultationApplicationDoc = consulation.getConsultationApplicationDoc();
                ConsultationResultsDoc consultationResultsDoc = consulation.getConsultationResultsDoc();
                Map<String, String> applicationMap = consultationApplicationDoc.getStructureMap();
                Map<String, String> resultMap = consultationResultsDoc.getStructureMap();
                if (applicationMap != null && resultMap != null) {
                    String applicationDate = applicationMap.get("申请日期");
                    String resultDate = resultMap.get("会诊到达时间");
                    if (applicationDate != null && resultDate != null) {
                        Date date_in = StringUtil.parseDateTime(applicationDate);
                        Date date_out = StringUtil.parseDateTime(resultDate);
                        if (date_in == null || date_out == null) {
                            return;
                        }
                        long times = date_out.getTime() - date_in.getTime();
                        long day = times / (24 * 60 * 60 * 1000);
                        long hour = (times / (60 * 60 * 1000) - day * 24);
                        long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
                        long min1 = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
                        if (min1 > 10) {
                            status.set("-1");
                        }

                    }
                }
            }
        }*/

    }
}