net.mingsoft.basic.aop.BaseLogAop Maven / Gradle / Ivy
The newest version!
/**
* Copyright (c) 2012-present 铭软科技(mingsoft.net)
* 本软件及相关文档文件(以下简称“软件”)的版权归 铭软科技 所有
* 遵循 铭软科技《服务协议》中的《保密条款》
*/
package net.mingsoft.basic.aop;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.mutable.MutablePair;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import net.mingsoft.base.entity.BaseEntity;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.biz.ILogBiz;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.entity.LogEntity;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
* @author by 铭软开发团队
* @Description TODO
* @date 2019/11/20 12:04
*/
@Aspect
public abstract class BaseLogAop extends BaseAop{
/**
* 获取用户名
* @return
*/
public abstract String getUserName();
/**
* 是否切面
* @return
*/
public abstract boolean isCut(LogAnn log);
/**
* 切入点
*/
@Pointcut("@annotation(net.mingsoft.basic.annotation.LogAnn)")
public void logPointCut()
{ }
/**
* 处理完请求后执行
*
* @param joinPoint 切点
*/
@AfterReturning(pointcut = "logPointCut()", returning = "result")
public void doAfterReturning(JoinPoint joinPoint, Object result)
{
handleLog(joinPoint, null, result);
}
/**
* 拦截异常操作
*
* @param joinPoint 切点
* @param e 异常
*/
@AfterThrowing(value = "logPointCut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Exception e)
{
handleLog(joinPoint, e, null);
}
/**
* 成功状态
*/
private static final String SUCCESS="success";
/**
* 失败状态
*/
private static final String ERROR="error";
/**
* 日志业务层
*/
@Autowired
private ILogBiz logBiz;
private static final Logger LOG = LoggerFactory.getLogger(SystemLogAop.class);
protected void handleLog(final JoinPoint joinPoint, final Exception e, Object result) {
try{
// 获得注解
LogAnn controllerLog = getAnnotation(joinPoint, LogAnn.class);
if (controllerLog == null){
return;
}
if(!isCut(controllerLog)){
return;
}
LogEntity logEntity = new LogEntity();
//是否保存业务id
if (controllerLog.saveId()){
BaseEntity baseEntity = getType(joinPoint, BaseEntity.class,true);
if (baseEntity != null){
logEntity.setBusinessId(baseEntity.getId());
} else {// 批量操作的情况
ArrayList baseEntities = getType(joinPoint, ArrayList.class);
if (CollectionUtil.isNotEmpty(baseEntities)) {
List ids = (List) baseEntities.stream().map(entity -> {
if (entity instanceof BaseEntity) {
return ((BaseEntity) entity).getId();
}
return null;
}).collect(Collectors.toList());
ids.remove(null);
logEntity.setBusinessId(StrUtil.join(",",ids));
}
}
}
logEntity.setLogUser(getUserName());
logEntity.setLogStatus(SUCCESS);
// 请求的地址
String ip = BasicUtil.getIp();
//设置IP
logEntity.setLogIp(ip);
//设置返回参数
logEntity.setLogResult(JSONUtil.parseObj(result).toJSONString(0, new Filter>() {
@Override
public boolean accept(MutablePair