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

import com.alibaba.fastjson.JSONArray;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.client.ChiefPresentSimilarityServiceClient;
import com.lantone.qc.kernel.structure.ai.ModelAI;
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.Clinical;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @ClassName : BEH0449
 * @Description : 现病史主症状与主诉症状不一致
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class BEH0449 extends QCCatalogue {
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        String chief_text = beHospitalizedDoc.getChiefLabel().getText();
        String Present_text = beHospitalizedDoc.getPresentLabel().getText();
        if (chief_text.contains("检查") || chief_text.contains("术后") || chief_text.contains("药物")
                || chief_text.contains("发现") || chief_text.contains("体检") || chief_text.contains("确诊")
                || chief_text.contains("复查") || chief_text.contains("诊断") || StringUtil.isBlank(chief_text)
                || StringUtil.isBlank(Present_text)
        ) {
            status.set("0");
            return;
        } else {
            List<Clinical> chiefClinicals = beHospitalizedDoc.getChiefLabel().getClinicals();
            List<Clinical> presentClinicals = beHospitalizedDoc.getPresentLabel().getClinicals();
            if (ListUtil.isNotEmpty(chiefClinicals) && ListUtil.isNotEmpty(presentClinicals)) {
                Clinical chiefClinical = chiefClinicals.get(0);
                String chiefClinic = chiefClinical.getName();
                /* 去除阴性症状，只保留阳性症状 */
                presentClinicals = presentClinicals.stream().filter(clinical -> clinical.getNegative() == null).collect(Collectors.toList());
                for (Clinical cliPre : presentClinicals) {
                    String presentClinical = cliPre.getName();
                    if (chiefClinic.equals(presentClinical)) {
                        status.set("0");
                        return;
                    } else if (presentClinical.contains(chiefClinic) || chiefClinic.contains(presentClinical)) {
                        status.set("0");
                        return;
                    }
                }
                List<String> clinicName = getClinicName(presentClinicals);
                /* 文本相似度模型 */
                ModelAI modelAI = new ModelAI();
                JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(chiefClinic, clinicName,
                        false, "chief_present", chiefPresentSimilarityServiceClient);
                if (jsonArray.size() == 2) {
                    double likeRate = jsonArray.getDoubleValue(1);
                    if (likeRate > 0.9) {
                        status.set("0");
                        return;
                    }
                }
            }
        }

        String chiefDiag = "";
        List<Diag> diags = beHospitalizedDoc.getChiefLabel().getDiags();
        if (diags != null && diags.size() > 0) {
            chiefDiag = diags.get(0).getHospitalDiagName();
        }
        String presentDiag = "";
        diags = beHospitalizedDoc.getPresentLabel().getDiags();
        if (diags != null && diags.size() > 0) {
            presentDiag = diags.get(0).getHospitalDiagName();
        }
        if (StringUtils.isNotEmpty(chiefDiag) && StringUtils.isNotEmpty(presentDiag) && chiefDiag.equals(presentDiag)) {
            status.set("0");
        }
    }

    private List<String> getClinicName(List<Clinical> presentClinicals) {
        List<String> clinicNames = new ArrayList<>();
        for (Clinical clinical : presentClinicals) {
            clinicNames.add(clinical.getName());
        }
        return clinicNames;
    }
}