From 94669d6e605ac9ebd9016b064fc318f9d141432f Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Sat, 9 Sep 2023 23:54:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/lihongjie/coal/base/dto/CommonDto.java | 2 +- .../lihongjie/coal/base/dto/CommonQuery.java | 4 +- .../cn/lihongjie/coal/base/dto/TreeDto.java | 3 + .../coal/base/mapper/CommonMapper.java | 9 +++ .../controller/DictionaryController.java | 5 +- .../dictionary/dto/DictionaryTreeDto.java | 12 ++++ .../dictionary/mapper/DictionaryMapper.java | 8 +-- .../repository/DictionaryRepository.java | 5 ++ .../dictionary/service/DictionaryService.java | 59 ++++++++++++------- .../coal/script/entity/ScriptEntity.java | 3 + .../coal/script/service/ScriptService.java | 33 ++++++++--- src/main/resources/application.yaml | 3 +- .../enum/roleDict.groovy} | 5 +- .../enum/scriptDict.groovy} | 5 +- 14 files changed, 113 insertions(+), 43 deletions(-) create mode 100644 src/main/java/cn/lihongjie/coal/dictionary/dto/DictionaryTreeDto.java rename src/main/resources/scripts/{roleTree.groovy => dict/enum/roleDict.groovy} (68%) rename src/main/resources/scripts/{scriptTree.groovy => dict/enum/scriptDict.groovy} (65%) diff --git a/src/main/java/cn/lihongjie/coal/base/dto/CommonDto.java b/src/main/java/cn/lihongjie/coal/base/dto/CommonDto.java index 9bb3c785..8b0c01a6 100644 --- a/src/main/java/cn/lihongjie/coal/base/dto/CommonDto.java +++ b/src/main/java/cn/lihongjie/coal/base/dto/CommonDto.java @@ -26,6 +26,6 @@ public class CommonDto extends BaseDto { @Comment("常用状态 0 禁用 1 启用") - private Integer status; + private String status; } diff --git a/src/main/java/cn/lihongjie/coal/base/dto/CommonQuery.java b/src/main/java/cn/lihongjie/coal/base/dto/CommonQuery.java index d5538424..66c24ba4 100644 --- a/src/main/java/cn/lihongjie/coal/base/dto/CommonQuery.java +++ b/src/main/java/cn/lihongjie/coal/base/dto/CommonQuery.java @@ -31,9 +31,9 @@ public class CommonQuery { private List items; - private Integer pageNo; + private Integer pageNo = 0; - private Integer pageSize; + private Integer pageSize = Integer.MAX_VALUE ; private List orders; diff --git a/src/main/java/cn/lihongjie/coal/base/dto/TreeDto.java b/src/main/java/cn/lihongjie/coal/base/dto/TreeDto.java index 3ad65c84..cca0e0c3 100644 --- a/src/main/java/cn/lihongjie/coal/base/dto/TreeDto.java +++ b/src/main/java/cn/lihongjie/coal/base/dto/TreeDto.java @@ -53,6 +53,9 @@ public class TreeDto { return ans.stream().flatMap(x -> { + if (x == null) { + return Stream.empty(); + } if (x.getChildren() != null) { diff --git a/src/main/java/cn/lihongjie/coal/base/mapper/CommonMapper.java b/src/main/java/cn/lihongjie/coal/base/mapper/CommonMapper.java index af234a49..5565d17c 100644 --- a/src/main/java/cn/lihongjie/coal/base/mapper/CommonMapper.java +++ b/src/main/java/cn/lihongjie/coal/base/mapper/CommonMapper.java @@ -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)) { diff --git a/src/main/java/cn/lihongjie/coal/dictionary/controller/DictionaryController.java b/src/main/java/cn/lihongjie/coal/dictionary/controller/DictionaryController.java index 5222d2f9..fbe1f0b6 100644 --- a/src/main/java/cn/lihongjie/coal/dictionary/controller/DictionaryController.java +++ b/src/main/java/cn/lihongjie/coal/dictionary/controller/DictionaryController.java @@ -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 tree(@RequestBody DictTreeRequest dto) { + public DictionaryTreeDto tree(@RequestBody DictTreeRequest dto) { return this.service.tree(dto); } @PostMapping("/list") diff --git a/src/main/java/cn/lihongjie/coal/dictionary/dto/DictionaryTreeDto.java b/src/main/java/cn/lihongjie/coal/dictionary/dto/DictionaryTreeDto.java new file mode 100644 index 00000000..2cab3a69 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/dictionary/dto/DictionaryTreeDto.java @@ -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 tree; +} diff --git a/src/main/java/cn/lihongjie/coal/dictionary/mapper/DictionaryMapper.java b/src/main/java/cn/lihongjie/coal/dictionary/mapper/DictionaryMapper.java index a1b85548..6025c0fe 100644 --- a/src/main/java/cn/lihongjie/coal/dictionary/mapper/DictionaryMapper.java +++ b/src/main/java/cn/lihongjie/coal/dictionary/mapper/DictionaryMapper.java @@ -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{ DictionaryDetailedDto toDetailedDto(DictionaryEntity entity); + + DictionaryTreeDto toDictTree(DictionaryEntity dict); + } diff --git a/src/main/java/cn/lihongjie/coal/dictionary/repository/DictionaryRepository.java b/src/main/java/cn/lihongjie/coal/dictionary/repository/DictionaryRepository.java index 67669ff8..07bd6035 100644 --- a/src/main/java/cn/lihongjie/coal/dictionary/repository/DictionaryRepository.java +++ b/src/main/java/cn/lihongjie/coal/dictionary/repository/DictionaryRepository.java @@ -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 findByCode(String code); + + Optional findByScript(ScriptEntity script); } \ No newline at end of file diff --git a/src/main/java/cn/lihongjie/coal/dictionary/service/DictionaryService.java b/src/main/java/cn/lihongjie/coal/dictionary/service/DictionaryService.java index a6f2ba15..7dc4470b 100644 --- a/src/main/java/cn/lihongjie/coal/dictionary/service/DictionaryService.java +++ b/src/main/java/cn/lihongjie/coal/dictionary/service/DictionaryService.java @@ -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 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 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); + } + + } } diff --git a/src/main/java/cn/lihongjie/coal/script/entity/ScriptEntity.java b/src/main/java/cn/lihongjie/coal/script/entity/ScriptEntity.java index 7162ffaa..03c782b9 100644 --- a/src/main/java/cn/lihongjie/coal/script/entity/ScriptEntity.java +++ b/src/main/java/cn/lihongjie/coal/script/entity/ScriptEntity.java @@ -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; diff --git a/src/main/java/cn/lihongjie/coal/script/service/ScriptService.java b/src/main/java/cn/lihongjie/coal/script/service/ScriptService.java index e69f4d13..4887022f 100644 --- a/src/main/java/cn/lihongjie/coal/script/service/ScriptService.java +++ b/src/main/java/cn/lihongjie/coal/script/service/ScriptService.java @@ -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 { 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 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 diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 74828861..ee44372c 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -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 diff --git a/src/main/resources/scripts/roleTree.groovy b/src/main/resources/scripts/dict/enum/roleDict.groovy similarity index 68% rename from src/main/resources/scripts/roleTree.groovy rename to src/main/resources/scripts/dict/enum/roleDict.groovy index 25879536..3179c2b0 100644 --- a/src/main/resources/scripts/roleTree.groovy +++ b/src/main/resources/scripts/dict/enum/roleDict.groovy @@ -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()) diff --git a/src/main/resources/scripts/scriptTree.groovy b/src/main/resources/scripts/dict/enum/scriptDict.groovy similarity index 65% rename from src/main/resources/scripts/scriptTree.groovy rename to src/main/resources/scripts/dict/enum/scriptDict.groovy index 05fe5943..a091bc80 100644 --- a/src/main/resources/scripts/scriptTree.groovy +++ b/src/main/resources/scripts/dict/enum/scriptDict.groovy @@ -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()