mirror of https://gitee.com/stylefeng/roses
【dict】增加字典编码翻译通过jackson注解序列化
parent
813f998537
commit
f9e85456e3
|
@ -53,7 +53,7 @@ public enum TreeNodeEnum {
|
||||||
* @author liuhanqing
|
* @author liuhanqing
|
||||||
* @date 2021/1/15 13:36
|
* @date 2021/1/15 13:36
|
||||||
*/
|
*/
|
||||||
public static String codeToMessage(String code) {
|
public static String codeToName(String code) {
|
||||||
if (null != code) {
|
if (null != code) {
|
||||||
for (TreeNodeEnum e : TreeNodeEnum.values()) {
|
for (TreeNodeEnum e : TreeNodeEnum.values()) {
|
||||||
if (e.getCode().equals(code)) {
|
if (e.getCode().equals(code)) {
|
||||||
|
|
|
@ -24,13 +24,6 @@
|
||||||
<version>1.0.0</version>
|
<version>1.0.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!--短信校验的业务-->
|
|
||||||
<dependency>
|
|
||||||
<groupId>cn.stylefeng.roses</groupId>
|
|
||||||
<artifactId>sms-business-validation</artifactId>
|
|
||||||
<version>1.0.0</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -16,7 +16,14 @@
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-annotations</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -9,11 +9,22 @@ package cn.stylefeng.roses.kernel.dict.api;
|
||||||
public interface DictApi {
|
public interface DictApi {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取字典名称通过id
|
* 获取字典名称通过code
|
||||||
*
|
*
|
||||||
* @author fengshuonan
|
* @author fengshuonan
|
||||||
* @date 2020/12/25 14:14
|
* @date 2020/12/25 14:14
|
||||||
*/
|
*/
|
||||||
String getDictNameByDictCode(String dictCode);
|
String getDictNameByDictCode(String dictCode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过字典类型编码和字典编码获取名称
|
||||||
|
*
|
||||||
|
* @param dictTypeCode 字典类型编码
|
||||||
|
* @param dictCode 字典编码
|
||||||
|
* @return 字典名称
|
||||||
|
* @author liuhanqing
|
||||||
|
* @date 2021/1/16 23:18
|
||||||
|
*/
|
||||||
|
String getDictName(String dictTypeCode, String dictCode);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,116 @@
|
||||||
|
package cn.stylefeng.roses.kernel.dict.api.serializer;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.stylefeng.roses.kernel.dict.api.context.DictContext;
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
|
||||||
|
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
|
||||||
|
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
|
||||||
|
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* jackson 序列化获取字典名称
|
||||||
|
* <p>
|
||||||
|
* 使用@JsonSerialize(using=DictValueSerializer.class)
|
||||||
|
*
|
||||||
|
* @author liuhanqing
|
||||||
|
* @date 2021/1/16 22:21
|
||||||
|
*/
|
||||||
|
@JacksonStdImpl
|
||||||
|
public final class DictValueSerializer
|
||||||
|
extends StdScalarSerializer<Object> {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
/**
|
||||||
|
* 字典类型编码和字典值的分隔符
|
||||||
|
*/
|
||||||
|
private static final String SEPARATOR = "|";
|
||||||
|
/**
|
||||||
|
* 空值字符串
|
||||||
|
*/
|
||||||
|
private static final String NULL_STR = "null";
|
||||||
|
/**
|
||||||
|
* 字典值之前分隔符
|
||||||
|
*/
|
||||||
|
private static final String VALUE_SEPARATOR = ",";
|
||||||
|
|
||||||
|
public DictValueSerializer() {
|
||||||
|
super(String.class, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty(SerializerProvider prov, Object value) {
|
||||||
|
String str = (String) value;
|
||||||
|
return str.length() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||||
|
|
||||||
|
if (value == null) {
|
||||||
|
gen.writeNull();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String strVal = value.toString();
|
||||||
|
// 判断需要翻译字典值是否为不空且含有字典标识
|
||||||
|
if (StrUtil.isNotBlank(strVal) && strVal.indexOf(SEPARATOR) > 0) {
|
||||||
|
// 分隔需要序列化的值
|
||||||
|
String[] dictArr = strVal.split("\\|");
|
||||||
|
if (dictArr.length == 2) {
|
||||||
|
// 获取字典编码值
|
||||||
|
String dictCode = dictArr[1];
|
||||||
|
if (StrUtil.isBlank(dictCode) || NULL_STR.equals(dictArr[1])) {
|
||||||
|
strVal = StrUtil.EMPTY;
|
||||||
|
} else {
|
||||||
|
// 获取字典名称逻辑,多个名称用逗号分隔
|
||||||
|
String[] codeArr = dictCode.split(VALUE_SEPARATOR);
|
||||||
|
if (codeArr.length > 0) {
|
||||||
|
if (codeArr.length == 1) {
|
||||||
|
strVal = DictContext.me().getDictName(dictArr[0], codeArr[0]);
|
||||||
|
} else {
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
for (String dic : codeArr) {
|
||||||
|
String dicVal = DictContext.me().getDictName(dictArr[0], dic);
|
||||||
|
if (StrUtil.isNotBlank(dicVal)) {
|
||||||
|
sb.append(dicVal).append(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotEmpty(sb)) {
|
||||||
|
strVal = StrUtil.removeSuffix(sb.toString(), ",");
|
||||||
|
} else {
|
||||||
|
strVal = dictArr[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
strVal = StrUtil.EMPTY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gen.writeString(strVal);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void serializeWithType(Object value, JsonGenerator gen, SerializerProvider provider,
|
||||||
|
TypeSerializer typeSer) throws IOException {
|
||||||
|
// no type info, just regular serialization
|
||||||
|
gen.writeString((String) value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
|
||||||
|
return createSchemaNode("string", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
|
||||||
|
visitStringFormat(visitor, typeHint);
|
||||||
|
}
|
||||||
|
}
|
|
@ -296,6 +296,33 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
|
||||||
return StrUtil.EMPTY;
|
return StrUtil.EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public String getDictName(String dictTypeCode, String dictCode) {
|
||||||
|
LambdaQueryWrapper<SysDict> sysDictLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
sysDictLambdaQueryWrapper.eq(SysDict::getDictTypeCode, dictTypeCode);
|
||||||
|
sysDictLambdaQueryWrapper.eq(SysDict::getDictCode, dictCode);
|
||||||
|
sysDictLambdaQueryWrapper.ne(SysDict::getDelFlag, YesOrNotEnum.Y.getCode());
|
||||||
|
|
||||||
|
List<SysDict> list = this.list(sysDictLambdaQueryWrapper);
|
||||||
|
|
||||||
|
// 如果查询不到字典,则返回空串
|
||||||
|
if (list.isEmpty()) {
|
||||||
|
return StrUtil.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 字典code存在多个重复的,返回空串并打印错误日志
|
||||||
|
if (list.size() > 1) {
|
||||||
|
log.error(DICT_CODE_REPEAT.getUserTip(), "", dictCode);
|
||||||
|
return StrUtil.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
String dictName = list.get(0).getDictName();
|
||||||
|
if (dictName != null) {
|
||||||
|
return dictName;
|
||||||
|
} else {
|
||||||
|
return StrUtil.EMPTY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量修改pids的请求
|
* 批量修改pids的请求
|
||||||
|
|
|
@ -61,6 +61,13 @@
|
||||||
<version>1.0.0</version>
|
<version>1.0.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--字典模块-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.stylefeng.roses</groupId>
|
||||||
|
<artifactId>dict-api</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
package cn.stylefeng.roses.kernel.notice.modular.entity;
|
package cn.stylefeng.roses.kernel.notice.modular.entity;
|
||||||
|
|
||||||
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
|
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
|
||||||
import cn.stylefeng.roses.kernel.message.api.enums.MessagePriorityLevelEnum;
|
import cn.stylefeng.roses.kernel.dict.api.serializer.DictValueSerializer;
|
||||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通知表
|
* 通知表
|
||||||
|
@ -79,12 +78,16 @@ public class SysNotice extends BaseEntity {
|
||||||
@TableField(value = "del_flag", fill = FieldFill.INSERT)
|
@TableField(value = "del_flag", fill = FieldFill.INSERT)
|
||||||
private String delFlag;
|
private String delFlag;
|
||||||
|
|
||||||
public String getPriorityLevelValue(){
|
/*public String getPriorityLevelValue(){
|
||||||
AtomicReference<String> value = new AtomicReference<>("");
|
AtomicReference<String> value = new AtomicReference<>("");
|
||||||
Optional.ofNullable(this.priorityLevel).ifPresent(val ->{
|
Optional.ofNullable(this.priorityLevel).ifPresent(val ->{
|
||||||
value.set(MessagePriorityLevelEnum.getName(this.priorityLevel));
|
value.set(MessagePriorityLevelEnum.getName(this.priorityLevel));
|
||||||
});
|
});
|
||||||
return value.get();
|
return value.get();
|
||||||
|
}*/
|
||||||
|
@JsonSerialize(using = DictValueSerializer.class)
|
||||||
|
public String getPriorityLevelValue() {
|
||||||
|
return "priority_level|" + this.priorityLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue