mirror of https://gitee.com/stylefeng/roses
【7.2.5】【rule】简化代码,封装抽象类
parent
f51961dd4b
commit
7b12597135
|
@ -0,0 +1,80 @@
|
||||||
|
package cn.stylefeng.roses.kernel.rule.format;
|
||||||
|
|
||||||
|
import cn.stylefeng.roses.kernel.rule.base.SimpleFieldFormatProcess;
|
||||||
|
import cn.stylefeng.roses.kernel.rule.enums.FieldTypeEnum;
|
||||||
|
import cn.stylefeng.roses.kernel.rule.util.ClassTypeUtil;
|
||||||
|
import cn.stylefeng.roses.kernel.rule.util.MixFieldTypeUtil;
|
||||||
|
import cn.stylefeng.roses.kernel.rule.util.ObjectConvertUtil;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对一般业务型的主键id的转化
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2022/9/7 11:50
|
||||||
|
*/
|
||||||
|
public abstract class BaseSimpleFieldFormatProcess implements SimpleFieldFormatProcess {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canFormat(Object originValue) {
|
||||||
|
return MixFieldTypeUtil.whetherAssignClass(originValue, Long.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object formatProcess(Object originValue) {
|
||||||
|
|
||||||
|
// 先获取是基础类型还是集合类型
|
||||||
|
FieldTypeEnum classFieldType = ClassTypeUtil.getClassFieldType(originValue.getClass());
|
||||||
|
|
||||||
|
// 基础类型,直接转化
|
||||||
|
if (FieldTypeEnum.BASIC.equals(classFieldType)) {
|
||||||
|
return this.simpleItemFormat(originValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 集合类型
|
||||||
|
if (FieldTypeEnum.BASE_COLLECTION.equals(classFieldType)) {
|
||||||
|
Collection<?> originValueList = (Collection<?>) originValue;
|
||||||
|
List<Object> parsedList = new ArrayList<>();
|
||||||
|
for (Object itemObject : originValueList) {
|
||||||
|
parsedList.add(this.simpleItemFormat(itemObject));
|
||||||
|
}
|
||||||
|
return parsedList;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数组类型
|
||||||
|
if (FieldTypeEnum.BASE_ARRAY.equals(classFieldType)) {
|
||||||
|
Object[] originValueArray = ObjectConvertUtil.objToArray(originValue);
|
||||||
|
List<Object> parsedList = new ArrayList<>();
|
||||||
|
for (Object itemObject : originValueArray) {
|
||||||
|
parsedList.add(this.simpleItemFormat(itemObject));
|
||||||
|
}
|
||||||
|
return parsedList;
|
||||||
|
}
|
||||||
|
|
||||||
|
return originValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 原始值得类型
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2022/9/7 11:52
|
||||||
|
*/
|
||||||
|
public abstract Class<?> getItemClass();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化转化的过程
|
||||||
|
* <p>
|
||||||
|
* 注意,这里只需写基本类型转化的逻辑,例如userId -> 用户名称
|
||||||
|
*
|
||||||
|
* @param businessId 业务id
|
||||||
|
* @return 转化之后的值
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2022/9/7 12:58
|
||||||
|
*/
|
||||||
|
public abstract Object simpleItemFormat(Object businessId);
|
||||||
|
|
||||||
|
}
|
|
@ -2,75 +2,26 @@ package cn.stylefeng.roses.kernel.system.modular.user.format;
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import cn.stylefeng.roses.kernel.rule.base.SimpleFieldFormatProcess;
|
import cn.stylefeng.roses.kernel.rule.format.BaseSimpleFieldFormatProcess;
|
||||||
import cn.stylefeng.roses.kernel.rule.enums.FieldTypeEnum;
|
|
||||||
import cn.stylefeng.roses.kernel.rule.util.ClassTypeUtil;
|
|
||||||
import cn.stylefeng.roses.kernel.rule.util.MixFieldTypeUtil;
|
|
||||||
import cn.stylefeng.roses.kernel.rule.util.ObjectConvertUtil;
|
|
||||||
import cn.stylefeng.roses.kernel.system.api.UserServiceApi;
|
import cn.stylefeng.roses.kernel.system.api.UserServiceApi;
|
||||||
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO;
|
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Json响应的针对用户的处理
|
* Json响应的针对用户的处理
|
||||||
*
|
*
|
||||||
* @author fengshuonan
|
* @author fengshuonan
|
||||||
* @date 2022/9/7 10:09
|
* @date 2022/9/7 10:09
|
||||||
*/
|
*/
|
||||||
public class UserFormatProcess implements SimpleFieldFormatProcess {
|
public class UserFormatProcess extends BaseSimpleFieldFormatProcess {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canFormat(Object originValue) {
|
public Class<?> getItemClass() {
|
||||||
return MixFieldTypeUtil.whetherAssignClass(originValue, Long.class);
|
return Long.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object formatProcess(Object originValue) {
|
public Object simpleItemFormat(Object businessId) {
|
||||||
|
Long userId = Convert.toLong(businessId);
|
||||||
// 先获取是基础类型还是集合类型
|
|
||||||
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<String> 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<String> 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);
|
UserServiceApi bean = SpringUtil.getBean(UserServiceApi.class);
|
||||||
SysUserDTO userInfoByUserId = bean.getUserInfoByUserId(userId);
|
SysUserDTO userInfoByUserId = bean.getUserInfoByUserId(userId);
|
||||||
return userInfoByUserId.getRealName();
|
return userInfoByUserId.getRealName();
|
||||||
|
|
Loading…
Reference in New Issue