package com.lantone.qc.kernel.catalogue.ywzxyy.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.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) {
            String mainDiagnosis = "";
            Map<String, Object> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureExtMap();
            List<Map<String, String>> dischargeDiag = (List) firstpageStructureMap.get(Content.dischargeDiag);
            if (ListUtil.isEmpty(dischargeDiag)) {
                return;
            }
            if (dischargeDiag.get(0) != null) {
                mainDiagnosis = dischargeDiag.get(0).get("诊断名称");
            }

            /* 病案首页出院小结诊断 除去主诊断*/
            List<String> firstpageLeaveDiags = getFirstPageDiag(dischargeDiag);
            if (ListUtil.isEmpty(firstpageLeaveDiags)) {
                return;
            }

            /*DiagLabel leaveDiagLabel = inputInfo.getLeaveHospitalDoc().getLeaveDiagLabel();
            if (leaveDiagLabel == null) {
                return;
            }
            List<Diag> leaveDiags = leaveDiagLabel.getDiags();
            List<String> leaveDiagsStr = putDiagToList(leaveDiags);*/
            //出院小结得出院诊断，模型提取出现问题，需要手动以1.2.3.为界限切开
            Map<String, String> leavehospitalStructureMap = inputInfo.getLeaveHospitalDoc().getStructureMap();

            String leaveDiag = leavehospitalStructureMap.get("出院诊断");
            List<String> leaveDiagsStr = cut(leaveDiag, mainDiagnosis);

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

    private List<String> cut(String leaveDiag, String mainDiagnosis) {
        List<String> diags = new ArrayList<>();
        if (leaveDiag != null) {
            String[] leaveDiags = leaveDiag.split("，| , |、");
            for (String string : leaveDiags) {
                if (!string.isEmpty()) {
                    if (string.equals(mainDiagnosis)) {
                        diags.add(0, string);
                    } else {
                        // 去重
                        if (!diags.contains(string)) {
                            diags.add(string);
                        }
                    }
                }
            }
        }
        return diags;
    }
}