package org.diagbot.bigdata.work;

import org.algorithm.util.AlgorithmClassify;
import org.apache.commons.beanutils.BeanUtils;
import org.diagbot.bigdata.util.BigDataConstants;
import org.diagbot.nlp.feature.FeatureAnalyze;
import org.diagbot.nlp.feature.FeatureType;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName org.diagbot.bigdata.work.ParamsDataProxy
 * @Description TODO
 * @Author fyeman
 * @Date 2019/1/16/016 14:04
 * @Version 1.0
 **/
public class ParamsDataProxy {
    public void createSearchData(HttpServletRequest request, BigDataSearchData 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 (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.setSymptom(searchData.getSymptom().trim());
        //一次推送多个类别信息
        String[] featureTypes = searchData.getFeatureType().split(",");
        searchData.setFeatureTypes(featureTypes);
        //featureType转算法模型类别
        searchData.setAlgorithmClassify(createAlgorithmClassify(searchData.getSysCode(), featureTypes, searchData));
        //获取入参中的特征信息
        FeatureAnalyze fa = new FeatureAnalyze();

        if (!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 (!StringUtils.isEmpty(searchData.getDiag()) && StringUtils.isEmpty(searchData.getSymptom())) {
            List<Map<String, Object>> featuresList = fa.start(searchData.getDiag(), FeatureType.DIAG);
            paramFeatureInit(searchData, featuresList);
        }
    }

    /**
     * featureType转算法模型类型
     * @param sysCode
     * @param featureTypes
     * @param searchData
     */
    private AlgorithmClassify[] createAlgorithmClassify(String sysCode, String[] featureTypes, BigDataSearchData searchData) {
        AlgorithmClassify[] classifies = new AlgorithmClassify[featureTypes.length];
        //下了诊断且其他信息全为空 反推标识
        boolean reverse = !StringUtils.isEmpty(searchData.getDiag()) && StringUtils.isEmpty(searchData.getSymptom());
        for (int i = 0; i < featureTypes.length; i++) {
            featureTypes[i] = convertFeatureType(sysCode, featureTypes[i]);
            if (featureTypes[i] != null) {
                //模型
                switch (FeatureType.parse(featureTypes[i])) {
                    case SYMPTOM:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_SYMPTOM;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_SYMPTOM;
                        }
                        break;
                    case DIAG:
                        if (reverse) {
                            classifies[i] = null;
                        }  else {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG;
                        }
                        break;
                    case VITAL:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_VITAL;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_VITAL;
                        }
                        break;
                    case LIS:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_LIS;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_LIS;
                        }
                        break;
                    case PACS:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_PACS;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_PACS;
                        }
                        break;
                    case TREAT:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_TREAT;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_TREAT;
                        }
                        break;
                    case HISTORY:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_HISTORY;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_HISTORY;
                        }
                        break;
                }
            }
        }
        return classifies;
    }

    /**
     * 外部系统featureType需要转化为大数据定义的featureType
     * @param sysCode
     * @param featureType
     * @return
     */
    private String convertFeatureType(String sysCode, String featureType) {
        if (StringUtils.isEmpty(sysCode) || sysCode.equals("1")) {
            if ("1".equals(featureType)) {
                return BigDataConstants.feature_type_symptom;
            }
            if ("7".equals(featureType)) {
                return BigDataConstants.feature_type_diag;
            }
            if ("4".equals(featureType)) {
                return BigDataConstants.feature_type_vital;
            }
            if ("5".equals(featureType)) {
                return BigDataConstants.feature_type_lis;
            }
            if ("6".equals(featureType)) {
                return BigDataConstants.feature_type_pacs;
            }
            if ("3".equals(featureType)) {
                return BigDataConstants.feature_type_history;
            }
            if ("8".equals(featureType)) {
                return BigDataConstants.feature_type_treat;
            }
            return null;
        }
        return featureType;
    }

    /**
     * 推送模型入参
     *
     * @param searchData
     * @throws Exception
     */
    private void paramFeatureInit(BigDataSearchData 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);
            }
        }
    }
}
