中央热水项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.8 KiB

package com.mh.user.aspect;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import com.mh.common.utils.StringUtils;
import com.mh.user.utils.SecurityUtils;
import org.apache.commons.beanutils.BeanUtils;
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.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* DAO切面,插入创建人,创建时间,修改人,修改时间
* @author Louis
* @date Oct 29, 2018
*/
@Aspect
@Component
@Configuration
public class DaoAspect {
private static final String createBy = "createBy";
private static final String createTime = "createTime";
private static final String lastUpdateBy = "lastUpdateBy";
private static final String lastUpdateTime = "lastUpdateTime";
@Pointcut("execution(* com.mh.*.mapper.*.update*(..))")
public void daoUpdate() {
}
@Pointcut("execution(* com.mh.*.mapper.*.insert*(..))")
public void daoCreate() {
}
@Around("daoUpdate()")
public Object doAroundUpdate(ProceedingJoinPoint pjp) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return pjp.proceed();
}
HttpServletRequest request = attributes.getRequest();
String token = request.getHeader("token");
String username = getUserName();
if (token != null && username != null) {
Object[] objects = pjp.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
BeanUtils.setProperty(arg, lastUpdateBy, username);
BeanUtils.setProperty(arg, lastUpdateTime, new Date());
}
}
}
Object object = pjp.proceed();
return object;
}
@Around("daoCreate()")
public Object doAroundCreate(ProceedingJoinPoint pjp) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return pjp.proceed();
}
Object[] objects = pjp.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
String username = getUserName();
if (username != null) {
if (StringUtils.isBlank(BeanUtils.getProperty(arg, createBy))) {
BeanUtils.setProperty(arg, createBy, username);
}
if (StringUtils.isBlank(BeanUtils.getProperty(arg, createTime))) {
BeanUtils.setProperty(arg, createTime, new Date());
}
}
}
}
Object object = pjp.proceed();
return object;
}
private String getUserName() {
return SecurityUtils.getUsername();
}
}