完善脚本

This commit is contained in:
2023-09-09 23:54:42 +08:00
parent 6a7ac03b25
commit 94669d6e60
14 changed files with 113 additions and 43 deletions

View File

@@ -26,6 +26,6 @@ public class CommonDto extends BaseDto {
@Comment("常用状态 0 禁用 1 启用")
private Integer status;
private String status;
}

View File

@@ -31,9 +31,9 @@ public class CommonQuery {
private List<QueryItem> items;
private Integer pageNo;
private Integer pageNo = 0;
private Integer pageSize;
private Integer pageSize = Integer.MAX_VALUE ;
private List<Order> orders;

View File

@@ -53,6 +53,9 @@ public class TreeDto {
return ans.stream().flatMap(x -> {
if (x == null) {
return Stream.empty();
}
if (x.getChildren() != null) {

View File

@@ -17,6 +17,15 @@ import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface CommonMapper {
public default Integer toInt(String s) {
try {
return Integer.valueOf(s);
} catch (Exception e) {
return null;
}
}
public default UserEntity createUser(String id) {
if (StringUtils.isEmpty(id)) {

View File

@@ -4,7 +4,6 @@ import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.base.controller.BaseController;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.dto.TreeDto;
import cn.lihongjie.coal.dictionary.dto.*;
import cn.lihongjie.coal.dictionary.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -14,8 +13,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/dictionary")
@SysLog(module = "数据字典管理")
@@ -46,7 +43,7 @@ public class DictionaryController extends BaseController {
}
@PostMapping("/tree")
public List<TreeDto> tree(@RequestBody DictTreeRequest dto) {
public DictionaryTreeDto tree(@RequestBody DictTreeRequest dto) {
return this.service.tree(dto);
}
@PostMapping("/list")

View File

@@ -0,0 +1,12 @@
package cn.lihongjie.coal.dictionary.dto;
import cn.lihongjie.coal.base.dto.TreeDto;
import lombok.Data;
import java.util.List;
@Data
public class DictionaryTreeDto extends DictionaryDto {
private List<TreeDto> tree;
}

View File

@@ -3,10 +3,7 @@ package cn.lihongjie.coal.dictionary.mapper;
import cn.lihongjie.coal.base.mapper.BaseMapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.dictionary.dto.CreateDictionaryDto;
import cn.lihongjie.coal.dictionary.dto.DictionaryDetailedDto;
import cn.lihongjie.coal.dictionary.dto.DictionaryDto;
import cn.lihongjie.coal.dictionary.dto.UpdateDictionaryDto;
import cn.lihongjie.coal.dictionary.dto.*;
import cn.lihongjie.coal.dictionary.entity.DictionaryEntity;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
@@ -20,4 +17,7 @@ import org.mapstruct.control.DeepClone;
)
public interface DictionaryMapper extends BaseMapper<DictionaryEntity, DictionaryDto, CreateDictionaryDto, UpdateDictionaryDto>{
DictionaryDetailedDto toDetailedDto(DictionaryEntity entity);
DictionaryTreeDto toDictTree(DictionaryEntity dict);
}

View File

@@ -2,9 +2,14 @@ package cn.lihongjie.coal.dictionary.repository;
import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.dictionary.entity.DictionaryEntity;
import cn.lihongjie.coal.script.entity.ScriptEntity;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface DictionaryRepository extends BaseRepository<DictionaryEntity> {
DictionaryEntity findByCode(String code);
Optional<DictionaryEntity> findByScript(ScriptEntity script);
}

View File

@@ -12,6 +12,7 @@ import cn.lihongjie.coal.dictionary.mapper.DictionaryMapper;
import cn.lihongjie.coal.dictionary.repository.DictionaryRepository;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.script.dto.ScriptExecResultDto;
import cn.lihongjie.coal.script.entity.ScriptEntity;
import cn.lihongjie.coal.script.service.ScriptService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -30,6 +31,7 @@ import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
@Service
@Slf4j
@@ -50,29 +52,33 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
@Autowired
ScriptService scriptService;
@SneakyThrows
public List<TreeDto> tree(DictTreeRequest request) {
public DictionaryTreeDto tree(DictTreeRequest request) {
DictionaryEntity dict = this.repository.findByCode(request.getCode());
boolean flatten = StringUtils.equalsIgnoreCase(dict.getComponentType(), "1");
DictionaryTreeDto ans = this.mapper.toDictTree(dict);
if (dict.getDictType().equalsIgnoreCase("1")) {
return TreeDto.buildList(dict.getItem(), flatten);
}
if (dict.getDictType().equalsIgnoreCase("2")) {
ans.setTree(TreeDto.buildList(dict.getItem(), flatten));
} else if (dict.getDictType().equalsIgnoreCase("2")) {
ScriptExecResultDto resultDto = new ScriptExecResultDto();
resultDto.setId(dict.getScript().getId());
resultDto.setParams(request.getParams());
ScriptExecResultDto result = scriptService.exec(resultDto);
if (StringUtils.isNotEmpty(result.getStackTrace())) {
log.warn("执行脚本出错 id:{}\nparams:{}\nstacktrace:{}\nlogs:{}", result.getId(), result.getParams().toString(), result.getStackTrace(), result.getLogs());
log.warn("执行脚本出错 id:{}\nparams:{}\nstacktrace:{}\nlogs:{}", result.getId(), result.getParams()+"", result.getStackTrace(), result.getLogs());
}
return TreeDto.buildList(result.getResponse(), flatten);
ans.setTree(TreeDto.buildList(result.getResponse(), flatten));
} else {
throw new BizException("不支持的字典类型 " + dict.getDictTypeName());
}
throw new BizException("不支持的字典类型 " + dict.getDictTypeName());
return ans;
}
@@ -101,9 +107,6 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
}
public void delete(IdRequest request) {
this.repository.deleteAllById(request.getIds());
@@ -120,8 +123,6 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
}
@Autowired
ConversionService conversionService;
@@ -201,15 +202,33 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
}
}
}
public void createScriptDict(ScriptEntity scriptEntity) {
Optional<DictionaryEntity> byScript = this.repository.findByScript(scriptEntity);
if (byScript.isPresent()) {
return;
}else {
DictionaryEntity dictionary = new DictionaryEntity();
dictionary.setName(scriptEntity.getName());
dictionary.setCode(scriptEntity.getCode());
dictionary.setDictType("2");
dictionary.setComponentType(scriptEntity.getScriptPath().contains("tree") ? "2" : "1");
dictionary.setScript(scriptEntity);
dictionary.setStatus(1);
this.repository.save(dictionary);
}
}
}

View File

@@ -10,10 +10,13 @@ import org.hibernate.annotations.Comment;
@Comment("脚本")
public class ScriptEntity extends CommonEntity {
@Comment("脚本路径")
private String scriptPath;
@Comment("脚本内容")
private String content;
@Comment("脚本类型")
private String scriptType;

View File

@@ -5,6 +5,7 @@ import ch.qos.logback.core.read.ListAppender;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.dictionary.service.DictionaryService;
import cn.lihongjie.coal.script.dto.CreateScriptDto;
import cn.lihongjie.coal.script.dto.ScriptDto;
import cn.lihongjie.coal.script.dto.ScriptExecResultDto;
@@ -18,6 +19,7 @@ import jakarta.annotation.PostConstruct;
import lombok.Cleanup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.codehaus.groovy.control.CompilerConfiguration;
@@ -107,36 +109,53 @@ public class ScriptService extends BaseService<ScriptEntity, ScriptRepository> {
return page.map(this.mapper::toDto);
}
@Value("classpath:scripts/*.groovy")
Resource[] resources;
@Value("classpath:scripts/dict/**/*.groovy")
Resource[] dictScripts;
@Autowired
DictionaryService dictionaryService;
@SneakyThrows
public void initFromResource(){
public void initFromResource() {
List<ScriptEntity> all = findAll();
for (Resource resource : resources) {
for (Resource resource : dictScripts) {
boolean find = false;
String baseName = FilenameUtils.getBaseName(resource.getFilename());
String path = FilenameUtils.getPath(resource.getFilename());
String ext = FilenameUtils.getExtension(resource.getFilename());
for (ScriptEntity scriptEntity : all) {
if (StringUtils.equals(scriptEntity.getName(), resource.getFilename())) {
if (StringUtils.equals(scriptEntity.getName(), baseName)) {
find = true;
scriptEntity.setContent(resource.getContentAsString(StandardCharsets.UTF_8));
scriptEntity.setScriptPath(path);
scriptEntity.setScriptType(ext);
scriptEntity.setCode(baseName);
this.repository.save(scriptEntity);
dictionaryService.createScriptDict(scriptEntity);
break;
}
}
if (!find) {
ScriptEntity scriptEntity = new ScriptEntity();
scriptEntity.setName(resource.getFilename());
scriptEntity.setName(baseName);
scriptEntity.setCode(baseName);
scriptEntity.setContent(resource.getContentAsString(StandardCharsets.UTF_8));
scriptEntity.setScriptPath(path);
scriptEntity.setScriptType(ext);
this.repository.save(scriptEntity);
dictionaryService.createScriptDict(scriptEntity);
}
}
}
@Autowired

View File

@@ -10,7 +10,6 @@ server:
# forward-headers-strategy: framework
system:
anonymous:
url:
@@ -83,6 +82,8 @@ spring:
flyway:
enabled: true
baseline-on-migrate: true
main:
allow-circular-references: true
logging:
file:
path: /data/logs

View File

@@ -1,5 +1,6 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.base.dto.TreeDto
import cn.lihongjie.coal.role.controller.RoleController
import org.springframework.context.ApplicationContext
@@ -10,5 +11,5 @@ def controller = ioc.getBean(RoleController.class)
return TreeDto.buildList(controller.list(new CommonQuery()), flatten)
return controller.list(new CommonQuery())

View File

@@ -1,4 +1,5 @@
import cn.lihongjie.coal.base.dto.TreeDto
package scripts.dict
import cn.lihongjie.coal.script.service.ScriptService
import org.springframework.context.ApplicationContext
@@ -6,5 +7,5 @@ ApplicationContext ioc = ioc;
def scriptService = ioc.getBean(ScriptService.class)
return TreeDto.buildList(scriptService.findAll(), flatten)
return scriptService.findAll()