package com.dy.pipIrrGlobal.config;
|
|
/**
|
* @Author: liurunyu
|
* @Date: 2025/4/27 9:19
|
* @Description
|
*/
|
import com.dy.pipIrrGlobal.pojoSe.SeVirtualCard;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.ibatis.executor.Executor;
|
import org.apache.ibatis.mapping.MappedStatement;
|
import org.apache.ibatis.mapping.SqlCommandType;
|
import org.apache.ibatis.plugin.Interceptor;
|
import org.apache.ibatis.plugin.Intercepts;
|
import org.apache.ibatis.plugin.Invocation;
|
import org.apache.ibatis.plugin.Signature;
|
import java.lang.reflect.Method;
|
|
/**
|
* 拦截虚拟卡表的更新操作,使不能设置占用状态操作。
|
*/
|
@Intercepts({
|
@Signature(
|
type = Executor.class,
|
method = "update",
|
args = {
|
MappedStatement.class,
|
Object.class
|
}
|
)
|
})
|
@Slf4j
|
public class VirtualCardInUseInterceptor implements Interceptor {
|
|
static int MAPPED_STATEMENT_INDEX = 0;
|
static int PARAMETER_INDEX = 1;
|
static String BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETINUSE = "setInUse";
|
|
@Override
|
public Object intercept(Invocation invocation) throws Throwable {
|
Object entity = invocation.getArgs()[PARAMETER_INDEX];
|
if (entity instanceof SeVirtualCard) {
|
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[MAPPED_STATEMENT_INDEX];
|
SqlCommandType commandType = mappedStatement.getSqlCommandType();
|
if (commandType.equals(SqlCommandType.INSERT)) {
|
Class<?> entityClass = entity.getClass();
|
Method setInUse = null;
|
try {
|
setInUse = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETINUSE, Long.class);
|
} catch (Exception e) {
|
//当entityClass没有setInUse方法时,会抛出异常
|
}
|
if (setInUse != null) {
|
setInUse.invoke(entity, 0);
|
}
|
invocation.getArgs()[PARAMETER_INDEX] = entity;
|
}
|
}
|
return invocation.proceed();
|
}
|
}
|