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.client.SimilarityServiceClient;
import com.lantone.qc.kernel.structure.ai.ModelAI;
import com.lantone.qc.kernel.util.CatalogueUtil;
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.model.label.DiagLabel;
import com.lantone.qc.pub.util.ListUtil;
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;
import java.util.Map;

/**
 * @ClassName : FIRP0178
 * @Description :出院其他诊断不完全
 * @Author : 胡敬
 * @Date: 2020-03-16 14:01
 */
@Component
public class FIRP0178 extends QCCatalogue {
    @Autowired
    SimilarityServiceClient similarityServiceClient;
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getFirstPageRecordDoc() != null && inputInfo.getLeaveHospitalDoc() != null) {
            Map<String, Object> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureExtMap();
            List<Map<String, String>> dischargeDiag = (List) firstpageStructureMap.get(Content.dischargeDiag);
            if (ListUtil.isEmpty(dischargeDiag)) {
                return;
            }
            /* 病案首页出院小结诊断 除去主诊断*/
            List<String> firstpageLeaveDiags = getFirstPageDiag(dischargeDiag);

            DiagLabel leaveDiagLabel = inputInfo.getLeaveHospitalDoc().getLeaveDiagLabel();
            if (leaveDiagLabel == null) {
                return;
            }
            List<Diag> leaveDiags = leaveDiagLabel.getDiags();
            List<String> leaveDiagsStr = putDiagToList(leaveDiags);

            /* 修改为疾病相似度模型 */
            ModelAI modelAI = new ModelAI();
            String infoStr = "";
            //最主要是给医生提醒，看是否有出院诊断漏写
            for (String leaveDiag : leaveDiagsStr) {
                JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(leaveDiag, firstpageLeaveDiags, false
                        , "diagnose", chiefPresentSimilarityServiceClient);
                if (jsonArray.size() == 2) {
                    /* 相似度分数 */
                    double likeRate = jsonArray.getDoubleValue(1);
                    if (likeRate < 0.9) {
                        infoStr = CatalogueUtil.concatInfo(infoStr, leaveDiag);
                    }
                } else if (jsonArray.size() == 0) {
                    infoStr = CatalogueUtil.concatInfo(infoStr, leaveDiag);
                }
            }
            if (StringUtil.isNotBlank(infoStr)) {
                status.set("-1");
                info.set(infoStr);
            }
        }
    }

    private List<String> getFirstPageDiag(List<Map<String, String>> dischargeDiag) {
        List<String> firstpageDiag = new ArrayList<>();
        for (int i = 0; i < dischargeDiag.size(); i++) {
            if ("门诊诊断".equals(dischargeDiag.get(i).get("诊断类别"))) {
                continue;
            }
            String diagnoseName = dischargeDiag.get(i).get(Content.diagnoseName);
            if (StringUtil.isBlank(diagnoseName) || "-".equals(diagnoseName)) { // 诊断名称为空不算
                continue;
            }
            // 去重
            if (!firstpageDiag.contains(diagnoseName)) {
                firstpageDiag.add(diagnoseName);
            }
        }
        return firstpageDiag;
    }

    private List<String> putDiagToList(List<Diag> diagList) {
        List<String> diags = new ArrayList<>();
        for (Diag diag : diagList) {
            // 去重
            if (!diags.contains(diag.getHospitalDiagName())) {
                diags.add(diag.getHospitalDiagName());
            }
        }
        return diags;
    }
}