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

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.Content;
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.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.Map;
import java.util.stream.Collectors;

/**
 * @ClassName : FIRP0173
 * @Description :门（急）诊诊断与现病史里的内容不一致
 * 逻辑：门（急）诊诊断的第一个诊断要在现病史中“拟”、“以”后面的诊断中出现
 * @Author : 胡敬
 * @Date: 2020-03-18 16:30
 */
@Component
public class FIRP0173 extends QCCatalogue {
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getFirstPageRecordDoc() == null || inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        if (inputInfo.getBeHospitalizedDoc().getPresentLabel() != null) {
            Map<String, Object> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureExtMap();
            //病案首页门急诊诊断
            String outpatientEmergencyDiag = (String) firstpageStructureMap.get(Content.outpatientEmergencyDiag);
            if (StringUtils.isEmpty(outpatientEmergencyDiag)) {
                status.set("0");
            }
            //现病史所有诊断
            List<Diag> presentDiags = inputInfo.getBeHospitalizedDoc().getPresentLabel().getDiags();

            /* 现病史阳性诊断与病案首页门急诊诊断走相似度模型 */
            boolean isSimilar = false;
            List<Diag> presentNotNegDiag = presentDiags.stream().filter(diag -> diag.getNegative() == null).collect(Collectors.toList());
            List<String> presentNotNegDiags = getStringList(presentNotNegDiag);
            for (String diag : outpatientEmergencyDiag.split(" ")) {
                isSimilar = isSimilar(diag, presentNotNegDiags);
                if (isSimilar) {
                    status.set("0");
                    return;
                }
            }

            for (Diag diag : presentDiags) {
                if (diag.getNegative() != null) {
                    continue;
                }
                if (diag.getHospitalDiagName().equals(outpatientEmergencyDiag)) {
                    status.set("0");
                    return;
                }
            }

            List<Diag> presentPossibleDiag = presentDiags.stream().filter(diag -> diag.getPossible() != null).collect(Collectors.toList());
            if (presentPossibleDiag.size() > 0) {
                if (presentPossibleDiag.get(0).getHospitalDiagName().equals(outpatientEmergencyDiag)) {
                    status.set("0");
                }
            }
            /* 现病史可能诊断与病案首页门急诊诊断走相似度模型 */
            List<String> presentPossibleDiags = getStringList(presentPossibleDiag);
            for (String diag : outpatientEmergencyDiag.split(" ")) {
                isSimilar = isSimilar(diag, presentPossibleDiags);
                if (isSimilar) {
                    status.set("0");
                    return;
                }
            }
        }
    }

    private boolean isSimilar(String diag1, List<String> diagList) {
        ModelAI modelAI = new ModelAI();
        JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(diag1, diagList, false
                , "diagnose", chiefPresentSimilarityServiceClient);
        if (jsonArray.size() == 2) {
            /* 相似度分数 */
            double likeRate = jsonArray.getDoubleValue(1);
            return likeRate > 0.8;
        }
        return false;
    }

    private List<String> getStringList(List<Diag> diags) {
        List<String> diagStringList = new ArrayList<>();
        for (Diag diag : diags) {
            String hospitalDiagName = diag.getHospitalDiagName();
            if (StringUtil.isBlank(hospitalDiagName)) {
                continue;
            }
            diagStringList.add(hospitalDiagName);
        }
        return diagStringList;
    }
}