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.Cause;
import com.lantone.qc.pub.model.entity.Clinical;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.label.PresentLabel;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;


/**
 * @ClassName : BEH0014
 * @Description :  现病史症状缺少诱因
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class BEH0014 extends QCCatalogue {
    private List<String> containList = Arrays.asList("体检", "发现", "检查", "因", "确诊", "诊断", "复查", "术后"
            , "药物", "误服", "查", "撞", "伤", "月经", "暴力", "超", "术");

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        /*List<Clinical> clinicals = inputInfo.getBeHospitalizedDoc().getPresentLabel().getClinicals();
        if(clinicals.size()>0){
            Clinical clinical = clinicals.get(0);
            String clinicalName = clinical.getName();
            Cause cause = clinical.getCause();
            if(cause == null){
                info.set(clinicalName);
            }else {
                status.set("0");
            }
        }*/
        PresentLabel presentLabel = inputInfo.getBeHospitalizedDoc().getPresentLabel();
        if (presentLabel == null || StringUtil.isBlank(presentLabel.getText())) {
            status.set("0");
            return;
        }
        List<Clinical> clinicals = presentLabel.getClinicals();
        if (ListUtil.isNotEmpty(clinicals)) {
            for (Clinical clinical : clinicals) {
                if (clinical.getCause() != null) {
                    status.set("0");
                    return;
                }
            }
        }
        //硬规则匹配 前30个字有"体检"或"发现"或"检查"(containList)字样
        String present = presentLabel.getText();
        if (StringUtils.isNotEmpty(present)) {
            if (present.length() > 30) {
                present = present.substring(0, 30);
            }
            present = present.replaceAll("[\"“”]", "");
            for (String word : containList) {
                if (present.contains(word)) {
                    status.set("0");
                    return;
                }
            }
            //模型在现病史前30个字里能提出来疾病，那也可以算有诱因
            List<Diag> presentDiags = presentLabel.getDiags();
            if (presentDiags.size() > 0) {
                for (Diag diag : presentDiags) {
                    String hospitalDiagName = diag.getHospitalDiagName();
                    if (StringUtil.isBlank(hospitalDiagName)) {
                        continue;
                    }
                    if (present.contains(hospitalDiagName)) {
                        status.set("0");
                        return;
                    }
                }
            }
        }

        List<Cause> causes = presentLabel.getCauses();
        if (causes != null && causes.size() > 0) {
            status.set("0");
        }
    }
}