张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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();
        }
    }
 
 
}