package com.crm.rely.backend.configuration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * @program: crm-backend
 * @description: 默认配置
 * @author: houn
 * @create: 2019-07-26 13:48
 */
@Component
public class DefaultEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

        Properties properties = new Properties();

        //文件缓存地址
        properties.put("server.tomcat.basedir", "./tmp");

        //配置多语言 加载messages数据 如果在messages中没有 则在defaultMessages获取 如果还是没有 则返回填写的内容
        //就是messages覆盖defaultMessages messages为项目自己配置
        properties.put("spring.messages.basename", "i18n/messages,i18n/defaultMessages");

        properties.put("web.upload-path", "./upload");
        properties.put("web.front-path", "./front");
        properties.put("web.export-path", "./export");

        properties.put("ip.ip2LocationLiteDb3", "../IP2LOCATION");

        properties.put("spring.resources.static-locations", "file:${web.front-path},file:${web.upload-path}," +
                "file:${web.export-path}");

        /**
         * 默认activemq pool配置
         */
        properties.put("spring.activemq.pool.create-connection-on-startup", true);
        properties.put("spring.activemq.pool.enabled", true);
        properties.put("spring.activemq.pool.expiry-timeout", 0);
        properties.put("spring.activemq.pool.block-if-full", true);
        properties.put("spring.activemq.pool.idle-timeout", 30000);
        properties.put("spring.activemq.pool.max-connections", 100);
        properties.put("spring.activemq.pool.maximum-active-session-per-connection", 5000);
        properties.put("spring.activemq.pool.reconnect-on-exception", true);
        properties.put("spring.activemq.pool.time-between-expiration-check", -1);
        properties.put("spring.activemq.pool.use-anonymous-producers", true);

        /**
         * 默认activemq配置
         */
        properties.put("spring.activemq.broker-url", "tcp://localhost:61616");
        properties.put("spring.activemq.close-timeout", 15);
        properties.put("spring.activemq.in-memory", true);
        properties.put("spring.activemq.non-blocking-redelivery", false);
        properties.put("spring.activemq.user", "admin");
        properties.put("spring.activemq.masterPassword", "admin");
        properties.put("spring.activemq.send-timeout", 0);

        /**
         * 默认sql配置
         */
        properties.put("spring.datasource.type", "com.alibaba.druid.pool.DruidDataSource");
        properties.put("spring.datasource.filters", "stat");
        properties.put("spring.datasource.maxActive", 50);
        properties.put("spring.datasource.initialSize", 1);
        properties.put("spring.datasource.maxWait", 60000);
        properties.put("spring.datasource.minIdle", 10);
        properties.put("spring.datasource.testWhileIdle", true);
        properties.put("spring.datasource.timeBetweenEvictionRunsMillis", 60000);
        properties.put("spring.datasource.minEvictableIdleTimeMillis", 300000);
        properties.put("spring.datasource.validationQuery", "SELECT 'x'");

        properties.put("spring.datasource.tomcat.max-active", 50);
        properties.put("spring.datasource.tomcat.max-idle", 30);
        properties.put("spring.datasource.tomcat.min-idle", 10);
        properties.put("spring.datasource.tomcat.initial-size", 10);
        properties.put("spring.datasource.tomcat.max-wait", 60000);
        properties.put("spring.datasource.tomcat.remove-abandoned", true);
        properties.put("spring.datasource.tomcat.remove-abandoned-timeout", 60);
        /**
         * 默认redis配置
         * spring:
         *     redis:
         *         database: 0
         *         host: localhost
         *         pool:
         *             max-active: 8
         *             max-idle: 8
         *             max-wait: -1
         *             min-idle: 0
         *         port: 6379
         *         timeout: 0
         */
        properties.put("spring.redis.database", 0);
        properties.put("spring.redis.type", 1);
        properties.put("spring.redis.host", "127.0.0.1");
        properties.put("spring.redis.port", 6379);
        properties.put("spring.redis.timeout", 5000);
        properties.put("spring.redis.masterPassword", "");
        properties.put("spring.redis.pool.max-active", 3000);
        properties.put("spring.redis.pool.max-wait", 50);
        properties.put("spring.redis.pool.max-idle", 10);
        properties.put("spring.redis.pool.min-idle", 5);

        properties.put("spring.redis.lettuce.pool.max-active", 3000);
        properties.put("spring.redis.lettuce.pool.max-wait", 50);
        properties.put("spring.redis.lettuce.pool.max-idle", 100);
        properties.put("spring.redis.lettuce.pool.min-idle", 50);

        /**
         * db
         */
        properties.put("spring.data.mongodb.repositories.enabled", false);
        properties.put("spring.data.redis.repositories.enabled", false);

        properties.put("spring.freemarker.check-template-location", false);

        //默认模版为false
        properties.put("spring.thymeleaf.cache", false);

        //默认文件上传
        properties.put("spring.http.multipart.max-file-size", "150MB");
        properties.put("spring.http.multipart.max-request-size", "150MB");

        properties.put("spring.http.encoding.charset", "utf-8");
        properties.put("spring.http.encoding.force", true);
        properties.put("spring.http.encoding.enabled", true);
        //默认session过期时间7200(秒,两小时)
        properties.put("server.session.timeout", 7200);

        /**
         * 默认mybatis配置 在classpath:mapper/*.xml
         */
        properties.put("mybatis.mapper-locations", "classpath:mapper/*.xml");
        properties.put("mybatis.configuration.map-underscore-users-camel-case", true);
        properties.put("mybatis.configuration.use-column-label", true);
        properties.put("mybatis.configuration.use-generated-keys", true);
        properties.put("mybatis.configuration.map-underscore-to-camel-case", true);
        properties.put("mybatis.configuration.log-impl", "org.apache.ibatis.logging.stdout.StdOutImpl");

        properties.put("page.redirect.enable", false);
        properties.put("page.redirect.url", "/error404.html");

        /**
         * eureka
         */
        properties.put("eureka.client.serviceUrl.defaultZone", "http://admin:admin123456@localhost:7000/eureka");
        properties.put("eureka.instance.prefer-ip-address", true);
        properties.put("eureka.instance.instance-id", "${spring.application.name}:${spring.cloud.client" +
                ".ipAddress}:${spring.application.instance_id:${server.port}}");
        properties.put("ribbon.ReadTimeout", 60000);
        properties.put("ribbon.ConnectTimeout", 60000);
        //默认打印日志等级
//        properties.put("logging.level.root","error");
        properties.put("login.single-sign-on", false);

        //Feign Token
        properties.put("feign-token", "b20a6a368a17d39f62f380f3a2830679");

        properties.put("logging.level.com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver", "WARN");
        properties.put("logging.file.max-history", 30);
        properties.put("logging.file.max-size", "100MB");

        //springboot默认启动线程
        //#最大并发数
        //server.tomcat.max-threads=1000
        //#接受和处理的最大连接数
        //server.tomcat.max-connections=9
        //#初始化时创建的线程数
        //server.tomcat.min-SpareThreads=6
        //#可以放到处理队列中的请求数
        //server.tomcat.acceptCount=10
        //# session最大超时时间（分钟），默认为30分钟
        //server.session-timeout=60
        properties.put("server.tomcat.max-connections", 500);
        properties.put("server.tomcat.accept-count", 5000);//等待
        properties.put("server.tomcat.max-threads", 5000);//最大线程
        properties.put("server.tomcat.min-spare-threads", 500);//最小线程

        PropertiesPropertySource propertySource = new PropertiesPropertySource("default", properties);

        environment.getPropertySources().addLast(propertySource);
    }

}