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

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.BeHospitalizedDoc;
import com.lantone.qc.pub.model.doc.DeathRecordDoc;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
import com.lantone.qc.pub.model.label.ChiefLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 出院记录内容缺主诉
 * @author: rengb
 * @time: 2020/3/10 13:53
 */
@Component
public class LEA0149 extends QCCatalogue {
    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        if (leaveHospitalDoc == null || beHospitalizedDoc == null) {
            status.set("0");
            return;
        }
        DeathRecordDoc deathRecordDoc = inputInfo.getDeathRecordDoc();
        if (deathRecordDoc != null) {
            status.set("0");
            return;
        }
        Map<String, String> lhStructureMap = leaveHospitalDoc.getStructureMap();
        ChiefLabel chiefLabel = beHospitalizedDoc.getChiefLabel();
        if (lhStructureMap != null && chiefLabel != null) {
            String bhChief = CatalogueUtil.int2ChineseNum(chiefLabel.getText().replaceAll("[\\p{Punct}\\pP。-]", ""));
            String leaveChief = lhStructureMap.get("主诉");
            String bhThings = lhStructureMap.get("入院情况");
            String reThings = lhStructureMap.get("入院原因");   //邵逸夫主诉在入院原因内
            if (StringUtil.isNotBlank(bhThings)) {
                bhThings = CatalogueUtil.int2ChineseNum(bhThings.replaceAll("[\\p{Punct}\\pP。-]", ""));
            }
            if (StringUtil.isNotBlank(reThings)) {
                reThings = CatalogueUtil.int2ChineseNum(reThings.replaceAll("[\\p{Punct}\\pP。-]", ""));
            }

            //如果出院小结结构化数据能取出主诉，则直接用该主诉和入院记录主诉比较
            if (StringUtil.isNotBlank(leaveChief)) {
                leaveChief = CatalogueUtil.int2ChineseNum(leaveChief.replaceAll("[\\p{Punct}\\pP。-]", ""));
                if (bhChief.equals(leaveChief)) {
                    status.set("0");
                    return;
                }
            }
            if (StringUtil.isNotBlank(bhThings) && bhThings.replace(" ", "").contains(bhChief.replace(" ", ""))) {
                status.set("0");
                return;
            }
            //邵逸夫主诉写在入院原因
            if (StringUtil.isNotBlank(reThings) && reThings.replace(" ", "").contains(bhChief.replace(" ", ""))) {
                status.set("0");
                return;
            }
            if(StringUtils.isNotBlank(bhThings)){
                Pattern compile = Pattern.compile("(?<=2.).*(?=3.体格检查)");
                Matcher matcher = compile.matcher(bhThings);
                while (matcher.find()) {
                    String number = matcher.group(0);
                    if (CatalogueUtil.isEmpty(number)) {
                        status.set("-1");
                    }
                }
                //处理台州的
                if (bhThings.contains("患者因")) {
                    status.set("0");
                }
            }



            //                    bhThings = bhThings.replaceAll("[\\p{Punct}\\pP]", "");
            //                    if (bhThings.indexOf(bhChief) < 0) {
            //                        status.set("-1");
            //                    }
        }
    }
}