package com.hotent.base.listener;

import com.hotent.base.aop.OpenApiProcessor;
import com.hotent.base.cache.annotation.CachePut;
import com.hotent.base.cache.annotation.Cacheable;
import com.hotent.base.constants.CacheKeyConst;
import com.hotent.base.entity.URIEntity;
import com.hotent.base.util.AppUtil;
import com.hotent.base.util.BeanUtils;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 服务启动成功的监听器
 *
 * @company 广州宏天软件股份有限公司
 * @author heyifan
 * @email heyf@jee-soft.cn
 * @date 2019年6月27日
 */
@Component
public class ApplicationReadyEventListener implements  ApplicationListener<ApplicationReadyEvent>{

	@CachePut(value = CacheKeyConst.EIP_SYS_UC_OPEN_API,key = "#openApi", shortExpire = true)
	public Set<URIEntity> putCache(String openApi, Set<URIEntity> set){
		return set;
	}

	@Cacheable(value = CacheKeyConst.EIP_SYS_UC_OPEN_API,key = "#openApi", shortExpire = true)
	public Set<URIEntity> getOpenApiListFromCache(String openApi){
		return null;
	}

	@Override
	public void onApplicationEvent(ApplicationReadyEvent event) {
		System.out.println("***********************");
		System.out.println(String.format("********%s********", "应用已启动"));
		System.out.println("***********************");
		ApplicationReadyEventListener bean = AppUtil.getBean(ApplicationReadyEventListener.class);
		Set<URIEntity> fromCache = bean.getOpenApiListFromCache("openApi");
		if (BeanUtils.isNotEmpty(fromCache)){
			fromCache.addAll(OpenApiProcessor.uriEntitySet);
		}else{
			fromCache = OpenApiProcessor.uriEntitySet;
		}
		bean.putCache("openApi",fromCache);
	}
}
