package com.storlead.sales.util.report;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2025-01-03 10:03
 */
public class NumberCompareUtils {
    /**
     * 竖着计算list对象中各属性的值
     *
     * @param tList 需要计算的list
     * @param d     计算完的值放在此对象中
     * @return List<T>
     * @author blank
     * @date 2021-3-26 下午 3:51
     */
    public static BigDecimal computaRatio(BigDecimal val, BigDecimal val1) {
        if(Objects.isNull(val)) {
            val = BigDecimal.ZERO;
        }
        if(Objects.isNull(val1)) {
            val1 = BigDecimal.ZERO;
        }
        if(val.compareTo(val1) == 0){
            return BigDecimal.valueOf(0);
        }
        if(val.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.valueOf(-100);
        }
        if(val1.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.valueOf(100);
        }

        BigDecimal rate = (val.subtract(val1).multiply(BigDecimal.valueOf(100))).divide(val1,2, RoundingMode.HALF_UP);
        return rate.setScale(2, RoundingMode.HALF_UP);
    }

    public static BigDecimal computaRatio(Integer val, Integer val1) {

        BigDecimal bval;
        BigDecimal bval1;
        if(Objects.isNull(val)) {
            bval = BigDecimal.ZERO;
        } else {
            bval = BigDecimal.valueOf(val);
        }
        if(Objects.isNull(val1)) {
            bval1 = BigDecimal.ZERO;
        } else {
            bval1 = BigDecimal.valueOf(val1);
        }

        if(bval.compareTo(bval1) == 0){
            return BigDecimal.valueOf(0);
        }
        if(bval.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.valueOf(-100);
        }
        if(bval1.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.valueOf(100);
        }

        BigDecimal rate = (bval.subtract(bval1).multiply(BigDecimal.valueOf(100))).divide(bval,2, RoundingMode.HALF_UP);
        return rate.setScale(2, RoundingMode.HALF_UP);
    }


    public static BigDecimal computaPercentage(BigDecimal val,BigDecimal val1) {
        if(Objects.isNull(val)) {
            val = BigDecimal.ZERO;
        }
        if(Objects.isNull(val1)) {
            val1 = BigDecimal.ZERO;
        }
        if(val.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.valueOf(0);
        }
        if(val1.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.valueOf(0);
        }
        BigDecimal rate =val.divide(val1,2,RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
        return rate.setScale(2, RoundingMode.HALF_UP);
    }

    public static BigDecimal computaPercentage(Integer val, Integer val1) {

        BigDecimal bval;
        BigDecimal bval1;
        if(Objects.isNull(val)) {
            bval = BigDecimal.ZERO;
        } else {
            bval = BigDecimal.valueOf(val);
        }
        if(Objects.isNull(val1)) {
            bval1 = BigDecimal.ZERO;
        } else {
            bval1 = BigDecimal.valueOf(val1);
        }
        if(bval.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.valueOf(0);
        }
        if(bval1.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.valueOf(0);
        }
        BigDecimal rate =bval.divide(bval1,2,RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
        return rate.setScale(2, RoundingMode.HALF_UP);
    }
}
