package cn.reghao.tnb.content.app.aop;

import cn.reghao.tnb.content.app.config.DataSourceType;
import cn.reghao.tnb.content.app.config.mybatis.DynamicDataSource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

/**
 * @author reghao
 * @date 2023-12-31 03:01:16
 */
@Slf4j
//@Aspect
//@Component
//@EnableAspectJAutoProxy(proxyTargetClass = true)
public class DataSourceAspect {
    @Pointcut("execution(* cn.reghao.tnb.content.app.mall.db.mapper.*.*(..))")
    public void aspect() {
    }

    @Before("aspect()")
    public void before(JoinPoint point) {
        String method = point.getSignature().getName();
        if (method.startsWith("find") || method.startsWith("count")) {
            log.info("method {} hit slave DataSource", method);
            DynamicDataSource.name.set(DataSourceType.slave.getName());
        } else {
            log.info("method {} hit master DataSource", method);
            DynamicDataSource.name.set(DataSourceType.master.getName());
        }
    }
}
