package com.willalp.common.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.CaseInsensitiveMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ObjectToTypes {
    static final Logger logger = LoggerFactory.getLogger(ObjectToTypes.class);
    public static int parseInt(Object o) {
        if (o == null) {
            return 0;
        }
        return Integer.parseInt(o + "");
    }

    public static long parseLong(Object o) {
        if (o == null) {
            return 0L;
        }
        return Long.parseLong(o + "");
    }

    public static double parseDouble(Object o) {
        if (o == null) {
            return 0D;
        }
        return Double.parseDouble(o + "");
    }

    public static String parseString(Object o) {
        if (o == null) {
            return "";
        }
        return String.valueOf(o);
    }

    public static BigDecimal parseBigDecimal(Object o) {
        if (o == null) {
            return BigDecimal.ZERO;
        }
        return new BigDecimal(String.valueOf(o));
    }

    /**
     * double四舍五入保留小数
     *
     * @param d
     * @param i
     * @return
     */
    public static double getDouble(Object d, int i) {
        if (d == null) {
            d = 0d;
        }
        String rStr = String.format("%." + i + "f", Double.parseDouble(d + ""));
        double rd = Double.parseDouble(rStr);
        return rd;
    }

    public static String null2Str(Object obj) {
        String str = obj == null ? "" : String.valueOf(obj);
        return str;
    }

    public static int toInt(Object obj) {
        return toInt(obj, 0);
    }

    public static int toInt(Object obj, int def) {
        int n = def;
        try {
            n = obj == null ? def : Integer.parseInt(String.valueOf(obj));
        } catch (Exception e) {
        }
        return n;
    }

    public static double toDouble(Object obj) {
        return toDouble(obj, 0);
    }

    public static double toDouble(Object obj, double def) {
        double d = def;
        try {
            d = obj == null ? def : Double.parseDouble(String.valueOf(obj));
        } catch (Exception e) {
        }
        return d;
    }

    public static double getDoubleValue(Object value) {
        if (value == null) {
            return 0.0;
        }
        return Double.parseDouble(value.toString());

    }

    public static int getIntValue(Object object) {
        if (object == null) {
            return 0;
        }
        return Integer.parseInt(object + "");
    }

    public static long toLong(Object obj) {
        return toLong(obj, 0);
    }

    public static long toLong(Object obj, long def) {
        long d = def;
        try {
            d = obj == null ? def : Long.parseLong(String.valueOf(obj));
        } catch (Exception e) {
        }
        return d;
    }
    /**
     * 结果集List<Object> 转 List<Map<String,String>>
     *
     * @param objList
     * @return
     */
    @SuppressWarnings("hiding")
    public static <T> List<Map<String, Object>> ListObjToMap(List<T> objList)  {
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (T obj : objList) {
            Map<String, Object> map=null;
            try {
                map = BeanUtil.beanToMap(obj);
            } catch (Exception e) {
                logger.error(e.getMessage(),e);
            }
            Map<String, Object> result = new CaseInsensitiveMap<String, Object>(map);
            resultList.add(result);
        }
        return resultList;
    }
}
