Compare commits
No commits in common. "335befb3ca9aa03b74b1d7f94ed2cf6f6b32f269" and "cf7924b14351e79bda1f66e4359029dbd7455d1f" have entirely different histories.
335befb3ca
...
cf7924b143
|
|
@ -1,5 +1,6 @@
|
||||||
package com.hzs.common.core.config;
|
package com.hzs.common.core.config;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.fasterxml.jackson.databind.BeanProperty;
|
import com.fasterxml.jackson.databind.BeanProperty;
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
|
|
@ -7,11 +8,14 @@ import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
|
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
|
||||||
import com.hzs.common.core.annotation.BigDecimalFormat;
|
import com.hzs.common.core.annotation.BigDecimalFormat;
|
||||||
|
import com.hzs.common.core.constant.CacheConstants;
|
||||||
|
import com.hzs.common.core.context.SecurityContextHolder;
|
||||||
|
import com.hzs.common.core.service.RedisService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.jackson.JsonComponent;
|
import org.springframework.boot.jackson.JsonComponent;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
|
@ -24,6 +28,15 @@ public class BigDecimalSerializer extends JsonSerializer<BigDecimal> implements
|
||||||
// 默认保留2位小数
|
// 默认保留2位小数
|
||||||
private String format = "#0.00";
|
private String format = "#0.00";
|
||||||
|
|
||||||
|
private String zeroFormat = "#0";
|
||||||
|
|
||||||
|
private RedisService redisService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public void setRedisService(RedisService redisService) {
|
||||||
|
this.redisService = redisService;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 序列化处理方式
|
* 序列化处理方式
|
||||||
*
|
*
|
||||||
|
|
@ -34,10 +47,12 @@ public class BigDecimalSerializer extends JsonSerializer<BigDecimal> implements
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||||
DecimalFormat decimalFormat = new DecimalFormat(format);
|
// if(ComputeUtil.compareEqual(ComputeUtil.computeSubtract(bigDecimal, bigDecimal.setScale(0, RoundingMode.DOWN)), BigDecimal.ZERO)){
|
||||||
// 改为使用去尾方式显示
|
// jsonGenerator.writeString(new DecimalFormat(zeroFormat).format(bigDecimal));
|
||||||
decimalFormat.setRoundingMode(RoundingMode.DOWN);
|
// }else{
|
||||||
jsonGenerator.writeString(decimalFormat.format(bigDecimal));
|
// jsonGenerator.writeString(bigDecimal.setScale(6, RoundingMode.HALF_UP).stripTrailingZeros().toString());
|
||||||
|
jsonGenerator.writeString(new DecimalFormat(format).format(bigDecimal));
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -51,13 +66,15 @@ public class BigDecimalSerializer extends JsonSerializer<BigDecimal> implements
|
||||||
@Override
|
@Override
|
||||||
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
|
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
|
||||||
if (beanProperty != null) {
|
if (beanProperty != null) {
|
||||||
|
Object bdCountry = redisService.getCacheObject(CacheConstants.BD_COUNTRY + SecurityContextHolder.getUserCountry());
|
||||||
if (Objects.equals(beanProperty.getType().getRawClass(), BigDecimal.class)) {
|
if (Objects.equals(beanProperty.getType().getRawClass(), BigDecimal.class)) {
|
||||||
BigDecimalFormat bigDecimalFormat = beanProperty.getAnnotation((BigDecimalFormat.class));
|
BigDecimalFormat bigDecimalFormat = beanProperty.getAnnotation((BigDecimalFormat.class));
|
||||||
if (bigDecimalFormat == null) {
|
if (bigDecimalFormat == null) {
|
||||||
bigDecimalFormat = beanProperty.getContextAnnotation(BigDecimalFormat.class);
|
bigDecimalFormat = beanProperty.getContextAnnotation(BigDecimalFormat.class);
|
||||||
}
|
}
|
||||||
BigDecimalSerializer bigDecimalSerializer = new BigDecimalSerializer();
|
BigDecimalSerializer bigDecimalSerializer = new BigDecimalSerializer();
|
||||||
bigDecimalSerializer.format = replaceNumber();
|
int numberPlaces = Integer.parseInt(JSONUtil.parseObj(bdCountry).get("numberPlaces").toString());
|
||||||
|
bigDecimalSerializer.format = replaceNumber(numberPlaces);
|
||||||
if (bigDecimalFormat != null) {
|
if (bigDecimalFormat != null) {
|
||||||
bigDecimalSerializer.format = bigDecimalFormat.value();
|
bigDecimalSerializer.format = bigDecimalFormat.value();
|
||||||
}
|
}
|
||||||
|
|
@ -68,12 +85,11 @@ public class BigDecimalSerializer extends JsonSerializer<BigDecimal> implements
|
||||||
return new BigDecimalSerializer();
|
return new BigDecimalSerializer();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String replaceNumber() {
|
private String replaceNumber(int numberPlaces) {
|
||||||
StringBuilder number = new StringBuilder("#0.");
|
StringBuilder number = new StringBuilder("#0.");
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < numberPlaces; i++) {
|
||||||
number.append("0");
|
number.append("0");
|
||||||
}
|
}
|
||||||
return number.toString();
|
return number.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue