mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 23:57:12 +08:00
脚本添加缓存
This commit is contained in:
@@ -46,10 +46,13 @@ import org.codehaus.groovy.ast.builder.AstStringCompiler;
|
||||
import org.codehaus.groovy.ast.expr.*;
|
||||
import org.codehaus.groovy.control.CompilerConfiguration;
|
||||
import org.codehaus.groovy.control.customizers.SecureASTCustomizer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jgrapht.alg.cycle.CycleDetector;
|
||||
import org.jgrapht.graph.DefaultDirectedGraph;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.traverse.TopologicalOrderIterator;
|
||||
import org.redisson.api.RBucket;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -58,6 +61,7 @@ import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
@@ -185,50 +189,7 @@ public class EmpSalaryItemService
|
||||
|
||||
@Autowired EmpSalarySysItemService empSalarySysItemService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
||||
groovyScriptManager =
|
||||
new GroovyScriptManager(
|
||||
"salaryItemScript",
|
||||
new Consumer<CompilerConfiguration>() {
|
||||
@Override
|
||||
public void accept(CompilerConfiguration compilerConfiguration) {
|
||||
|
||||
compilerConfiguration.addCompilationCustomizers(
|
||||
new SecureASTCustomizer() {
|
||||
{
|
||||
setAllowedImports(List.of("java.lang.Math"));
|
||||
|
||||
setAllowedReceiversClasses(
|
||||
Arrays.asList(
|
||||
Object[].class,
|
||||
Arrays.class,
|
||||
RoundingMode.class,
|
||||
Closure.class,
|
||||
Map.class,
|
||||
Object.class,
|
||||
Math.class,
|
||||
Integer.class,
|
||||
Float.class,
|
||||
Double.class,
|
||||
Long.class,
|
||||
BigDecimal.class,
|
||||
YearMonth.class,
|
||||
LocalDateTime.class,
|
||||
LocalDate.class,
|
||||
LocalTime.class,
|
||||
ZonedDateTime.class,
|
||||
DateTimeFormatter.class,
|
||||
GString.class
|
||||
));
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
true);
|
||||
}
|
||||
@Autowired RedissonClient redissonClient;
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
@@ -409,27 +370,8 @@ public class EmpSalaryItemService
|
||||
kfHeji.setPriority(1000);
|
||||
}
|
||||
|
||||
public EmpSalaryItemDto create(CreateEmpSalaryItemDto request) {
|
||||
EmpSalaryItemEntity entity = mapper.toEntity(request);
|
||||
|
||||
if (repository.count(
|
||||
(Specification<EmpSalaryItemEntity>)
|
||||
(root, query, criteriaBuilder) ->
|
||||
criteriaBuilder.and(
|
||||
criteriaBuilder.equal(
|
||||
root.get("organizationId"),
|
||||
Ctx.currentUser().getOrganizationId()),
|
||||
criteriaBuilder.equal(
|
||||
root.get("name"), request.getName())))
|
||||
!= 0) {
|
||||
throw new BizException("工资项目名称重复");
|
||||
}
|
||||
|
||||
checkSysItemName(entity);
|
||||
|
||||
this.repository.save(entity);
|
||||
this.reSyncAll(entity.getOrganizationId(), null, request.getName());
|
||||
return getById(entity.getId());
|
||||
private static @NotNull String genCacheKey(String organizationId, boolean debug) {
|
||||
return "salaryItemScript:" + organizationId + ":" + debug;
|
||||
}
|
||||
|
||||
private void checkSysItemName(EmpSalaryItemEntity entity) {
|
||||
@@ -494,6 +436,126 @@ public class EmpSalaryItemService
|
||||
this.repository.saveAll(allItems);
|
||||
}
|
||||
|
||||
private static @NotNull String genMd5CacheKey(String organizationId, boolean debug) {
|
||||
return genCacheKey(organizationId, debug) + ":" + "md5";
|
||||
}
|
||||
|
||||
public EmpSalaryItemDto getById(String id) {
|
||||
EmpSalaryItemEntity entity = repository.get(id);
|
||||
|
||||
return mapper.toDto(entity);
|
||||
}
|
||||
|
||||
public Page<EmpSalaryItemDto> list(CommonQuery query) {
|
||||
Page<EmpSalaryItemEntity> page =
|
||||
repository.findAll(
|
||||
query.specification(conversionService),
|
||||
PageRequest.of(
|
||||
query.getPageNo(),
|
||||
query.getPageSize(),
|
||||
Sort.by(query.getOrders())));
|
||||
|
||||
return page.map(this.mapper::toDto);
|
||||
}
|
||||
|
||||
public void delete(IdRequest request) {
|
||||
throw new BizException("不支持删除, 请禁用相关工资项目");
|
||||
// this.repository.deleteAllById(request.getIds());
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
||||
groovyScriptManager =
|
||||
new GroovyScriptManager(
|
||||
"salaryItemScript",
|
||||
new Consumer<CompilerConfiguration>() {
|
||||
@Override
|
||||
public void accept(CompilerConfiguration compilerConfiguration) {
|
||||
|
||||
compilerConfiguration.addCompilationCustomizers(
|
||||
new SecureASTCustomizer() {
|
||||
{
|
||||
setAllowedImports(List.of("java.lang.Math"));
|
||||
|
||||
setAllowedReceiversClasses(
|
||||
Arrays.asList(
|
||||
Object[].class,
|
||||
Arrays.class,
|
||||
RoundingMode.class,
|
||||
Closure.class,
|
||||
Map.class,
|
||||
Object.class,
|
||||
Math.class,
|
||||
Integer.class,
|
||||
Float.class,
|
||||
Double.class,
|
||||
Long.class,
|
||||
BigDecimal.class,
|
||||
YearMonth.class,
|
||||
LocalDateTime.class,
|
||||
LocalDate.class,
|
||||
LocalTime.class,
|
||||
ZonedDateTime.class,
|
||||
DateTimeFormatter.class,
|
||||
GString.class));
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
true);
|
||||
}
|
||||
|
||||
public Object genScriptWithCompileResult(String organizationId, boolean debug) {
|
||||
|
||||
String script = genScript(organizationId, debug);
|
||||
|
||||
Exception exception = null;
|
||||
try {
|
||||
|
||||
groovyScriptManager.compile(script);
|
||||
} catch (Exception e) {
|
||||
|
||||
exception = e;
|
||||
}
|
||||
|
||||
return Map.of(
|
||||
"script",
|
||||
script,
|
||||
"error",
|
||||
exception != null,
|
||||
"exception",
|
||||
exception == null ? "" : ExceptionUtil.stacktraceToString(exception));
|
||||
}
|
||||
|
||||
public String genScript(String organizationId) {
|
||||
return genScript(organizationId, true);
|
||||
}
|
||||
|
||||
public EmpSalaryItemDto create(CreateEmpSalaryItemDto request) {
|
||||
EmpSalaryItemEntity entity = mapper.toEntity(request);
|
||||
|
||||
if (repository.count(
|
||||
(Specification<EmpSalaryItemEntity>)
|
||||
(root, query, criteriaBuilder) ->
|
||||
criteriaBuilder.and(
|
||||
criteriaBuilder.equal(
|
||||
root.get("organizationId"),
|
||||
Ctx.currentUser().getOrganizationId()),
|
||||
criteriaBuilder.equal(
|
||||
root.get("name"), request.getName())))
|
||||
!= 0) {
|
||||
throw new BizException("工资项目名称重复");
|
||||
}
|
||||
|
||||
checkSysItemName(entity);
|
||||
|
||||
this.repository.save(entity);
|
||||
this.reSyncAll(entity.getOrganizationId(), null, request.getName());
|
||||
this.clearCache(entity.getOrganizationId());
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
public EmpSalaryItemDto update(UpdateEmpSalaryItemDto request) {
|
||||
EmpSalaryItemEntity entity = this.repository.get(request.getId());
|
||||
String oldName = entity.getName();
|
||||
@@ -556,34 +618,12 @@ public class EmpSalaryItemService
|
||||
|
||||
this.repository.save(entity);
|
||||
this.reSyncAll(entity.getOrganizationId(), oldName, newName);
|
||||
this.clearCache(entity.getOrganizationId());
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
public EmpSalaryItemDto getById(String id) {
|
||||
EmpSalaryItemEntity entity = repository.get(id);
|
||||
|
||||
return mapper.toDto(entity);
|
||||
}
|
||||
|
||||
public Page<EmpSalaryItemDto> list(CommonQuery query) {
|
||||
Page<EmpSalaryItemEntity> page =
|
||||
repository.findAll(
|
||||
query.specification(conversionService),
|
||||
PageRequest.of(
|
||||
query.getPageNo(),
|
||||
query.getPageSize(),
|
||||
Sort.by(query.getOrders())));
|
||||
|
||||
return page.map(this.mapper::toDto);
|
||||
}
|
||||
|
||||
public void delete(IdRequest request) {
|
||||
throw new BizException("不支持删除, 请禁用相关工资项目");
|
||||
// this.repository.deleteAllById(request.getIds());
|
||||
}
|
||||
|
||||
public void initOrg(String organizationId) {
|
||||
|
||||
this.clearCache(organizationId);
|
||||
this.repository.deleteByOrganizationId(organizationId);
|
||||
for (int i = 0; i < 50; i++) {
|
||||
|
||||
@@ -670,34 +710,257 @@ public class EmpSalaryItemService
|
||||
repository.save(item3);
|
||||
}
|
||||
|
||||
public Object genScriptWithCompileResult(String organizationId, boolean debug) {
|
||||
|
||||
String script = genScript(organizationId, debug);
|
||||
|
||||
Exception exception = null;
|
||||
try {
|
||||
|
||||
groovyScriptManager.compile(script);
|
||||
} catch (Exception e) {
|
||||
|
||||
exception = e;
|
||||
}
|
||||
|
||||
return Map.of(
|
||||
"script",
|
||||
script,
|
||||
"error",
|
||||
exception != null,
|
||||
"exception",
|
||||
exception == null ? "" : ExceptionUtil.stacktraceToString(exception));
|
||||
}
|
||||
|
||||
public String genScript(String organizationId) {
|
||||
return genScript(organizationId, true);
|
||||
}
|
||||
|
||||
public String genScript(String organizationId, boolean debug) {
|
||||
|
||||
String key = genCacheKey(organizationId, debug);
|
||||
RBucket<Object> scriptBucket = redissonClient.getBucket(key);
|
||||
|
||||
if (scriptBucket.isExists()) {
|
||||
log.info("生成脚本: 使用缓存脚本 {}", key);
|
||||
|
||||
return (String) scriptBucket.get();
|
||||
}
|
||||
|
||||
String string = _genScript(organizationId, debug);
|
||||
|
||||
log.info("生成脚本: 生成新脚本 {}", key);
|
||||
|
||||
scriptBucket.set(string);
|
||||
|
||||
String md5Key = key + ":" + "md5";
|
||||
String md5DigestAsHex = DigestUtils.md5DigestAsHex(string.getBytes());
|
||||
log.info("生成脚本: 生成新脚本MD5 {} -> {}", md5Key, md5DigestAsHex);
|
||||
redissonClient.getBucket(md5Key).set(md5DigestAsHex);
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
public void clearCacheAll() {
|
||||
|
||||
log.info("清除所有脚本缓存");
|
||||
redissonClient
|
||||
.getKeys()
|
||||
.getKeysByPattern("salaryItemScript:*")
|
||||
.forEach(x -> redissonClient.getBucket(x).delete());
|
||||
}
|
||||
|
||||
public void clearCache(String organizationId) {
|
||||
|
||||
log.info("清除缓存: {}", organizationId);
|
||||
|
||||
redissonClient.getBucket(genCacheKey(organizationId, true)).delete();
|
||||
redissonClient.getBucket(genCacheKey(organizationId, false)).delete();
|
||||
|
||||
redissonClient.getBucket(genCacheKey(organizationId, true) + ":md5").delete();
|
||||
redissonClient.getBucket(genCacheKey(organizationId, false) + ":md5").delete();
|
||||
}
|
||||
|
||||
private String genDebugStatement(
|
||||
EmpSalaryItemEntity item,
|
||||
List<EmpSalarySysItemEntity> sysItems,
|
||||
List<EmpSalaryItemEntity> items) {
|
||||
|
||||
AstBuilder astBuilder = new AstBuilder();
|
||||
|
||||
List<ASTNode> astNodes = astBuilder.buildFromString(item.getFormula());
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.append("\n");
|
||||
|
||||
sb.append("/**\n");
|
||||
sb.append(" * 调试信息");
|
||||
sb.append(" ")
|
||||
.append(item.getCode())
|
||||
.append(" ")
|
||||
.append(" ")
|
||||
.append(item.getName())
|
||||
.append("\n");
|
||||
sb.append("*/");
|
||||
sb.append("\n");
|
||||
|
||||
sb.append("salary.debug.").append(item.getCode()).append(" = \"");
|
||||
for (ASTNode astNode : astNodes) {
|
||||
|
||||
astNode.visit(
|
||||
new CodeVisitorSupport() {
|
||||
|
||||
@Override
|
||||
public void visitMethodCallExpression(MethodCallExpression call) {
|
||||
|
||||
sb.append(call.getMethod().getText());
|
||||
sb.append("(");
|
||||
|
||||
call.getArguments().visit(this);
|
||||
|
||||
sb.append(")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitShortTernaryExpression(
|
||||
ElvisOperatorExpression expression) {
|
||||
|
||||
if (expression.getFalseExpression()
|
||||
instanceof ConstantExpression falseExpression) {
|
||||
|
||||
if (falseExpression.getText().equals("0")) {
|
||||
|
||||
expression.getTrueExpression().visit(this);
|
||||
}
|
||||
} else if (expression.getTrueExpression()
|
||||
instanceof ConstantExpression trueExpression) {
|
||||
|
||||
if (trueExpression.getText().equals("0")) {
|
||||
|
||||
expression.getFalseExpression().visit(this);
|
||||
}
|
||||
} else {
|
||||
|
||||
super.visitShortTernaryExpression(expression);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitNotExpression(NotExpression expression) {
|
||||
sb.append("!");
|
||||
super.visitNotExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitTupleExpression(TupleExpression expression) {
|
||||
super.visitTupleExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitListExpression(ListExpression expression) {
|
||||
|
||||
sb.append("[");
|
||||
|
||||
expression
|
||||
.getExpressions()
|
||||
.forEach(
|
||||
x -> {
|
||||
x.visit(this);
|
||||
sb.append(",");
|
||||
});
|
||||
|
||||
if (sb.charAt(sb.length() - 1) == ',') {
|
||||
sb.deleteCharAt(sb.length() - 1);
|
||||
}
|
||||
|
||||
sb.append("]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitConstantExpression(ConstantExpression expression) {
|
||||
|
||||
sb.append(" ");
|
||||
if (expression.getValue() instanceof String) {
|
||||
sb.append("\\\"").append(expression.getValue()).append("\\\"");
|
||||
} else {
|
||||
sb.append(expression.getText());
|
||||
}
|
||||
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitVariableExpression(VariableExpression expression) {
|
||||
|
||||
for (EmpSalarySysItemEntity sysItem : sysItems) {
|
||||
|
||||
if (StringUtils.equals(sysItem.getCode(), expression.getName())) {
|
||||
|
||||
sb.append(sysItem.getName())
|
||||
.append("(${debugPrint(")
|
||||
.append(expression.getName())
|
||||
.append(")})");
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
super.visitVariableExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBinaryExpression(BinaryExpression expression) {
|
||||
|
||||
expression.getLeftExpression().visit(this);
|
||||
|
||||
sb.append(" ").append(expression.getOperation().getText()).append(" ");
|
||||
|
||||
expression.getRightExpression().visit(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPropertyExpression(PropertyExpression expression) {
|
||||
|
||||
if (expression
|
||||
.getObjectExpression()
|
||||
.getText()
|
||||
.equalsIgnoreCase("salary")) {
|
||||
|
||||
for (EmpSalaryItemEntity item : items) {
|
||||
|
||||
if (StringUtils.equals(
|
||||
item.getCode(), expression.getPropertyAsString())) {
|
||||
|
||||
sb.append(item.getName())
|
||||
.append("(${debugPrint(")
|
||||
.append("salary.")
|
||||
.append(expression.getPropertyAsString())
|
||||
.append(")})");
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super.visitPropertyExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitFieldExpression(FieldExpression expression) {
|
||||
super.visitFieldExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitArgumentlistExpression(ArgumentListExpression expression) {
|
||||
|
||||
List<Expression> expressions = expression.getExpressions();
|
||||
for (int i = 0; i < expressions.size(); i++) {
|
||||
Expression expressionExpression = expressions.get(i);
|
||||
|
||||
expressionExpression.visit(this);
|
||||
if (i != expressions.size() - 1) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
sb.append("\".toString()\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public List<EmpSalaryItemEntity> getItems(String organizationId) {
|
||||
|
||||
return this.findAll(
|
||||
new Specification<EmpSalaryItemEntity>() {
|
||||
@Override
|
||||
public Predicate toPredicate(
|
||||
Root<EmpSalaryItemEntity> root,
|
||||
CriteriaQuery<?> query,
|
||||
CriteriaBuilder criteriaBuilder) {
|
||||
return criteriaBuilder.and(
|
||||
criteriaBuilder.equal(root.get("organizationId"), organizationId),
|
||||
criteriaBuilder.equal(root.get("status"), 1));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private @NotNull String _genScript(String organizationId, boolean debug) {
|
||||
List<EmpSalaryItemEntity> items =
|
||||
this.findAll(
|
||||
new Specification<EmpSalaryItemEntity>() {
|
||||
@@ -914,7 +1177,6 @@ def debugPrint(def x) {
|
||||
if (debug) {
|
||||
|
||||
script.append(genDebugStatement(item, sysItems, items));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -923,217 +1185,25 @@ def debugPrint(def x) {
|
||||
roundScript.append("// 保留小数位处理\n");
|
||||
|
||||
script.append(roundScript);
|
||||
return script.toString();
|
||||
}
|
||||
|
||||
private String genDebugStatement(
|
||||
EmpSalaryItemEntity item,
|
||||
List<EmpSalarySysItemEntity> sysItems,
|
||||
List<EmpSalaryItemEntity> items) {
|
||||
|
||||
AstBuilder astBuilder = new AstBuilder();
|
||||
|
||||
List<ASTNode> astNodes = astBuilder.buildFromString(item.getFormula());
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.append("\n");
|
||||
|
||||
sb.append("/**\n");
|
||||
sb.append(" * 调试信息");
|
||||
sb.append(" ")
|
||||
.append(item.getCode())
|
||||
.append(" ")
|
||||
.append(" ")
|
||||
.append(item.getName())
|
||||
.append("\n");
|
||||
sb.append("*/");
|
||||
sb.append("\n");
|
||||
|
||||
sb.append("salary.debug.").append(item.getCode()).append(" = \"");
|
||||
for (ASTNode astNode : astNodes) {
|
||||
|
||||
astNode.visit(
|
||||
new CodeVisitorSupport() {
|
||||
|
||||
@Override
|
||||
public void visitMethodCallExpression(MethodCallExpression call) {
|
||||
|
||||
sb.append(call.getMethod().getText());
|
||||
sb.append("(");
|
||||
|
||||
call.getArguments().visit(this);
|
||||
|
||||
sb.append(")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitShortTernaryExpression(
|
||||
ElvisOperatorExpression expression) {
|
||||
|
||||
if (expression.getFalseExpression()
|
||||
instanceof ConstantExpression falseExpression) {
|
||||
|
||||
if (falseExpression.getText().equals("0")) {
|
||||
|
||||
expression.getTrueExpression().visit(this);
|
||||
}
|
||||
} else if (expression.getTrueExpression()
|
||||
instanceof ConstantExpression trueExpression) {
|
||||
|
||||
if (trueExpression.getText().equals("0")) {
|
||||
|
||||
expression.getFalseExpression().visit(this);
|
||||
}
|
||||
} else {
|
||||
|
||||
super.visitShortTernaryExpression(expression);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitNotExpression(NotExpression expression) {
|
||||
sb.append("!");
|
||||
super.visitNotExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitTupleExpression(TupleExpression expression) {
|
||||
super.visitTupleExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitListExpression(ListExpression expression) {
|
||||
|
||||
sb.append("[");
|
||||
|
||||
expression
|
||||
.getExpressions()
|
||||
.forEach(
|
||||
x -> {
|
||||
x.visit(this);
|
||||
sb.append(",");
|
||||
});
|
||||
|
||||
if (sb.charAt(sb.length() - 1) == ',') {
|
||||
sb.deleteCharAt(sb.length() - 1);
|
||||
}
|
||||
|
||||
sb.append("]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitConstantExpression(ConstantExpression expression) {
|
||||
|
||||
sb.append(" ");
|
||||
if (expression.getValue() instanceof String) {
|
||||
sb.append("\\\"").append(expression.getValue()).append("\\\"");
|
||||
} else {
|
||||
sb.append(expression.getText());
|
||||
}
|
||||
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitVariableExpression(VariableExpression expression) {
|
||||
|
||||
for (EmpSalarySysItemEntity sysItem : sysItems) {
|
||||
|
||||
if (StringUtils.equals(sysItem.getCode(), expression.getName())) {
|
||||
|
||||
sb.append(sysItem.getName())
|
||||
.append("(${debugPrint(")
|
||||
.append(expression.getName())
|
||||
.append(")})");
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
super.visitVariableExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBinaryExpression(BinaryExpression expression) {
|
||||
|
||||
expression.getLeftExpression().visit(this);
|
||||
|
||||
sb.append(" ").append(expression.getOperation().getText()).append(" ");
|
||||
|
||||
expression.getRightExpression().visit(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPropertyExpression(PropertyExpression expression) {
|
||||
|
||||
if (expression
|
||||
.getObjectExpression()
|
||||
.getText()
|
||||
.equalsIgnoreCase("salary")) {
|
||||
|
||||
for (EmpSalaryItemEntity item : items) {
|
||||
|
||||
if (StringUtils.equals(
|
||||
item.getCode(), expression.getPropertyAsString())) {
|
||||
|
||||
sb.append(item.getName())
|
||||
.append("(${debugPrint(")
|
||||
.append("salary.")
|
||||
.append(expression.getPropertyAsString())
|
||||
.append(")})");
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super.visitPropertyExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitFieldExpression(FieldExpression expression) {
|
||||
super.visitFieldExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitArgumentlistExpression(ArgumentListExpression expression) {
|
||||
|
||||
List<Expression> expressions = expression.getExpressions();
|
||||
for (int i = 0; i < expressions.size(); i++) {
|
||||
Expression expressionExpression = expressions.get(i);
|
||||
|
||||
expressionExpression.visit(this);
|
||||
if (i != expressions.size() - 1) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
sb.append("\".toString()\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public List<EmpSalaryItemEntity> getItems(String organizationId) {
|
||||
|
||||
return this.findAll(
|
||||
new Specification<EmpSalaryItemEntity>() {
|
||||
@Override
|
||||
public Predicate toPredicate(
|
||||
Root<EmpSalaryItemEntity> root,
|
||||
CriteriaQuery<?> query,
|
||||
CriteriaBuilder criteriaBuilder) {
|
||||
return criteriaBuilder.and(
|
||||
criteriaBuilder.equal(root.get("organizationId"), organizationId),
|
||||
criteriaBuilder.equal(root.get("status"), 1));
|
||||
}
|
||||
});
|
||||
String string = script.toString();
|
||||
return string;
|
||||
}
|
||||
|
||||
public String computeCacheKey(String organizationId, boolean debug) {
|
||||
|
||||
return this.repository.computeCacheKey(organizationId) + "_" + debug;
|
||||
String key = genMd5CacheKey(organizationId, debug);
|
||||
RBucket<Object> bucket = redissonClient.getBucket(key);
|
||||
|
||||
if (bucket.isExists()) {
|
||||
log.info("工资脚本MD5缓存存在: {}", key);
|
||||
return bucket.get().toString();
|
||||
} else {
|
||||
|
||||
log.info("工资脚本MD5缓存不存在: {}", key);
|
||||
genScript(organizationId, debug);
|
||||
|
||||
return computeCacheKey(organizationId, debug);
|
||||
}
|
||||
}
|
||||
|
||||
public Script newScriptInstance(String organizationId) {
|
||||
@@ -1142,9 +1212,6 @@ def debugPrint(def x) {
|
||||
|
||||
public Script newScriptInstance(String organizationId, boolean debug) {
|
||||
|
||||
|
||||
|
||||
|
||||
return groovyScriptManager.newInstance(
|
||||
computeCacheKey(organizationId, debug), () -> genScript(organizationId, debug));
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import cn.lihongjie.coal.empSalarySysItem.entity.EmpSalarySysItemEntity;
|
||||
import cn.lihongjie.coal.empSalarySysItem.mapper.EmpSalarySysItemMapper;
|
||||
import cn.lihongjie.coal.empSalarySysItem.repository.EmpSalarySysItemRepository;
|
||||
import cn.lihongjie.coal.exception.BizException;
|
||||
import cn.lihongjie.coal.organization.service.OrganizationService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -26,7 +27,6 @@ import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -50,6 +50,8 @@ public class EmpSalarySysItemService
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
@Autowired OrganizationService organizationService;
|
||||
|
||||
@Autowired EmpSalaryItemService empSalaryItemService;
|
||||
|
||||
public EmpSalarySysItemDto update(UpdateEmpSalarySysItemDto request) {
|
||||
@@ -91,6 +93,8 @@ public class EmpSalarySysItemService
|
||||
|
||||
this.repository.save(entity);
|
||||
|
||||
empSalaryItemService.clearCacheAll();
|
||||
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
@@ -119,6 +123,7 @@ public class EmpSalarySysItemService
|
||||
}
|
||||
});
|
||||
|
||||
empSalaryItemService.clearCacheAll();
|
||||
this.repository.deleteAllById(request.getIds());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user