package com.bohuikeji.frame.module.admin.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author ：mmzs
 * @date ：Created in 2021/5/27 17:31
 * @description：对象处理工具类
 * @modified By：
 * @version: $
 */
public class ObjectUtils {
    /**
     * Object转成指定的类型
     * @param obj
     * @param type
     * @param <T>
     * @return
     */
    public static<T> T convert(Object obj, Class<T> type) {
        if (obj != null && StringUtils.isNotBlank(obj.toString())) {
            if (type.equals(Integer.class)||type.equals(int.class)) {
                return (T)Integer.valueOf(StringUtils.trim(obj.toString()));
            } else if (type.equals(Long.class)||type.equals(long.class)) {
                return (T)Long.valueOf(StringUtils.trim(obj.toString()));
            } else if (type.equals(Boolean.class)||type.equals(boolean.class)) {
                return (T)Boolean.valueOf(StringUtils.trim(obj.toString()));
            } else if (type.equals(Short.class)||type.equals(short.class)) {
                return (T)Short.valueOf(StringUtils.trim(obj.toString()));
            } else if (type.equals(Float.class)||type.equals(float.class)) {
                return (T)Float.valueOf(StringUtils.trim(obj.toString()));
            } else if (type.equals(Double.class)||type.equals(double.class)) {
                return (T)Double.valueOf(StringUtils.trim(obj.toString()));
            } else if (type.equals(Byte.class)||type.equals(byte.class)) {
                return (T)Byte.valueOf(StringUtils.trim(obj.toString()));
            } else if (type.equals(Character.class)||type.equals(char.class)) {
                return (T)Character.valueOf(obj.toString().charAt(0));
            } else if (type.equals(String.class)) {
                return (T) obj;
            } else if (type.equals(BigDecimal.class)) {
                return (T) new BigDecimal(StringUtils.trim(obj.toString()));
            } else if (type.equals(LocalDateTime.class)) {
                return (T) LocalDateTime.parse(obj.toString());
            } else if (type.equals(Date.class)) {
                try {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                    return (T) formatter.parse(obj.toString());
                } catch (ParseException e) {
                    throw new RuntimeException(e.getMessage());
                }
            }else{
                return null;
            }
        } else {
            if (type.equals(int.class)) {
                return (T)Integer.valueOf(0);
            } else if (type.equals(long.class)) {
                return (T)Long.valueOf(0L);
            } else if (type.equals(boolean.class)) {
                return (T)Boolean.valueOf(false);
            } else if (type.equals(short.class)) {
                return (T) Short.valueOf("0");
            } else if (type.equals(float.class)) {
                return (T)Float.valueOf(0.0f);
            } else if (type.equals(double.class)) {
                return (T)Double.valueOf(0.0d);
            } else if (type.equals(byte.class)) {
                return (T)Byte.valueOf("0");
            } else if (type.equals(char.class)) {
                return (T)Character.valueOf('\u0000');
            }else {
                return null;
            }
        }
    }

}
