package com.lantone.qc.kernel.catalogue.ywzxyy.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.entity.PD;
import com.lantone.qc.pub.model.label.ChiefLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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


/**
 * @ClassName : BEH0001
 * @Description : 症状缺少时间描述
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class BEH0001 extends QCCatalogue {

    private List<String> containList = Arrays.asList("检查", "术后", "药物", "发现", "误服", "确诊", "经", "异常", "诊断"
            , "示", "超", "伤", "术", "复查", "体检", "血透", "血液透析");

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        //主诉缺少主症状不提示信息
        if (outputInfo != null && outputInfo.getResult() != null) {
            Map<String, Object> outputMap = outputInfo.getResult().get("BEH0009");
            if (outputMap != null && "-1".equals(outputMap.get("status"))) {
                status.set("0");
                return;
            }
        }

        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        ChiefLabel chiefLabel = inputInfo.getBeHospitalizedDoc().getChiefLabel();
        if (chiefLabel == null) {
            status.set("0");
            return;
        }
        String chiefText = chiefLabel.getText();
        if (StringUtil.isBlank(chiefText)) {
            status.set("0");
            return;
        }
        // 暂时逻辑，BEH0009执行在BEH0001之前时可以撤销
        boolean isExistence = true;
        List<Clinical> clinicals = chiefLabel.getClinicals();
        if (clinicals != null && clinicals.size() > 0) {
            isExistence = false;
        }
        for (String word : containList) {
            if (chiefText.contains(word)) {
                isExistence = false;
            }
        }
        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("复诊")) {
                    isExistence = false;
                    break;
                }
            }
        }
        //主诉缺少主症状不提示信息
        if (isExistence) {
            status.set("0");
            return;
        }

        if (StringUtil.isNotBlank(chiefText)) {
            if (chiefText.contains("检查") || chiefText.contains("术后") || chiefText.contains("药物")
                    || chiefText.contains("天") || chiefText.contains("周") || chiefText.contains("月")
                    || chiefText.contains("时") || chiefText.contains("秒") || chiefText.contains("半年")
                    || chiefText.contains("日") || chiefText.contains("今") || chiefText.contains("余年")
                    || chiefText.contains("年") || chiefText.contains("分") || isDate(chiefText)) {
                status.set("0");
                return;
            }
        } else {
            List<PD> pds = chiefLabel.getPds();
            if (pds != null && pds.size() > 0) {
                for (PD pd : pds) {
                    String pdName = pd.getName();
                    if (StringUtil.isNotBlank(pdName)) {
                        if (pdName.contains("天") || pdName.contains("周") || pdName.contains("月")
                                || pdName.contains("时") || pdName.contains("秒") || pdName.contains("半年")
                                || pdName.contains("日") || pdName.contains("今") || pdName.contains("余年")
                                || pdName.contains("年") || pdName.contains("分") || isDate(pdName)) {
                            status.set("0");
                            break;
                        }
                    }
                }
            }
       /*     List<Clinical> clinicals = inputInfo.getBeHospitalizedDoc().getChiefLabel().getClinicals();
            if (clinicals.size() > 0) {
                for (Clinical clinical : clinicals) {
                    String clinicalName = clinical.getName();
                    List<PD> timestamp = clinical.getTimestamp();
                    if (ListUtil.isEmpty(timestamp)) {
                        if (StringUtils.isEmpty(info.get())) {
                            info.set(clinicalName);
                        } else {
                            info.set(info.get() + "," + clinicalName);
                        }
                    }
                }
            }*/
        }
     /*   if (StringUtils.isNotEmpty(info.get())) {
            status.set("-1");
        }*/
    }

    private boolean isDate(String pdName) {
        String regex = "[0-9]{4}.?[0-9]{2}.?[0-9]{2}";
        return pdName.matches(regex);
    }
}