From 2a536c8db428a391aae0e7bdaefd592d512880ce Mon Sep 17 00:00:00 2001 From: hangsman Date: Sun, 16 Jan 2022 16:54:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E9=A2=9D=E5=A4=96=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hangsman/operationlog/OperationLog.java | 2 + .../operationlog/annotation/OperationLog.java | 8 ++- .../OperationLogAnnotationParser.java | 54 +++++++++++-------- .../SpelFunctionExpressionParser.java | 12 ++--- .../OperationLogAspectSupport.java | 18 +++++-- .../interceptor/OperationLogParam.java | 2 + .../test/service/UserService.java | 3 +- 7 files changed, 65 insertions(+), 34 deletions(-) diff --git a/operation-log-core/src/main/java/cn/hangsman/operationlog/OperationLog.java b/operation-log-core/src/main/java/cn/hangsman/operationlog/OperationLog.java index d8eae7f..cb6b999 100644 --- a/operation-log-core/src/main/java/cn/hangsman/operationlog/OperationLog.java +++ b/operation-log-core/src/main/java/cn/hangsman/operationlog/OperationLog.java @@ -4,6 +4,7 @@ import lombok.Data; import java.util.Date; +import java.util.Map; /** * Created by 2022/1/11 14:09 @@ -21,5 +22,6 @@ public class OperationLog { private String detail; private String category; private Date operationTime; + private Map additional; } diff --git a/operation-log-core/src/main/java/cn/hangsman/operationlog/annotation/OperationLog.java b/operation-log-core/src/main/java/cn/hangsman/operationlog/annotation/OperationLog.java index cf3fe9a..629badf 100644 --- a/operation-log-core/src/main/java/cn/hangsman/operationlog/annotation/OperationLog.java +++ b/operation-log-core/src/main/java/cn/hangsman/operationlog/annotation/OperationLog.java @@ -57,7 +57,13 @@ * 前置处理 格式{"变量名={spel表达式}"} * 之后在其他模板中可以使用 #变量名 获取表达式返回值 * - * @return string + * @return string[] */ String[] before() default ""; + + /** + * 格式同 before + * @return string[] + */ + String[] additional() default ""; } diff --git a/operation-log-core/src/main/java/cn/hangsman/operationlog/annotation/OperationLogAnnotationParser.java b/operation-log-core/src/main/java/cn/hangsman/operationlog/annotation/OperationLogAnnotationParser.java index c937ae6..ceb1d29 100644 --- a/operation-log-core/src/main/java/cn/hangsman/operationlog/annotation/OperationLogAnnotationParser.java +++ b/operation-log-core/src/main/java/cn/hangsman/operationlog/annotation/OperationLogAnnotationParser.java @@ -2,13 +2,11 @@ import cn.hangsman.operationlog.interceptor.OperationLogParam; import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import java.lang.reflect.AnnotatedElement; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; /** @@ -37,25 +35,35 @@ private Collection parseAnnotations(AnnotatedElement ae, bool if (ans.isEmpty()) { return null; } - return ans.stream().map(an -> { - Map beforeHandles = new HashMap<>(); - for (String template : an.before()) { - if (StringUtils.hasText(template)) { - int delimiterIndex = template.indexOf("="); - String variableName = template.substring(0, delimiterIndex); - String expressionStr = template.substring(delimiterIndex + 1); - beforeHandles.put(variableName, expressionStr); - } - } - return OperationLogParam.builder() - .name(ae.toString()) - .content(an.content()) - .fail(an.fail()) - .category(an.category()) - .detail(an.detail()) - .condition(an.condition()) - .before(beforeHandles).build(); - }).collect(Collectors.toCollection(ArrayList::new)); + return ans.stream().map(an -> OperationLogParam.builder() + .name(ae.toString()) + .content(an.content()) + .fail(an.fail()) + .category(an.category()) + .detail(an.detail()) + .condition(an.condition()) + .before(paresToMap(an.before())) + .additional(paresToMap(an.additional())) + .build()).collect(Collectors.toCollection(ArrayList::new)); } + + /** + * 将 变量名={spel表达式}解析为键值对形式 + */ + private Map paresToMap(String[] templates) { + if (ObjectUtils.isEmpty(templates)) { + return Collections.emptyMap(); + } + Map map = new HashMap<>(); + for (String template : templates) { + if (StringUtils.hasText(template)) { + int delimiterIndex = template.indexOf("="); + String variableName = template.substring(0, delimiterIndex); + String expressionStr = template.substring(delimiterIndex + 1); + map.put(variableName, expressionStr); + } + } + return map; + } } diff --git a/operation-log-core/src/main/java/cn/hangsman/operationlog/expression/SpelFunctionExpressionParser.java b/operation-log-core/src/main/java/cn/hangsman/operationlog/expression/SpelFunctionExpressionParser.java index f6cc8d2..a17f2c7 100644 --- a/operation-log-core/src/main/java/cn/hangsman/operationlog/expression/SpelFunctionExpressionParser.java +++ b/operation-log-core/src/main/java/cn/hangsman/operationlog/expression/SpelFunctionExpressionParser.java @@ -59,9 +59,9 @@ private Expression doParseFunctionExpression(String expressionString, ParserCont String originExpressionString = expressionString; // 将解析出来的方法替换成变量形式 // #_ret != null ? $json(#_ret) : '' 将被替换成 #_ret != null ? #fun_uuid : '' - for (String key : variableExpressionMap.keySet()) { - Expression expression = variableExpressionMap.get(key); - expressionString = expressionString.replace(expression.getExpressionString(), "#" + key); + for (Map.Entry entry : variableExpressionMap.entrySet()) { + Expression expression = entry.getValue(); + expressionString = expressionString.replace(expression.getExpressionString(), "#" + entry.getKey()); } // 然后解析上面替换好的表达式 Expression proxyExpression = doParseExpression(expressionString, null); @@ -150,10 +150,10 @@ public String getExpressionString() { @Override public Object getValue(EvaluationContext context) throws EvaluationException { - for (String key : variableExpressionMap.keySet()) { - Expression expression = variableExpressionMap.get(key); + for (Map.Entry entry : variableExpressionMap.entrySet()) { + Expression expression = entry.getValue(); Object value = expression.getValue(context); - context.setVariable(key, value); + context.setVariable(entry.getKey(), value); } return proxyExpression.getValue(context); } diff --git a/operation-log-core/src/main/java/cn/hangsman/operationlog/interceptor/OperationLogAspectSupport.java b/operation-log-core/src/main/java/cn/hangsman/operationlog/interceptor/OperationLogAspectSupport.java index 9baf11f..98a4fd8 100644 --- a/operation-log-core/src/main/java/cn/hangsman/operationlog/interceptor/OperationLogAspectSupport.java +++ b/operation-log-core/src/main/java/cn/hangsman/operationlog/interceptor/OperationLogAspectSupport.java @@ -15,6 +15,7 @@ import org.springframework.context.expression.AnnotatedElementKey; import org.springframework.core.BridgeMethodResolver; import org.springframework.expression.EvaluationContext; +import org.springframework.expression.Expression; import org.springframework.lang.Nullable; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; @@ -22,9 +23,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Proxy; -import java.util.Collection; -import java.util.Date; -import java.util.Map; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; /** @@ -81,6 +80,7 @@ protected void recordLog(LogOperationContext operationContext, boolean invokeSuc builder.operationTime(operationTime); builder.category(operation.category); builder.detail(operationContext.parseTemplate(operation.detail)); + builder.additional(operationContext.parseMapTemplate(operation.additional)); if (invokeSuccess) { builder.content(operationContext.parseTemplate(operation.content)); } else { @@ -245,6 +245,18 @@ protected void resolveBeforeHandle() { }); } + protected Map parseMapTemplate(Map templateMap) { + if (templateMap.isEmpty()){ + return Collections.emptyMap(); + } + HashMap resultMap = new HashMap<>(); + for (Map.Entry entry : templateMap.entrySet()) { + Object result = evaluator.parseExpression(entry.getValue(), metadata.methodKey, evaluationContext, Object.class); + resultMap.put(entry.getKey(), result); + } + return resultMap; + } + protected String parseTemplate(String template) { return evaluator.parseExpression(template, metadata.methodKey, evaluationContext, String.class); } diff --git a/operation-log-core/src/main/java/cn/hangsman/operationlog/interceptor/OperationLogParam.java b/operation-log-core/src/main/java/cn/hangsman/operationlog/interceptor/OperationLogParam.java index 327be2f..bcc5a8f 100644 --- a/operation-log-core/src/main/java/cn/hangsman/operationlog/interceptor/OperationLogParam.java +++ b/operation-log-core/src/main/java/cn/hangsman/operationlog/interceptor/OperationLogParam.java @@ -29,4 +29,6 @@ public class OperationLogParam { Map before; + Map additional; + } diff --git a/operation-log-test/src/main/java/cn/hangsman/operationlog/test/service/UserService.java b/operation-log-test/src/main/java/cn/hangsman/operationlog/test/service/UserService.java index 5e1820b..ecc8268 100644 --- a/operation-log-test/src/main/java/cn/hangsman/operationlog/test/service/UserService.java +++ b/operation-log-test/src/main/java/cn/hangsman/operationlog/test/service/UserService.java @@ -18,7 +18,8 @@ public class UserService implements IUserService { @OperationLog(category = "创建用户", content = "添加了一个用户名为 {#user.username} 的用户", fail = "用户添加失败:{#_errorMsg}", - detail = "{#_ret != null ? $json(#_ret) : ''}") + detail = "{#_ret != null ? $json(#_ret) : ''}", + additional = {"type={'修改用户'}"}) public User createUser(User user) { user.setId(10000); return user;