package com.dxhy.core.util;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.codec.Base64;

/**
 * @author user
 */
public class ObjectUtil {
    public static String fixNull(Object obj) {
        String result = "";
        if (obj != null) {
            result = obj.toString();
        }
        return result;
    }

    public static String base64(Object obj) {
        if (obj == null) {
            return null;
        }
        return Base64.encode(JSONObject.toJSONString(obj));
    }

    public static String getRandom(int num) {
        double key = Math.random() * 100000;
        StringBuilder result = new StringBuilder(Integer.toString((int)key));
        if (result.length() > num) {
            result = new StringBuilder(result.substring(result.length() - num));
        } else {
            for (int i = num - result.length(); i > 0; i--) {
                result.insert(0, "0");
            }
        }
        return result.toString();
    }
}
