package com.lantone.qc.kernel.catalogue.ninghaiyiyi.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.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.DateUtil;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 * @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<ConsultationResultsDoc> consultationResultsDocs = ListUtil.newArrayList();//会诊结果单
        List<ConsultationApplicationDoc> consultationApplicationDocs = ListUtil.newArrayList();//会诊申请单
        for (ConsultationDoc consultationDoc : inputInfo.getConsultationDocs()) {
            if (consultationDoc.getConsultationResultsDoc() != null) {
                consultationResultsDocs.add(consultationDoc.getConsultationResultsDoc());
            }
            if (consultationDoc.getConsultationApplicationDoc() != null) {
                consultationApplicationDocs.add(consultationDoc.getConsultationApplicationDoc());
            }
        }
        Iterator<ConsultationResultsDoc> consultationResultsDocIterator = consultationResultsDocs.iterator();
        Iterator<ConsultationApplicationDoc> consultationApplicationDocIterator = consultationApplicationDocs.iterator();
        while (consultationResultsDocIterator.hasNext()) {
            if (!CatalogueUtil.compareTime(
                    StringUtil.parseDateTime(consultationResultsDocIterator.next().getStructureMap().get("记录日期")),
                    StringUtil.parseDateTime(DateUtil.nowString()),
                    Long.valueOf(24 * 60))) {//如果接收未超过24小时，规则不判断
                consultationResultsDocIterator.remove();
            }
        }
        while (consultationApplicationDocIterator.hasNext()) {
            if (!CatalogueUtil.compareTime(
                    StringUtil.parseDateTime(consultationApplicationDocIterator.next().getStructureMap().get("会诊时间")),
                    StringUtil.parseDateTime(DateUtil.nowString()),
                    Long.valueOf(24 * 60))) {//如果接收未超过24小时，规则不判断
                consultationResultsDocIterator.remove();
            }
        }
        Collections.sort(consultationResultsDocs, new Comparator<ConsultationResultsDoc>() {
            public int compare(ConsultationResultsDoc o1, ConsultationResultsDoc o2) {
                return o1.getStructureMap().get("记录日期")
                        .compareTo(o2.getStructureMap().get("记录日期"));
            }
        });
        Collections.sort(consultationApplicationDocs, new Comparator<ConsultationApplicationDoc>() {
            public int compare(ConsultationApplicationDoc o1, ConsultationApplicationDoc o2) {
                return o1.getStructureMap().get("会诊时间")
                        .compareTo(o2.getStructureMap().get("会诊时间"));
            }
        });
        if (consultationResultsDocs.size() != consultationApplicationDocs.size()) {
            status.set("-1");
            return;
        }
        for (ConsultationApplicationDoc consultationApplicationDoc : consultationApplicationDocs) {
            for (ConsultationResultsDoc consultationResultsDoc : consultationResultsDocs) {
                if (consultationResultsDoc.getStructureMap().get("会诊医师").contains(consultationApplicationDoc.getStructureMap().get("签名")) && CatalogueUtil.compareTime(
                        StringUtil.parseDateTime(consultationApplicationDoc.getStructureMap().get("记录日期")),
                        StringUtil.parseDateTime(consultationResultsDoc.getStructureMap().get("会诊时间")),
                        Long.valueOf(1))) {//结果单时间要比申请单大的
                    if (CatalogueUtil.compareTime(
                            StringUtil.parseDateTime(consultationApplicationDoc.getStructureMap().get("记录日期")),
                            StringUtil.parseDateTime(consultationResultsDoc.getStructureMap().get("会诊时间")),
                            Long.valueOf(24 * 60))) {//未超过24小时
                        status.set("-1");
                        return ;
                    }
                }
            }
        }
        /*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("会诊到达时间");
        }*/
    }
}