mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-07-27 07:55:37 +08:00
数据字典初始化优化
This commit is contained in:
@@ -73,12 +73,13 @@ import javax.lang.model.element.Modifier;
|
||||
* required when running on JDK 16 and newer, due to JEP 396: Strongly Encapsulate JDK Internals by
|
||||
* Default:
|
||||
*
|
||||
* <p>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
|
||||
* --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
|
||||
* --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
|
||||
* --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
|
||||
* --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
|
||||
* --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
|
||||
* <p>
|
||||
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
|
||||
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
|
||||
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
|
||||
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
|
||||
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
|
||||
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
|
||||
*/
|
||||
public class Codegen {
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@ 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 cn.lihongjie.coal.sysState.entity.SysStateEntity;
|
||||
import cn.lihongjie.coal.sysState.service.SysStateService;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
@@ -43,6 +45,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.DigestUtils;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
import java.io.InputStream;
|
||||
@@ -60,6 +63,7 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
|
||||
@Autowired ScriptService scriptService;
|
||||
@Autowired ConversionService conversionService;
|
||||
@Autowired DictionaryItemMapper dictionaryItemMapper;
|
||||
@Autowired private SysStateService sysStateService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {}
|
||||
@@ -154,6 +158,20 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
|
||||
public void initDefault() {
|
||||
|
||||
ClassPathResource classPathResource = new ClassPathResource("/config/dictionary.json");
|
||||
SysStateEntity state = sysStateService.getByCode("dictionary.json.md5");
|
||||
|
||||
try (InputStream inputStream = classPathResource.getInputStream()) {
|
||||
String digestAsHex = DigestUtils.md5DigestAsHex(inputStream);
|
||||
|
||||
if (StringUtils.equals(digestAsHex, state.getStateValue())) {
|
||||
|
||||
log.info("数据字典 /config/dictionary.json 未发生变化, 跳过数据字典初始化 {}", digestAsHex);
|
||||
return;
|
||||
} else {
|
||||
log.info("数据字典 /config/dictionary.json 发生变化, 开始数据字典初始化 {}", digestAsHex);
|
||||
state.setStateValue(digestAsHex);
|
||||
}
|
||||
}
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
@@ -196,6 +214,8 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
|
||||
this.repository.save(nd);
|
||||
}
|
||||
}
|
||||
|
||||
sysStateService.save(state);
|
||||
}
|
||||
|
||||
public void createScriptDict(ScriptEntity scriptEntity) {
|
||||
@@ -299,27 +319,29 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
|
||||
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
|
||||
String where = tuple2s.stream()
|
||||
.map(x -> "(d.code = '" + x._1 + "' and di.code = '" + x._2 + "')")
|
||||
.reduce((x, y) -> x + " or " + y)
|
||||
.get();
|
||||
String where =
|
||||
tuple2s.stream()
|
||||
.map(x -> "(d.code = '" + x._1 + "' and di.code = '" + x._2 + "')")
|
||||
.reduce((x, y) -> x + " or " + y)
|
||||
.get();
|
||||
|
||||
stopWatch.start("createNativeQuery");
|
||||
Query nativeQuery = em.createNativeQuery(
|
||||
"""
|
||||
Query nativeQuery =
|
||||
em.createNativeQuery(
|
||||
"""
|
||||
select d.code as dict_key, di.code as dict_code, di.name as dict_name from t_dictionary d inner join t_dictionary_item di on d.id = di.dictionary_id
|
||||
where
|
||||
|
||||
|
||||
""" + where, Tuple.class);
|
||||
|
||||
"""
|
||||
+ where,
|
||||
Tuple.class);
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
stopWatch.start("getResultList");
|
||||
List<Tuple> resultList = nativeQuery.getResultList();
|
||||
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
stopWatch.start("collect");
|
||||
@@ -336,7 +358,7 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
// log.info(stopWatch.prettyPrint());
|
||||
// log.info(stopWatch.prettyPrint());
|
||||
return collect;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package cn.lihongjie.coal.sysState.controller;
|
||||
|
||||
import cn.lihongjie.coal.annotation.SysLog;
|
||||
import cn.lihongjie.coal.base.dto.CommonQuery;
|
||||
import cn.lihongjie.coal.base.dto.IdRequest;
|
||||
import cn.lihongjie.coal.sysState.dto.CreateSysStateDto;
|
||||
import cn.lihongjie.coal.sysState.dto.SysStateDto;
|
||||
import cn.lihongjie.coal.sysState.dto.UpdateSysStateDto;
|
||||
import cn.lihongjie.coal.sysState.service.SysStateService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/sysState")
|
||||
@SysLog(module = "系统状态")
|
||||
@Slf4j
|
||||
public class SysStateController {
|
||||
@Autowired private SysStateService service;
|
||||
|
||||
@PostMapping("/create")
|
||||
public SysStateDto create(@RequestBody CreateSysStateDto request) {
|
||||
return this.service.create(request);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
public SysStateDto update(@RequestBody UpdateSysStateDto request) {
|
||||
return this.service.update(request);
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public Object delete(@RequestBody IdRequest request) {
|
||||
this.service.delete(request);
|
||||
return true;
|
||||
}
|
||||
|
||||
@PostMapping("/getById")
|
||||
public SysStateDto getById(@RequestBody IdRequest request) {
|
||||
return this.service.getById(request.getId());
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
public Page<SysStateDto> list(@RequestBody CommonQuery request) {
|
||||
return this.service.list(request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package cn.lihongjie.coal.sysState.dto;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.CommonDto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CreateSysStateDto extends CommonDto {}
|
||||
@@ -0,0 +1,8 @@
|
||||
package cn.lihongjie.coal.sysState.dto;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.CommonDto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SysStateDto extends CommonDto {}
|
||||
@@ -0,0 +1,8 @@
|
||||
package cn.lihongjie.coal.sysState.dto;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.CommonDto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UpdateSysStateDto extends CommonDto {}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.lihongjie.coal.sysState.entity;
|
||||
|
||||
import cn.lihongjie.coal.base.entity.CommonEntity;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
public class SysStateEntity extends CommonEntity {
|
||||
|
||||
|
||||
String stateValue;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cn.lihongjie.coal.sysState.mapper;
|
||||
|
||||
import cn.lihongjie.coal.base.mapper.BaseMapper;
|
||||
import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
|
||||
import cn.lihongjie.coal.base.mapper.CommonMapper;
|
||||
import cn.lihongjie.coal.sysState.dto.CreateSysStateDto;
|
||||
import cn.lihongjie.coal.sysState.dto.SysStateDto;
|
||||
import cn.lihongjie.coal.sysState.dto.UpdateSysStateDto;
|
||||
import cn.lihongjie.coal.sysState.entity.SysStateEntity;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.control.DeepClone;
|
||||
|
||||
@Mapper(
|
||||
componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING,
|
||||
uses = {CommonMapper.class, CommonEntityMapper.class},
|
||||
mappingControl = DeepClone.class)
|
||||
public interface SysStateMapper
|
||||
extends BaseMapper<SysStateEntity, SysStateDto, CreateSysStateDto, UpdateSysStateDto> {}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.lihongjie.coal.sysState.repository;
|
||||
|
||||
import cn.lihongjie.coal.base.dao.BaseRepository;
|
||||
import cn.lihongjie.coal.sysState.entity.SysStateEntity;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface SysStateRepository extends BaseRepository<SysStateEntity> {
|
||||
@Query("select false")
|
||||
boolean isLinked(List<String> ids);
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package cn.lihongjie.coal.sysState.service;
|
||||
|
||||
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.dbFunctions.DbFunctionService;
|
||||
import cn.lihongjie.coal.exception.BizException;
|
||||
import cn.lihongjie.coal.sysState.dto.CreateSysStateDto;
|
||||
import cn.lihongjie.coal.sysState.dto.SysStateDto;
|
||||
import cn.lihongjie.coal.sysState.dto.UpdateSysStateDto;
|
||||
import cn.lihongjie.coal.sysState.entity.SysStateEntity;
|
||||
import cn.lihongjie.coal.sysState.mapper.SysStateMapper;
|
||||
import cn.lihongjie.coal.sysState.repository.SysStateRepository;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional
|
||||
public class SysStateService extends BaseService<SysStateEntity, SysStateRepository> {
|
||||
@PersistenceContext EntityManager em;
|
||||
@Autowired private SysStateRepository repository;
|
||||
@Autowired private SysStateMapper mapper;
|
||||
@Autowired private ConversionService conversionService;
|
||||
@Autowired private DbFunctionService dbFunctionService;
|
||||
|
||||
public SysStateDto create(CreateSysStateDto request) {
|
||||
SysStateEntity entity = mapper.toEntity(request);
|
||||
|
||||
this.repository.save(entity);
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
public SysStateDto update(UpdateSysStateDto request) {
|
||||
SysStateEntity entity = this.repository.get(request.getId());
|
||||
this.mapper.updateEntity(entity, request);
|
||||
|
||||
this.repository.save(entity);
|
||||
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
public void delete(IdRequest request) {
|
||||
boolean linked = this.repository.isLinked(request.getIds());
|
||||
|
||||
if (linked) {
|
||||
throw new BizException("数据已被关联,无法删除");
|
||||
}
|
||||
|
||||
this.repository.deleteAllById(request.getIds());
|
||||
}
|
||||
|
||||
public SysStateDto getById(String id) {
|
||||
SysStateEntity entity = repository.get(id);
|
||||
|
||||
return mapper.toDto(entity);
|
||||
}
|
||||
|
||||
public Page<SysStateDto> list(CommonQuery query) {
|
||||
Page<SysStateEntity> page =
|
||||
repository.findAll(
|
||||
query.specification(conversionService),
|
||||
PageRequest.of(
|
||||
query.getPageNo(),
|
||||
query.getPageSize(),
|
||||
Sort.by(query.getOrders())));
|
||||
|
||||
return page.map(this.mapper::toDto);
|
||||
}
|
||||
|
||||
public SysStateEntity getByCode(String code) {
|
||||
|
||||
|
||||
SysStateEntity sysState = em.createQuery("from SysStateEntity where code = :code", SysStateEntity.class)
|
||||
.setParameter("code", code)
|
||||
.getResultStream().findFirst().orElse(null);
|
||||
|
||||
if (sysState == null) {
|
||||
|
||||
SysStateEntity state = new SysStateEntity();
|
||||
state.setCode(code);
|
||||
state.setName(code);
|
||||
state.setStateValue("");
|
||||
em.persist(state);
|
||||
return state;
|
||||
} else {
|
||||
return sysState;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
17
src/main/resources/scripts/dict/enum/sysStateDict.groovy
Normal file
17
src/main/resources/scripts/dict/enum/sysStateDict.groovy
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
package scripts.dict
|
||||
|
||||
import cn.lihongjie.coal.base.dto.CommonQuery
|
||||
import cn.lihongjie.coal.sysState.controller.SysStateController
|
||||
import org.springframework.context.ApplicationContext
|
||||
|
||||
ApplicationContext ioc = ioc
|
||||
|
||||
def controller = ioc.getBean(SysStateController.class)
|
||||
|
||||
|
||||
|
||||
|
||||
return controller.list(new CommonQuery())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user