package com.cool.store.aspect;

import com.cool.store.annotation.PlatformDB;
import com.cool.store.datasource.DataSourceContextHolder;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 数据源切换 切面
 * </p>
 *
 * @author wangff
 * @since 2025/9/4
 */
@Aspect
@Component
public class DataSourceAspect {

    @Before("@annotation(platformDB)")
    public void before(PlatformDB platformDB) {
        DataSourceContextHolder.setDataSourceType("platform");
    }

    @After("@annotation(platformDB)")
    public void after(PlatformDB platformDB) {
        DataSourceContextHolder.clearDataSourceType();
    }

    @AfterThrowing("@annotation(platformDB)")
    public void afterThrowing(PlatformDB platformDB) {
        DataSourceContextHolder.clearDataSourceType();
    }
}
