package com.hdkj.lt.base.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

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

/**
 * @Description:
 * @Author: wenhao
 * @Create: 2022/10/18 15:10
 * @Version: 1.0
 */
public class BigDecimalSerializer {

    /**
     * 去除无效0后缀
     */
    public static class StripTrailingZerosSerializer extends JsonSerializer<BigDecimal> {

        @Override
        public void serialize(BigDecimal value, JsonGenerator generator, SerializerProvider provider) throws IOException {
            if (Objects.nonNull(value)) {
                generator.writeNumber(value.stripTrailingZeros().toPlainString());
            } else {
                generator.writeNull();
            }
        }

    }

    /**
     * 四舍五入，保留2位小数点，去除无效0后缀
     */
    public static class Keep2DecimalSerializer extends JsonSerializer<BigDecimal> {

        @Override
        public void serialize(BigDecimal value, JsonGenerator generator, SerializerProvider provider) throws IOException {
            if (Objects.nonNull(value)) {
                generator.writeNumber(value.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString());
            } else {
                generator.writeNull();
            }
        }

    }

    /**
     * 四舍五入，保留3位小数点，去除无效0后缀
     */
    public static class Keep3DecimalSerializer extends JsonSerializer<BigDecimal> {

        @Override
        public void serialize(BigDecimal value, JsonGenerator generator, SerializerProvider provider) throws IOException {
            if (Objects.nonNull(value)) {
                generator.writeNumber(value.setScale(3, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString());
            } else {
                generator.writeNull();
            }
        }

    }

    /**
     * 四舍五入，保留4位小数点，去除无效0后缀
     */
    public static class Keep4DecimalSerializer extends JsonSerializer<BigDecimal> {

        @Override
        public void serialize(BigDecimal value, JsonGenerator generator, SerializerProvider provider) throws IOException {
            if (Objects.nonNull(value)) {
                generator.writeNumber(value.setScale(4, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString());
            } else {
                generator.writeNull();
            }
        }

    }

    /**
     * 四舍五入，保留6位小数点，去除无效0后缀
     */
    public static class Keep6DecimalSerializer extends JsonSerializer<BigDecimal> {

        @Override
        public void serialize(BigDecimal value, JsonGenerator generator, SerializerProvider provider) throws IOException {
            if (Objects.nonNull(value)) {
                generator.writeNumber(value.setScale(6, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString());
            } else {
                generator.writeNull();
            }
        }

    }

    /**
     * 四舍五入，保留10位小数点，去除无效0后缀
     */
    public static class Keep10DecimalSerializer extends JsonSerializer<BigDecimal> {

        @Override
        public void serialize(BigDecimal value, JsonGenerator generator, SerializerProvider provider) throws IOException {
            if (Objects.nonNull(value)) {
                generator.writeNumber(value.setScale(10, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString());
            } else {
                generator.writeNull();
            }
        }

    }
    /**
     * 四舍五入，保留2位小数点，直接截取(非四舍五入)
     */
    public static class Keep2DecimalDownSerializer extends JsonSerializer<BigDecimal> {

        @Override
        public void serialize(BigDecimal value, JsonGenerator generator, SerializerProvider provider) throws IOException {
            if (Objects.nonNull(value)) {
                generator.writeNumber(value.setScale(2, BigDecimal.ROUND_DOWN));
            } else {
                generator.writeNull();
            }
        }
    }


    /**
     * 四舍五入，保留2位小数点，去除无效0后缀
     */
    public static class Keep2DoubleSerializer extends JsonSerializer<Double> {

        @Override
        public void serialize(Double value, JsonGenerator generator, SerializerProvider provider) throws IOException {
            if (Objects.nonNull(value)) {
                generator.writeNumber( BigDecimal.valueOf(value).setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString());
            } else {
                generator.writeNull();
            }
        }

    }
}
