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

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
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.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;

/**
 * @Description: 出院记录出院诊断与首页出院诊断不一致
 * @author: 胡敬
 * @time: 2020/3/27 15:48
 */
@Component
public class LEA0514 extends QCCatalogue {
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;

    List<String> diags = Lists.newArrayList("2型糖尿病");

    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);
            //出院小结诊断
            List<Diag> leaveDiags = null;
            if (inputInfo.getLeaveHospitalDoc().getLeaveDiagLabel() != null) {
                leaveDiags = inputInfo.getLeaveHospitalDoc().getLeaveDiagLabel().getDiags();
            }
            if (ListUtil.isEmpty(dischargeDiag) || ListUtil.isEmpty(leaveDiags)) {
                return;
            }
            String firstpageleaveDiag = "";
            for (Map<String, String> diag : dischargeDiag) {
                String diagCategory = diag.get("诊断类别");
                if (StringUtil.isNotBlank(diagCategory) && "主要诊断".equals(diagCategory)) {
                    firstpageleaveDiag = diag.get(Content.diagnoseName);
                    break;
                }
            }
            String leaveDiag = leaveDiags.get(0).getHospitalDiagName();
            //判断是否有2型糖尿病这种病如果没有，把疾病前的数字去掉
            if (!diags.contains(leaveDiag)) {
                leaveDiag = leaveDiag.replaceAll("^[0-9]", "");
            }
            /* 存放出院小结主要诊断,为了调用疾病相似度接口，特意存成list格式 */
            List<String> firstLeaveDiag = new ArrayList<>();
            firstLeaveDiag.add(leaveDiag);
            ModelAI modelAI = new ModelAI();
            JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(firstpageleaveDiag, firstLeaveDiag, false
                    , "diagnose", chiefPresentSimilarityServiceClient);
            if (jsonArray.size() == 2) {
                /* 相似度最高症状 */
                String symptom = jsonArray.getString(0);
                /* 相似度分数 */
                double likeRate = jsonArray.getDoubleValue(1);
                if (likeRate < 0.9) {
                    status.set("-1");
                }
            }
        }
    }
}