package com.thhy.general.config;
|
|
import com.thhy.general.annotations.Idkey;
|
import com.thhy.general.utils.UUIDUtils;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Pointcut;
|
import org.springframework.stereotype.Component;
|
|
import java.lang.annotation.Annotation;
|
import java.lang.reflect.Field;
|
import java.lang.reflect.Method;
|
import java.util.HashMap;
|
import java.util.LinkedHashMap;
|
import java.util.Map;
|
|
@Component
|
@Aspect
|
public class InsertAop {
|
|
@Pointcut("execution(public * com.thhy.*.modules.*.*.mapper.*.*(..))")
|
public void pointcut(){
|
|
}
|
|
@Around("pointcut()")
|
public Object invoke(ProceedingJoinPoint invocation) throws Throwable {
|
String methodName = invocation.getSignature().getName();
|
|
if(methodName.contains("insert")||methodName.contains("Insert")){
|
Object j = invocation.getArgs()[0];
|
if(j!=null){
|
String className = j.getClass().getName();
|
Class methodargClazz;
|
if(HashMap.class.getName().equals(className)){
|
methodargClazz = HashMap.class;
|
}else{
|
methodargClazz = Map.class;
|
}
|
if(HashMap.class.getName().equals(className)|| LinkedHashMap.class.getName().equals(className)){
|
System.out.println("Map 类型");
|
Annotation annotation = Class.forName(invocation.getSignature().getDeclaringType().getName()).getMethod(invocation.getSignature().getName(), methodargClazz).getParameters()[0].getDeclaredAnnotation(Idkey.class);
|
Idkey idkey = (Idkey) annotation;
|
|
Class clazz = Class.forName(j.getClass().getName());
|
Method method = clazz.getMethod("put",new Class[] { Object.class, Object.class });
|
method.invoke(j,idkey.value(),UUIDUtils.mongo());
|
}else{
|
Class clazz = Class.forName(j.getClass().getName());
|
Field[] fields = clazz.getDeclaredFields();
|
for(Field field : fields){
|
Annotation annotation = field.getAnnotation(Idkey.class);
|
if(annotation!=null){
|
//String value = annotation.
|
System.out.println(field.getName());
|
field.setAccessible(true);
|
field.set(j, UUIDUtils.mongo());
|
break;
|
}
|
}
|
}
|
}
|
|
return invocation.proceed();
|
}else{
|
return invocation.proceed();
|
}
|
}
|
|
|
}
|