|
@@ -3,21 +3,22 @@ package com.sckw.core.aspect;
|
|
|
import com.sckw.core.model.auth.context.LoginUserHolder;
|
|
import com.sckw.core.model.auth.context.LoginUserHolder;
|
|
|
import com.sckw.core.model.constant.Global;
|
|
import com.sckw.core.model.constant.Global;
|
|
|
import com.sckw.core.utils.BeanUtils;
|
|
import com.sckw.core.utils.BeanUtils;
|
|
|
|
|
+import com.sckw.core.utils.CollectionUtils;
|
|
|
import com.sckw.core.utils.IdWorker;
|
|
import com.sckw.core.utils.IdWorker;
|
|
|
-import com.sckw.core.utils.StringUtils;
|
|
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
-import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
|
+
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* DAO切面,插入创建人,创建时间,修改人,修改时间
|
|
* DAO切面,插入创建人,创建时间,修改人,修改时间
|
|
|
|
|
+ *
|
|
|
* @author zk
|
|
* @author zk
|
|
|
* @date Oct 29, 2018
|
|
* @date Oct 29, 2018
|
|
|
*/
|
|
*/
|
|
@@ -51,20 +52,24 @@ public class DaoAspect {
|
|
|
}
|
|
}
|
|
|
HttpServletRequest request = attributes.getRequest();*/
|
|
HttpServletRequest request = attributes.getRequest();*/
|
|
|
Object[] objects = point.getArgs();
|
|
Object[] objects = point.getArgs();
|
|
|
- if (objects != null && objects.length > 0) {
|
|
|
|
|
|
|
+ if (objects != null) {
|
|
|
for (Object arg : objects) {
|
|
for (Object arg : objects) {
|
|
|
- Long userId = LoginUserHolder.getUserId();
|
|
|
|
|
- String userName = LoginUserHolder.getUserName();
|
|
|
|
|
- if (isProperty(arg, UPDATE_BY) && Objects.isNull(BeanUtils.getProperty(arg, UPDATE_BY)) && userId != null) {
|
|
|
|
|
- BeanUtils.setProperty(arg, UPDATE_BY, userId);
|
|
|
|
|
- }
|
|
|
|
|
- if (isProperty(arg, UPDATE_TIME) && Objects.isNull(BeanUtils.getProperty(arg, UPDATE_TIME))) {
|
|
|
|
|
- BeanUtils.setProperty(arg, UPDATE_TIME, new Date());
|
|
|
|
|
|
|
+ List<Object> objs = CollectionUtils.objectCastList(arg, Object.class);
|
|
|
|
|
+ if (CollectionUtils.isEmpty(objs)){
|
|
|
|
|
+ objs.add(arg);
|
|
|
}
|
|
}
|
|
|
|
|
+ Long userId = LoginUserHolder.getUserId();
|
|
|
|
|
+ objs.forEach(obj->{
|
|
|
|
|
+ if (isProperty(obj, UPDATE_BY) && Objects.isNull(BeanUtils.getProperty(obj, UPDATE_BY)) && userId != null) {
|
|
|
|
|
+ BeanUtils.setProperty(obj, UPDATE_BY, userId);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isProperty(obj, UPDATE_TIME) && Objects.isNull(BeanUtils.getProperty(obj, UPDATE_TIME))) {
|
|
|
|
|
+ BeanUtils.setProperty(obj, UPDATE_TIME, new Date());
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- Object object = point.proceed();
|
|
|
|
|
- return object;
|
|
|
|
|
|
|
+ return point.proceed();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Around("daoCreate()")
|
|
@Around("daoCreate()")
|
|
@@ -75,26 +80,24 @@ public class DaoAspect {
|
|
|
}
|
|
}
|
|
|
HttpServletRequest request = attributes.getRequest();*/
|
|
HttpServletRequest request = attributes.getRequest();*/
|
|
|
Object[] objects = point.getArgs();
|
|
Object[] objects = point.getArgs();
|
|
|
- if (objects != null && objects.length > 0) {
|
|
|
|
|
|
|
+ if (objects != null) {
|
|
|
for (Object arg : objects) {
|
|
for (Object arg : objects) {
|
|
|
- Long userId = LoginUserHolder.getUserId();
|
|
|
|
|
|
|
+ List<Object> objs = CollectionUtils.objectCastList(arg, Object.class);
|
|
|
|
|
+ if (CollectionUtils.isEmpty(objs)){
|
|
|
|
|
+ objs.add(arg);
|
|
|
|
|
+ }
|
|
|
Date date = new Date();
|
|
Date date = new Date();
|
|
|
- if (userId != null) {
|
|
|
|
|
- if (isProperty(arg, CREATE_BY) && Objects.isNull(BeanUtils.getProperty(arg, CREATE_BY))) {
|
|
|
|
|
- BeanUtils.setProperty(arg, CREATE_BY, userId);
|
|
|
|
|
|
|
+ objs.forEach(obj -> {
|
|
|
|
|
+ Long userId = LoginUserHolder.getUserId();
|
|
|
|
|
+ if (userId == null) {
|
|
|
|
|
+ userId = 1L;
|
|
|
}
|
|
}
|
|
|
- if (isProperty(arg, UPDATE_BY) && Objects.isNull(BeanUtils.getProperty(arg, UPDATE_BY))) {
|
|
|
|
|
- BeanUtils.setProperty(arg, UPDATE_BY, userId);
|
|
|
|
|
|
|
+ if (isProperty(obj, CREATE_BY) && Objects.isNull(BeanUtils.getProperty(obj, CREATE_BY))) {
|
|
|
|
|
+ BeanUtils.setProperty(obj, CREATE_BY, userId);
|
|
|
}
|
|
}
|
|
|
- } else {
|
|
|
|
|
- userId = 1L;
|
|
|
|
|
- if (isProperty(arg, CREATE_BY) && Objects.isNull(BeanUtils.getProperty(arg, CREATE_BY))) {
|
|
|
|
|
- BeanUtils.setProperty(arg, CREATE_BY, userId);
|
|
|
|
|
|
|
+ if (isProperty(obj, UPDATE_BY) && Objects.isNull(BeanUtils.getProperty(obj, UPDATE_BY))) {
|
|
|
|
|
+ BeanUtils.setProperty(obj, UPDATE_BY, userId);
|
|
|
}
|
|
}
|
|
|
- if (isProperty(arg, UPDATE_BY) && Objects.isNull(BeanUtils.getProperty(arg, UPDATE_BY))) {
|
|
|
|
|
- BeanUtils.setProperty(arg, UPDATE_BY, userId);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
// if (userName != null){
|
|
// if (userName != null){
|
|
|
// if (isProperty(arg, CREATE_BY_NAME) && StringUtils.isBlank(BeanUtils.getProperty(arg, CREATE_BY_NAME))) {
|
|
// if (isProperty(arg, CREATE_BY_NAME) && StringUtils.isBlank(BeanUtils.getProperty(arg, CREATE_BY_NAME))) {
|
|
@@ -105,32 +108,32 @@ public class DaoAspect {
|
|
|
// }
|
|
// }
|
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
- if (isProperty(arg, CREATE_TIME) && Objects.isNull(BeanUtils.getProperty(arg, CREATE_TIME))) {
|
|
|
|
|
- BeanUtils.setProperty(arg, CREATE_TIME, date);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (isProperty(obj, CREATE_TIME) && Objects.isNull(BeanUtils.getProperty(obj, CREATE_TIME))) {
|
|
|
|
|
+ BeanUtils.setProperty(obj, CREATE_TIME, date);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (isProperty(arg, UPDATE_TIME) && Objects.isNull(BeanUtils.getProperty(arg, UPDATE_TIME))) {
|
|
|
|
|
- BeanUtils.setProperty(arg, UPDATE_TIME, date);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (isProperty(obj, UPDATE_TIME) && Objects.isNull(BeanUtils.getProperty(obj, UPDATE_TIME))) {
|
|
|
|
|
+ BeanUtils.setProperty(obj, UPDATE_TIME, date);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (isProperty(arg, ID) && Objects.isNull(BeanUtils.getProperty(arg, ID))) {
|
|
|
|
|
- BeanUtils.setProperty(arg, ID, new IdWorker(1).nextId());
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (isProperty(obj, ID) && Objects.isNull(BeanUtils.getProperty(obj, ID))) {
|
|
|
|
|
+ BeanUtils.setProperty(obj, ID, new IdWorker(1).nextId());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (isProperty(arg, STATUS) && Objects.isNull(BeanUtils.getProperty(arg, STATUS))) {
|
|
|
|
|
- BeanUtils.setProperty(arg, STATUS, Global.NO);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (isProperty(obj, STATUS) && Objects.isNull(BeanUtils.getProperty(obj, STATUS))) {
|
|
|
|
|
+ BeanUtils.setProperty(obj, STATUS, Global.NO);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (isProperty(arg, DEL_FLAG) && Objects.isNull((BeanUtils.getProperty(arg, DEL_FLAG)))) {
|
|
|
|
|
- BeanUtils.setProperty(arg, DEL_FLAG, Global.NO);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (isProperty(obj, DEL_FLAG) && Objects.isNull((BeanUtils.getProperty(obj, DEL_FLAG)))) {
|
|
|
|
|
+ BeanUtils.setProperty(obj, DEL_FLAG, Global.NO);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- Object object = point.proceed();
|
|
|
|
|
- return object;
|
|
|
|
|
|
|
+ return point.proceed();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static boolean isProperty(Object bean, String field){
|
|
|
|
|
|
|
+ public static boolean isProperty(Object bean, String field) {
|
|
|
return BeanUtils.isProperty(bean, field);
|
|
return BeanUtils.isProperty(bean, field);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|