package cn.com.ty.lift.push.conf;

import cn.com.ty.lift.common.sql.SqlAnalysisInterceptor;
import cn.com.ty.lift.common.sql.SqlIllegalInterceptor;
import cn.com.ty.lift.common.utils.ValuePool;
import cn.com.ty.lift.push.app.XingePush;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
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 lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
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 javax.annotation.PostConstruct;
import javax.jms.Queue;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *     配置bean注入
 * </p>
 * @author wcz
 * @since  2019/12/3
 */
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "xinge")
public class SystemConfiguration {
    private String appId;
    private String secretKey;

    @PostConstruct
    private void print(){
        log.info("Hint: the Configuration for tencent xinge push.");
        log.info("============================================================");
        log.info("###| appId        : {}", appId);
        log.info("###| secretKey    : {}", secretKey);
        log.info("============================================================");
    }
    /**
     * 创建tencent xinge 消息推送
     */
    @Bean
    public XingePush xingePush(){
        if(StrUtil.hasEmpty(appId,secretKey)){
            return new XingePush();
        }
        return new XingePush(appId, secretKey);
    }
    /**
     * 全局格式化日期
     */
    @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性能监测
     */
    @Bean
    @ConditionalOnMissingBean(PerformanceInterceptor.class)
    public SqlAnalysisInterceptor sqlAnalysisInterceptor(){
        SqlAnalysisInterceptor sqlAnalysisInterceptor = new SqlAnalysisInterceptor();
        //格式化执行的sql
        sqlAnalysisInterceptor.setFormat(true);
        //sql写入日志文件
        sqlAnalysisInterceptor.setLogWrite(true);
        return sqlAnalysisInterceptor;
    }

    /**
     * SQL是影响系统性能最重要的因素，所以拦截提示不合理的SQL语句
     */
//    @Bean
//    @ConditionalOnMissingBean
    public SqlIllegalInterceptor sqlIllegalInterceptor(){
        SqlIllegalInterceptor sqlIllegalInterceptor = new SqlIllegalInterceptor();
        return sqlIllegalInterceptor;
    }

    /**
     * 分页插件
     * @return 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    @Bean
    public Queue androidTokenQueue() {
        return new ActiveMQQueue(ValuePool.PUSH_QUEUE_ANDROID_TOKEN);
    }
    @Bean
    public Queue androidAllQueue() {
        return new ActiveMQQueue(ValuePool.PUSH_QUEUE_ANDROID_ALL);
    }
    @Bean
    public Queue iosTokenQueue() {
        return new ActiveMQQueue(ValuePool.PUSH_QUEUE_IOS_TOKEN);
    }
    @Bean
    public Queue iosAllQueue() {
        return new ActiveMQQueue(ValuePool.PUSH_QUEUE_IOS_ALL);
    }
}
