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

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.CrisisInfoDoc;
import com.lantone.qc.pub.model.doc.CrisisValueReportDoc;
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.Date;
import java.util.List;

/**
 * @Description: 接到危急值报告后6小时内无病程记录
 * @author: rengb
 * @time: 2020/3/19 19:54
 */
@Component
public class CRI0382 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        //无危急值结构化信息
        List<CrisisInfoDoc> crisisInfoDocs = inputInfo.getCrisisInfoDocs();
        if (crisisInfoDocs == null || crisisInfoDocs.size() == 0) {
            return;
        }
        //有结构化信息但无危急值文书，如果报告时间都未超过6小时 允许无文书
        boolean isOutTime = false;
        Date currentDate = new Date();
        for (CrisisInfoDoc crisisInfoDoc : crisisInfoDocs) {
            String reptTime = crisisInfoDoc.getStructureMap().get("报告时间");
            if (StringUtils.isNotEmpty(reptTime)) {
                if (CatalogueUtil.compareTime(StringUtil.parseDateTime(reptTime), currentDate, 6 * 60L)) {
                    isOutTime = true;
                    break;
                }
            }
        }
        if (!isOutTime) {   //所有文书都未超过6小时，规则直接通过
            return;
        }

        List<CrisisValueReportDoc> crisisValueReportDocs = inputInfo.getCrisisValueReportDocs();
        //如果有危急值结构化数据 但无文书则直接提醒无危急值报告
        if (crisisValueReportDocs == null || crisisValueReportDocs.size() == 0) {
            return;
        }
        List<String> findCrisises = new ArrayList<>();
        for (CrisisInfoDoc crisisInfoDoc : crisisInfoDocs) {
            String reptTimeStr = crisisInfoDoc.getStructureMap().get("报告时间");
            String crisisName = crisisInfoDoc.getStructureMap().get("危急值名称");
            String crisisValue = crisisInfoDoc.getStructureMap().get("危急结果值");
            crisisName = crisisName == null ? "" : crisisName.split("\\[|\\(|\\（")[0];
            if (StringUtil.isBlank(reptTimeStr)) {
                continue;
            }
            Date reptTime = StringUtil.parseDateTime(reptTimeStr);//报告时间
            //当前时间和报告时间未超过6小时，规则通过
            if (!CatalogueUtil.compareTime(reptTime, currentDate, 6 * 60L)) {
                continue;
            }
            for (CrisisValueReportDoc crisisValueReportDoc : crisisValueReportDocs) {
                String recordTimeStr = crisisValueReportDoc.getStructureMap().get("记录时间");
                String docReptContent = crisisValueReportDoc.getStructureMap().get("危急值记录内容");
                if (StringUtil.isBlank(recordTimeStr)) {
                    continue;
                }
                Date recordTime = StringUtil.parseDateTime(recordTimeStr);//记录时间
                if (!docReptContent.contains(crisisName)) { //危急值报告内容不包含危急值内容，跳过
                    continue;
                }
                if (recordTime.before(reptTime)) {//记录时间比报告早，跳过
                    continue;
                }
                if (docReptContent.contains(crisisValue) && CatalogueUtil.compareTime(recordTime, reptTime, 6 * 60L)) {//报告时间和记录时间超过6小时，存该报告时间
                    findCrisises.add(crisisName + "：" + crisisValue + "->" + reptTimeStr);
                    break;
                }
            }
        }

        if (findCrisises.size() > 0) {
            status.set("-1");
            info.set(findCrisises.toString().replaceAll("[\\[\\]]", ""));
        }
    }
}