package com.zhgd.jeecg.common.mybatis;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zhgd.mybatis.Aes;

import java.util.HashMap;

import static com.baomidou.mybatisplus.core.toolkit.StringUtils.UNDERLINE;

/**
 * 自定义Map
 */
public class EntityMap extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;
    public static EnumConvertInterceptor interceptors = null;
    // private RedisUtils redisUtils = SpringContextHolder.getBean("redisUtils");
    //private Map<Object, Object> dataMaps = redisUtils.getMap("DICTDATA_MAPS");

    public EntityMap() {

    }

    private static String getField(String field) {
        String str = "";
        int s = field.indexOf(".");
        if (s > -1) {
            str = field.substring(s + 1);
        } else {
            str = field;
        }
        return str;
    }

    public static void setEnumConvertInterceptor(EnumConvertInterceptor interceptor) {
        interceptors = interceptor;
    }

    @Override
    public EntityMap put(String key, Object value) {
        /* List<Object> dictKeys = redisUtils.getList("DICT_KEYS");
         *//*判断字段是否是字典类型*//*
        if (dictKeys.contains(key) && ObjectUtils.isNotEmpty(value)) {
            Object dictValue = dataMaps.get(key + "_" + value.toString());
            *//*返回数据中添加字典显示值*//*
            super.put(key + "Title", dictValue);
        }*/
        if (ObjectUtils.isNotEmpty(interceptors)) {
            interceptors.convert(this, key, value);
        }
        if (key.startsWith("ase_")) {
            super.put(underlineToCamel(key.substring("ase_".length())), Aes.decrypt(value.toString()));
        } else {
            String newk = underlineToCamel(key);
            //log.info(newk);
            if (ObjectUtils.isNotNull(value)) {
                //判断数据是否是Long类型，如果是则转为字符串
                if (Long.class.isAssignableFrom(value.getClass())) {
                    super.put(newk, String.valueOf(value));
                } else {
                    super.put(newk, value);
                }
                //super.put(newk, value);
            } else {
                super.put(newk, "");
            }
        }
        return this;
    }

    @SuppressWarnings("unchecked")
    public <T> T get(String key) {
        T t = null;
        Object obj = super.get(key);
        if (ObjectUtils.isNotEmpty(obj)) {
            t = (T) obj;
        }
        return t;
    }

    @SuppressWarnings("unchecked")
    public <T> T get(String key, T def) {
        Object obj = super.get(key);
        if (ObjectUtils.isNotEmpty(obj)) {
            return (T) obj;
        } else {
            return def;
        }
    }

    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(Character.toLowerCase(param.charAt(i)));
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        int a = 4;
        Object param = a;
        if (param instanceof Integer) {
            System.out.println("Integer类型");
        } else if (param instanceof String) {
            System.out.println("字符串");
        } else if (param instanceof Double) {
            System.out.println("Double类型");
        } else if (param instanceof Float) {
            System.out.println("Float类型");
        } else if (param instanceof Long) {
            System.out.println("Long类型");
        } else if (param instanceof Boolean) {
            System.out.println("Boolean类型");
        }
    }
}
