package com.ho.common.tools.util;

import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Description BigDecimal工具类
 * Author yule
 * Date 2022/10/31 14:31
 */
@Component
public class BigDecimalUtil {


    /**
     * 将传入对象中的BigDecimal类型的属性保留两位小数
     *
     * @param object
     * @return
     */
    public Object keepTwoDecimalPlaces(Object object) {
        for (Field f : object.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            if (f.getType().equals(BigDecimal.class)) {
                try {
                    if (f.get(object) != null) {
                        BigDecimal bigDecimal = new BigDecimal(Objects.toString(f.get(object))).setScale(2, BigDecimal.ROUND_HALF_UP);
                        f.set(object, bigDecimal);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            if (f.getType().equals(Double.class)) {
                try {
                    if (f.get(object) != null) {
                        BigDecimal bigDecimal = new BigDecimal(Objects.toString(f.get(object))).setScale(2, BigDecimal.ROUND_HALF_UP);
                        double doubleValue = bigDecimal.doubleValue();
                        f.set(object, doubleValue);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return object;
    }

    /**
     * 将传入对象中的BigDecimal类型的属性保留num位小数
     *
     * @param object
     * @param num
     * @return
     */
    public Object keepDecimalPlaces(Object object,Integer num) {
        for (Field f : object.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            if (f.getType().equals(BigDecimal.class)) {
                try {
                    if (f.get(object) != null) {
                        BigDecimal bigDecimal = new BigDecimal(Objects.toString(f.get(object))).setScale(num, BigDecimal.ROUND_HALF_UP);
                        f.set(object, bigDecimal);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return object;
    }

    public Object ifIsNUll(Object object) {
        for (Field f : object.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            if (f.getType().equals(BigDecimal.class)) {

                try {
                    if (f.get(object) == null) {
                        BigDecimal bigDecimal = BigDecimal.ZERO;
                        f.set(object, bigDecimal);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            else if (f.getType().equals(Integer.class)) {
                try {
                    if (f.get(object) == null) {
                        Integer integer = 0;
                        f.set(object, integer);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            else if (f.getType().equals(Long.class)) {
                try {
                    if (f.get(object) == null) {
                        Long num = 0L;
                        f.set(object, num);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            else if (f.getType().equals(Double.class)) {
                try {
                    if (f.get(object) == null) {
                        Double num = 0.00;
                        f.set(object, num);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            else if (f.getType().equals(Float.class)) {
                try {
                    if (f.get(object) == null) {
                        Float num = 0f;
                        f.set(object, num);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

        }
        return object;
    }


    public Boolean ifStringLength(Object object) {
        Boolean isString = false;
        for (Field f : object.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            if (f.getType().equals(String.class)) {

                try {
                    if(((String)f.get(object)) != null){
                        if (((String)f.get(object)).length() > 50) {
                            isString = true;
                           break;
                        }
                    }

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return isString;
    }

    /**
     * 根据unitType放大，2：放大1000倍；3: 放大1000000倍；
     * @param data
     * @param unitType
     * @return
     */
    public BigDecimal unitConversion(BigDecimal data, Integer unitType) {
        BigDecimal result = null;
        switch (unitType) {
            case 2:
                result = data.multiply(new BigDecimal(1000));
                break;
            case 3:
                result = data.multiply(new BigDecimal(1000)).multiply(new BigDecimal(1000));
                break;
            default:
                result = data;
                break;
        }
        return result;
    }

}
