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

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.entity.Clinical;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.label.ChiefLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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


/**
 * @ClassName : BEH0009
 * @Description :主诉中缺少主症状
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class BEH0009 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        Map<String, Integer> indexMap = new HashMap<>();
        ChiefLabel chiefLabel = inputInfo.getBeHospitalizedDoc().getChiefLabel();
        if (chiefLabel == null) {
            status.set("0");
            return;
        }
        String chiefText = chiefLabel.getText();
        if (StringUtil.isNotBlank(chiefText) &&
                (chiefText.contains("检查") || chiefText.contains("术后") || chiefText.contains("药物") || chiefText.contains("发现") || chiefText.contains("误服"))) {
            status.set("0");
        } else {
            List<Diag> diags = chiefLabel.getDiags();
            if (diags != null && diags.size() > 0) {
                for (Diag diag : diags) {
                    String hospitalDiagName = diag.getHospitalDiagName();
                    if (hospitalDiagName.contains("术后") || hospitalDiagName.contains("癌") ||
                            hospitalDiagName.contains("瘤") || hospitalDiagName.contains("复诊")) {
                        status.set("0");
                        break;
                    }
                }
            }
            if (StringUtil.isNotBlank(chiefText) && status.get().equals("-1")) {
                List<Clinical> clinicals = chiefLabel.getClinicals();
                if (clinicals.size() > 0) {
                    /* 主诉中没有伴字，有症状即可 */
                    if (!chiefText.contains("伴")){
                        status.set("0");
                        return;
                    }
                    for (Clinical clinical : clinicals) {
                        indexMap.put(clinical.getName(), chiefText.indexOf(clinical.getName()));
                    }
                }
                int index_ban = chiefText.indexOf("伴");
                if (indexMap.size() > 0) {
                    if (index_ban == -1) {
                        status.set("0");
                    } else {
                        for (Map.Entry<String, Integer> s : indexMap.entrySet()) {
                            Integer value = s.getValue();
                            if (value < index_ban) {
                                status.set("0");
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}