mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-03-22 21:39:01 +08:00
@@ -5,7 +5,7 @@ import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 表格分页数据对象
|
||||
@@ -27,23 +27,12 @@ public class PageResult<T> implements Serializable {
|
||||
/**
|
||||
* 列表数据
|
||||
*/
|
||||
private Collection<T> rows;
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
* @param list 列表数据
|
||||
* @param total 总记录数
|
||||
*/
|
||||
public PageResult(Collection<T> list, long total) {
|
||||
this.rows = list;
|
||||
this.total = total;
|
||||
}
|
||||
private List<T> rows;
|
||||
|
||||
/**
|
||||
* 根据分页对象构建表格分页数据对象
|
||||
*/
|
||||
public static <T> PageResult<T> build(Collection<T> list, long total) {
|
||||
public static <T> PageResult<T> build(List<T> list, long total) {
|
||||
PageResult<T> rspData = new PageResult<>();
|
||||
rspData.setRows(list);
|
||||
rspData.setTotal(total);
|
||||
@@ -53,7 +42,7 @@ public class PageResult<T> implements Serializable {
|
||||
/**
|
||||
* 根据数据列表构建表格分页数据对象
|
||||
*/
|
||||
public static <T> PageResult<T> build(Collection<T> list) {
|
||||
public static <T> PageResult<T> build(List<T> list) {
|
||||
PageResult<T> rspData = new PageResult<>();
|
||||
rspData.setRows(list);
|
||||
rspData.setTotal(list.size());
|
||||
|
||||
@@ -6,7 +6,6 @@ import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
@@ -43,7 +42,7 @@ public class TaskAssigneeDTO implements Serializable {
|
||||
* 将源列表转换为 TaskHandler 列表
|
||||
*
|
||||
* @param <T> 通用类型
|
||||
* @param sourceCollection 待转换的源列表
|
||||
* @param sourceList 待转换的源列表
|
||||
* @param storageId 提取 storageId 的函数
|
||||
* @param handlerCode 提取 handlerCode 的函数
|
||||
* @param handlerName 提取 handlerName 的函数
|
||||
@@ -52,13 +51,13 @@ public class TaskAssigneeDTO implements Serializable {
|
||||
* @return 转换后的 TaskHandler 列表
|
||||
*/
|
||||
public static <T> List<TaskHandler> convertToHandlerList(
|
||||
Collection<T> sourceCollection,
|
||||
List<T> sourceList,
|
||||
Function<T, String> storageId,
|
||||
Function<T, String> handlerCode,
|
||||
Function<T, String> handlerName,
|
||||
Function<T, String> groupName,
|
||||
Function<T, Date> createTimeMapper) {
|
||||
return sourceCollection.stream()
|
||||
return sourceList.stream()
|
||||
.map(item -> new TaskHandler(
|
||||
storageId.apply(item),
|
||||
handlerCode.apply(item),
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package org.dromara.common.translation.collection;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 集合翻译(声明到需要翻译的实体上)
|
||||
*
|
||||
* @author 秋辞未寒
|
||||
*/
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE})
|
||||
@Documented
|
||||
public @interface TranslationCollection {
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
String type();
|
||||
|
||||
/**
|
||||
* 其他条件 例如: 字典type(sys_user_gender)
|
||||
*/
|
||||
String other() default "";
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package org.dromara.common.translation.collection;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||
import tools.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 序列化器绑定注解
|
||||
*
|
||||
* @author 秋辞未寒
|
||||
*/
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE})
|
||||
@Documented
|
||||
@JacksonAnnotationsInside
|
||||
@JsonSerialize(using = TranslationCollectionSerializer.class)
|
||||
public @interface TranslationCollectionBinding {
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package org.dromara.common.translation.collection;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 翻译集合处理器
|
||||
* @param <T> 输入类型
|
||||
* @param <R> 目标类型
|
||||
*
|
||||
* @author 秋辞未寒
|
||||
*/
|
||||
public interface TranslationCollectionProcessor<T,R> {
|
||||
|
||||
|
||||
Collection<R> process(Collection<T> value,String other) throws Exception;
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package org.dromara.common.translation.collection;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.common.core.utils.reflect.AnnotationUtils;
|
||||
import tools.jackson.core.JacksonException;
|
||||
import tools.jackson.core.JsonGenerator;
|
||||
import tools.jackson.databind.SerializationContext;
|
||||
import tools.jackson.databind.ValueSerializer;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 集合翻译序列化器
|
||||
*
|
||||
* @author 秋辞未寒
|
||||
*/
|
||||
@Slf4j
|
||||
public class TranslationCollectionSerializer extends ValueSerializer<TranslationCollectionWrapper<?>> {
|
||||
|
||||
/**
|
||||
* 全局翻译实现类映射器
|
||||
*/
|
||||
public static final Map<String, TranslationCollectionProcessor<?,?>> TRANSLATION_MAPPER = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void serialize(TranslationCollectionWrapper<?> value, JsonGenerator gen, SerializationContext provider) throws JacksonException {
|
||||
if (CollUtil.isEmpty(value)) {
|
||||
return;
|
||||
}
|
||||
Class<?> elementType = value.getElementType();
|
||||
if (elementType == null) {
|
||||
elementType = ClassUtil.getClass(CollUtil.getFirst(value));
|
||||
}
|
||||
// 获取包装原内容(很重要!!!!!!如果原样使用的话,会无限递归!)
|
||||
Collection<?> collection = value.getCollection();
|
||||
TranslationCollection annotation = AnnotationUtils.getAnnotation(elementType, TranslationCollection.class);
|
||||
if (annotation != null ) {
|
||||
String type = annotation.type();
|
||||
String other = annotation.other();
|
||||
TranslationCollectionProcessor processor = TRANSLATION_MAPPER.get(type);
|
||||
if (processor != null) {
|
||||
try {
|
||||
collection = processor.process(collection,other);
|
||||
} catch (Exception e) {
|
||||
log.error("翻译处理异常,type: {}, value: {}", type, value, e);
|
||||
// 出现异常时输出原始值而不是中断序列化
|
||||
collection = value.getCollection();
|
||||
}
|
||||
}
|
||||
}
|
||||
gen.writeStartArray();
|
||||
for (Object o : collection) {
|
||||
gen.writePOJO(o);
|
||||
}
|
||||
gen.writeEndArray();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package org.dromara.common.translation.collection;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
|
||||
/**
|
||||
* 集合翻译类型
|
||||
*
|
||||
* @author 秋辞未寒
|
||||
*/
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE})
|
||||
@Documented
|
||||
public @interface TranslationCollectionType {
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
String type();
|
||||
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package org.dromara.common.translation.collection;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 翻译集合包装器
|
||||
*
|
||||
* @param <E> 元素类型
|
||||
*
|
||||
* @author 秋辞未寒
|
||||
*/
|
||||
@Getter
|
||||
@TranslationCollectionBinding
|
||||
public class TranslationCollectionWrapper<E> implements Collection<E> {
|
||||
|
||||
private final Collection<E> collection;
|
||||
private final Class<E> elementType;
|
||||
|
||||
/**
|
||||
* 不建议直接使用构造函数去构建包装器
|
||||
*/
|
||||
private TranslationCollectionWrapper(Collection<E> collection, Class<E> elementType) {
|
||||
this.collection = collection;
|
||||
this.elementType = elementType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 包装集合成翻译集合
|
||||
* @param collection 待包装的集合
|
||||
* @return 包装后的集合类型
|
||||
* @param <E> 元素类型
|
||||
*/
|
||||
public static <E> TranslationCollectionWrapper<E> form(Collection<E> collection) {
|
||||
return form(collection,null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 包装集合成翻译集合 (推荐使用该函数,可以减少不必要的迭代器取值~)
|
||||
* @param collection 待包装的集合
|
||||
* @param elementType 元素Class实例
|
||||
* @return 包装后的集合类型
|
||||
* @param <E> 元素类型
|
||||
*/
|
||||
public static <E> TranslationCollectionWrapper<E> form(Collection<E> collection, Class<E> elementType) {
|
||||
if (Objects.isNull(collection)) {
|
||||
collection = Collections.emptyList();
|
||||
}
|
||||
if (Objects.isNull(elementType)) {
|
||||
elementType = ClassUtil.getClass(CollUtil.getFirst(collection));
|
||||
}
|
||||
return new TranslationCollectionWrapper<>(collection,elementType);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return collection.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return collection.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return collection.contains(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return collection.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return collection.toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
return collection.toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
return collection.add(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return collection.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
return collection.containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
return collection.addAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
return collection.removeAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
return collection.retainAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
collection.clear();
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,6 @@ package org.dromara.common.translation.config;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.common.translation.annotation.TranslationType;
|
||||
import org.dromara.common.translation.collection.TranslationCollectionProcessor;
|
||||
import org.dromara.common.translation.collection.TranslationCollectionSerializer;
|
||||
import org.dromara.common.translation.collection.TranslationCollectionType;
|
||||
import org.dromara.common.translation.core.TranslationInterface;
|
||||
import org.dromara.common.translation.core.handler.TranslationBeanSerializerModifier;
|
||||
import org.dromara.common.translation.core.handler.TranslationHandler;
|
||||
@@ -29,15 +26,12 @@ import java.util.Map;
|
||||
public class TranslationConfig {
|
||||
|
||||
@Autowired
|
||||
private List<TranslationInterface<?>> translationInterfaces;
|
||||
|
||||
@Autowired
|
||||
private List<TranslationCollectionProcessor<?,?>> translationCollectionHandlers;
|
||||
private List<TranslationInterface<?>> list;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
Map<String, TranslationInterface<?>> map = new HashMap<>(translationInterfaces.size());
|
||||
for (TranslationInterface<?> trans : translationInterfaces) {
|
||||
Map<String, TranslationInterface<?>> map = new HashMap<>(list.size());
|
||||
for (TranslationInterface<?> trans : list) {
|
||||
if (trans.getClass().isAnnotationPresent(TranslationType.class)) {
|
||||
TranslationType annotation = trans.getClass().getAnnotation(TranslationType.class);
|
||||
map.put(annotation.type(), trans);
|
||||
@@ -46,17 +40,6 @@ public class TranslationConfig {
|
||||
}
|
||||
}
|
||||
TranslationHandler.TRANSLATION_MAPPER.putAll(map);
|
||||
|
||||
Map<String, TranslationCollectionProcessor<Object,Object>> handlerMaps = new HashMap<>(translationCollectionHandlers.size());
|
||||
for (TranslationCollectionProcessor<?,?> trans : translationCollectionHandlers) {
|
||||
if (trans.getClass().isAnnotationPresent(TranslationCollectionType.class)) {
|
||||
TranslationCollectionType annotation = trans.getClass().getAnnotation(TranslationCollectionType.class);
|
||||
handlerMaps.put(annotation.type(), (TranslationCollectionProcessor<Object, Object>) trans);
|
||||
} else {
|
||||
log.warn(trans.getClass().getName() + " 翻译实现类未标注 TranslationCollectionType 注解!");
|
||||
}
|
||||
}
|
||||
TranslationCollectionSerializer.TRANSLATION_MAPPER.putAll(handlerMaps);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package org.dromara.demo.domain.vo;
|
||||
|
||||
import io.github.linpeilie.annotations.AutoMapper;
|
||||
import lombok.Data;
|
||||
import org.apache.fesod.sheet.annotation.ExcelIgnoreUnannotated;
|
||||
import org.apache.fesod.sheet.annotation.ExcelProperty;
|
||||
import org.apache.fesod.sheet.annotation.format.DateTimeFormat;
|
||||
import org.dromara.common.excel.annotation.ExcelNotation;
|
||||
import org.dromara.common.excel.annotation.ExcelRequired;
|
||||
import org.dromara.common.translation.annotation.Translation;
|
||||
import org.dromara.common.translation.collection.TranslationCollection;
|
||||
import org.dromara.common.translation.constant.TransConstant;
|
||||
import org.dromara.demo.domain.TestDemo;
|
||||
import io.github.linpeilie.annotations.AutoMapper;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
@@ -23,7 +22,6 @@ import java.util.Date;
|
||||
* @author Lion Li
|
||||
* @date 2021-07-26
|
||||
*/
|
||||
@TranslationCollection(type = "test")
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
@AutoMapper(target = TestDemo.class)
|
||||
|
||||
@@ -9,7 +9,6 @@ import org.dromara.common.core.exception.ServiceException;
|
||||
import org.dromara.common.core.utils.MapstructUtils;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.mybatis.core.page.PageQuery;
|
||||
import org.dromara.common.translation.collection.TranslationCollectionWrapper;
|
||||
import org.dromara.demo.domain.TestDemo;
|
||||
import org.dromara.demo.domain.bo.TestDemoBo;
|
||||
import org.dromara.demo.domain.vo.TestDemoVo;
|
||||
@@ -54,7 +53,7 @@ public class TestDemoServiceImpl implements ITestDemoService {
|
||||
public PageResult<TestDemoVo> queryPageList(TestDemoBo bo, PageQuery pageQuery) {
|
||||
LambdaQueryWrapper<TestDemo> lqw = buildQueryWrapper(bo);
|
||||
Page<TestDemoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||||
return PageResult.build(TranslationCollectionWrapper.form(result.getRecords()), result.getTotal());
|
||||
return PageResult.build(result.getRecords(), result.getTotal());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package org.dromara.demo.translation;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.common.json.utils.JsonUtils;
|
||||
import org.dromara.common.translation.collection.TranslationCollectionType;
|
||||
import org.dromara.common.translation.collection.TranslationCollectionProcessor;
|
||||
import org.dromara.demo.domain.vo.TestDemoVo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
@Slf4j
|
||||
@TranslationCollectionType(type = "test")
|
||||
public class TestDemoVoTranslationCollectionCollectionHandler implements TranslationCollectionProcessor<TestDemoVo,TestDemoVo> {
|
||||
|
||||
@Override
|
||||
public Collection<TestDemoVo> process(Collection<TestDemoVo> value, String other) throws Exception {
|
||||
log.info("翻译前的数据:{}", JsonUtils.toJsonString(value));
|
||||
ArrayList<TestDemoVo> vos = new ArrayList<>(value);
|
||||
for (TestDemoVo vo : vos) {
|
||||
vo.setValue("啊啦啦啦啦啦啦啦啦啦:"+ IdUtil.fastSimpleUUID());
|
||||
}
|
||||
log.info("翻译后的数据:{}", JsonUtils.toJsonString(vos));
|
||||
return vos;
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
org.dromara.demo.translation.TestDemoVoTranslationCollectionCollectionHandler
|
||||
Reference in New Issue
Block a user