简单树,生成工具(反射和范型的使用)
1.新增annotation类
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeAnnotation {
//该字段为主键id
boolean id() default false;
//该字段为父级主键id
boolean pId() default false;
//该字段为序号
boolean order() default false;
//该字段为子集
boolean children() default false;
}
2.生成树工具
@AllArgsConstructor
@NoArgsConstructor
@Data
public class MyTreeUtil<T> {
private List<T> vos;
public void getTree(){
try {
// 新增自动排序
sort();
vos = assemblyTree(vos);
}catch (Exception e){
e.printStackTrace();
}finally {
}
}
public List<T> assemblyTree(List<T> vos) {
List<T> dd = new ArrayList<>();
List<T> zy = new ArrayList<>();
try {
for (T vo : vos) {
Class<?> cl = vo.getClass();
Field[] fields = cl.getDeclaredFields();
Boolean isDD = false;
for (Field field : fields) {
TreeAnnotation annotation = field.getAnnotation(TreeAnnotation.class);
if (null != annotation && annotation.pId()){
Object o = field.get(vo);
if (null != o){
String pid = (String) o;
if ("".equals(pid)){
isDD = true;
break;
}
}
}
}
if (isDD){
dd.add(vo);
}else {
zy.add(vo);
}
}
dd = assemblyTree(dd,zy);
}catch (Exception e){
e.printStackTrace();
}finally {
return dd;
}
}
public List<T> assemblyTree(List<T> dd,List<T> zy){
try {
Map<String, List<T>> zyMap = new LinkedHashMap<>();
zyMap = getZyMap(zyMap,zy);
for (T d : dd) {
List<T> equipmentTemplateTreeVos = new ArrayList<>();
// String id = "";
Class<?> cl = d.getClass();
Field[] fields = cl.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
TreeAnnotation annotation = field.getAnnotation(TreeAnnotation.class);
if (null != annotation && annotation.pId()){
Object o = field.get(d);
if (null != o){
String id = (String) o;
equipmentTemplateTreeVos = zyMap.get(id);
}
break;
}
}
if (null != equipmentTemplateTreeVos && equipmentTemplateTreeVos.size() != 0){
//排序后面来整 先使用LinkedHashMap
// equipmentTemplateTreeVos = equipmentTemplateTreeVos.stream().sorted(Comparator.comparing(T::getFldOrder)).collect(Collectors.toList());
List<T> cj = zy;
cj.removeAll(equipmentTemplateTreeVos);
equipmentTemplateTreeVos = assemblyTree(equipmentTemplateTreeVos,cj);
for (Field field : fields) {
field.setAccessible(true);
TreeAnnotation annotation = field.getAnnotation(TreeAnnotation.class);
if (null != annotation && annotation.children()){
field.set(d,equipmentTemplateTreeVos);
break;
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
return dd;
}
}
public Map<String, List<T>> getZyMap(Map<String, List<T>> zyMap,List<T> zy){
try {
for (T t : zy) {
Class<?> cl = t.getClass();
Field[] fields = cl.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
TreeAnnotation annotation = field.getAnnotation(TreeAnnotation.class);
if (null != annotation && annotation.pId()){
Object o = field.get(t);
if (null != o){
String pid = (String) o;
List<T> ts = zyMap.get(pid);
if (null == ts){
ts = new ArrayList<>();
ts.add(t);
zyMap.put(pid,ts);
}else {
ts.add(t);
zyMap.put(pid,ts);
}
break;
}
}
}
}
}catch (Exception e){
}finally {
return zyMap;
}
}
}
3.树工具的使用
实体添加@TreeAnnotation注解,注意只能一个值为true
@Data
@ApiModel("零配件树返回vo")
public class SparePartsTreeVo {
@TreeAnnotation(id = true)
private String fldGuid;
@TreeAnnotation(pId = true)
private String fldPGuid;
private String fldName;
private String fldCode;
@TreeAnnotation(order = true)
private String fldOrder;
private Integer fldType;
private Integer fldDIsAbled;
@TreeAnnotation(children = true)
private List<SparePartsTreeVo> children;
}
调用方法
List<SparePartsTreeVo> vos = new ArrayList<>();
MyTreeUtil<SparePartsTreeVo> sparePartsTreeVoMyTreeUtil = new MyTreeUtil<>();
sparePartsTreeVoMyTreeUtil.setVos(vos);
vos = sparePartsTreeVoMyTreeUtil.getTree();
-----------------------------------------新增内容---------------------------------------
4. 新增自动排序
/**
* 排序
*/
public void sort() throws IllegalAccessException {
Map<String,List<T>> map = new HashMap<>();
for (T vo : vos) {
Class<?> cl = vo.getClass();
Field[] fields = cl.getDeclaredFields();
for (Field field : fields) {
TreeAnnotation annotation = field.getAnnotation(TreeAnnotation.class);
if (null != annotation && annotation.order()){
Object o = field.get(vo);
String order = "";
if (null != o){
order = (String) o;
}
List<T> ts = map.get(order);
if (null == ts){
ts = new ArrayList<>();
}
ts.add(vo);
map.put(order,ts);
break;
}
}
}
List<String> orders = new ArrayList<>(map.keySet());
Collections.sort(orders);
vos.clear();
for (String order : orders) {
vos.addAll(map.get(order));
}
}