package com.njcn.harmonic.utils;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.reflect.Type;
import java.sql.Timestamp;

@Component
public class TimestampAsLongSerializer implements ObjectSerializer {

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        if (object instanceof Timestamp) {
            Timestamp timestamp = (Timestamp) object;
            // 将Timestamp转换为时间戳（毫秒数）
            long time = timestamp.getTime();
            serializer.write(time);
        } else {
            serializer.write(object);
        }
    }
}