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.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : CON0280
 * @Description : 普通会诊未在24小时内完成
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class CON0280 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getConsultationDocs() == null || inputInfo.getDoctorAdviceDocs().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();
            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, (long) (24 * 60))) {
                        status.set("-1");
                        return;
                    }
                }
            }
            //            String applicationDateStr = structureMap.get("会诊申请日期");
            //            String arrivalDateStr = structureMap.get("会诊到达时间");
        }
    }
}