package com.codvision.commoncore.utils;


import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zjm
 * @version V1.0
 * @Package com.codvision.serverboot.util
 * @date 2020/3/4 9:48
 * @Copyright © 2017-2020 杭州码全科技有限公司
 */
public class ParamUtil {

    public static boolean isKeyExist(Map<String, Object> params, String key) {
        return isKeyExist(params, key, true);
    }

    public static boolean isKeyExist(Map<String, Object> params, String key, boolean ignoreEmptyValue) {
        if (ignoreEmptyValue) {
            return params.containsKey(key) && null != params.get(key) && StringUtils.isNotEmpty(params.get(key).toString());
        } else {
            return params.containsKey(key) && null != params.get(key);
        }
    }

    public static Map<String, String> objectToMap(Object obj) throws IllegalAccessException {
        Map<String, String> map = new HashMap<String, String>(16);
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            if (!(ObjectUtils.isEmpty(field.get(obj)))) {
                Object value = field.get(obj);
                map.put(fieldName, String.valueOf(value));
            }
        }
        return map;
    }
}

