mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 15:55:18 +08:00
数据字典下拉数据
This commit is contained in:
155
src/main/java/cn/lihongjie/coal/base/dto/TreeDto.java
Normal file
155
src/main/java/cn/lihongjie/coal/base/dto/TreeDto.java
Normal file
@@ -0,0 +1,155 @@
|
||||
package cn.lihongjie.coal.base.dto;
|
||||
|
||||
import cn.lihongjie.coal.base.entity.BaseEntity;
|
||||
import cn.lihongjie.coal.base.entity.CommonEntity;
|
||||
import cn.lihongjie.coal.common.ReflectUtils;
|
||||
import cn.lihongjie.coal.dictionary.dto.DictionaryItemDto;
|
||||
import cn.lihongjie.coal.dictionary.entity.DictionaryItemEntity;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class TreeDto {
|
||||
|
||||
private String id;
|
||||
|
||||
private String code;
|
||||
|
||||
private String name;
|
||||
|
||||
private List<TreeDto> children;
|
||||
|
||||
|
||||
|
||||
|
||||
public static List<TreeDto> buildList(Object object){
|
||||
|
||||
|
||||
if (object instanceof Iterable<?>) {
|
||||
|
||||
return buildList0((Iterable<?>) object);
|
||||
}
|
||||
|
||||
return buildList0(Arrays.asList(object));
|
||||
|
||||
}
|
||||
|
||||
private static List<TreeDto> buildList0(Iterable<?> object) {
|
||||
|
||||
|
||||
return StreamSupport.stream(object.spliterator(), false).map(x -> buildTree(x)).toList();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static TreeDto buildTree(Object x) {
|
||||
|
||||
|
||||
if (x == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
TreeDto dto = new TreeDto();
|
||||
|
||||
dto.setId(getId(x));
|
||||
dto.setName(getName(x));
|
||||
dto.setCode(getCode(x));
|
||||
dto.setChildren(getChildren(x));
|
||||
return dto;
|
||||
}
|
||||
|
||||
private static List<TreeDto> getChildren(Object x) {
|
||||
|
||||
if (x instanceof TreeDto) {
|
||||
return ((TreeDto) x).getChildren().stream().map( TreeDto::buildTree).toList();
|
||||
}
|
||||
|
||||
|
||||
Object c = ReflectUtils.getFieldValue(x, "children");
|
||||
|
||||
if (c == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
if (c instanceof Iterable<?>) {
|
||||
return buildList0((Iterable<?>) c);
|
||||
}
|
||||
|
||||
return buildList0(Arrays.asList(c));
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static String getCode(Object x) {
|
||||
if (x instanceof CommonEntity) {
|
||||
return ((CommonEntity) x).getCode();
|
||||
}
|
||||
|
||||
if (x instanceof CommonDto) {
|
||||
return ((CommonDto) x).getCode();
|
||||
}
|
||||
|
||||
if (x instanceof TreeDto) {
|
||||
return ((TreeDto) x).getCode();
|
||||
}
|
||||
|
||||
return ReflectUtils.getFieldValue(x, "code") + "";
|
||||
}
|
||||
|
||||
private static String getName(Object x) {
|
||||
|
||||
if (x instanceof CommonEntity) {
|
||||
return ((CommonEntity) x).getName();
|
||||
}
|
||||
|
||||
if (x instanceof CommonDto) {
|
||||
return ((CommonDto) x).getName();
|
||||
}
|
||||
|
||||
if (x instanceof TreeDto) {
|
||||
return ((TreeDto) x).getName();
|
||||
}
|
||||
|
||||
return ReflectUtils.getFieldValue(x, "name") + "";
|
||||
}
|
||||
|
||||
private static String getId(Object x) {
|
||||
|
||||
if (x instanceof DictionaryItemEntity) {
|
||||
return ((DictionaryItemEntity) x).getCode();
|
||||
}
|
||||
|
||||
if (x instanceof DictionaryItemDto) {
|
||||
return ((DictionaryItemDto) x).getCode();
|
||||
}
|
||||
|
||||
|
||||
if (x instanceof BaseEntity) {
|
||||
return ((BaseEntity) x).getId();
|
||||
}
|
||||
|
||||
if (x instanceof BaseDto) {
|
||||
return ((BaseDto) x).getId();
|
||||
}
|
||||
|
||||
if (x instanceof TreeDto) {
|
||||
return ((TreeDto) x).getId();
|
||||
}
|
||||
|
||||
|
||||
return ReflectUtils.getFieldValue(x, "id") + "";
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
29
src/main/java/cn/lihongjie/coal/common/ReflectUtils.java
Normal file
29
src/main/java/cn/lihongjie/coal/common/ReflectUtils.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package cn.lihongjie.coal.common;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
@UtilityClass
|
||||
public class ReflectUtils {
|
||||
|
||||
public static Object getFieldValue(Object object, String fieldName) {
|
||||
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(fieldName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Field field = ReflectionUtils.findField(object.getClass(), fieldName);
|
||||
if (field != null) {
|
||||
field.setAccessible(true);
|
||||
return ReflectionUtils.getField(field, object);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,9 @@ 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 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 org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -16,6 +14,8 @@ 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 = "数据字典管理")
|
||||
@@ -45,7 +45,10 @@ public class DictionaryController extends BaseController {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/tree")
|
||||
public List<TreeDto> tree(@RequestBody DictTreeRequest dto) {
|
||||
return this.service.tree(dto);
|
||||
}
|
||||
@PostMapping("/list")
|
||||
public Page<DictionaryDto> list(@RequestBody CommonQuery dto) {
|
||||
return this.service.list(dto);
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.lihongjie.coal.dictionary.dto;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DictTreeRequest {
|
||||
private String code;
|
||||
|
||||
private JsonNode params;
|
||||
}
|
||||
@@ -6,4 +6,5 @@ import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface DictionaryRepository extends BaseRepository<DictionaryEntity> {
|
||||
DictionaryEntity findByCode(String code);
|
||||
}
|
||||
@@ -2,16 +2,17 @@ package cn.lihongjie.coal.dictionary.service;
|
||||
|
||||
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.base.service.BaseService;
|
||||
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 cn.lihongjie.coal.dictionary.entity.DictionaryItemEntity;
|
||||
import cn.lihongjie.coal.dictionary.mapper.DictionaryItemMapper;
|
||||
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.service.ScriptService;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -47,6 +48,33 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
|
||||
|
||||
}
|
||||
|
||||
@Autowired
|
||||
ScriptService scriptService;
|
||||
@SneakyThrows
|
||||
public List<TreeDto> tree(DictTreeRequest request) {
|
||||
|
||||
DictionaryEntity dict = this.repository.findByCode(request.getCode());
|
||||
|
||||
if (dict.getDictType().equalsIgnoreCase("1")) {
|
||||
|
||||
return TreeDto.buildList(dict.getItem());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
return TreeDto.buildList(result.getResponse());
|
||||
}
|
||||
|
||||
throw new BizException("不支持的字典类型 " + dict.getDictTypeName());
|
||||
|
||||
}
|
||||
|
||||
public DictionaryDto create(CreateDictionaryDto request) {
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import cn.lihongjie.coal.dictionary.service.DictionaryService;
|
||||
import cn.lihongjie.coal.organization.entity.OrganizationEntity;
|
||||
import cn.lihongjie.coal.organization.service.OrganizationService;
|
||||
import cn.lihongjie.coal.resource.service.ResourceService;
|
||||
import cn.lihongjie.coal.script.service.ScriptService;
|
||||
import cn.lihongjie.coal.session.SessionService;
|
||||
import cn.lihongjie.coal.user.entity.UserEntity;
|
||||
import cn.lihongjie.coal.user.service.UserService;
|
||||
@@ -37,6 +38,9 @@ public class InitDataRunner implements CommandLineRunner {
|
||||
@Autowired
|
||||
Flyway flyway;
|
||||
|
||||
@Autowired
|
||||
ScriptService scriptService;
|
||||
|
||||
|
||||
@Autowired
|
||||
DictionaryService dictionaryService;
|
||||
@@ -67,6 +71,8 @@ public class InitDataRunner implements CommandLineRunner {
|
||||
|
||||
dictionaryService.initDefault();
|
||||
|
||||
scriptService.initFromResource();
|
||||
|
||||
|
||||
} finally {
|
||||
SecurityContextHolder.clearContext();
|
||||
|
||||
@@ -18,19 +18,24 @@ import jakarta.annotation.PostConstruct;
|
||||
import lombok.Cleanup;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.codehaus.groovy.control.CompilerConfiguration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@@ -101,6 +106,37 @@ public class ScriptService extends BaseService<ScriptEntity, ScriptRepository> {
|
||||
|
||||
return page.map(this.mapper::toDto);
|
||||
|
||||
}
|
||||
@Value("classpath:scripts/*.groovy")
|
||||
Resource[] resources;
|
||||
@SneakyThrows
|
||||
public void initFromResource(){
|
||||
|
||||
List<ScriptEntity> all = findAll();
|
||||
for (Resource resource : resources) {
|
||||
|
||||
boolean find = false;
|
||||
for (ScriptEntity scriptEntity : all) {
|
||||
|
||||
if (StringUtils.equals(scriptEntity.getName(), resource.getFilename())) {
|
||||
find = true;
|
||||
scriptEntity.setContent(resource.getContentAsString(StandardCharsets.UTF_8));
|
||||
this.repository.save(scriptEntity);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!find) {
|
||||
|
||||
ScriptEntity scriptEntity = new ScriptEntity();
|
||||
scriptEntity.setName(resource.getFilename());
|
||||
scriptEntity.setContent(resource.getContentAsString(StandardCharsets.UTF_8));
|
||||
this.repository.save(scriptEntity);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Autowired
|
||||
|
||||
14
src/main/resources/scripts/roleTree.groovy
Normal file
14
src/main/resources/scripts/roleTree.groovy
Normal file
@@ -0,0 +1,14 @@
|
||||
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
|
||||
|
||||
ApplicationContext ioc = ioc;
|
||||
|
||||
def controller = ioc.getBean(RoleController.class)
|
||||
|
||||
|
||||
|
||||
|
||||
return TreeDto.buildList(controller.list(new CommonQuery()))
|
||||
|
||||
10
src/main/resources/scripts/scriptTree.groovy
Normal file
10
src/main/resources/scripts/scriptTree.groovy
Normal file
@@ -0,0 +1,10 @@
|
||||
import cn.lihongjie.coal.base.dto.TreeDto
|
||||
import cn.lihongjie.coal.script.service.ScriptService
|
||||
import org.springframework.context.ApplicationContext
|
||||
|
||||
ApplicationContext ioc = ioc;
|
||||
|
||||
def scriptService = ioc.getBean(ScriptService.class)
|
||||
|
||||
return TreeDto.buildList(scriptService.findAll())
|
||||
|
||||
Reference in New Issue
Block a user