package com.fdkankan.common.util;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author abnerhou
 * @date 2020/4/23 17:35
 * @desciption
 */
public class DataUtils {

    public static BigDecimal getBigDecimalObj(Object data) {

        if (null == data) {
            return null;
        } else if (data instanceof String) {
            String dataStr = (String) data;
            if (!StringUtils.isEmpty(dataStr.trim())) {
                return new BigDecimal(dataStr.trim());
            }
        } else if (data instanceof Long) {
            Long dataLong = (Long) data;
            return BigDecimal.valueOf(dataLong);
        } else if (data instanceof Integer) {
            Integer dataInt = (Integer) data;
            return BigDecimal.valueOf(dataInt);

        } else if (data instanceof Double) {
            Double dataDouble = (Double) data;
            return BigDecimal.valueOf(dataDouble);
        }

        return null;

    }

    public static Integer getInteger(Object object) {
        if (null == object) {
            return new Integer(0);
        }
        if (object instanceof String) {
            String ojStr = (String) object;
            if (StringUtils.isEmpty(ojStr)) {
                //TODO:在斟酌这里的处理方式
                return new Integer(0);
            } else {
                return new Integer(Integer.parseInt(ojStr.trim()));
            }
        } else if (object instanceof Integer) {
            return (Integer) object;
        } else if (object instanceof Long) {
            return (Integer) object;
        } else if (object instanceof Double) {
            return (Integer) object;
        } else {
            return new Integer(0);
        }
    }

    public static Long getLongReturnNullIfNotExit(Object num){
        if(null == num){
            return null;
        }else if(num instanceof String){
            if(StrUtil.isNotBlank((String) num)){
                String numStr = (String) num;
                return Long.parseLong(numStr.trim());
            }
        }else if(num instanceof Integer){
            return (Long) num;
        }else if(num instanceof  Long){
            return (Long) num;
        }
        return null;
    }

    public static Integer getIntegerWithDefault(Object object, boolean withDefault) {
        if (null == object) {

            return withDefault ? new Integer(0) : null;
        }
        if (object instanceof String) {
            String ojStr = (String) object;
            if (StringUtils.isEmpty(ojStr)) {

                return withDefault ? new Integer(0) : null;
            } else {
                return new Integer(Integer.parseInt(ojStr.trim()));
            }
        } else if (object instanceof Integer) {
            return (Integer) object;
        } else if (object instanceof Long) {
            return (Integer) object;
        } else if (object instanceof Double) {
            return (Integer) object;
        } else {
            return withDefault ? new Integer(0) : null;
        }
    }

    public static Map<String, Object> assembleResult(long totalNum, long totalPageNum, long currPageNum, Object list) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("totalNum", totalNum);
        resultMap.put("totalPageNum", totalPageNum);
        resultMap.put("curPage", currPageNum);
        resultMap.put("list", list);
        return resultMap;
    }
}
