InsertAndUpdateAspect.java
3.72 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.bs.common.aop;
import com.bs.common.core.domain.BaseEntity;
import com.bs.common.exception.CustomException;
import com.bs.common.utils.SecurityUtils;
import com.bs.common.utils.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import java.util.Collection;
import java.util.Date;
/**
* @author danmo
*/
@Aspect
@Configuration
public class InsertAndUpdateAspect {
@Pointcut("execution( * com.bs.*.mapper.*.*Mapper.insert*(..)))")
public void executeInsert() {
}
@Pointcut("execution( * com.bs.*.mapper.*.*Mapper.update*(..)))")
public void executeUpdate() {
}
@Before(value = "executeInsert()")
public void insertHandle(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
if (args.length > 0L) {
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
if (arg instanceof Collection) {
Collection collection = (Collection) arg;
for (Object next : collection) {
isInsertBaseEntity(next);
}
} else if (arg instanceof BaseEntity) {
isInsertBaseEntity(arg);
}
}
}
}
private void isInsertBaseEntity(Object arg) {
if (arg instanceof BaseEntity) {
BaseEntity next1 = (BaseEntity) arg;
String userName = getUserName();
if(StringUtils.isEmpty(next1.getCreateBy())){
next1.setCreateBy(userName);
}
if(StringUtils.isEmpty(next1.getUpdateBy())){
next1.setUpdateBy(userName);
}
Date date = new Date();
if (next1.getCreateTime() == null) {
next1.setCreateTime(date);
}
if (next1.getUpdateTime() == null) {
next1.setUpdateTime(date);
}
}
}
private String getUserName() {
String userName;
try {
userName = SecurityUtils.getUserName();
} catch (CustomException e){
userName = "admin";
}
return userName;
}
@Before(value = "executeUpdate()")
public void updateHandle(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
if (args.length > 0L) {
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
if (arg instanceof Collection) {
Collection collection = (Collection) arg;
for (Object next : collection) {
if (next instanceof BaseEntity) {
BaseEntity next1 = (BaseEntity) next;
if(StringUtils.isEmpty(next1.getUpdateBy())){
next1.setUpdateBy(getUserName());
}
Date date = new Date();
if (next1.getUpdateTime() == null) {
next1.setUpdateTime(date);
}
}
}
} else if (arg instanceof BaseEntity) {
BaseEntity arg1 = (BaseEntity) arg;
if(StringUtils.isEmpty(arg1.getUpdateBy())){
arg1.setUpdateBy(getUserName());
}
Date date = new Date();
if (arg1.getUpdateTime() == null) {
arg1.setUpdateTime(date);
}
}
}
}
}
}