package cn.superdesk.uniorder.common.json.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

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

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/10/26 下午7:21
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/10/26 下午7:21
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class BigDecimalScale4ConvertDeserializer extends JsonDeserializer<BigDecimal> {

	/**
	 * 出参保留两位小数
	 *
	 * @param jsonParser
	 * @param deserializationContext
	 * @return
	 * @throws IOException
	 */
	@Override
	public BigDecimal deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {

		if (Objects.isNull(jsonParser.getDecimalValue())) {
//			return null;
			return BigDecimal.ZERO;
		} else {
			// 这里取floor
			return jsonParser.getDecimalValue().setScale(4, RoundingMode.FLOOR);
		}
	}

}
