|
|
@@ -1,18 +1,19 @@
|
|
|
package com.jzg.commons.aop.aspect;
|
|
|
|
|
|
|
|
|
-import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
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.Crud;
|
|
|
import com.jzg.commons.core.base.BaseController;
|
|
|
-import com.jzg.commons.core.base.HistoryDataService;
|
|
|
import com.jzg.commons.core.page.HttpResult;
|
|
|
import com.jzg.commons.exception.SystemException;
|
|
|
import com.jzg.commons.service.OperatorTrajectoryService;
|
|
|
+import com.jzg.commons.util.GetSelectIdDataUtils;
|
|
|
import com.jzg.commons.util.StringUtils;
|
|
|
+import com.jzg.commons.util.comparble.DescriptionUtils;
|
|
|
import com.jzg.commons.util.idgen.IdGenerate;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -23,7 +24,6 @@ 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.ApplicationContext;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
@@ -43,10 +43,6 @@ public class OperatorTrajectoryAspect {
|
|
|
@Lazy
|
|
|
OperatorTrajectoryService operatorTrajectoryService;
|
|
|
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ApplicationContext applicationContext;
|
|
|
-
|
|
|
@Autowired
|
|
|
private BaseController baseController;
|
|
|
|
|
|
@@ -56,6 +52,8 @@ public class OperatorTrajectoryAspect {
|
|
|
public void doAfter(JoinPoint joinPoint, HttpResult result) {
|
|
|
MethodContext context = methodContext.get();
|
|
|
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
|
|
|
+ Object arg = joinPoint.getArgs()[0];
|
|
|
+
|
|
|
OperatorTrajectory annotation = method.getAnnotation(OperatorTrajectory.class);
|
|
|
if (context != null) {
|
|
|
// 验证结果
|
|
|
@@ -67,57 +65,53 @@ public class OperatorTrajectoryAspect {
|
|
|
com.jzg.commons.entity.po.OperatorTrajectory byId = operatorTrajectoryService.getById(context.getId());
|
|
|
if(result.getCode() == 200){
|
|
|
if(byId.getOperatorType().equals("C")){
|
|
|
- Object data = result.getData();
|
|
|
- if (ObjectUtil.isNotEmpty(data)){
|
|
|
- JSONObject jsonObject = JSON.parseObject(data.toString());
|
|
|
- byId.setPrimaryValue(jsonObject.getString(annotation.primary()));
|
|
|
- }
|
|
|
+ JSONObject agr = JSON.parseObject(JSONObject.toJSONString(arg));
|
|
|
+ byId.setPrimaryValue(agr.getString(annotation.primary()));
|
|
|
}
|
|
|
byId.setExecuteStatus(1);
|
|
|
}else{
|
|
|
byId.setExecuteStatus(2);
|
|
|
byId.setErrorMsg(result.getMsg());
|
|
|
}
|
|
|
-// operatorTrajectoryService.updateById(byId);
|
|
|
+ operatorTrajectoryService.updateById(byId);
|
|
|
}
|
|
|
}
|
|
|
// 清除上下文
|
|
|
methodContext.remove();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public String getContent(Object obj,String type) {
|
|
|
+ String generated = DescriptionUtils.generateDescription(obj);
|
|
|
+ if (StringUtils.isNotEmpty(generated)){
|
|
|
+ generated = Crud.getNameByCode(type) + generated;
|
|
|
+ }
|
|
|
+ return generated;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
//拦截异常消息
|
|
|
@AfterThrowing(pointcut = "@annotation(com.jzg.commons.aop.OperatorTrajectory)", throwing = "ex")
|
|
|
public void doAfterThrowing(JoinPoint joinPoint, Throwable ex) {
|
|
|
MethodContext context = methodContext.get();
|
|
|
- log.info("异常信息:{}", ex.getMessage());
|
|
|
- log.info("ID:{}", context.getId());
|
|
|
- log.info("ID:{}", ex.toString());
|
|
|
- operatorTrajectoryService.deleteById(context.getId(),baseController.getUserSystemCode());
|
|
|
-
|
|
|
-
|
|
|
-// if (context != null) {
|
|
|
-// 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 (context != null) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
// 清除上下文
|
|
|
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();
|
|
|
- }
|
|
|
+ public void doBefore(JoinPoint point, OperatorTrajectory operatorTrajectory){
|
|
|
+ Object obj = point.getArgs()[0];
|
|
|
+ String id = JSON.parseObject(JSONObject.toJSONString(obj)).getString(operatorTrajectory.primary());
|
|
|
if (isRecorded(operatorTrajectory.isRecorded(),obj)){
|
|
|
return;
|
|
|
}
|
|
|
@@ -125,41 +119,40 @@ public class OperatorTrajectoryAspect {
|
|
|
String trajectoryId = IdGenerate.nextId();
|
|
|
trajectory.setId(trajectoryId);
|
|
|
trajectory.setOperatorType(operatorTrajectory.type());
|
|
|
- trajectory.setSystemCode(operatorTrajectory.systemCode());
|
|
|
- trajectory.setServiceTag(operatorTrajectory.serviceTag());
|
|
|
+ trajectory.setSystemCode(baseController.getUserSystemCode());
|
|
|
+ String className = point.getTarget().getClass().getName();
|
|
|
+ String methodName = point.getSignature().getName();
|
|
|
+ trajectory.setServiceTag(className + "." + methodName + "()");
|
|
|
trajectory.setModuleName(operatorTrajectory.moduleName());
|
|
|
trajectory.setCreateTime(LocalDateTime.now());
|
|
|
trajectory.setCreateBy(baseController.getUserName());
|
|
|
- trajectory.setSystemCode(baseController.getUserSystemCode());
|
|
|
- trajectory.setEntityName(operatorTrajectory.entity());
|
|
|
+ trajectory.setEntityName(operatorTrajectory.entityClass().getName());
|
|
|
if(!Objects.isNull(obj)) {
|
|
|
trajectory.setAfterJson(JSON.toJSONString(obj));
|
|
|
}
|
|
|
- HistoryDataService historyDataService;
|
|
|
- JSONObject jsonObject;
|
|
|
Object beforeData;
|
|
|
-
|
|
|
-
|
|
|
switch(operatorTrajectory.type()){
|
|
|
//创建
|
|
|
case "C":
|
|
|
+ trajectory.setOperationContent(getContent(obj,operatorTrajectory.type()));
|
|
|
operatorTrajectoryService.save(trajectory);
|
|
|
break;
|
|
|
//更新
|
|
|
case "U":
|
|
|
- historyDataService = (HistoryDataService)applicationContext.getBean(operatorTrajectory.serviceTag() + "Impl");
|
|
|
- jsonObject = JSON.parseObject(JSONObject.toJSONString(obj));
|
|
|
- trajectory.setPrimaryValue(jsonObject.getString(operatorTrajectory.primary()));
|
|
|
- beforeData = historyDataService.getHistoryData(jsonObject.getString(operatorTrajectory.primary()));
|
|
|
- trajectory.setBeforeJson(JSON.toJSONString(beforeData));
|
|
|
+ beforeData = GetSelectIdDataUtils.selectById(operatorTrajectory.entityClass(), operatorTrajectory.mapperClass(), id);
|
|
|
+ trajectory.setPrimaryValue(id);
|
|
|
+ if (!Objects.isNull(beforeData)){
|
|
|
+ trajectory.setBeforeJson(JSON.toJSONString(beforeData));
|
|
|
+ }
|
|
|
+ trajectory.setOperationContent(getContent(obj,operatorTrajectory.type()));
|
|
|
operatorTrajectoryService.save(trajectory);
|
|
|
break;
|
|
|
//删除
|
|
|
case "D":
|
|
|
- historyDataService = (HistoryDataService)applicationContext.getBean(operatorTrajectory.serviceTag() + "Impl");
|
|
|
- beforeData = historyDataService.getHistoryData(id);
|
|
|
+ beforeData = GetSelectIdDataUtils.selectById(operatorTrajectory.entityClass(), operatorTrajectory.mapperClass(), id);
|
|
|
trajectory.setPrimaryValue(id);
|
|
|
trajectory.setBeforeJson(JSON.toJSONString(beforeData));
|
|
|
+ trajectory.setOperationContent(getContent(beforeData,operatorTrajectory.type()));
|
|
|
operatorTrajectoryService.save(trajectory);
|
|
|
break;
|
|
|
|
|
|
@@ -168,6 +161,7 @@ public class OperatorTrajectoryAspect {
|
|
|
methodContext.set(new MethodContext(point.getSignature().getName(), trajectoryId));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public boolean isRecorded(String isRecorded,Object obj) {
|
|
|
if(StringUtils.isNotBlank(isRecorded)){
|
|
|
Map<String,Object> map = JSONObject.parseObject(JSONObject.toJSONString(obj), Map.class);
|
|
|
@@ -183,7 +177,6 @@ public class OperatorTrajectoryAspect {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
public void saveRecord(com.jzg.commons.entity.po.OperatorTrajectory operatorTrajectory) {
|
|
|
AsyncManager.getInstance().execute(AsyncFactory.saveRecord(operatorTrajectory));
|
|
|
}
|