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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
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.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 危急值无报告处理记录
 * @author: 胡敬
 * @time: 2020/8/12 14:18
 */
@Component
public class CRI03062 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;
        }

        List<CrisisValueReportDoc> crisisValueReportDocs = inputInfo.getCrisisValueReportDocs();
        //如果有危急值结构化数据 但无文书则直接提醒无危急值报告
        if (crisisValueReportDocs == null || crisisValueReportDocs.size() == 0) {
            status.set("-1");
            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;
            }
            int crisisMatchSum = 0;
            for (CrisisValueReportDoc crisisValueReportDoc : crisisValueReportDocs) {
                String docReptContent = crisisValueReportDoc.getStructureMap().get("危急值记录内容");
                if (!docReptContent.contains(crisisName) && !docReptContent.contains(crisisValue)) { //危急值报告内容不包含危急值内容，跳过
                    crisisMatchSum++;
                }
            }

            if (crisisMatchSum == crisisValueReportDocs.size()) {//该条危急值记录没有报告
                findCrisises.add(crisisName + "：" + crisisValue + "->" + reptTimeStr);
            }
        }

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