package cn.com.ty.lift.quan.config;

import cn.com.ty.lift.common.aliservice.aliyunoss.AliyunOSS;
import cn.com.ty.lift.common.sql.SqlAnalysisInterceptor;
import cn.com.ty.lift.common.verify.Validation;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.Data;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RestController;

import java.time.format.DateTimeFormatter;

/**
 * <p>
 *     配置bean注入
 * </p>
 * @author wcz
 * @since  2019/12/3
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
public class SystemConfiguration {

    private String endpoint;

    private String bucketName;

    private String accessKeySecret;

    private String accessKeyId;

    /**
     * 创建aliyun OSS bean
     * @return
     */
    @Bean
    @Scope("prototype")
    public AliyunOSS build(){
        if(StrUtil.hasEmpty(endpoint,accessKeyId,accessKeySecret,bucketName)){
            return AliyunOSS.me();
        }
        return AliyunOSS.me(endpoint,accessKeyId,accessKeySecret,bucketName);
    }

    /**
     * 全局格式化日期
     *
     * @return Jackson2ObjectMapperBuilderCustomizer
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> {
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
            builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
            builder.serializers(new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));

            builder.deserializers(new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
            builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
            builder.deserializers(new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));

            builder.serializerByType(Long.class, ToStringSerializer.instance);
        };
    }

    /**
     * mybatis-plus 执行sql性能监测
     * @return SqlAnalysisInterceptor
     */
    @Bean
    @ConditionalOnMissingBean(PerformanceInterceptor.class)
    public SqlAnalysisInterceptor sqlAnalysisInterceptor(){
        SqlAnalysisInterceptor sqlAnalysisInterceptor = new SqlAnalysisInterceptor();
        //格式化执行的sql
        sqlAnalysisInterceptor.setFormat(true);
        //sql写入日志文件
        sqlAnalysisInterceptor.setLogWrite(true);
        return sqlAnalysisInterceptor;
    }
    /**
     * 方法参数校验拦截器,标注了@Verifier的方法，会检查参数，如果参数不满足条件，直接抛出异常
     */
    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor(){
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(new AnnotationMatchingPointcut(RestController.class, Validation.class));
        advisor.setAdvice(new ValidateMethodInterceptor());
        advisor.setOrder(2);
        return advisor;
    }
}
