/**
 * Copyright 2008-2021. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved.
 * <a>http://www.crunii.com</a>
 */

package com.crunii.micro.common.config;

import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.cloud.nacos.discovery.NacosWatch;
import com.crunii.micro.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * @author 田平 create 2023-02-10 03:54:47
 */
@Slf4j
@Configuration
@ConditionalOnNacosDiscoveryEnabled
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class, CommonsClientAutoConfiguration.class })
public class StartTimeConfiguration {

	public static final String START_TIME = "startTime";

	@Bean
	@ConditionalOnProperty(value = { "spring.cloud.nacos.discovery.watch.enabled" }, matchIfMissing = true)
	public NacosWatch nacosWatch(NacosServiceManager nacosServiceManager, NacosDiscoveryProperties nacosDiscoveryProperties) {
		//添加服务注册时间
		nacosDiscoveryProperties.getMetadata().put(START_TIME, DateUtil.format(new Date()));
		return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties);
	}

}
