package org.diagbot.nlp.common;

import org.diagbot.common.service.StandardInfoService;
import org.diagbot.nlp.util.NegativeEnum;
import org.diagbot.nlp.util.NlpCache;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Map;

public class ApplicationCacheUtil {

    public void putStandardInfoContext(ServletContext servletContext, StandardInfoService standardInfoService) {
        //推送词典库缓存
        Map<String, String> standard_info_push_map = NlpCache.getStandard_info_push_map();
        //症状名称缓存
        this.putStandardInfoNameContext(servletContext, standard_info_push_map, NegativeEnum.SYMPTOM.toString(), NlpWebConstants.standard_info_symptom_propel);
        //推送诊断名称缓存
        this.putStandardInfoNameContext(servletContext, standard_info_push_map, NegativeEnum.DISEASE.toString(), NlpWebConstants.standard_info_diag_propel);
        //推送体征名称缓存
        this.putStandardInfoNameContext(servletContext, standard_info_push_map, NegativeEnum.VITAL_RESULT.toString(), NlpWebConstants.standard_info_vital_propel);
        //推送Lis名称缓存
        this.putStandardInfoNameContext(servletContext, standard_info_push_map, NegativeEnum.LIS_NAME.toString(), NlpWebConstants.standard_info_lis_propel);
        //推送Pacs名称缓存
        this.putStandardInfoNameContext(servletContext, standard_info_push_map, NegativeEnum.PACS_NAME.toString(), NlpWebConstants.standard_info_pacs_propel);
    }

    public void putStandardInfoNameContext(ServletContext servletContext, Map<String, String> standard_info_push_map, String categoryId, String key) {
        Map<String, String> standardInfoMap = new HashMap<>(10, 0.8f);
        String[] categoryIds = null;
        for (Map.Entry<String, String> entry : standard_info_push_map.entrySet()) {
            if (categoryId.equals(entry.getValue())) {
                standardInfoMap.put(entry.getKey(), entry.getKey());
            } else if (entry.getValue().indexOf(",") > -1) {
                categoryIds = entry.getValue().split(",");
                for (int i = 0; i < categoryIds.length; i++) {
                    if (categoryId.equals(categoryIds[i])) {
                        standardInfoMap.put(entry.getKey(), entry.getKey());
                    }
                }
            }
        }
        servletContext.setAttribute(key, standardInfoMap);
    }
}
