72 lines
2.3 KiB
Java
72 lines
2.3 KiB
Java
|
package com.isu.gaswellwatch.utils;
|
||
|
|
||
|
import cn.hutool.core.bean.BeanUtil;
|
||
|
import cn.hutool.core.collection.CollUtil;
|
||
|
import cn.hutool.core.util.ReflectUtil;
|
||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||
|
import com.isu.gaswellwatch.vo.TreeVO;
|
||
|
import org.slf4j.Logger;
|
||
|
import org.slf4j.LoggerFactory;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
import java.util.stream.Collectors;
|
||
|
|
||
|
public class ConverterUtil {
|
||
|
|
||
|
private static final Logger logger = LoggerFactory.getLogger(ConverterUtil.class);
|
||
|
|
||
|
public static <T> T convert(Object source, Class<T> tClass) {
|
||
|
T tar = ReflectUtil.newInstanceIfPossible(tClass);
|
||
|
BeanUtil.copyProperties(source, tar);
|
||
|
return tar;
|
||
|
}
|
||
|
|
||
|
public static <T> List<T> convert(List<?> source, Class<T> tClass) {
|
||
|
if(CollUtil.isEmpty(source)) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
return source.stream().map(o -> {
|
||
|
T tar = ReflectUtil.newInstanceIfPossible(tClass);
|
||
|
BeanUtil.copyProperties(o,tar);
|
||
|
return tar;
|
||
|
}).collect(Collectors.toList());
|
||
|
}
|
||
|
|
||
|
public static <T> IPage<T> convertIPage(IPage<?> source, Class<T> tClass) {
|
||
|
IPage<T> result = new Page<>();
|
||
|
if(null == source){
|
||
|
return result ;
|
||
|
}
|
||
|
|
||
|
BeanUtil.copyProperties(source,result);
|
||
|
result.setRecords(convert(source.getRecords(),tClass));
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
public static <T> Page<T> convertPage(Page<?> source, Class<T> tClass) {
|
||
|
Page<T> result = new Page<>();
|
||
|
if(null == source){
|
||
|
return result ;
|
||
|
}
|
||
|
|
||
|
BeanUtil.copyProperties(source,result);
|
||
|
result.setRecords(convert(source.getRecords(),tClass));
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
public static <T extends TreeVO> List<T> convertListToTree(List<T> list, Long parentId) {
|
||
|
if(CollUtil.isEmpty(list)) return new ArrayList<>() ;
|
||
|
return list.stream()
|
||
|
// 筛选父节点
|
||
|
.filter(t -> t.getParent().equals(parentId))
|
||
|
// 递归设置子节点
|
||
|
.peek(item -> item.setChildren(convertListToTree(list,item.getId())))
|
||
|
//排序
|
||
|
//.sorted(Comparator.comparing(TreeVO::getSort))
|
||
|
.collect(Collectors.toList());
|
||
|
}
|
||
|
}
|