package cn.reghao.autodop.dmaster.spring.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

/**
 * TODO 若 AppOrchestration 发生修改，则重新初始化 AppIntegrate
 *
 * @author reghao
 * @date 2020-03-06 16:04:27
 */
@Slf4j
@Component
@Aspect
public class AppIntegrateReinitInterceptor {
    private Set<String> changedApp = new ConcurrentSkipListSet<>();
    // 拦截 cn.reghao.autodop.dmaster.app.db 包中的所有方法
    private final static String POINT = "execution (* cn.reghao.autodop.dmaster.app.db..*.*(..))";

    @Pointcut(POINT)
    public void reinit(){
        // TODO 这个方法中可以做些什么？
    }

    @Around("reinit()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getName();
        Object[] args = joinPoint.getArgs();
        //log.info("拦截 {} 方法", methodName);
        /*if ("update".equals(methodName) && args[0] instanceof AppOrchestration) {
            AppOrchestration app = (AppOrchestration) args[0];
            String appId = app.getAppId();
            changedApp.add(appId);
        }*/

        return joinPoint.proceed(joinPoint.getArgs());
    }

    public boolean isAppChanged(String appId) {
        return changedApp.remove(appId);
    }
}
