feat(enum): 统一结构化枚举值传输与同步
This commit is contained in:
@@ -5,7 +5,7 @@ import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum CommonStatusEnum {
|
||||
public enum CommonStatusEnum implements PersistableSysEnumDefinition {
|
||||
|
||||
DISABLED(0, "禁用"),
|
||||
ENABLED(1, "启用"),
|
||||
@@ -14,7 +14,28 @@ public enum CommonStatusEnum {
|
||||
COMPLETED(4, "已完成"),
|
||||
FAILED(5, "失败");
|
||||
|
||||
private static final String CATALOG = "common";
|
||||
|
||||
private static final String TYPE = "common_status";
|
||||
|
||||
private static final String REMARK = "通用状态";
|
||||
|
||||
private final Integer value;
|
||||
|
||||
private final String label;
|
||||
|
||||
@Override
|
||||
public String getCatalog() {
|
||||
return CATALOG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRemark() {
|
||||
return REMARK;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,33 @@ import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum EnableStatusEnum {
|
||||
public enum EnableStatusEnum implements PersistableSysEnumDefinition {
|
||||
|
||||
DISABLED(0, "禁用"),
|
||||
ENABLED(1, "启用");
|
||||
|
||||
private static final String CATALOG = "common";
|
||||
|
||||
private static final String TYPE = "enable_status";
|
||||
|
||||
private static final String REMARK = "通用启用状态";
|
||||
|
||||
private final Integer value;
|
||||
|
||||
private final String label;
|
||||
|
||||
@Override
|
||||
public String getCatalog() {
|
||||
return CATALOG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRemark() {
|
||||
return REMARK;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.bruce.common.enums;
|
||||
|
||||
/**
|
||||
* 可同步到 sys_enum 的枚举定义契约。
|
||||
* <p>
|
||||
* 长期固定的结构化文本字段统一通过该契约描述,
|
||||
* 便于前后端传值、数据库初始化和后续协作保持同一事实来源。
|
||||
*/
|
||||
public interface PersistableSysEnumDefinition {
|
||||
|
||||
/**
|
||||
* 枚举所属模块目录,例如 common、rag。
|
||||
*/
|
||||
String getCatalog();
|
||||
|
||||
/**
|
||||
* 枚举所属类型,同一 catalog/type 视为同一个枚举组。
|
||||
*/
|
||||
String getType();
|
||||
|
||||
/**
|
||||
* 落库到 sys_enum.name 的展示名称。
|
||||
*/
|
||||
default String getName() {
|
||||
return getLabel();
|
||||
}
|
||||
|
||||
/**
|
||||
* 枚举整型值,前后端协议统一传该值。
|
||||
*/
|
||||
Integer getValue();
|
||||
|
||||
/**
|
||||
* 可选的字符串值,当前大多数业务枚举不使用。
|
||||
*/
|
||||
default String getStrvalue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 排序值,默认与枚举值保持一致。
|
||||
*/
|
||||
default Integer getSort() {
|
||||
return getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* sys_enum.remark 中的说明文本。
|
||||
*/
|
||||
String getRemark();
|
||||
|
||||
/**
|
||||
* 枚举显示文案,通常与 name 保持一致。
|
||||
*/
|
||||
String getLabel();
|
||||
}
|
||||
@@ -25,4 +25,6 @@ public interface ISysEnumService extends IService<SysEnum> {
|
||||
boolean saveOrUpdate(SysEnumSaveRequest request);
|
||||
|
||||
boolean batchSave(SysEnumBatchSaveRequest request);
|
||||
|
||||
boolean replaceByCatalogAndType(String catalog, String type, List<SysEnum> items);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.bruce.common.dto.response.SysEnumResponse;
|
||||
import com.bruce.common.mapper.SysEnumMapper;
|
||||
import com.bruce.common.service.ISysEnumService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -137,6 +138,24 @@ public class SysEnumServiceImpl extends ServiceImpl<SysEnumMapper, SysEnum> impl
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean replaceByCatalogAndType(String catalog, String type, List<SysEnum> items) {
|
||||
log.info("SysEnumServiceImpl.replaceByCatalogAndType start, catalog={}, type={}, itemCount={}",
|
||||
catalog, type, items == null ? 0 : items.size());
|
||||
validateReplaceByCatalogAndTypeRequest(catalog, type, items);
|
||||
|
||||
boolean removed = lambdaUpdate()
|
||||
.eq(SysEnum::getCatalog, catalog)
|
||||
.eq(SysEnum::getType, type)
|
||||
.remove();
|
||||
boolean saved = saveBatch(items);
|
||||
boolean result = removed || saved;
|
||||
log.info("SysEnumServiceImpl.replaceByCatalogAndType success, catalog={}, type={}, removed={}, saved={}, result={}",
|
||||
catalog, type, removed, saved, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void validateBatchSaveRequest(SysEnumBatchSaveRequest request, List<SysEnum> existingEnums) {
|
||||
log.info("SysEnumServiceImpl.validateBatchSaveRequest start");
|
||||
if (request == null) {
|
||||
@@ -183,6 +202,44 @@ public class SysEnumServiceImpl extends ServiceImpl<SysEnumMapper, SysEnum> impl
|
||||
request.getCatalog(), request.getType(), requestValues.size(), existingValues.size());
|
||||
}
|
||||
|
||||
private void validateReplaceByCatalogAndTypeRequest(String catalog, String type, List<SysEnum> items) {
|
||||
if (!StringUtils.hasText(catalog)) {
|
||||
throw new IllegalArgumentException("模块目录不能为空");
|
||||
}
|
||||
if (!StringUtils.hasText(type)) {
|
||||
throw new IllegalArgumentException("枚举类型不能为空");
|
||||
}
|
||||
if (items == null || items.isEmpty()) {
|
||||
throw new IllegalArgumentException("枚举项不能为空");
|
||||
}
|
||||
|
||||
Set<Integer> values = new HashSet<>();
|
||||
Set<Integer> sorts = new HashSet<>();
|
||||
for (SysEnum item : items) {
|
||||
if (item == null) {
|
||||
throw new IllegalArgumentException("枚举项不能为空");
|
||||
}
|
||||
if (!catalog.equals(item.getCatalog()) || !type.equals(item.getType())) {
|
||||
throw new IllegalArgumentException("替换的枚举项 catalog/type 必须与目标分组一致");
|
||||
}
|
||||
if (!StringUtils.hasText(item.getName())) {
|
||||
throw new IllegalArgumentException("枚举名称不能为空");
|
||||
}
|
||||
if (item.getValue() == null) {
|
||||
throw new IllegalArgumentException("枚举整型值不能为空");
|
||||
}
|
||||
if (!values.add(item.getValue())) {
|
||||
throw new IllegalArgumentException("枚举值重复: " + item.getValue());
|
||||
}
|
||||
if (item.getSort() == null) {
|
||||
throw new IllegalArgumentException("枚举排序不能为空");
|
||||
}
|
||||
if (!sorts.add(item.getSort())) {
|
||||
throw new IllegalArgumentException("枚举排序重复: " + item.getSort());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<SysEnumResponse> toResponses(List<SysEnum> enums) {
|
||||
return enums.stream()
|
||||
.map(SysEnumResponse::fromEntity)
|
||||
|
||||
Reference in New Issue
Block a user