feat: 对接记录
This commit is contained in:
@@ -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";
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.system.service.param.dao.Param;
|
||||
import org.nl.wms.ext.acs.service.dto.to.BaseResponse;
|
||||
import org.nl.wms.ext.record.service.dao.SysInteractRecord;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -51,6 +52,16 @@ public interface ISysInteractRecordService extends IService<SysInteractRecord> {
|
||||
*/
|
||||
void saveRecord(Object request, BaseResponse response, String direction);
|
||||
|
||||
/**
|
||||
* 创建记录
|
||||
* @param request 、
|
||||
* @param response 、
|
||||
* @param direction 、
|
||||
* @param interactName \
|
||||
*/
|
||||
@Async
|
||||
void saveRecord(String request, Object response, String direction, String interactName);
|
||||
|
||||
/**
|
||||
* 保存记录
|
||||
* @param name 、
|
||||
|
||||
@@ -94,6 +94,23 @@ public class SysInteractRecordServiceImpl extends ServiceImpl<SysInteractRecordM
|
||||
sysInteractRecordMapper.insert(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveRecord(String request, Object response, String direction, String interactName) {
|
||||
String responseJsonString = JSONObject.toJSONString(response);
|
||||
JSONObject responseJson = JSONObject.parseObject(responseJsonString);
|
||||
SysInteractRecord entity = new SysInteractRecord();
|
||||
entity.setInteract_id(IdUtil.getSnowflake(1, 1).nextIdStr());
|
||||
entity.setInteract_name(interactName);
|
||||
entity.setCode(responseJson.getInteger("status"));
|
||||
entity.setMessage(responseJson.getString("message"));
|
||||
entity.setRecord_time(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
||||
entity.setDirection(direction);
|
||||
entity.setRequest_param(request);
|
||||
entity.setResponse_param(JSONObject.toJSONString(response));
|
||||
entity.setIs_success(responseJson.getInteger("status") == HttpStatus.HTTP_OK);
|
||||
sysInteractRecordMapper.insert(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K, V> void saveRecord(String name, K request, V response, String direction) {
|
||||
SysInteractRecord entity = new SysInteractRecord();
|
||||
|
||||
Reference in New Issue
Block a user