package org.diagbot.graphWeb.work;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.diagbot.common.work.SearchData;
import org.diagbot.nlp.feature.FeatureAnalyze;
import org.diagbot.nlp.feature.FeatureType;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class ParamsDataProxy {
    public void createSearchData(HttpServletRequest request, SearchData searchData)throws Exception {
        //消除空格
        List<String> features=new ArrayList<>();
        features.add(searchData.getSymptom().trim());
        features.add(searchData.getOther().trim());
        features.add(searchData.getVital().trim());
        features.add(searchData.getLis().trim());
        features.add(searchData.getPacs().trim());
//        features.add(searchData.getDiag().trim());
        searchData.setSymptom(StringUtils.join(features.toArray(),","));
        if (searchData.getDiag() != null) {
            searchData.setDiag(searchData.getDiag().trim());
        }
        //获取入参中的特征信息
        FeatureAnalyze fa = new FeatureAnalyze();

        if (!org.springframework.util.StringUtils.isEmpty(searchData.getSymptom())) {
            List<Map<String, Object>> featuresList = fa.start(searchData.getSymptom(), FeatureType.FEATURE);
            paramFeatureInit(searchData, featuresList);
            //如果既往史中诊断信息，需要提取这个特征
            featuresList = fa.start(searchData.getOther(), FeatureType.DIAG);
            paramFeatureInit(searchData, featuresList);
        }
        if (!org.springframework.util.StringUtils.isEmpty(searchData.getDiag()) && org.springframework.util.StringUtils.isEmpty(searchData.getSymptom())) {
            List<Map<String, Object>> featuresList = fa.start(searchData.getDiag(), FeatureType.DIAG);
            paramFeatureInit(searchData, featuresList);
        }

    }

    /**
     * 推送模型入参
     *
     * @param searchData
     * @throws Exception
     */
    private void paramFeatureInit(SearchData searchData, List<Map<String, Object>> featuresList) throws Exception {
        if (featuresList != null && featuresList.size() > 0) {
//            BeanUtils.setProperty(searchData, property_list, featuresList);
            Map<String, String> map = new HashMap<>();
            Map<String, Object> featureMap = null;
            for (int i = 0; i < featuresList.size(); i++) {
                featureMap = featuresList.get(i);
                for (Map.Entry<String, Object> entry : featureMap.entrySet()) {
                    map.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
                map.put("featureType", String.valueOf(featureMap.get("feature_type")));
                map.put("featureName", String.valueOf(featureMap.get("feature_name")));
                searchData.getInputs().put(map.get("feature_name"), map);
            }
        }
    }
}
