package com.lantone.qc.kernel.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.lantone.qc.kernel.client.NewSimilarityBatchServiceClient;
import com.lantone.qc.kernel.client.NewSimilarityServiceClient;
import com.lantone.qc.pub.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: rengb
 * @time: 2020/8/24 10:06
 */
@Slf4j
@Component
public class SimilarityUtil {

    @Autowired
    private NewSimilarityServiceClient newSimilarityServiceClient;

    @Autowired
    private NewSimilarityBatchServiceClient newSimilarityBatchServiceClient;

    /**
     * 根据相似度，获取药品标准词
     *
     * @param drugWord
     * @return
     */
    @Cacheable(value = "drugCache", key = "#drugWord", unless = "#result == null")
    public String getDrugStandardWord(String drugWord) {
        String ret = null;
        try {
            JSONArray standardWords = similarityRequest("drug", drugWord, 1);
            if (standardWords != null && standardWords.size() == 1) {
                JSONObject standardWord = standardWords.getJSONObject(0);
                if (standardWord.getDoubleValue("rate") > 0.8) {
                    ret = standardWord.getString("standard_word");
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return ret;
    }

    /**
     * 发送相似度请求
     *
     * @param word_type 由服务方定的词的类型，取值：疾病—disease、症状—symptom、手术和操作—operation、药品—drug、实验室检查-lis、辅助检查-pacs、体征-vital
     * @param word      原始词
     * @param number    最多返回个数，如果不是有效的值，默认返回1个，最多返回10个
     * @return 返回具体数据集合
     */
    public JSONArray similarityRequest(String word_type, String word, int number) {
        if (StringUtil.isBlank(word_type) || StringUtil.isBlank(word) || number == 0) {
            return null;
        }
        JSONArray ret = null;
        try {
            JSONObject detailContent = new JSONObject();
            detailContent.put("word_type", word_type);
            detailContent.put("word", word);
            detailContent.put("number", number);
            String similarJson = newSimilarityServiceClient.getAnnotation(detailContent);
            JSONObject similarJsonObject = JSON.parseObject(similarJson);
            if (similarJsonObject.getBooleanValue("status")) {
                ret = similarJsonObject.getJSONArray("standard_words");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return ret;
    }

    /**
     * 根据相似度，获取药品标准词
     *
     * @param input
     * @param wordType
     * @return
     */
    public Map<String, String> getDrugStandardWordBatch(List<String> input, String wordType) {
        Map<String, String> wordMap = Maps.newHashMap();
        JSONArray inputData = new JSONArray();
        JSONObject micro = null;
        JSONObject similarJsonObject = null;
        JSONArray ret = null;
        try {
            for (String word : input) {
                micro = new JSONObject();
                micro.put("word_type", wordType);
                micro.put("word", word);
                micro.put("number", 1);
                inputData.add(micro);
            }
            if (inputData.size() > 0) {
                JSONObject data = similarityBatchRequest(inputData);
                JSONObject wordTypeJson = data.getJSONObject(wordType);
                for (String word : input) {
                    similarJsonObject = wordTypeJson.getJSONObject(word);
                    if (similarJsonObject != null && similarJsonObject.getBooleanValue("status")) {
                        ret = similarJsonObject.getJSONArray("standard_words");
                        if (ret != null && ret.size() == 1) {
                            JSONObject standardWord = ret.getJSONObject(0);
                            if (standardWord.getDoubleValue("rate") > 0.8) {
                                String result = standardWord.getString("standard_word");
                                wordMap.put(word, result);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return wordMap;
    }

    /**
     * 发送相似度批量请求
     *
     * @param jsonArray 全部需要查询的标准词封装后的集合
     * @return 返回具体数据集合
     */
    public JSONObject similarityBatchRequest(JSONArray jsonArray) {
        if (jsonArray.size() == 0) {
            return null;
        }
        JSONObject ret = null;
        try {
            String similarJson = newSimilarityBatchServiceClient.getAnnotation(jsonArray);
            JSONObject similarJsonObject = JSON.parseObject(similarJson);
            if (similarJsonObject.getBooleanValue("status")) {
                ret = similarJsonObject.getJSONObject("data");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return ret;
    }
}