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 org.diagbot.nlp.util.Constants;

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

public class ParamsDataProxy {
    public void createSearchData(HttpServletRequest request, SearchData searchData)throws Exception {
        //消除空格
        if (searchData.getSymptom() != null) {
            searchData.setSymptom(searchData.getSymptom().trim());
        }
        if (searchData.getDiag() != null) {
            searchData.setDiag(searchData.getDiag().trim());
        }
        //计算年龄区间
        if (searchData.getAge() > 0) {
            searchData.setAge_start(searchData.getAge() - 5);
            searchData.setAge_end(searchData.getAge() + 5);
        }
        //默认查询门诊数据
//        if (org.springframework.util.StringUtils.isEmpty(searchData.getResourceType())) {
//            searchData.setResourceType(BigDataConstants.resource_type_o);
//        }
        //所有信息参与推送
        searchData.setSymptom(searchData.getSymptom() + searchData.getVital()
                + searchData.getLis() + searchData.getPacs() + searchData.getPast() + searchData.getOther() + searchData.getIndications());
        searchData.setSymptom(searchData.getSymptom().trim());
        //一次推送多个类别信息
        String[] featureTypes = searchData.getFeatureType().split(",");
        searchData.setFeatureTypes(featureTypes);

        //获取入参中的特征信息
        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, Object> featureMap = null;
            for (int i = 0; i < featuresList.size(); i++) {
                featureMap = featuresList.get(i);
                Map<String, String> map = new HashMap<>();
                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")));
                if (Constants.default_negative.equals(featureMap.get("negative"))) {
                    searchData.getInputs().put(map.get("feature_name"), map);
                } else {
                    searchData.getFilters().put(map.get("feature_name"), map);
                }
            }
        }
    }
}
