package com.yxcheng.token.utils;

import org.apache.commons.lang.StringUtils;

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

public class ObjectUtil {

    public static boolean isBigger(Object s,int v){
        return Integer.valueOf(s.toString()) > v;
    }

    public static boolean isNotBigger(Object s,int v){
        return Integer.valueOf(s.toString()) <= v;
    }

    public static String fixNull(Object obj){
        String result = "";
        if(obj!=null){
            result = obj.toString();
        }
        return result;
    }

    public static boolean checkData(Object...objs){
        boolean flag = true;
        if(objs!=null && objs.length>0){
            for(Object o : objs){
                if(StringUtils.isBlank(fixNull(o))){
                    flag = false;
                    break;
                }
            }
        }
        return flag;
    }


    /**
     * 未来版本会删除
     */
    @Deprecated
    public static Map<String,String> getParameterMap(HttpServletRequest request) {
        // 参数Map
        Map properties = request.getParameterMap();
        // 返回值Map
        Map<String,String>  returnMap = new HashMap<String,String>();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if(null == valueObj){
                value = "";
            }else if(valueObj instanceof String[]){
                String[] values = (String[])valueObj;
                for(int i=0;i<values.length;i++){
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length()-1);
            }else{
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }


    public static boolean dataBaseListIsExist(List list){
        return list!=null && list.size()==1;
    }
}
