5 changed files with 167 additions and 0 deletions
@ -0,0 +1,23 @@ |
|||
package org.nl.wms.ext.record.core; |
|||
|
|||
import java.lang.annotation.ElementType; |
|||
import java.lang.annotation.Retention; |
|||
import java.lang.annotation.RetentionPolicy; |
|||
import java.lang.annotation.Target; |
|||
|
|||
/** |
|||
* 对接记录注解 |
|||
* @Author: lyd |
|||
* @Date: 2024/9/23 |
|||
*/ |
|||
@Target(ElementType.METHOD) |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
public @interface Record { |
|||
/** 对接名称 */ |
|||
String interactName() default ""; |
|||
/** |
|||
* 方向 |
|||
* @see RecordDefinition |
|||
* */ |
|||
String direction() default ""; |
|||
} |
@ -0,0 +1,99 @@ |
|||
package org.nl.wms.ext.record.core; |
|||
|
|||
import cn.hutool.core.util.StrUtil; |
|||
import cn.hutool.json.JSONUtil; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.aspectj.lang.JoinPoint; |
|||
import org.aspectj.lang.ProceedingJoinPoint; |
|||
import org.aspectj.lang.annotation.AfterThrowing; |
|||
import org.aspectj.lang.annotation.Around; |
|||
import org.aspectj.lang.annotation.Aspect; |
|||
import org.aspectj.lang.annotation.Pointcut; |
|||
import org.aspectj.lang.reflect.MethodSignature; |
|||
import org.nl.common.utils.RequestHolder; |
|||
import org.nl.common.utils.StringUtils; |
|||
import org.nl.common.utils.ThrowableUtil; |
|||
import org.nl.system.service.logging.dao.SysLog; |
|||
import org.nl.wms.ext.record.service.ISysInteractRecordService; |
|||
import org.nl.wms.ext.record.service.dao.SysInteractRecord; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestParam; |
|||
import org.springframework.web.context.request.RequestContextHolder; |
|||
import org.springframework.web.context.request.ServletRequestAttributes; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
import javax.servlet.http.HttpServletResponse; |
|||
import java.lang.reflect.Method; |
|||
import java.lang.reflect.Parameter; |
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @Author: lyd |
|||
* @Date: 2024/9/23 |
|||
*/ |
|||
@Component |
|||
@Aspect |
|||
@Slf4j |
|||
public class RecordAspect { |
|||
|
|||
@Autowired |
|||
private ISysInteractRecordService sysInteractRecordService; |
|||
|
|||
@Pointcut("@annotation(org.nl.wms.ext.record.core.Record)") |
|||
public void logPointcut() { |
|||
// 该方法无方法体,主要为了让同类中其他方法使用此切入点
|
|||
} |
|||
|
|||
@Around("logPointcut()") |
|||
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { |
|||
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
|||
Method method = signature.getMethod(); |
|||
Record annotation = method.getAnnotation(Record.class); |
|||
// 方法路径
|
|||
String params = getParameter(method, joinPoint.getArgs()); |
|||
Object result = joinPoint.proceed(); |
|||
sysInteractRecordService.saveRecord(params, result, annotation.direction(), annotation.interactName()); |
|||
return result; |
|||
} |
|||
|
|||
/** |
|||
* 根据方法和传入的参数获取请求参数 |
|||
*/ |
|||
|
|||
private String getParameter(Method method, Object[] args) { |
|||
List<Object> argList = new ArrayList<>(); |
|||
Parameter[] parameters = method.getParameters(); |
|||
for (int i = 0; i < parameters.length; i++) { |
|||
//将RequestBody注解修饰的参数作为请求参数
|
|||
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class); |
|||
if (requestBody != null) { |
|||
argList.add(args[i]); |
|||
} |
|||
//将RequestParam注解修饰的参数作为请求参数
|
|||
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class); |
|||
if (requestParam != null) { |
|||
Map<String, Object> map = new HashMap<>(); |
|||
String key = parameters[i].getName(); |
|||
if (!StrUtil.isEmpty(requestParam.value())) { |
|||
key = requestParam.value(); |
|||
} |
|||
map.put(key, args[i]); |
|||
argList.add(map); |
|||
} |
|||
} |
|||
if (argList.size() == 0) { |
|||
return ""; |
|||
} |
|||
return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList); |
|||
} |
|||
|
|||
@AfterThrowing(pointcut = "logPointcut()", throwing = "e") |
|||
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { |
|||
log.error("{}", e); |
|||
} |
|||
} |
@ -0,0 +1,17 @@ |
|||
package org.nl.wms.ext.record.core; |
|||
|
|||
/** |
|||
* @Author: lyd |
|||
* @Date: 2024/9/23 |
|||
*/ |
|||
public interface RecordDefinition { |
|||
// 请求方向
|
|||
/**ACS->LMS**/ |
|||
public static final String ACS_LMS = "1"; |
|||
/**LMS->ACS**/ |
|||
public static final String LMS_ACS = "2"; |
|||
/**MES->LMS**/ |
|||
public static final String MES_LMS = "3"; |
|||
/**LMS->MES**/ |
|||
public static final String LMS_MES = "4"; |
|||
} |
Loading…
Reference in new issue