package com.lantone.qc.kernel.catalogue.ninghaiyiyi.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.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();
        int timeCha = 21600000;
        String[] dateFormats = new String[]{"yyyy年MM月dd日HH时mm分", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm"};
        for (CrisisInfoDoc crisisInfoDoc : crisisInfoDocs) {
            String reptTime = crisisInfoDoc.getStructureMap().get("报告时间");
            if (StringUtils.isNotEmpty(reptTime)) {
                if (currentDate.getTime() - StringUtil.parseDateTime(reptTime, dateFormats).getTime() > timeCha) {
                    isOutTime = true;
                }
            }
        }
        ;
        if (isOutTime == false) {   //所有文书都未超过6小时，规则直接通过
            return;
        }

        List<CrisisValueReportDoc> crisisValueReportDocs = inputInfo.getCrisisValueReportDocs();
        //如果有危急值结构化数据 但无文书则直接提醒无危急值报告
        if (crisisValueReportDocs == null || crisisValueReportDocs.size() == 0) {
            status.set("-1");
            return;
        }
        List<String> findCrisises = new ArrayList<>();
        List<String> allCrisises = new ArrayList<>();
        crisisInfoDocs.forEach(crisisInfoDoc -> {
            String reptTime = crisisInfoDoc.getStructureMap().get("报告时间");
            String crisisName = crisisInfoDoc.getStructureMap().get("危急结果值");
            String crisisNm = "";
            String companyNum = "";
            if (crisisName.contains("项目为")) {
                crisisName = crisisName.substring(crisisName.indexOf("项目为") + 3);
            }
            if (crisisName.contains(",结果：")) {
                crisisNm = crisisName.split(",结果：")[0];
                companyNum = crisisName.split(",结果：")[1];
                if (StringUtil.isNotBlank(companyNum) && companyNum.contains("单位：")) {
                    companyNum = companyNum.replaceAll("单位：", "");
                }
            }
            allCrisises.add(reptTime);
            if (StringUtils.isNotEmpty(reptTime)) {
                //当前时间和报告时间未超过6小时，规则通过
                if (currentDate.getTime() - StringUtil.parseDateTime(reptTime, dateFormats).getTime() < timeCha) {
                    findCrisises.add(reptTime);
                } else {
                    for (CrisisValueReportDoc crisisValueReportDoc : crisisValueReportDocs) {
                        String recordTimeStr = crisisValueReportDoc.getStructureMap().get("病历日期");
                        String docReptContent = crisisValueReportDoc.getStructureMap().get("病情分析及处理");
                        if ((StringUtil.parseDateTime(recordTimeStr, dateFormats).getTime() - StringUtil.parseDateTime(reptTime, dateFormats).getTime()) < timeCha
                                && (StringUtil.removeBlank(docReptContent).contains(StringUtil.removeBlank(crisisName)) ||
                                (StringUtil.isNotBlank(crisisNm) && StringUtil.isNotBlank(companyNum)
                                        && StringUtil.removeBlank(docReptContent).contains(StringUtil.removeBlank(crisisNm))
                                        && StringUtil.removeBlank(docReptContent).contains(StringUtil.removeBlank(companyNum))
                                ))) {
                            findCrisises.add(reptTime);
                            break;
                        }
                    }
                }
            }
        });
        // (StringUtil.parseDateTime(recordTimeStr, dateFormats).getTime() - StringUtil.parseDateTime(reptTime, dateFormats).getTime()) > 0
        //                                &&
        if (findCrisises.size() != allCrisises.size()) {
            status.set("-1");
            allCrisises.forEach(reptTime -> {
                String criticalValueName = "";
                if (!findCrisises.contains(reptTime) && !info.get().contains(reptTime)) {
                    for (CrisisInfoDoc crisisInfoDoc : crisisInfoDocs) {
                        if (reptTime.equals(crisisInfoDoc.getStructureMap().get("报告时间"))) {
                            criticalValueName = crisisInfoDoc.getStructureMap().get("危急值名称");
                        }
                    }
                    if (StringUtils.isEmpty(info.get())) {
                        info.set(reptTime + " 危急值名称(" + criticalValueName + ")");
                    } else {
                        info.set(info.get() + "； " + reptTime + " 危急值名称(" + criticalValueName + ")");
                    }
                }
            });
        }
    }

}