岗位添加人数

This commit is contained in:
2024-03-18 13:55:22 +08:00
parent 8c64e1a9d5
commit 80fb403f5f
3 changed files with 38 additions and 6 deletions

View File

@@ -4,8 +4,10 @@ import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import java.util.List;
@Data
public class JobPostDto extends OrgCommonDto {
private Integer empCount;
private List<String> selfEmpIds;
}

View File

@@ -2,16 +2,25 @@ package cn.lihongjie.coal.jobPost.entity;
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import io.hypersistence.utils.hibernate.type.array.ListArrayType;
import jakarta.persistence.Entity;
import lombok.Data;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.Type;
import java.util.List;
@Data
@Entity
public class JobPostEntity extends OrgCommonEntity {
@Formula("(select count(*) from t_employee e where e.job_post_id = id)")
private Integer empCount;
@Type(ListArrayType.class)
@Formula("(select array_agg(e.id) from t_employee e where e.job_post_id = id)")
private List<String> selfEmpIds;
}

View File

@@ -16,6 +16,8 @@ import cn.lihongjie.coal.script.mapper.ScriptMapper;
import cn.lihongjie.coal.script.repository.ScriptRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
@@ -26,6 +28,7 @@ import lombok.Cleanup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@@ -43,6 +46,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;
import java.nio.charset.StandardCharsets;
import java.util.List;
@@ -199,21 +203,35 @@ class ScriptService extends BaseService<ScriptEntity, ScriptRepository> {
@Autowired
ObjectMapper objectMapper;
private final Cache<String, Class<?>> scriptCache = CacheBuilder.newBuilder().maximumSize(10000).build();
@SneakyThrows
public ScriptExecResultDto exec(ScriptExecResultDto json) throws ScriptException {
StopWatch stopWatch = new StopWatch("script");
stopWatch
.start("get");
// 执行groovy脚本
// 1. 从数据库中获取脚本
ScriptEntity scriptEntity = this.get(json.getId());
stopWatch.stop();
// 2. 执行脚本
stopWatch.start("parseClass");
Class parsedClass =
groovyClassLoader.parseClass(scriptEntity.getContent(), scriptEntity.getId());
scriptCache.get(scriptEntity.getId() + DigestUtils.md5Hex(scriptEntity.getContent()), () ->groovyClassLoader.parseClass(scriptEntity.getContent(), scriptEntity.getId()));
stopWatch.stop();
stopWatch.start("newInstance");
Script script = (Script) parsedClass.newInstance();
stopWatch.stop();
stopWatch.start("prepare");
Logger scriptLogger = LoggerFactory.getLogger("scriptLogger");
ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) scriptLogger;
@@ -237,6 +255,9 @@ class ScriptService extends BaseService<ScriptEntity, ScriptRepository> {
script.setProperty("params", ObjectUtils.defaultIfNull(json.getParams(), objectMapper.createObjectNode()));
script.setProperty("ioc", applicationContext);
stopWatch.stop();
stopWatch.start("run");
long start = System.currentTimeMillis();
try {
@@ -245,11 +266,11 @@ class ScriptService extends BaseService<ScriptEntity, ScriptRepository> {
json.setStackTrace(ExceptionUtils.getStackTrace(e));
}
long end = System.currentTimeMillis();
stopWatch.stop();
// 3. 返回结果
json.setLogs(appender.list.stream().map(x -> x.getFormattedMessage()).toList());
json.setTime(end - start);
// logger.info("{}", stopWatch.prettyPrint());
return json;
}
}