/*
 * Copyright (c) 2018 無色眼镜
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package club.dawdler.core.db.mybatis.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import club.dawdler.core.db.datasource.WriteDataSourceResolver;
import club.dawdler.core.db.mybatis.DawdlerMybatisTransaction;
import club.dawdler.core.tx.transaction.LocalConnectionFactory;
import club.dawdler.util.TLS;

/**
 * @author jackson.song
 * @version V1.0
 * 通过aop切换数据库连接
 */
@Aspect
public class SwitchConnectionAspect {

	@Around("execution(*  org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(..)) && args(String,Object,org.apache.ibatis.session.RowBounds)")
	public Object select(ProceedingJoinPoint pjp) throws Throwable {
		try {
			TLS.set(DawdlerMybatisTransaction.CURRENT_CONNECTION, LocalConnectionFactory.getReadConnection());
			return pjp.proceed();
		} catch (Throwable t) {
			throw t;
		} finally {
			TLS.remove(DawdlerMybatisTransaction.CURRENT_CONNECTION);
		}
	}

	@Around("execution(* org.apache.ibatis.session.defaults.DefaultSqlSession.selectCursor(..)) && args(String,Object,org.apache.ibatis.session.RowBounds)")
	public Object selectCursor(ProceedingJoinPoint pjp) throws Throwable {
		try {
			TLS.set(DawdlerMybatisTransaction.CURRENT_CONNECTION, LocalConnectionFactory.getReadConnection());
			return pjp.proceed();
		} catch (Throwable t) {
			throw t;
		} finally {
			TLS.remove(DawdlerMybatisTransaction.CURRENT_CONNECTION);
		}
	}

	@Around("execution(* org.apache.ibatis.session.defaults.DefaultSqlSession.select(..)) && args(String,Object,org.apache.ibatis.session.RowBounds,org.apache.ibatis.session.ResultHandler)")
	public Object selectWithResultHandler(ProceedingJoinPoint pjp) throws Throwable {
		try {
			TLS.set(DawdlerMybatisTransaction.CURRENT_CONNECTION, LocalConnectionFactory.getReadConnection());
			return pjp.proceed();
		} catch (Throwable t) {
			throw t;
		} finally {
			TLS.remove(DawdlerMybatisTransaction.CURRENT_CONNECTION);
		}
	}

	@Around("execution(* org.apache.ibatis.session.defaults.DefaultSqlSession.update(..)) && args(String,Object)")
	public Object update(ProceedingJoinPoint pjp) throws Throwable {
		try {
			TLS.set(DawdlerMybatisTransaction.CURRENT_CONNECTION, LocalConnectionFactory
					.getWriteConnection(WriteDataSourceResolver.resolveMapperDataSource((String) pjp.getArgs()[0])));
			return pjp.proceed();
		} catch (Throwable t) {
			throw t;
		} finally {
			TLS.remove(DawdlerMybatisTransaction.CURRENT_CONNECTION);
		}
	}

}
