|
|
@@ -0,0 +1,158 @@
|
|
|
+package com.jzg.aop.aspect;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.jzg.aop.OperatorTrajectory;
|
|
|
+import com.jzg.core.base.HistoryDataService;
|
|
|
+import com.jzg.core.page.HttpResult;
|
|
|
+import com.jzg.exception.SystemException;
|
|
|
+import com.jzg.service.OperatorTrajectoryService;
|
|
|
+import com.jzg.util.idgen.IdGenerate;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.annotation.*;
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class OperatorTrajectoryAspect {
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ OperatorTrajectoryService operatorTrajectoryService;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ApplicationContext applicationContext;
|
|
|
+
|
|
|
+ private ThreadLocal<MethodContext> methodContext = new ThreadLocal<>();
|
|
|
+
|
|
|
+ @AfterReturning(pointcut = "@annotation(com.jzg.aop.OperatorTrajectory)",returning = "result")
|
|
|
+ public void doAfter(JoinPoint joinPoint, HttpResult result) {
|
|
|
+ MethodContext context = methodContext.get();
|
|
|
+ Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
|
|
|
+ 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.entity.po.OperatorTrajectory byId = operatorTrajectoryService.getById(context.getId());
|
|
|
+ if(result.getCode() == 200){
|
|
|
+ if(byId.getOperatorType().equals("C")){
|
|
|
+ JSONObject jsonObject = JSON.parseObject(result.getData().toString());
|
|
|
+ byId.setPrimaryValue(jsonObject.getString(annotation.primary()));
|
|
|
+ }
|
|
|
+ byId.setExecuteStatus(1);
|
|
|
+ }else{
|
|
|
+ byId.setExecuteStatus(2);
|
|
|
+ byId.setErrorMsg(result.getMsg());
|
|
|
+ }
|
|
|
+ operatorTrajectoryService.updateById(byId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 清除上下文
|
|
|
+ methodContext.remove();
|
|
|
+ }
|
|
|
+
|
|
|
+ //拦截异常消息
|
|
|
+ @AfterThrowing(pointcut = "@annotation(com.jzg.aop.OperatorTrajectory)", throwing = "ex")
|
|
|
+ public void doAfterThrowing(JoinPoint joinPoint, Throwable ex) {
|
|
|
+ MethodContext context = methodContext.get();
|
|
|
+ if (context != null) {
|
|
|
+ String methodName = context.getMethodName();
|
|
|
+ if(joinPoint.getSignature().getName().equals(methodName)){
|
|
|
+ com.jzg.entity.po.OperatorTrajectory byId = operatorTrajectoryService.getById(context.getId());
|
|
|
+ byId.setExecuteStatus(2);
|
|
|
+ byId.setErrorMsg(ex.getMessage());
|
|
|
+ operatorTrajectoryService.updateById(byId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 清除上下文
|
|
|
+ methodContext.remove();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Before("@annotation(operatorTrajectory)")
|
|
|
+ public void doBefore(JoinPoint point, OperatorTrajectory operatorTrajectory) {
|
|
|
+ Object obj = null;
|
|
|
+ String id = null;
|
|
|
+ if(operatorTrajectory.type().equals("C") || operatorTrajectory.type().equals("U")) {
|
|
|
+ //强转参数
|
|
|
+ obj = point.getArgs()[0];
|
|
|
+ }else if(operatorTrajectory.type().equals("D")){
|
|
|
+ id = point.getArgs()[0].toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ com.jzg.entity.po.OperatorTrajectory trajectory = new com.jzg.entity.po.OperatorTrajectory();
|
|
|
+ String trajectoryId = IdGenerate.nextId();
|
|
|
+ trajectory.setId(trajectoryId);
|
|
|
+ trajectory.setOperatorType(operatorTrajectory.type());
|
|
|
+ trajectory.setSystemCode(operatorTrajectory.systemCode());
|
|
|
+ trajectory.setServiceTag(operatorTrajectory.serviceTag());
|
|
|
+
|
|
|
+ if(!Objects.isNull(obj)) {
|
|
|
+ trajectory.setAfterJson(JSON.toJSONString(obj));
|
|
|
+ }
|
|
|
+ HistoryDataService historyDataService;
|
|
|
+ JSONObject jsonObject;
|
|
|
+ Object beforeData;
|
|
|
+
|
|
|
+
|
|
|
+ switch(operatorTrajectory.type()){
|
|
|
+ //创建
|
|
|
+ case "C":
|
|
|
+ operatorTrajectoryService.save(trajectory);
|
|
|
+ break;
|
|
|
+ //更新
|
|
|
+ case "U":
|
|
|
+ historyDataService = (HistoryDataService)applicationContext.getBean(operatorTrajectory.serviceTag());
|
|
|
+ jsonObject = JSON.parseObject(JSONObject.toJSONString(obj));
|
|
|
+ trajectory.setPrimaryValue(jsonObject.getString(operatorTrajectory.primary()));
|
|
|
+ beforeData = historyDataService.getHistoryData(jsonObject.getString(operatorTrajectory.primary()));
|
|
|
+ trajectory.setBeforeJson(JSON.toJSONString(beforeData));
|
|
|
+ operatorTrajectoryService.save(trajectory);
|
|
|
+ break;
|
|
|
+ //删除
|
|
|
+ case "D":
|
|
|
+ historyDataService = (HistoryDataService)applicationContext.getBean(operatorTrajectory.serviceTag() + "Impl");
|
|
|
+ beforeData = historyDataService.getHistoryData(id);
|
|
|
+ trajectory.setPrimaryValue(id);
|
|
|
+ trajectory.setBeforeJson(JSON.toJSONString(beforeData));
|
|
|
+ operatorTrajectoryService.save(trajectory);
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+ // 用于验证上下文
|
|
|
+ methodContext.set(new MethodContext(point.getSignature().getName(), trajectoryId));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static class MethodContext {
|
|
|
+ private final String methodName;
|
|
|
+ private final String id;
|
|
|
+
|
|
|
+ public MethodContext(String methodName, String id) {
|
|
|
+ this.methodName = methodName;
|
|
|
+ this.id = id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getMethodName() {
|
|
|
+ return methodName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getId() {
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|