完善数据字典

This commit is contained in:
2023-09-07 22:20:13 +08:00
parent 1f3b31cf42
commit d4eb758a38
12 changed files with 214 additions and 12 deletions

View File

@@ -122,22 +122,19 @@ public interface CommonMapper {
}
public default String toString(Object o){
public default String toString(Object o) {
if (o instanceof String) {
return ((String) o);
}
if (o == null) {
return null;
}else if (o instanceof BaseEntity) {
} else if (o instanceof String) {
return ((String) o);
} else if (o instanceof BaseEntity) {
return ((BaseEntity) o).getId();
} else if (o instanceof BaseDto) {
return ((BaseDto) o).getId();
} else if (o instanceof String) {
return (String) o;
}else {
} else {
return o.toString();
}

View File

@@ -24,4 +24,5 @@ public interface CoalBlendMapper {
void updateEntity(@MappingTarget CoalBlendEntity entity, UpdateCoalBlendDto dto);
}

View File

@@ -1,8 +1,8 @@
package cn.lihongjie.coal.common;
import cn.lihongjie.coal.user.entity.UserEntity;
import cn.lihongjie.coal.session.SessionService;
import cn.lihongjie.coal.user.entity.UserEntity;
import lombok.experimental.UtilityClass;
import org.springframework.security.core.context.SecurityContextHolder;
@@ -47,4 +47,6 @@ public class Ctx {
public static UserEntity currentUser() {
return getAuthentication().getUser();
}
}

View File

@@ -8,8 +8,11 @@ import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;
import java.util.ArrayList;
import java.util.List;
@Entity
@@ -26,6 +29,15 @@ public class DictionaryEntity extends CommonEntity {
@Comment("字典类型 1 静态字典 2 动态字典")
private String dictType;
@Formula("(select i.name\n" +
"from t_dictionary d,\n" +
" t_dictionary_item i\n" +
"where d.id = i.dictionary_id\n" +
" and d.code = 'dict.type'\n" +
" and i.code = dict_type)")
private String dictTypeName;
@OneToOne
@Comment("动态字典关联的脚本")
private ScriptEntity script;
@@ -39,4 +51,37 @@ public class DictionaryEntity extends CommonEntity {
}
}
public void addItem(DictionaryItemEntity ni) {
if (this.item == null) {
this.item = new ArrayList<>();
}
;
boolean found = false;
for (DictionaryItemEntity di : item) {
if (StringUtils.equalsIgnoreCase(di.getCode(), ni.getCode())) {
found = true;
return;
}
}
if (!found) {
ni.initParent(null);
ni.initDict(this);
this.item.add(ni);
}
}
}

View File

@@ -28,6 +28,22 @@ public class DictionaryItemEntity extends CommonEntity {
private DictionaryItemEntity parent;
public void initParent(DictionaryItemEntity o) {
this.parent = o;
if (children != null) {
children.forEach(x -> x.initParent(this));
}
}
public void initDict(DictionaryEntity dictionaryEntity) {
this.dictionary= dictionaryEntity;
if (children != null) {
children.forEach(x -> x.initDict(dictionaryEntity));
}
}
}

View File

@@ -2,6 +2,7 @@ package cn.lihongjie.coal.dictionary.mapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.coalBlend.mapper.CoalBlendMapper;
import cn.lihongjie.coal.dictionary.dto.CreateDictionaryItemDto;
import cn.lihongjie.coal.dictionary.dto.DictionaryItemDto;
import cn.lihongjie.coal.dictionary.dto.UpdateDictionaryItemDto;
@@ -16,6 +17,9 @@ import org.mapstruct.MappingTarget;
)
public interface DictionaryItemMapper {
CoalBlendMapper INSTANCE = org.mapstruct.factory.Mappers.getMapper(CoalBlendMapper.class);
DictionaryItemEntity copy(DictionaryItemEntity i);
DictionaryItemDto toDto(DictionaryItemEntity user);
DictionaryItemEntity toEntity(CreateDictionaryItemDto request);

View File

@@ -1,4 +1,4 @@
package cn.lihongjie.coal.dictionary.mapper;
package cn.lihongjie.coal.dictionary.repository;
import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.dictionary.entity.DictionaryEntity;

View File

@@ -8,17 +8,28 @@ 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.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.mapper.DictionaryRepository;
import cn.lihongjie.coal.dictionary.repository.DictionaryRepository;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.util.List;
@Service
@Slf4j
public class DictionaryService extends BaseService<DictionaryEntity, DictionaryRepository> {
@@ -101,4 +112,76 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
return mapper.toDetailedDto(entity);
}
@Autowired
DictionaryItemMapper dictionaryItemMapper;
@SneakyThrows
public void initDefault() {
ClassPathResource classPathResource = new ClassPathResource("/config/dictionary.json");
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
List<DictionaryEntity> defaultDicts;
List<DictionaryEntity> exists = findAll();
try (InputStream inputStream = classPathResource.getInputStream()) {
defaultDicts = mapper.readValue(inputStream, new TypeReference<List<DictionaryEntity>>() {
});
}
for (DictionaryEntity dictionary : defaultDicts) {
boolean found = false;
for (DictionaryEntity exist : exists) {
if (StringUtils.equalsIgnoreCase(dictionary.getCode(), exist.getCode())) {
for (DictionaryItemEntity item : dictionary.getItem()) {
found = true;
exist.addItem(dictionaryItemMapper.copy(item));
}
}
}
if (!found) {
DictionaryEntity nd = new DictionaryEntity();
nd.setCode(dictionary.getCode());
nd.setName(dictionary.getName());
nd.setDictType("1");
for (DictionaryItemEntity item : dictionary.getItem()) {
nd.addItem(item);
}
this.repository.save(nd);
}
}
}
}

View File

@@ -10,7 +10,7 @@ public class ResourceDto extends CommonDto {
@Comment("资源类型")
private String type;
private String typeName;
@Comment("资源地址")

View File

@@ -8,6 +8,7 @@ import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;
import java.util.ArrayList;
import java.util.List;
@@ -34,6 +35,15 @@ public class ResourceEntity extends CommonEntity {
private String type;
@Formula("(select i.name\n" +
"from t_dictionary d,\n" +
" t_dictionary_item i\n" +
"where d.id = i.dictionary_id\n" +
" and d.code = 'resource.type'\n" +
" and i.code = type)")
private String typeName;
@Comment("资源地址")
private String url;

View File

@@ -1,6 +1,7 @@
package cn.lihongjie.coal.runner;
import cn.lihongjie.coal.coalParameterDef.service.CoalParameterDefService;
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;
@@ -31,6 +32,9 @@ public class InitDataRunner implements CommandLineRunner {
@Autowired
ResourceService resourceService;
@Autowired
DictionaryService dictionaryService;
@Override
public void run(String... args) throws Exception {
@@ -53,6 +57,9 @@ public class InitDataRunner implements CommandLineRunner {
resourceService.initUrlResource();
dictionaryService.initDefault();
} finally {
SecurityContextHolder.clearContext();

View File

@@ -0,0 +1,37 @@
[
{
"code": "resource.type",
"name": "资源类型",
"item": [
{
"code": "0",
"name": "菜单"
},
{
"code": "1",
"name": "按钮"
},
{
"code": "2",
"name": "url"
}
]
},
{
"code": "dict.type",
"name": "字典类型",
"item": [
{
"code": "1",
"name": "静态字典"
},
{
"code": "2",
"name": "动态字典"
}
]
}
]