package com.gxwebsoft.common.core.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * BigDecimal 自定义反序列化器
 * 处理null值和空字符串，避免反序列化异常
 *
 * @author WebSoft
 * @since 2025-01-15
 */
@Slf4j
public class BigDecimalDeserializer extends JsonDeserializer<BigDecimal> {

    @Override
    public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String value = p.getValueAsString();
        
        if (value == null || value.trim().isEmpty() || "null".equals(value)) {
            return null;
        }
        
        try {
            return new BigDecimal(value);
        } catch (NumberFormatException e) {
            log.warn("无法解析BigDecimal值: {}, 返回null", value);
            return null;
        }
    }

    @Override
    public BigDecimal getNullValue(DeserializationContext ctxt) {
        return null;
    }
}
