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.ConsultationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationResultsDoc;
import com.lantone.qc.pub.util.DateUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @ClassName : CON0281
 * @Description : 急会诊未在十分钟内到达
 * @Author : 王宇
 * @Date: 2020-03-06 17:28
 */
@Component
public class CON0281 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        DateUtil dateUtil = new DateUtil();
        if (inputInfo.getConsultationDocs() == null) {
            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("医嘱开始时间"));
            }
        });
        StringBuffer stringBuffer = new StringBuffer();
        for (DoctorAdviceDoc doctorAdviceDoc : doctorAdviceDocs) {//循环取最近一条医嘱和会诊申请单中的急会诊比较
            for (ConsultationDoc consultationDoc : consultationDocs) {
                if(consultationDoc.getConsultationRecordDoc() == null || doctorAdviceDocs == null){
                    continue;
                }
                if(doctorAdviceDoc.getStructureMap().get("医嘱项目名称") != null && doctorAdviceDoc.getStructureMap().get("医嘱项目名称").contains("会诊")){
                    if(consultationDoc.getConsultationResultsDoc().getStructureMap().get("会诊分类") == null && consultationDoc.getConsultationResultsDoc().getStructureMap().get("会诊分类").contains("急会诊")){
                        if(dateUtil.getDistanceTime3(dateUtil.parseDate(doctorAdviceDoc.getStructureMap().get("医嘱开始时间"),DateUtil.DATE_TIME_FORMAT)
                                ,dateUtil.parseDate(consultationDoc.getConsultationResultsDoc().getStructureMap().get("会诊时间"),DateUtil.DATE_TIME_FORMAT)) > 10){//如果医嘱中的日期和会诊结果中的日期大于十分钟
                            status.set("-1");
                        }else {
                            stringBuffer.append(doctorAdviceDoc.getStructureMap().get("医嘱项目名称")
                                    +":"
                                    +doctorAdviceDoc.getStructureMap().get("医嘱开始时间")
                                    +"会诊时间："
                                    +consultationDoc.getConsultationResultsDoc().getStructureMap().get("会诊时间"));
                            continue;
                        }
                    }
                }

            }
        }
        info.set(stringBuffer.toString());

    }
}