Skip to content

Commit

Permalink
Merge pull request #2 from hangsman/dev
Browse files Browse the repository at this point in the history
支持自定义额外字段
  • Loading branch information
hangsman authored Jan 16, 2022
2 parents b0914ec + 2a536c8 commit 94fc8ce
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Data;

import java.util.Date;
import java.util.Map;

/**
* Created by 2022/1/11 14:09
Expand All @@ -21,5 +22,6 @@ public class OperationLog {
private String detail;
private String category;
private Date operationTime;
private Map<String, Object> additional;

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@
* 前置处理 格式{"变量名={spel表达式}"}
* 之后在其他模板中可以使用 #变量名 获取表达式返回值
*
* @return string
* @return string[]
*/
String[] before() default "";

/**
* 格式同 before
* @return string[]
*/
String[] additional() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -37,25 +35,35 @@ private Collection<OperationLogParam> parseAnnotations(AnnotatedElement ae, bool
if (ans.isEmpty()) {
return null;
}
return ans.stream().map(an -> {
Map<String, String> 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<String, String> paresToMap(String[] templates) {
if (ObjectUtils.isEmpty(templates)) {
return Collections.emptyMap();
}
Map<String, String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Expression> entry : variableExpressionMap.entrySet()) {
Expression expression = entry.getValue();
expressionString = expressionString.replace(expression.getExpressionString(), "#" + entry.getKey());
}
// 然后解析上面替换好的表达式
Expression proxyExpression = doParseExpression(expressionString, null);
Expand Down Expand Up @@ -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<String, Expression> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
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;
import org.springframework.util.StringUtils;

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;

/**
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -245,6 +245,18 @@ protected void resolveBeforeHandle() {
});
}

protected Map<String, Object> parseMapTemplate(Map<String, String> templateMap) {
if (templateMap.isEmpty()){
return Collections.emptyMap();
}
HashMap<String, Object> resultMap = new HashMap<>();
for (Map.Entry<String, String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public class OperationLogParam {

Map<String, String> before;

Map<String, String> additional;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 94fc8ce

Please sign in to comment.