All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zys.mybatis.utils.ListUtils Maven / Gradle / Ivy

There is a newer version: 0.2.7
Show newest version
package com.zys.mybatis.utils;

import com.zys.mybatis.exception.MybatisZysException;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Slf4j
public class ListUtils {


    /**
     * 指定一个list里面指定对象的属性,根据规则赋值到另一个list中
     * 判断的字段和要赋值的字段,两个list中必须要一致,类型要相同,字段名称也必须相同
     * @param targets 目标list
     * @param sources 源list
     * @param targetRuleField 目标对象判断的字段
     * @param sourceRuleField 源对象判断的字段
     * @param map key源属性名,value目标属性名 要赋值的字段
     * @param relation 关系一对一true 一对多false
     */
    public static  List copySpecifiedProperty(List targets, List sources, String targetRuleField, String sourceRuleField, Map map, boolean relation){
        if (map == null || map.isEmpty()) return targets;
        if (relation) {
            return targets.stream().peek(target-> {
                Object fieldValue = getFieldValue(target, targetRuleField);
                sources.forEach(source->{
                    if (Objects.equals(fieldValue, getFieldValue(source, sourceRuleField))) {
                        setValue(target, source, map, true);
                    }
                });
            }).collect(Collectors.toList());
        }
        Map> groupingBy = sources.stream().collect(Collectors.groupingBy(source -> getFieldValue(source, sourceRuleField)));
        return targets.stream().peek(target-> {
            Object fieldValue = getFieldValue(target, targetRuleField);
            groupingBy.forEach( (k, v) ->{
                if (Objects.equals(fieldValue, k)) {
                    setValue(target, v, map, false);
                }
            });
        }).collect(Collectors.toList());
    }

    /**
     * 指定一个list里面指定对象的属性,根据规则赋值到另一个list中
     * 判断的字段和要赋值的字段,两个list中必须要一致,类型要相同,字段名称也必须相同
     * @param targets 目标list
     * @param sources 源list
     * @param targetRuleField 目标对象判断的字段
     * @param sourceRuleField 源对象判断的字段
     * @param value 取值,赋值字段相同
     * @param 
     * @param 
     * @return
     */
    public static  List copySpecifiedProperty(List targets, List sources, String targetRuleField, String sourceRuleField, String value){
        return copySpecifiedProperty(targets,sources,targetRuleField,sourceRuleField, Collections.singletonMap(value, value), true);
    }

    private static Object getFieldValue(Object object, String fieldName){
        Field field = getField(object, fieldName);
        try {
            return field.get(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new MybatisZysException("获取属性值失败 " + fieldName);
    }

    private static Field getField(Object object,String fieldName){
        Class classes = object.getClass();
        for (; classes != Object.class; classes = classes.getSuperclass()) {
            try {
                Field declaredField = classes.getDeclaredField(fieldName);
                declaredField.setAccessible(true);
                return declaredField;
            } catch (Exception e) {
                log.warn("未找到字段 = " + fieldName + " 正在找父类");
            }
        }
        throw new MybatisZysException("未找到字段 = " + fieldName);
    }

    private static  void setValue(T target, U source, Map map, boolean relation){
        map.forEach((k,v)->{
            Field field = getField(target, v);
            try {
                if (relation) {
                    field.set(target, getFieldValue(source, k));
                } else {
                    List sources = (List) source;
                    field.set(target, sources.stream().map(s -> getFieldValue(s, k)).collect(Collectors.toList()));
                }
            } catch (Exception e) {
                throw new MybatisZysException("未找到字段 " + k, e);
            }
        });
    }

}