package com.crm.gateway.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();

        //配置多语言 加载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");

        /**
         * 默认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", 2000);
        properties.put("spring.redis.masterPassword", "");
        properties.put("spring.redis.pool.max-active", 20000);
        properties.put("spring.redis.pool.max-wait", -1);
        properties.put("spring.redis.pool.max-idle", 10);
        properties.put("spring.redis.pool.min-idle", 5);

        //默认模版为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);

        /**
         * 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);


        properties.put("logging.level.com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver", "WARN");
        PropertiesPropertySource propertySource = new PropertiesPropertySource("default", properties);

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

}