package com.dayou.workflow.config;

import com.dayou.dto.WorkNodeDTO;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 类说明： (后期将删除此类)
 *
 * @author: wucl
 * @since: 2023/10/27
 * created with IntelliJ IDEA.
 */
@Configuration
public class WorkflowConfig {

    private static final String workNodeHandlers =  "execution(* com.dayou.workflow.handler.*.*(..))";

    @Autowired
    private WorkflowInterceptor workflowAdvisor;

    @Bean
    public DefaultPointcutAdvisor workflowAdvisor(){
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(workNodeHandlers);
        advisor.setPointcut(pointcut);
        advisor.setAdvice(workflowAdvisor);
        return advisor;
    }

    @Bean
    @Qualifier("workflowCache")
    public Cache<Long, List<WorkNodeDTO>> workflowCache() {
        Cache<Long, List<WorkNodeDTO>> cache = CacheBuilder.newBuilder()
                .expireAfterAccess(24, TimeUnit.HOURS).build();
        return cache;
    }
}
