From 3cbea40d4a20232861d0573a8ad6e64977398cf7 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Wed, 7 Sep 2022 11:28:02 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=907.2.5=E3=80=91=E3=80=90wrapper?= =?UTF-8?q?=E3=80=91=E5=AD=97=E6=AE=B5=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E3=80=81=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AF=B9=E7=94=A8=E6=88=B7id=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wrapper/api/util/MixFieldTypeUtil.java | 53 +++++++++++++ .../enums/EnumFieldFormatSerializer.java | 4 +- .../simple/SimpleFieldFormatSerializer.java | 29 +++++-- .../wrapper/field/util/CommonFormatUtil.java | 67 ++++++++-------- .../user/format/UserFormatProcess.java | 79 +++++++++++++++++++ 5 files changed, 187 insertions(+), 45 deletions(-) create mode 100644 kernel-d-wrapper/wrapper-api/src/main/java/cn/stylefeng/roses/kernel/wrapper/api/util/MixFieldTypeUtil.java create mode 100644 kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/format/UserFormatProcess.java diff --git a/kernel-d-wrapper/wrapper-api/src/main/java/cn/stylefeng/roses/kernel/wrapper/api/util/MixFieldTypeUtil.java b/kernel-d-wrapper/wrapper-api/src/main/java/cn/stylefeng/roses/kernel/wrapper/api/util/MixFieldTypeUtil.java new file mode 100644 index 000000000..e289b0a38 --- /dev/null +++ b/kernel-d-wrapper/wrapper-api/src/main/java/cn/stylefeng/roses/kernel/wrapper/api/util/MixFieldTypeUtil.java @@ -0,0 +1,53 @@ +package cn.stylefeng.roses.kernel.wrapper.api.util; + +import cn.hutool.core.util.ArrayUtil; +import cn.stylefeng.roses.kernel.rule.util.ObjectConvertUtil; + +import java.util.Collection; + +/** + * 字段类型判断 + * + * @author fengshuonan + * @date 2022/9/7 10:24 + */ +public class MixFieldTypeUtil { + + /** + * 判断fieldValue是否是Long、List、Long[]类型,如果是其中任何一种,则都返回true + * + * @author fengshuonan + * @date 2022/9/7 10:24 + */ + public static boolean getLongFlag(Object fieldValue) { + + // 直接判断是否是Long + if (fieldValue instanceof Long) { + return true; + } + + // 如果是集合类型 + else if (fieldValue instanceof Collection) { + Collection collectionList = (Collection) fieldValue; + for (Object item : collectionList) { + if (item instanceof Long) { + return true; + } + } + } + + // 如果是数组类型 + else if (ArrayUtil.isArray(fieldValue)) { + Object[] objects = ObjectConvertUtil.objToArray(fieldValue); + for (Object item : objects) { + if (item instanceof Long) { + return true; + } + } + } + + return false; + } + + +} diff --git a/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/enums/EnumFieldFormatSerializer.java b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/enums/EnumFieldFormatSerializer.java index 6832e7946..ec4c4b747 100644 --- a/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/enums/EnumFieldFormatSerializer.java +++ b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/enums/EnumFieldFormatSerializer.java @@ -8,6 +8,8 @@ import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import lombok.extern.slf4j.Slf4j; +import java.io.IOException; + /** * 针对@EnumFieldFormat注解的具体序列化过程 * @@ -33,7 +35,7 @@ public class EnumFieldFormatSerializer extends JsonSerializer { } @Override - public void serialize(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) { + public void serialize(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { // 最终转化的值 Object formattedValue = originValue; diff --git a/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/simple/SimpleFieldFormatSerializer.java b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/simple/SimpleFieldFormatSerializer.java index ca75b7d4a..21bb33cca 100644 --- a/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/simple/SimpleFieldFormatSerializer.java +++ b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/simple/SimpleFieldFormatSerializer.java @@ -8,6 +8,8 @@ import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import lombok.extern.slf4j.Slf4j; +import java.io.IOException; + /** * 针对@SimpleFieldFormat注解的具体序列化过程 * @@ -33,20 +35,31 @@ public class SimpleFieldFormatSerializer extends JsonSerializer { } @Override - public void serialize(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) { + public void serialize(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { + try { + this.action(originValue, jsonGenerator, serializerProvider); + } catch (Exception e) { + log.error("执行json的字段序列化出错", e); + // 报错后继续写入原始值,否则会响应的json不是规范的json + jsonGenerator.writeObject(originValue); + } + } + + /** + * 真正处理序列化的逻辑 + * + * @author fengshuonan + * @date 2022/9/7 11:11 + */ + private void action(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws InstantiationException, IllegalAccessException, IOException { // 创建具体字段转化的实现类 - SimpleFieldFormatProcess simpleFieldFormatProcess = null; - try { - simpleFieldFormatProcess = processClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - log.error("执行json的字段序列化出错", e); - return; - } + SimpleFieldFormatProcess simpleFieldFormatProcess = processClass.newInstance(); // 判断当前字段值是否可以转化 boolean canFormat = simpleFieldFormatProcess.canFormat(originValue); if (!canFormat) { + jsonGenerator.writeObject(originValue); return; } diff --git a/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/util/CommonFormatUtil.java b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/util/CommonFormatUtil.java index 6655be6a8..a6571364d 100644 --- a/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/util/CommonFormatUtil.java +++ b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/util/CommonFormatUtil.java @@ -28,44 +28,39 @@ public class CommonFormatUtil { * @author fengshuonan * @date 2022/9/6 17:16 */ - public static void writeField(FormatTypeEnum formatTypeEnum, Object originValue, Object formattedValue, JsonGenerator jsonGenerator) { - try { - // 如果原始值和转化值一样,则直接返回 - if (originValue.equals(formattedValue)) { - jsonGenerator.writeObject(originValue); - return; + public static void writeField(FormatTypeEnum formatTypeEnum, Object originValue, Object formattedValue, JsonGenerator jsonGenerator) throws IOException { + // 如果原始值和转化值一样,则直接返回 + if (originValue.equals(formattedValue)) { + jsonGenerator.writeObject(originValue); + return; + } + + // 如果转化模式是替换类型 + if (formatTypeEnum.equals(FormatTypeEnum.REPLACE)) { + jsonGenerator.writeObject(formattedValue); + } + + // 如果转化模式是新增一个包装字段 + else { + // 先写入原有值,保持不变 + jsonGenerator.writeObject(originValue); + + // 构造新的字段名,为原字段名+Wrapper + String fieldName = jsonGenerator.getOutputContext().getCurrentName(); + String newWrapperFieldName = fieldName + WrapperConstants.FILED_WRAPPER_SUFFIX; + + // 获取当前正在转化的对象 + Object currentObj = jsonGenerator.getOutputContext().getCurrentValue(); + + // 如果当前正在转化的对象中已经含有了字段名+Wrapper的字段,则生成时候带一个数字2 + Field declaredField = ClassUtil.getDeclaredField(currentObj.getClass(), newWrapperFieldName); + if (declaredField != null) { + newWrapperFieldName = newWrapperFieldName + "2"; } - // 如果转化模式是替换类型 - if (formatTypeEnum.equals(FormatTypeEnum.REPLACE)) { - jsonGenerator.writeObject(formattedValue); - } - - // 如果转化模式是新增一个包装字段 - else { - // 先写入原有值,保持不变 - jsonGenerator.writeObject(originValue); - - // 构造新的字段名,为原字段名+Wrapper - String fieldName = jsonGenerator.getOutputContext().getCurrentName(); - String newWrapperFieldName = fieldName + WrapperConstants.FILED_WRAPPER_SUFFIX; - - // 获取当前正在转化的对象 - Object currentObj = jsonGenerator.getOutputContext().getCurrentValue(); - - // 如果当前正在转化的对象中已经含有了字段名+Wrapper的字段,则生成时候带一个数字2 - Field declaredField = ClassUtil.getDeclaredField(currentObj.getClass(), newWrapperFieldName); - if (declaredField != null) { - newWrapperFieldName = newWrapperFieldName + "2"; - } - - // 写入新的字段名 - jsonGenerator.writeFieldName(newWrapperFieldName); - jsonGenerator.writeObject(formattedValue); - } - - } catch (IOException e) { - log.error("执行json的字段序列化出错", e); + // 写入新的字段名 + jsonGenerator.writeFieldName(newWrapperFieldName); + jsonGenerator.writeObject(formattedValue); } } diff --git a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/format/UserFormatProcess.java b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/format/UserFormatProcess.java new file mode 100644 index 000000000..6999d1365 --- /dev/null +++ b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/format/UserFormatProcess.java @@ -0,0 +1,79 @@ +package cn.stylefeng.roses.kernel.system.modular.user.format; + +import cn.hutool.core.convert.Convert; +import cn.hutool.extra.spring.SpringUtil; +import cn.stylefeng.roses.kernel.rule.base.SimpleFieldFormatProcess; +import cn.stylefeng.roses.kernel.rule.util.ObjectConvertUtil; +import cn.stylefeng.roses.kernel.scanner.api.enums.FieldTypeEnum; +import cn.stylefeng.roses.kernel.scanner.api.util.ClassTypeUtil; +import cn.stylefeng.roses.kernel.system.api.UserServiceApi; +import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO; +import cn.stylefeng.roses.kernel.wrapper.api.util.MixFieldTypeUtil; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * Json响应的针对用户的处理 + * + * @author fengshuonan + * @date 2022/9/7 10:09 + */ +public class UserFormatProcess implements SimpleFieldFormatProcess { + + @Override + public boolean canFormat(Object originValue) { + return MixFieldTypeUtil.getLongFlag(originValue); + } + + @Override + public Object formatProcess(Object originValue) { + + // 先获取是基础类型还是集合类型 + FieldTypeEnum classFieldType = ClassTypeUtil.getClassFieldType(originValue.getClass()); + + // 基础类型,直接转化 + if (FieldTypeEnum.BASIC.equals(classFieldType)) { + Long userId = Convert.toLong(originValue); + return this.userIdToName(userId); + } + + // 集合类型 + if (FieldTypeEnum.BASE_COLLECTION.equals(classFieldType)) { + Collection userIdList = (Collection) originValue; + List nameList = new ArrayList<>(); + for (Object userIdStr : userIdList) { + Long userId = Convert.toLong(userIdStr); + nameList.add(this.userIdToName(userId)); + } + return nameList; + } + + // 数组类型 + if (FieldTypeEnum.BASE_ARRAY.equals(classFieldType)) { + Object[] objects = ObjectConvertUtil.objToArray(originValue); + List nameList = new ArrayList<>(); + for (Object userIdStr : objects) { + Long userId = Convert.toLong(userIdStr); + nameList.add(this.userIdToName(userId)); + } + return nameList; + } + + return originValue; + } + + /** + * 用户id转化为姓名的过程 + * + * @author fengshuonan + * @date 2022/9/7 10:59 + */ + private String userIdToName(Long userId) { + UserServiceApi bean = SpringUtil.getBean(UserServiceApi.class); + SysUserDTO userInfoByUserId = bean.getUserInfoByUserId(userId); + return userInfoByUserId.getRealName(); + } + +}