package ink.wgink.common.config;

import ink.wgink.properties.TransactionProperties;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: GlobalTransactionConfig
 * @Description: 全局事务处理
 * @Author: WangGeng
 * @Date: 2019/2/26 4:18 PM
 * @Version: 1.0
 **/
@Aspect
@Configuration
public class TransactionConfig {

    /**
     * 默认新增事务
     */
    private static String[] DEFAULT_SAVE_ARRAY = {"add*", "save*", "insert*", "create*"};
    /**
     * 默认删除事务
     */
    private static String[] DEFAULT_REMOVE_ARRAY = {"delete*", "remove*"};
    /**
     * 默认执行事务
     */
    private static String[] DEFAULT_UPDATE_ARRAY = {"update*", "edit*"};
    /**
     * 默认其它事务
     */
    private static String[] DEFAULT_OTHER_ARRAY = {"send*", "exec*", "set*", "login*", "register*", "sign*", "rest*", "upload*"};
    /**
     * 默认查询事务
     */
    private static String[] DEFAULT_QUERY_ARRAY = {"get*", "query*", "find*", "list*", "count*", "is*", "size*", "show*"};

    @Autowired
    private TransactionProperties transactionProperties;
    @Autowired
    private PlatformTransactionManager transactionManager;

    /**
     * 事务拦截器
     *
     * @return
     */
    @Bean
    public TransactionInterceptor txAdvice() {
        DefaultTransactionAttribute required = new DefaultTransactionAttribute();
        required.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        required.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);

        DefaultTransactionAttribute readOnly = new DefaultTransactionAttribute();
        readOnly.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
        readOnly.setReadOnly(true);

        List<String> saveList = transactionProperties.getSaveList();
        saveList.addAll(Arrays.asList(DEFAULT_SAVE_ARRAY));

        List<String> removeList = transactionProperties.getSaveList();
        removeList.addAll(Arrays.asList(DEFAULT_REMOVE_ARRAY));

        List<String> updateList = transactionProperties.getSaveList();
        updateList.addAll(Arrays.asList(DEFAULT_UPDATE_ARRAY));

        List<String> otherList = transactionProperties.getSaveList();
        otherList.addAll(Arrays.asList(DEFAULT_OTHER_ARRAY));

        List<String> queryList = transactionProperties.getSaveList();
        queryList.addAll(Arrays.asList(DEFAULT_QUERY_ARRAY));

        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        for (String save : saveList) {
            source.addTransactionalMethod(save, required);
        }
        for (String remove : removeList) {
            source.addTransactionalMethod(remove, required);
        }
        for (String update : updateList) {
            source.addTransactionalMethod(update, required);
        }
        for (String other : otherList) {
            source.addTransactionalMethod(other, required);
        }
        for (String query : queryList) {
            source.addTransactionalMethod(query, readOnly);
        }
        return new TransactionInterceptor(transactionManager, source);
    }

    @Bean
    public Advisor txAdviceAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* *..*Service*.*(..))");
        return new DefaultPointcutAdvisor(pointcut, txAdvice());
    }

}
