package com.pm.framework.config;

import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * 程序注解配置
 * @author pm
 */
@Configuration
// 表示通过aop框架暴露该代理对象,AopContext能够访问
@EnableAspectJAutoProxy(exposeProxy = true)
// 指定要扫描的Mapper类的包的路径
@MapperScan("com.pm.**.mapper")
public class ApplicationConfig
{
    /**
     * 统一配置Jackson的时区和日期格式(返回页面的室间格式处理)
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
        return builder -> {
            // 1. 设置时区（使用系统默认时区）
            builder.timeZone(TimeZone.getDefault());

            // 2. 配置LocalDateTime序列化格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            builder.serializers(new LocalDateTimeSerializer(formatter));

            // 3. 配置LocalDateTime反序列化格式
            builder.deserializers(new LocalDateTimeDeserializer(formatter));

        };
    }
}
