|
|
@@ -6,8 +6,10 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import com.jzg.commons.aop.OperatorTrajectory;
|
|
|
import com.jzg.commons.async.AsyncFactory;
|
|
|
import com.jzg.commons.async.AsyncManager;
|
|
|
+import com.jzg.commons.constants.OperationTypeEnum;
|
|
|
import com.jzg.commons.core.base.BaseController;
|
|
|
import com.jzg.commons.core.page.HttpResult;
|
|
|
+import com.jzg.commons.entity.enums.YesNoEnum;
|
|
|
import com.jzg.commons.exception.SystemException;
|
|
|
import com.jzg.commons.service.OperatorTrajectoryService;
|
|
|
import com.jzg.commons.util.GetSelectIdDataUtils;
|
|
|
@@ -22,8 +24,14 @@ import org.aspectj.lang.annotation.AfterThrowing;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
import org.aspectj.lang.annotation.Before;
|
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.context.expression.MethodBasedEvaluationContext;
|
|
|
+import org.springframework.core.DefaultParameterNameDiscoverer;
|
|
|
+import org.springframework.core.ParameterNameDiscoverer;
|
|
|
+import org.springframework.expression.EvaluationContext;
|
|
|
+import org.springframework.expression.Expression;
|
|
|
+import org.springframework.expression.ExpressionParser;
|
|
|
+import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
@@ -38,77 +46,108 @@ import java.util.Objects;
|
|
|
@RequiredArgsConstructor
|
|
|
public class OperatorTrajectoryAspect {
|
|
|
|
|
|
-
|
|
|
- @Autowired
|
|
|
@Lazy
|
|
|
- OperatorTrajectoryService operatorTrajectoryService;
|
|
|
+ private final OperatorTrajectoryService operatorTrajectoryService;
|
|
|
+
|
|
|
+ private final BaseController baseController;
|
|
|
+
|
|
|
+ private final ThreadLocal<MethodContext> methodContext = new ThreadLocal<>();
|
|
|
+ private final ExpressionParser parser = new SpelExpressionParser();
|
|
|
+ private final ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
|
|
|
|
|
|
- @Autowired
|
|
|
- private BaseController baseController;
|
|
|
|
|
|
- private ThreadLocal<MethodContext> methodContext = new ThreadLocal<>();
|
|
|
|
|
|
@AfterReturning(pointcut = "@annotation(com.jzg.commons.aop.OperatorTrajectory)",returning = "result")
|
|
|
public void doAfter(JoinPoint joinPoint, HttpResult result) {
|
|
|
- MethodContext context = methodContext.get();
|
|
|
- Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
|
|
|
- Object arg = joinPoint.getArgs()[0];
|
|
|
+ try {
|
|
|
+ MethodContext context = methodContext.get();
|
|
|
+ if (context == null) {
|
|
|
+ log.warn("Method context is null for pointcut: {}", joinPoint.getSignature());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
|
|
|
+ OperatorTrajectory annotation = method.getAnnotation(OperatorTrajectory.class);
|
|
|
+ if (!joinPoint.getSignature().getName().equals(context.getMethodName())) {
|
|
|
+ log.warn("Mismatched method name in context: {}", context.getMethodName());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String id = context.getId();
|
|
|
+ if (StringUtils.isBlank(id)) {
|
|
|
+ throw new SystemException("线程上下文异常,请重试");
|
|
|
+ }
|
|
|
|
|
|
- OperatorTrajectory annotation = method.getAnnotation(OperatorTrajectory.class);
|
|
|
- if (context != null) {
|
|
|
- // 验证结果
|
|
|
- String methodName = context.getMethodName();
|
|
|
- if(joinPoint.getSignature().getName().equals(methodName)){
|
|
|
- if(Objects.isNull(context.getId())){
|
|
|
- throw new SystemException("线程上下文异常,请重试");
|
|
|
- }
|
|
|
- com.jzg.commons.entity.po.OperatorTrajectory byId = operatorTrajectoryService.getById(context.getId());
|
|
|
- if(result.getCode() == 200){
|
|
|
- if(byId.getOperatorType().equals("C")){
|
|
|
- JSONObject agr = JSON.parseObject(JSONObject.toJSONString(arg));
|
|
|
- byId.setPrimaryValue(agr.getString(annotation.primary()));
|
|
|
- }
|
|
|
- byId.setExecuteStatus(1);
|
|
|
- }else{
|
|
|
- byId.setExecuteStatus(2);
|
|
|
- byId.setErrorMsg(result.getMsg());
|
|
|
+ com.jzg.commons.entity.po.OperatorTrajectory byId = operatorTrajectoryService.getById(id);
|
|
|
+ if (byId == null) {
|
|
|
+ log.error("Operator trajectory record not found for ID: {}", id);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (result.getCode() == 200) {
|
|
|
+ if ("C".equals(byId.getOperatorType())) {
|
|
|
+ byId.setPrimaryValue(getId(joinPoint, annotation.primary()));
|
|
|
}
|
|
|
- operatorTrajectoryService.updateById(byId);
|
|
|
+ byId.setExecuteStatus(1); // 成功状态
|
|
|
+ } else {
|
|
|
+ byId.setExecuteStatus(2); // 失败状态
|
|
|
+ byId.setErrorMsg(result.getMsg());
|
|
|
}
|
|
|
+ operatorTrajectoryService.updateById(byId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Error occurred in doAfter", e);
|
|
|
+ } finally {
|
|
|
+ methodContext.remove();
|
|
|
}
|
|
|
- // 清除上下文
|
|
|
- methodContext.remove();
|
|
|
}
|
|
|
|
|
|
|
|
|
//拦截异常消息
|
|
|
@AfterThrowing(pointcut = "@annotation(com.jzg.commons.aop.OperatorTrajectory)", throwing = "ex")
|
|
|
public void doAfterThrowing(JoinPoint joinPoint, Throwable ex) {
|
|
|
- MethodContext context = methodContext.get();
|
|
|
- if (context != null) {
|
|
|
+ try {
|
|
|
+ MethodContext context = methodContext.get();
|
|
|
+ if (context == null) {
|
|
|
+ log.warn("获取上下文异常: {}", joinPoint.getSignature());
|
|
|
+ return;
|
|
|
+ }
|
|
|
String methodName = context.getMethodName();
|
|
|
- if(joinPoint.getSignature().getName().equals(methodName)){
|
|
|
- com.jzg.commons.entity.po.OperatorTrajectory byId = operatorTrajectoryService.getById(context.getId());
|
|
|
- byId.setExecuteStatus(2);
|
|
|
- byId.setErrorMsg(ex.getMessage());
|
|
|
- operatorTrajectoryService.updateById(byId);
|
|
|
+ if (!joinPoint.getSignature().getName().equals(methodName)) {
|
|
|
+ log.warn("方法名称不一致: {}", methodName);
|
|
|
+ return;
|
|
|
}
|
|
|
+ String id = context.getId();
|
|
|
+ com.jzg.commons.entity.po.OperatorTrajectory byId = operatorTrajectoryService.getById(id);
|
|
|
+ if (byId == null) {
|
|
|
+ log.error("获取上下文ID异常: {}", id);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ byId.setExecuteStatus(2); // 失败状态
|
|
|
+ byId.setErrorMsg(ex.getMessage());
|
|
|
+ operatorTrajectoryService.updateById(byId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Error occurred in doAfterThrowing", e);
|
|
|
+ } finally {
|
|
|
+ methodContext.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getId(JoinPoint joinPoint, String key) {
|
|
|
+ Object arg = joinPoint.getArgs()[0];
|
|
|
+ if (arg instanceof Collection) {
|
|
|
+ return StringUtils.join(arg);
|
|
|
+ } else if (arg instanceof String) {
|
|
|
+ return String.valueOf(arg);
|
|
|
+ } else if (arg != null) {
|
|
|
+ JSONObject jsonObject = (JSONObject) JSON.toJSON(arg);
|
|
|
+ return jsonObject.getString(key);
|
|
|
}
|
|
|
- // 清除上下文
|
|
|
- methodContext.remove();
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Before("@annotation(operatorTrajectory)")
|
|
|
public void doBefore(JoinPoint point, OperatorTrajectory operatorTrajectory){
|
|
|
Object obj = point.getArgs()[0];
|
|
|
- String id;
|
|
|
- if (obj instanceof String){
|
|
|
- id = obj.toString();
|
|
|
- }else if (obj instanceof Collection){
|
|
|
- id = StringUtils.join(obj);
|
|
|
- }else {
|
|
|
- id = JSON.parseObject(JSONObject.toJSONString(obj)).getString(operatorTrajectory.primary());
|
|
|
- }
|
|
|
+ String id = getId(point,operatorTrajectory.primary());
|
|
|
if (isRecorded(operatorTrajectory.isRecorded(),obj)){
|
|
|
return;
|
|
|
}
|
|
|
@@ -129,6 +168,13 @@ public class OperatorTrajectoryAspect {
|
|
|
}
|
|
|
Object beforeData;
|
|
|
String operatorName = operatorTrajectory.OperationDict().getName();
|
|
|
+ if (StringUtils.isNotEmpty(operatorTrajectory.enableDisableDict())){
|
|
|
+ if (getEnableDisable(point, operatorTrajectory)){
|
|
|
+ operatorName = OperationTypeEnum.ENABLED.getName();
|
|
|
+ }else {
|
|
|
+ operatorName = OperationTypeEnum.DISABLED.getName();
|
|
|
+ }
|
|
|
+ }
|
|
|
switch(operatorTrajectory.type()){
|
|
|
//创建
|
|
|
case "C":
|
|
|
@@ -153,6 +199,8 @@ public class OperatorTrajectoryAspect {
|
|
|
trajectory.setOperationContent(DescriptionUtils.generateDescription(beforeData,operatorName));
|
|
|
operatorTrajectoryService.save(trajectory);
|
|
|
break;
|
|
|
+ default:
|
|
|
+ log.warn("Unsupported operator type: {}", operatorTrajectory.type());
|
|
|
|
|
|
}
|
|
|
// 用于验证上下文
|
|
|
@@ -160,6 +208,31 @@ public class OperatorTrajectoryAspect {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ public boolean getEnableDisable(JoinPoint point, OperatorTrajectory operatorTrajectory){
|
|
|
+ Method method = ((MethodSignature) point.getSignature()).getMethod();
|
|
|
+ EvaluationContext context = new MethodBasedEvaluationContext(
|
|
|
+ null, method, point.getArgs(), parameterNameDiscoverer);
|
|
|
+ Expression expression = parser.parseExpression(operatorTrajectory.enableDisableDict());
|
|
|
+ Object value = expression.getValue(context);
|
|
|
+ if (value instanceof Boolean){
|
|
|
+ return (Boolean) value;
|
|
|
+ }else if (value instanceof String){
|
|
|
+ return Boolean.parseBoolean(String.valueOf(value));
|
|
|
+ }else if (value instanceof Integer){
|
|
|
+ return Integer.parseInt(String.valueOf(value)) == 1;
|
|
|
+ }else if (value instanceof Enum){
|
|
|
+ Class<?> enumClass = value.getClass();
|
|
|
+ Enum<?> enumObj = (Enum<?>) enumClass.cast(value);
|
|
|
+ if (YesNoEnum.YES.equals(enumObj)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
public boolean isRecorded(String isRecorded,Object obj) {
|
|
|
if(StringUtils.isNotBlank(isRecorded)){
|
|
|
Map<String,Object> map = JSONObject.parseObject(JSONObject.toJSONString(obj), Map.class);
|