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

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.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.entity.GeneralDesc;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.model.label.PastLabel;
import com.lantone.qc.pub.model.label.PresentLabel;
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.*;

/**
 * @ClassName : FIRC0095
 * @Description : 初步诊断不完整
 * 和入院记录的初步诊断比较
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class FIRC0095 extends QCCatalogue {
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;

    private List<String> containList = Arrays.asList("脑萎缩", "慢性", "纤颤", "高血压", "糖尿", "冠状", "冠心病", "支架", "起搏器", "房颤", "风湿");
    private List<String> filterList = Arrays.asList("心脏病", "低血糖", "急性", ";");

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getFirstCourseRecordDoc() == null) {
            status.set("0");
            return;
        }
        PresentLabel presentLabel = inputInfo.getBeHospitalizedDoc().getPresentLabel();
        DiagLabel initialDiagLabel = inputInfo.getFirstCourseRecordDoc().getInitialDiagLabel();
        PastLabel pastLabel = inputInfo.getBeHospitalizedDoc().getPastLabel();
        if (initialDiagLabel == null) {
            status.set("0");
            return;
        }
        Set<String> presentPastDiags = new HashSet<>();
        if (presentLabel != null) {
            List<GeneralDesc> generals = presentLabel.getGenerals();
            if (generals.size() > 0) {
                String presentText = presentLabel.getText();
                List<Diag> presentDiags = presentLabel.getDiags();
                /* 取现病史中一般情况之后的疾病名称 */
                if (StringUtil.isNotBlank(presentText) && presentDiags.size() > 0) {
                    String lastGeneral = generals.get(generals.size() - 1).getName();
                    int lastGeneralIndex = presentText.indexOf(lastGeneral);
                    for (Diag presentDiag : presentDiags) {
                        if (presentDiag.getNegative() != null || presentDiag.getHospitalDiagName().contains("否认")) {
                            continue;
                        }
                        /* 现病史中一般情况之后的疾病名称 */
                        if (presentText.indexOf(presentDiag.getHospitalDiagName()) > lastGeneralIndex) {
                            if (isContains(presentDiag.getHospitalDiagName()) && !isFilter(presentDiag.getHospitalDiagName())) {
                                presentPastDiags.add(presentDiag.getHospitalDiagName());
                            }
                        }
                    }
                }
            }
        }
        /* 取既往史中疾病名称 */
        if (pastLabel != null && StringUtils.isNotEmpty(pastLabel.getText())) {
            List<Diag> pastDiags = pastLabel.getDiags();
            addDiagHospitalName(presentPastDiags, pastDiags);
        } else {            //结构化数据
            List<Diag> pastDiags = new ArrayList<>();
            Map<String, String> structureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
            String a1 = structureMap.get("高血压");
            if (StringUtils.isNotEmpty(a1) && !"否认".equals(a1)) {
                Diag diag = new Diag();
                diag.setName("高血压");
                diag.setHospitalDiagName("高血压");
                if (!isHave(presentPastDiags, "高血压")) {
                    pastDiags.add(diag);
                }
            }
            a1 = structureMap.get("糖尿病");
            if  (StringUtils.isNotEmpty(a1) && !"否认".equals(a1)) {
                Diag diag = new Diag();
                diag.setName("糖尿病");
                diag.setHospitalDiagName("糖尿病");
                if (!isHave(presentPastDiags, "糖尿病")) {
                    pastDiags.add(diag);
                }
            }
            addDiagHospitalName(presentPastDiags, pastDiags);
        }
        /* 取初步诊断中疾病名称 */
        List<String> initDiags = new ArrayList<>();
        List<Diag> initialDiagDiags = initialDiagLabel.getDiags();
        addInitDiagHospitalName(initDiags, initialDiagDiags);

        String infoStr = "";
        int matchSum = 0;
        ModelAI modelAI = new ModelAI();
        for (String presentPastDiag : presentPastDiags) {
            JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(presentPastDiag, initDiags, false
                    , "diagnose", chiefPresentSimilarityServiceClient);
            if (jsonArray.size() == 2) {
                /* 相似度最高症状 */
                String symptom = jsonArray.getString(0);
                /* 相似度分数 */
                double likeRate = jsonArray.getDoubleValue(1);
                if (likeRate > 0.85) {
                    matchSum++;
                } else {
                    infoStr = CatalogueUtil.concatInfo(infoStr, presentPastDiag);
                }
            } else {
                infoStr = CatalogueUtil.concatInfo(infoStr, presentPastDiag);
            }
        }
        info.set(infoStr);
        if (matchSum == presentPastDiags.size()) {
            status.set("0");
        }
        /*
        if (initDiags.containsAll(presentPastDiags)) {
            status.set("0");
        }
         */
    }

    private void addDiagHospitalName(Set<String> presentPastDiag, List<Diag> pastDiags) {
        for (Diag pastDiag : pastDiags) {
            if (pastDiag.getNegative() != null) {
                continue;
            }
            if (isContains(pastDiag.getHospitalDiagName()) && !isFilter(pastDiag.getHospitalDiagName())) {
                presentPastDiag.add(pastDiag.getHospitalDiagName());
            }
        }
    }

    private void addInitDiagHospitalName(List<String> presentPastDiag, List<Diag> pastDiags) {
        for (Diag pastDiag : pastDiags) {
            if (pastDiag.getNegative() != null) {
                continue;
            }
            presentPastDiag.add(pastDiag.getHospitalDiagName());
        }
    }

    private boolean isHave(Set<String> presentDiag, String diagName) {
        for (String haveDiag : presentDiag) {
            if (haveDiag.contains(diagName) || diagName.contains(haveDiag)) {
                return true;
            }
        }
        return false;
    }

    private boolean isContains(String diagName) {
        for (String c : containList) {
            if (diagName.contains(c)) {
                return true;
            }
        }
        return false;
    }

    private boolean isFilter(String diagName) {
        for (String c : filterList) {
            if (diagName.contains(c)) {
                return true;
            }
        }
        return false;
    }
}
