package org.diagbot.graphWeb.work;

import org.apache.commons.lang3.StringUtils;
import org.diagbot.common.push.bean.FeatureRate;
import org.diagbot.common.push.bean.ResponseData;
import org.diagbot.common.push.cache.CacheUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 诊断的过滤和排序
 */
public class FilterSortDiag {
    Logger logger = LoggerFactory.getLogger(FilterSortDiag.class);

    /**
     * 过滤诊断
     */
    public List<FeatureRate> filterDiag(ResponseData graphResponseData, List<FeatureRate> graphFeatureRates) throws Exception {
        List<String> removeDiagList = graphResponseData.getExcludeDiag();
        //将需要排除的诊断从列表中删除
        List<FeatureRate> updateFeatureRates = new ArrayList<>();
        for (int j = 0; j < graphFeatureRates.size(); j++) {
            if (removeDiagList.indexOf(graphFeatureRates.get(j).getFeatureName()) == -1) {
                updateFeatureRates.add(graphFeatureRates.get(j));
            }
        }
        return updateFeatureRates;
    }

    public List<FeatureRate> sortDiag(List<FeatureRate> updateFeatureRates){
        Map<String, String> diagSortCache = CacheUtil.getDiagSortCache();
        List<String> finalDiagList = new LinkedList<>();
        List<String> neoDiagList = new LinkedList<>();
        List<String> bigDataIDiagList = new LinkedList<>();
        List<String> bigDataIIDiagList = new LinkedList<>();
        List<FeatureRate> finalDiagFeature = new LinkedList<>();
        if(updateFeatureRates != null || updateFeatureRates.size()>0){
            for (FeatureRate feature:updateFeatureRates) {
                String featureName = feature.getFeatureName();
                String source = feature.getSource();
                if("neo4j".equals(source)){
                    neoDiagList.add(featureName);
                }else {
                    String s = diagSortCache.get(featureName);
                    if(StringUtils.isNotEmpty(s)){
                        if("Ⅰ".equals(s)){
                            bigDataIDiagList.add(featureName);
                        }else if("Ⅱ".equals(s)){
                            bigDataIIDiagList.add(featureName);
                        }
                    }else {
                        bigDataIDiagList.add(featureName);
                    }
                }
            }
        }
        finalDiagList.addAll(neoDiagList);
        finalDiagList.addAll(bigDataIDiagList);
        finalDiagList.addAll(bigDataIIDiagList);
        if(finalDiagList != null && finalDiagList.size()>0){
            for (String diag:finalDiagList){
                for (FeatureRate f:updateFeatureRates) {
                    if(diag.equals(f.getFeatureName())){
                        finalDiagFeature.add(f);
                        break;
                    }
                }
            }
        }
        return finalDiagFeature;
    }
}
