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

import com.google.common.collect.ImmutableMap;
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.BeHospitalizedDoc;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.model.label.VitalLabelSpecial;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ClassName : BEH03051
 * @Description : 与诊断相关的主要体征未描述
 * @Author : 胡敬
 * @Date: 2020-07-28 09:52
 */
@Component
public class BEH03051 extends QCCatalogue {
    /**
     * 暂时先整理出以下诊断的体征
     * 用|分隔。
     * |前面的是且的关系，每个都要有
     * |后面的是或的关系，有一个就可以
     */

    Map<String, String> diagVital = ImmutableMap.<String, String>builder()
            .put("急性阑尾炎", "右下腹:专科查体无右下腹描述/反跳痛:专科查体无反跳痛描述|麦氏点:专科查体无麦氏点描述/麦氏点压痛/阑尾点")
            .put("冠状动脉粥样硬化性心脏病", "心率:专科查体无心率描述/律:专科查体无心律描述|瓣膜:专科查体无各瓣膜区听诊描述/杂音/二尖瓣/三尖瓣/主动脉瓣")
            .put("高血压病", "血压:专科查体无血压值描述")
            .put("高血压病1级", "血压:专科查体无血压值描述")
            .put("高血压病2级", "血压:专科查体无血压值描述")
            .put("高血压病3级", "血压:专科查体无血压值描述")
            .put("心房颤动", "心率:专科查体无心率描述/律:专科查体无心律描述|脉搏:专科查体无脉搏描述/心音:专科查体无心音描述")
            .build();

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getBeHospitalizedDoc() == null) {
            return;
        }

        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        DiagLabel initialDiagLabel = beHospitalizedDoc.getInitialDiagLabel();
        VitalLabelSpecial vitalLabelSpecial = beHospitalizedDoc.getVitalLabelSpecial();
        if (initialDiagLabel == null || vitalLabelSpecial == null) {
            return;
        }

        String vitalSpecialText = vitalLabelSpecial.getText();
        if (StringUtil.isBlank(vitalSpecialText)) {
            return;
        }

        List<Diag> initDiags = initialDiagLabel.getDiags();
        if (initDiags.size() > 0) {
            String firstInitDiag = initDiags.get(0).getHospitalDiagName();
            if (StringUtil.isNotBlank(firstInitDiag)) {
                if (diagVital.containsKey(firstInitDiag)) {
                    String vitals = diagVital.get(firstInitDiag);
                    String[] vitalArr = vitals.split("\\|");
                    String vitalAnd = vitalArr[0];//体征且的关系，每个必须有
                    for (String vitalInfo : vitalAnd.split("/")) {
                        String[] vitalInfoArr = vitalInfo.split(":");
                        String vital = "";
                        if (!vitalSpecialText.contains(vitalInfoArr[0])) {
                            status.set("-1");
                            if (vitalInfoArr.length > 1) {
                                vital = "：" + vitalInfoArr[1];
                            }
                            info.set(firstInitDiag + vital);
                            return;
                        }
                    }
                    if (vitalArr.length == 2) {
                        String vitalOr = vitalArr[1];//体征或的关系，有一个就行
                        int existNum = 0;
                        String vital = "";
                        for (String vitalInfo : vitalOr.split("/")) {
                            String[] vitalInfoArr = vitalInfo.split(":");
                            if (vitalSpecialText.contains(vitalInfoArr[0])) {
                                existNum++;
                            }
                            if (vitalInfoArr.length > 1) {
                                vital = "：" + vitalInfoArr[1];
                            }
                        }
                        if (existNum == 0) {
                            status.set("-1");
                            info.set(firstInitDiag + vital);
                        }
                    }
                }
            }
        }
    }
}