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.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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

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

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getFirstCourseRecordDoc() == null) {
            status.set("0");
            return;
        }
        PresentLabel presentLabel = inputInfo.getBeHospitalizedDoc().getPresentLabel();
        DiagLabel initialDiagLabel = inputInfo.getBeHospitalizedDoc().getInitialDiagLabel();
        PastLabel pastLabel = inputInfo.getBeHospitalizedDoc().getPastLabel();
        if (initialDiagLabel == null) {
            status.set("0");
            return;
        }
        List<String> presentPastDiags = new ArrayList<>();
        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 (presentDiag.getHospitalDiagName().contains("心")
                                    || presentDiag.getHospitalDiagName().contains("糖")
                                    || presentDiag.getHospitalDiagName().contains("脑萎缩")
                                    || presentDiag.getHospitalDiagName().contains("慢性")
                                    || presentDiag.getHospitalDiagName().contains("血")) {
                                presentPastDiags.add(presentDiag.getHospitalDiagName());
                            }
                        }
                    }
                }
            }
        }
        /* 取既往史中疾病名称 */
        if (pastLabel != null) {
            List<Diag> pastDiags = pastLabel.getDiags();
            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 = concatInfo(infoStr, presentPastDiag);
                }
            } else {
                infoStr = concatInfo(infoStr, presentPastDiag);
            }
        }
        info.set(infoStr);
        if (matchSum == presentPastDiags.size()) {
            status.set("0");
        }
        /*
        if (initDiags.containsAll(presentPastDiags)) {
            status.set("0");
        }
         */
    }

    private String concatInfo(String infoStr, String presentPastDiag) {
        if (StringUtil.isBlank(infoStr)) {
            infoStr += presentPastDiag;
        } else {
            if (!infoStr.contains(presentPastDiag)) {
                infoStr += "," + presentPastDiag;
            }
        }
        return infoStr;
    }

    private void addDiagHospitalName(List<String> presentPastDiag, List<Diag> pastDiags) {
        for (Diag pastDiag : pastDiags) {
            if (pastDiag.getNegative() != null) {
                continue;
            }
            if (pastDiag.getHospitalDiagName().contains("心")
                    || pastDiag.getHospitalDiagName().contains("糖")
                    || pastDiag.getHospitalDiagName().contains("脑萎缩")
                    || pastDiag.getHospitalDiagName().contains("慢性")
                    || pastDiag.getHospitalDiagName().contains("血")) {
                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());
        }
    }
}
