package com.ho.business.util;

import com.ho.business.vo.resp.colCount.ColCountResp;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

/**
 * 计算偏差值
 */
@Slf4j
public class CountOffsetUtils {

    //加
    public static final int add = 1;
    //减
    public static final int subtract = 2;
    //乘
    public static final int multiply = 3;
    //除
    public static final int divide = 4;

    public static BigDecimal countOffset(BigDecimal num,ColCountResp colCountResp){
        //计算方式（加减乘除分别以 1，2，3，4表示）
        Integer countMethod = colCountResp.getCountMethod();
        //偏差值
        BigDecimal offset = colCountResp.getOffset();
        num = getBigDecimal(num, countMethod, offset);
        return num;
    }

    /**
     * 传入值计算
     * @param num
     * @param countMethod
     * @param offset
     * @return
     */
    public static BigDecimal getBigDecimal(BigDecimal num, Integer countMethod, BigDecimal offset) {
        switch (countMethod){
            case add:
                num = num.add(offset);
                break;
            case subtract:
                num = num.subtract(offset);
                break;
            case multiply:
                num = num.multiply(offset);
                break;
            case divide:
                num = num.divide(offset, 4, BigDecimal.ROUND_HALF_UP);
                break;
            default:
        }
        return num;
    }

    public static void main(String[] args){
        ColCountResp colCountResp = new ColCountResp();
        colCountResp.setOffset(new BigDecimal(1.5));
        colCountResp.setCountMethod(1);
        System.out.println(countOffset(new BigDecimal(158),colCountResp));
        colCountResp.setCountMethod(2);
        System.out.println(countOffset(new BigDecimal(158),colCountResp));
        colCountResp.setCountMethod(3);
        System.out.println(countOffset(new BigDecimal(158),colCountResp));
        colCountResp.setCountMethod(4);
        System.out.println(countOffset(new BigDecimal(158),colCountResp));
    }

}
