package com.tievd.jyz.plugin;
|
|
import com.tievd.jyz.util.MultiMinioUtil;
|
import org.apache.ibatis.executor.resultset.ResultSetHandler;
|
import org.apache.ibatis.plugin.*;
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import java.lang.reflect.Field;
|
import java.sql.Statement;
|
import java.util.List;
|
|
/**
|
* Author: wqy
|
*/
|
@Intercepts({@Signature(
|
type= ResultSetHandler.class,
|
method = "handleResultSets",
|
args = {Statement.class})})
|
public class EventInterceptPlugin implements Interceptor {
|
|
@Autowired
|
MultiMinioUtil s3Util;
|
|
/**
|
* 全局s3路径转换
|
* @param invocation
|
* @return
|
* @throws Throwable
|
*/
|
@Override
|
public Object intercept(Invocation invocation) throws Throwable {
|
Object returnValue = invocation.proceed();
|
if (returnValue != null && returnValue instanceof List) {
|
try {
|
List dataList = (List) returnValue;
|
if (dataList.size() == 0) {
|
return returnValue;
|
}
|
Class clazz = dataList.get(0).getClass();
|
if (clazz.getAnnotation(S3DataParse.class) == null) {
|
return returnValue;
|
}
|
Field[] fields = clazz.getDeclaredFields();
|
for (Field field : fields) {
|
if (field.getAnnotation(S3DataParse.class) == null) {
|
continue;
|
}
|
field.setAccessible(true);
|
String split = field.getAnnotation(S3DataParse.class).value();
|
for (Object o : dataList) {
|
Object val = field.get(o);
|
String path[] = String.valueOf(val).split(split);
|
if (path.length == 2) {
|
String url = s3Util.getObjectURL(path[0], path[1], 6000);
|
field.set(o, url);
|
}
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
return returnValue;
|
}
|
|
@Override
|
public Object plugin(Object target) {
|
if (target instanceof ResultSetHandler) {
|
return Plugin.wrap(target, this);
|
}
|
return target;
|
}
|
}
|