【dict】增加字典编码翻译通过jackson注解序列化

pull/3/head
liuhanqing 2021-01-16 23:38:50 +08:00
parent 813f998537
commit f9e85456e3
8 changed files with 178 additions and 14 deletions

View File

@ -53,7 +53,7 @@ public enum TreeNodeEnum {
* @author liuhanqing
* @date 2021/1/15 13:36
*/
public static String codeToMessage(String code) {
public static String codeToName(String code) {
if (null != code) {
for (TreeNodeEnum e : TreeNodeEnum.values()) {
if (e.getCode().equals(code)) {

View File

@ -24,13 +24,6 @@
<version>1.0.0</version>
</dependency>
<!--短信校验的业务-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>sms-business-validation</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -16,7 +16,14 @@
<packaging>jar</packaging>
<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>
</project>

View File

@ -9,11 +9,22 @@ package cn.stylefeng.roses.kernel.dict.api;
public interface DictApi {
/**
* id
* code
*
* @author fengshuonan
* @date 2020/12/25 14:14
*/
String getDictNameByDictCode(String dictCode);
/**
*
*
* @param dictTypeCode
* @param dictCode
* @return
* @author liuhanqing
* @date 2021/1/16 23:18
*/
String getDictName(String dictTypeCode, String dictCode);
}

View File

@ -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);
}
}

View File

@ -296,6 +296,33 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
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

View File

@ -61,6 +61,13 @@
<version>1.0.0</version>
</dependency>
<!--字典模块-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>dict-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -1,17 +1,16 @@
package cn.stylefeng.roses.kernel.notice.modular.entity;
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.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import lombok.EqualsAndHashCode;
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)
private String delFlag;
public String getPriorityLevelValue(){
/*public String getPriorityLevelValue(){
AtomicReference<String> value = new AtomicReference<>("");
Optional.ofNullable(this.priorityLevel).ifPresent(val ->{
value.set(MessagePriorityLevelEnum.getName(this.priorityLevel));
});
return value.get();
}*/
@JsonSerialize(using = DictValueSerializer.class)
public String getPriorityLevelValue() {
return "priority_level|" + this.priorityLevel;
}
}