package api.common.util;

import api.common.pojo.annotation.WChildren;
import api.common.pojo.annotation.WId;
import api.common.pojo.annotation.WParentId;
import api.common.pojo.po.model.VehiclePO;
import api.common.pojo.vo.model.VehicleVO;
import api.common.pojo.vo.system.DictVO;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class ObjectUtil {

    /**
     * 空值判断
     */
    @SuppressWarnings("all")
    public static boolean isNull(Object obj) {
        boolean isNullFlag = true;
        if (obj != null) {
            if (obj instanceof List) {
                isNullFlag = isNull((List) obj);
            } else if (obj instanceof Set) {
                isNullFlag = isNull((Set) obj);
            } else if (obj instanceof Object[]) {
                isNullFlag = isNull((Object[]) ((Object[]) obj));
            } else if (obj instanceof Map) {
                isNullFlag = isNull((Map) obj);
            } else if (obj instanceof String) {
                isNullFlag = isNull((String) obj);
            } else if (obj instanceof Integer) {
                isNullFlag = isNull((Integer) obj);
            } else if (obj instanceof Boolean) {
                isNullFlag = isNull((Boolean) obj);
            } else if (obj instanceof StringBuffer) {
                isNullFlag = isNull((StringBuffer) obj);
            } else if (obj instanceof JsonNode) {
                isNullFlag = isNull((JsonNode) obj);
            } else {
                isNullFlag = false;
            }
        }
        return isNullFlag;
    }

    public static boolean isNotNull(Object obj) {
        return !isNull(obj);
    }

    public static boolean isNull(List<?> list) {
        return list == null || list.size() == 0 || list.get(0) == null;
    }

    public static boolean isNull(Set<?> set) {
        return set == null || set.size() == 0;
    }

    public static boolean isNull(Object[] objects) {
        return objects == null || objects.length == 0;
    }

    public static boolean isNull(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    public static boolean isNull(String str) {
        return str == null || "".equals(str.trim()) || "null".equalsIgnoreCase(str.trim()) || "undefined".equalsIgnoreCase(str.trim());
    }

    public static boolean isNull(Integer integer) {
        return integer == null;
    }

    public static boolean isNull(Boolean bol) {
        return bol == null;
    }

    public static boolean isNull(StringBuffer buffer) {
        return buffer == null || buffer.length() == 0;
    }

    public static boolean isNull(JsonNode jsonNode) {
        return jsonNode.isNull() || jsonNode.isEmpty();
    }

    public static List<DictVO> DictListToTree(List<DictVO> list) {
        List<DictVO> treeList = new ArrayList<>();
        for (DictVO dictVO : list) {
            if ("0".equals(dictVO.getPid())) {
                treeList.add(findChildren(dictVO, list));
            }
        }
        return treeList;
    }

    private static DictVO findChildren(DictVO dictVO, List<DictVO> list) {
        for (DictVO node : list) {
            if (dictVO.getId().equals(node.getPid())) {
                if (dictVO.getChildren() == null || dictVO.getChildren().size() == 0) {
                    dictVO.setChildren(new ArrayList<>());
                }
                dictVO.getChildren().add(findChildren(node, list));
            }
        }
        return dictVO;
    }

    public static void voToPo(Object source, Object target) {
        BeanUtils.copyProperties(source, target);
    }

    public static VehiclePO vehicleVoToVehiclePo(VehicleVO vehicleVO) {
        VehiclePO vehiclePO = new VehiclePO();
        vehiclePO.setId(vehicleVO.getId());  //主键id
        vehiclePO.setVehicleName(vehicleVO.getVehicleName());  //车辆名称
        vehiclePO.setDescription(vehicleVO.getDescription()); //车辆描述
        vehiclePO.setVehicleType(vehicleVO.getVehicleTypeStr());    //车辆类型（字典代码）
        vehiclePO.setModelLabel(vehicleVO.getModelLabel());    //车辆模型标签
        vehiclePO.setShare(vehicleVO.getShare());    //是否分享
        vehiclePO.setParameterType(vehicleVO.getParameterType());    // 1 vtd 2 carsim
        //* -------------------------------- vtd --------------------------------
        vehiclePO.setVehicleCode(vehicleVO.getVehicleCode()); //车辆编码
        vehiclePO.setVehicleFrontView(vehicleVO.getVehicleFrontView());    //车辆前视图
        vehiclePO.setVehicleTopView(vehicleVO.getVehicleTopView());    //车辆俯视图
        vehiclePO.setMaxSpeed(vehicleVO.getMaxSpeed());    //最大速度（千米/小时）
        vehiclePO.setEnginePower(vehicleVO.getEnginePower());    //发动机功率（千瓦）
        vehiclePO.setMaxDeceleration(vehicleVO.getMaxDeceleration());    //最大减速度（米/秒2）
        vehiclePO.setMaxSteeringAngle(vehicleVO.getMaxSteeringAngle());    //最大转角（度）
        vehiclePO.setMass(vehicleVO.getMass());    //质量（千克）
        vehiclePO.setFrontSurfaceEffective(vehicleVO.getFrontSurfaceEffective());    //前表面有效面积（平方米）
        vehiclePO.setAirDragCoefficient(vehicleVO.getAirDragCoefficient());    //空气阻力系数
        vehiclePO.setRollingResistanceCoefficient(vehicleVO.getRollingResistanceCoefficient());    //滚动阻力系数
        vehiclePO.setWheelDiameter(vehicleVO.getWheelDiameter());    //车轮直径（米）
        vehiclePO.setWheelDrive(vehicleVO.getWheelDrive());    //驱动方式
        vehiclePO.setOverallEfficiency(vehicleVO.getOverallEfficiency());    //总效率
        vehiclePO.setFrontDistance(vehicleVO.getFrontDistance());    //车前距（米）
        vehiclePO.setRearDistance(vehicleVO.getRearDistance());    //车后距（米）
        vehiclePO.setLeftDistance(vehicleVO.getLeftDistance());    //车左距（米）
        vehiclePO.setRightDistance(vehicleVO.getRightDistance());    //车右距（米）
        vehiclePO.setHeightDistance(vehicleVO.getHeightDistance());    //车高（米）
        vehiclePO.setWheelbase(vehicleVO.getWheelbase());    //轴距（米）
        //* -------------------------------- carsim --------------------------------
        vehiclePO.setWheelCenterToTop(vehicleVO.getWheelCenterToTop());//轮胎中心到车顶的高度
        vehiclePO.setWheelCenterToGround(vehicleVO.getWheelCenterToGround());//轮胎中心到地面的高度
        vehiclePO.setCentroidToFrontWheelCenter(vehicleVO.getCentroidToFrontWheelCenter());//质心到前轮中心
        vehiclePO.setCentroidToGround(vehicleVO.getCentroidToGround());//质心到地面
        //    private String wheelbase(vehicleVO.get());//轴距
        vehiclePO.setSprungMass(vehicleVO.getSprungMass());//簧上质量
        vehiclePO.setVehicleLength(vehicleVO.getVehicleLength());//车辆长度 LEN_SM
        vehiclePO.setVehicleWidth(vehicleVO.getVehicleWidth());//车辆宽度
        vehiclePO.setRotationalInertiaX(vehicleVO.getRotationalInertiaX());//整车绕x轴转动惯量
        vehiclePO.setRotationalInertiaY(vehicleVO.getRotationalInertiaY());//整车绕y轴转动惯量
        vehiclePO.setRotationalInertiaZ(vehicleVO.getRotationalInertiaZ());//整车绕z轴转动惯量

        vehiclePO.setFrontLeftWheelEffectiveRollingRadius(vehicleVO.getFrontLeftWheelEffectiveRollingRadius());//左前轮有效滚动半径
        vehiclePO.setFrontLeftWheelRollingResistanceCoefficient(vehicleVO.getFrontLeftWheelRollingResistanceCoefficient());//左前轮滚动阻力系数
        vehiclePO.setFrontLeftWheelFreeRadius(vehicleVO.getFrontLeftWheelFreeRadius());//左前轮自由半径
        vehiclePO.setFrontLeftWheelRelationCurvePositiveMomentAndSideslipAngle(vehicleVO.getFrontLeftWheelRelationCurvePositiveMomentAndSideslipAngle());//左前轮不同载荷下正力矩和侧偏角关系曲线
        vehiclePO.setFrontLeftWheelRelationCurvePositiveMomentAndSideslipAngleJson(vehicleVO.getFrontLeftWheelRelationCurvePositiveMomentAndSideslipAngleJson());//左前轮不同载荷下正力矩和侧偏角关系曲线
        vehiclePO.setFrontLeftWheelRelationCurveLateralForceAndSideslipAngle(vehicleVO.getFrontLeftWheelRelationCurveLateralForceAndSideslipAngle());//左前轮不同载荷下侧向力和侧偏角关系曲线
        vehiclePO.setFrontLeftWheelRelationCurveLateralForceAndSideslipAngleJson(vehicleVO.getFrontLeftWheelRelationCurveLateralForceAndSideslipAngleJson());//左前轮不同载荷下侧向力和侧偏角关系曲线
        vehiclePO.setFrontLeftWheelRelationCurveLongitudinalForceAndSideslipAngle(vehicleVO.getFrontLeftWheelRelationCurveLongitudinalForceAndSideslipAngle());//左前轮不同载荷下纵向力和侧偏角关系曲线
        vehiclePO.setFrontLeftWheelRelationCurveLongitudinalForceAndSideslipAngleJson(vehicleVO.getFrontLeftWheelRelationCurveLongitudinalForceAndSideslipAngleJson());//左前轮不同载荷下纵向力和侧偏角关系曲线

        vehiclePO.setRearLeftWheelEffectiveRollingRadius(vehicleVO.getRearLeftWheelEffectiveRollingRadius());//左前轮有效滚动半径
        vehiclePO.setRearLeftWheelRollingResistanceCoefficient(vehicleVO.getRearLeftWheelRollingResistanceCoefficient());//左前轮滚动阻力系数
        vehiclePO.setRearLeftWheelFreeRadius(vehicleVO.getRearLeftWheelFreeRadius());//左前轮自由半径
        vehiclePO.setRearLeftWheelRelationCurvePositiveMomentAndSideslipAngle(vehicleVO.getRearLeftWheelRelationCurvePositiveMomentAndSideslipAngle());//左后轮不同载荷下正力矩和侧偏角关系曲线
        vehiclePO.setRearLeftWheelRelationCurvePositiveMomentAndSideslipAngleJson(vehicleVO.getRearLeftWheelRelationCurvePositiveMomentAndSideslipAngleJson());//左后轮不同载荷下正力矩和侧偏角关系曲线
        vehiclePO.setRearLeftWheelRelationCurveLateralForceAndSideslipAngle(vehicleVO.getRearLeftWheelRelationCurveLateralForceAndSideslipAngle());//左后轮不同载荷下侧向力和侧偏角关系曲线
        vehiclePO.setRearLeftWheelRelationCurveLateralForceAndSideslipAngleJson(vehicleVO.getRearLeftWheelRelationCurveLateralForceAndSideslipAngleJson());//左后轮不同载荷下侧向力和侧偏角关系曲线
        vehiclePO.setRearLeftWheelRelationCurveLongitudinalForceAndSideslipAngle(vehicleVO.getRearLeftWheelRelationCurveLongitudinalForceAndSideslipAngle());//左后轮不同载荷下纵向力和侧偏角关系曲线
        vehiclePO.setRearLeftWheelRelationCurveLongitudinalForceAndSideslipAngleJson(vehicleVO.getRearLeftWheelRelationCurveLongitudinalForceAndSideslipAngleJson());//左后轮不同载荷下纵向力和侧偏角关系曲线

        vehiclePO.setFrontRightWheelEffectiveRollingRadius(vehicleVO.getFrontRightWheelEffectiveRollingRadius());//右前轮有效滚动半径
        vehiclePO.setFrontRightWheelRollingResistanceCoefficient(vehicleVO.getFrontRightWheelRollingResistanceCoefficient());//右前轮滚动阻力系数
        vehiclePO.setFrontRightWheelFreeRadius(vehicleVO.getFrontRightWheelFreeRadius());//右前轮自由半径
        vehiclePO.setFrontRightWheelRelationCurvePositiveMomentAndSideslipAngle(vehicleVO.getFrontRightWheelRelationCurvePositiveMomentAndSideslipAngle());//右前轮不同载荷下正力矩和侧偏角关系曲线
        vehiclePO.setFrontRightWheelRelationCurvePositiveMomentAndSideslipAngleJson(vehicleVO.getFrontRightWheelRelationCurvePositiveMomentAndSideslipAngleJson());//右前轮不同载荷下正力矩和侧偏角关系曲线
        vehiclePO.setFrontRightWheelRelationCurveLateralForceAndSideslipAngle(vehicleVO.getFrontRightWheelRelationCurveLateralForceAndSideslipAngle());//右前轮不同载荷下侧向力和侧偏角关系曲线
        vehiclePO.setFrontRightWheelRelationCurveLateralForceAndSideslipAngleJson(vehicleVO.getFrontRightWheelRelationCurveLateralForceAndSideslipAngleJson());//右前轮不同载荷下侧向力和侧偏角关系曲线
        vehiclePO.setFrontRightWheelRelationCurveLongitudinalForceAndSideslipAngle(vehicleVO.getFrontRightWheelRelationCurveLongitudinalForceAndSideslipAngle());//右前轮不同载荷下纵向力和侧偏角关系曲线
        vehiclePO.setFrontRightWheelRelationCurveLongitudinalForceAndSideslipAngleJson(vehicleVO.getFrontRightWheelRelationCurveLongitudinalForceAndSideslipAngleJson());//右前轮不同载荷下纵向力和侧偏角关系曲线

        vehiclePO.setRearRightWheelEffectiveRollingRadius(vehicleVO.getRearRightWheelEffectiveRollingRadius());//右后轮有效滚动半径
        vehiclePO.setRearRightWheelRollingResistanceCoefficient(vehicleVO.getRearRightWheelRollingResistanceCoefficient());//右后轮滚动阻力系数
        vehiclePO.setRearRightWheelFreeRadius(vehicleVO.getRearRightWheelFreeRadius());//右后轮自由半径
        vehiclePO.setRearRightWheelRelationCurvePositiveMomentAndSideslipAngle(vehicleVO.getRearRightWheelRelationCurvePositiveMomentAndSideslipAngle());//右后轮不同载荷下正力矩和侧偏角关系曲线
        vehiclePO.setRearRightWheelRelationCurvePositiveMomentAndSideslipAngleJson(vehicleVO.getRearRightWheelRelationCurvePositiveMomentAndSideslipAngleJson());//右后轮不同载荷下正力矩和侧偏角关系曲线
        vehiclePO.setRearRightWheelRelationCurveLateralForceAndSideslipAngle(vehicleVO.getRearRightWheelRelationCurveLateralForceAndSideslipAngle());//右后轮不同载荷下侧向力和侧偏角关系曲线
        vehiclePO.setRearRightWheelRelationCurveLateralForceAndSideslipAngleJson(vehicleVO.getRearRightWheelRelationCurveLateralForceAndSideslipAngleJson());//右后轮不同载荷下侧向力和侧偏角关系曲线
        vehiclePO.setRearRightWheelRelationCurveLongitudinalForceAndSideslipAngle(vehicleVO.getRearRightWheelRelationCurveLongitudinalForceAndSideslipAngle());//右后轮不同载荷下纵向力和侧偏角关系曲线
        vehiclePO.setRearRightWheelRelationCurveLongitudinalForceAndSideslipAngleJson(vehicleVO.getRearRightWheelRelationCurveLongitudinalForceAndSideslipAngleJson());//右后轮不同载荷下纵向力和侧偏角关系曲线

        vehiclePO.setFrontWheelSuspensionType(vehicleVO.getFrontWheelSuspensionType());//前轮悬架类型 1 独立悬架 2 非独立悬架
        vehiclePO.setFrontWheelSuspensionKinematicsCoefficient(vehicleVO.getFrontWheelSuspensionKinematicsCoefficient());//前轮悬架运动学系数
        vehiclePO.setFrontWheelUnsprungMass(vehicleVO.getFrontWheelUnsprungMass());//前轮簧下质量
        vehiclePO.setFrontWheelTrackWidth(vehicleVO.getFrontWheelTrackWidth());//前轮轮距
        vehiclePO.setRearWheelSuspensionType(vehicleVO.getRearWheelSuspensionType());//后轮悬架类型 1 独立悬架 2 非独立悬架
        vehiclePO.setRearWheelSuspensionKinematicsCoefficient(vehicleVO.getRearWheelSuspensionKinematicsCoefficient());//后轮悬架运动学系数
        vehiclePO.setRearWheelUnsprungMass(vehicleVO.getRearWheelUnsprungMass());//后轮簧下质量
        vehiclePO.setRearWheelTrackWidth(vehicleVO.getRearWheelTrackWidth());//后轮轮距
        vehiclePO.setWindwardArea(vehicleVO.getWindwardArea());//迎风面积
        vehiclePO.setAirDensity(vehicleVO.getAirDensity());//空气密度
        vehiclePO.setEngineCrankshaftMomentInertia(vehicleVO.getEngineCrankshaftMomentInertia());//发动机曲轴转动惯量
        vehiclePO.setIdleSpeed(vehicleVO.getIdleSpeed());//怠速转速
        vehiclePO.setRelationCurveEngineTorqueSpeedAndThrottleOpening(vehicleVO.getRelationCurveEngineTorqueSpeedAndThrottleOpening());//发动机扭矩、转速和节气门开度关系曲线
        vehiclePO.setRelationCurveEngineTorqueSpeedAndThrottleOpeningJson(vehicleVO.getRelationCurveEngineTorqueSpeedAndThrottleOpeningJson());//发动机扭矩、转速和节气门开度关系曲线
        vehiclePO.setMomentInertiaInputShaft(vehicleVO.getMomentInertiaInputShaft());//输入轴的转动惯量
        vehiclePO.setMomentInertiaOutputShaft(vehicleVO.getMomentInertiaOutputShaft());//输出轴的转动惯量
        vehiclePO.setFinalDriveRatio(vehicleVO.getFinalDriveRatio());//主减速器传动比
        vehiclePO.setTransmissionEfficiency(vehicleVO.getTransmissionEfficiency());//传动效率
        vehiclePO.setGearRatio(vehicleVO.getGearRatio());//R,N,1,2,3,4,5,6
        vehiclePO.setGearRatioJson(vehicleVO.getGearRatioJson());//R,N,1,2,3,4,5,6
        vehiclePO.setInertia(vehicleVO.getInertia());//R,N,1,2,3,4,5,6
        vehiclePO.setInertiaJson(vehicleVO.getInertiaJson());//R,N,1,2,3,4,5,6
        vehiclePO.setDriving(vehicleVO.getDriving());//R,N,1,2,3,4,5,6
        vehiclePO.setDrivingJson(vehicleVO.getDrivingJson());//R,N,1,2,3,4,5,6
        vehiclePO.setCoasting(vehicleVO.getCoasting());//R,N,1,2,3,4,5,6
        vehiclePO.setCoastingJson(vehicleVO.getCoastingJson());//R,N,1,2,3,4,5,6
        vehiclePO.setShiftTime(vehicleVO.getShiftTime());//换挡时间
        vehiclePO.setShiftTimeUp12(vehicleVO.getShiftTimeUp12());//
        vehiclePO.setShiftTimeUp12Json(vehicleVO.getShiftTimeUp12Json());//
        vehiclePO.setShiftTimeDown12(vehicleVO.getShiftTimeDown12());//
        vehiclePO.setShiftTimeDown12Json(vehicleVO.getShiftTimeDown12Json());//
        vehiclePO.setShiftTimeUp23(vehicleVO.getShiftTimeUp23());//
        vehiclePO.setShiftTimeUp23Json(vehicleVO.getShiftTimeUp23Json());//
        vehiclePO.setShiftTimeDown23(vehicleVO.getShiftTimeDown23());//
        vehiclePO.setShiftTimeDown23Json(vehicleVO.getShiftTimeDown23Json());//
        vehiclePO.setShiftTimeUp34(vehicleVO.getShiftTimeUp34());//
        vehiclePO.setShiftTimeUp34Json(vehicleVO.getShiftTimeUp34Json());//
        vehiclePO.setShiftTimeDown34(vehicleVO.getShiftTimeDown34());//
        vehiclePO.setShiftTimeDown34Json(vehicleVO.getShiftTimeDown34Json());//
        vehiclePO.setShiftTimeUp45(vehicleVO.getShiftTimeUp45());//
        vehiclePO.setShiftTimeUp45Json(vehicleVO.getShiftTimeUp45Json());//
        vehiclePO.setShiftTimeDown45(vehicleVO.getShiftTimeDown45());//
        vehiclePO.setShiftTimeDown45Json(vehicleVO.getShiftTimeDown45Json());//
        vehiclePO.setShiftTimeUp56(vehicleVO.getShiftTimeUp56());//
        vehiclePO.setShiftTimeUp56Json(vehicleVO.getShiftTimeUp56Json());//
        vehiclePO.setShiftTimeDown56(vehicleVO.getShiftTimeDown56());//
        vehiclePO.setShiftTimeDown56Json(vehicleVO.getShiftTimeDown56Json());//
        vehiclePO.setBrakeType(vehicleVO.getBrakeType());//制动类型 1 前轮制动 2 后轮制动 3 四轮制动
        vehiclePO.setFrontLeftWheelBrakingTorque(vehicleVO.getFrontLeftWheelBrakingTorque());//左前轮制动力矩
        vehiclePO.setRearLeftWheelBrakingTorque(vehicleVO.getRearLeftWheelBrakingTorque());//左后轮制动力矩
        vehiclePO.setFrontRightWheelBrakingTorque(vehicleVO.getFrontRightWheelBrakingTorque());//右前轮制动力矩
        vehiclePO.setRearRightWheelBrakingTorque(vehicleVO.getFrontRightWheelBrakingTorque());//右后轮制动力矩
        vehiclePO.setSteeringType(vehicleVO.getSteeringType());//转向类型 1 前轮转向 2 后轮转向 3 四轮转向
        vehiclePO.setSteeringGearType(vehicleVO.getSteeringGearType());//转向器类型 1 齿轮齿条转向器 2 循环球式转向器
        vehiclePO.setSteeringGearRatio(vehicleVO.getSteeringGearRatio());//转向器传动比
        vehiclePO.setRelationCurveRightWheelAngleAndSteeringGearOutput(vehicleVO.getRelationCurveRightWheelAngleAndSteeringGearOutput());//右轮转角与转向器输出的关系
        vehiclePO.setRelationCurveRightWheelAngleAndSteeringGearOutputJson(vehicleVO.getRelationCurveRightWheelAngleAndSteeringGearOutputJson());//右轮转角与转向器输出的关系
        vehiclePO.setRelationCurveLeftWheelAngleAndSteeringGearOutput(vehicleVO.getRelationCurveLeftWheelAngleAndSteeringGearOutput());//左轮转角与转向器输出的关系
        vehiclePO.setRelationCurveLeftWheelAngleAndSteeringGearOutputJson(vehicleVO.getRelationCurveLeftWheelAngleAndSteeringGearOutputJson());//左轮转角与转向器输出的关系
        vehiclePO.setDriverType(vehicleVO.getDriverType());  // 驱动类型
        return vehiclePO;
    }

    /**
     * list转tree递归调用
     */
    private static <T, String> T findChildren(T parent, List<T> list, Function<T, String> getId, Function<T, String> getPid, Function<T, List<T>> getChildren, BiConsumer<T, List<T>> setChildren) {
        for (T node : list)
            if (getId.apply(parent).equals(getPid.apply(node))) {
                if (getChildren.apply(parent) == null || getChildren.apply(parent).size() == 0) {
                    setChildren.accept(parent, new ArrayList<T>());
                }
                getChildren.apply(parent).add(findChildren(node, list, getId, getPid, getChildren, setChildren));
            }
        return parent;
    }

    /**
     * list转tree结构，<T>类型需要在属性上添加注解，标识树形结构：@WId 、@WParentId、 @WChildren;
     */
    public static <T> List<T> listToTree(List<T> list) {
        if (isNull(list)) {
            return new LinkedList<>();
        }
        return listToTree(list, "0");
    }

    public static <T> List<T> listToTree(List<T> list, String root) {
        try {
            List<T> treeList = new LinkedList<>();
            Class<?> tClass = null;
            for (T t : list) {
                tClass = t.getClass();
                break;
            }
            //反射解析Getter和Setter
            Field[] fields = tClass.getDeclaredFields();
            Method getId = null;
            Method getParentId = null;
            Method getChildren = null;
            Method setChildren = null;
            for (Field field : fields) {
                String fieldName = field.getName();
                String firstChar = fieldName.substring(0, 1);
                if (field.isAnnotationPresent(WId.class)) {
                    getId = tClass.getMethod("get" + fieldName.replaceFirst(firstChar, firstChar.toUpperCase()));
                }
                ;
                if (field.isAnnotationPresent(WParentId.class)) {
                    getParentId = tClass.getMethod("get" + fieldName.replaceFirst(firstChar, firstChar.toUpperCase()));
                }
                ;
                if (field.isAnnotationPresent(WChildren.class)) {
                    getChildren = tClass.getMethod("get" + fieldName.replaceFirst(firstChar, firstChar.toUpperCase()));
                    setChildren = tClass.getMethod("set" + fieldName.replaceFirst(firstChar, firstChar.toUpperCase()), List.class);
                }
                ;
            }
            //解析列表
            for (T node : list) {
                if (root.equals(getParentId.invoke(node))) {
                    treeList.add(findChildren(node, list, getId, getParentId, getChildren, setChildren));
                }
            }
            return treeList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * list转tree递归调用
     */
    @SuppressWarnings("all")
    private static <T> T findChildren(T parent, List<T> list, Method getId, Method getParentId, Method getChildren, Method setChildren) throws InvocationTargetException, IllegalAccessException {
        for (T node : list) {
            String id = (String) getId.invoke(parent);
            String parentId = (String) getParentId.invoke(node);
            if (id.equals(parentId)) {
                List<T> children = (List<T>) getChildren.invoke(parent);
                if (ObjectUtil.isNull(children)) {
                    children = new ArrayList<T>();
                    setChildren.invoke(parent, children);
                }
                children.add(findChildren(node, list, getId, getParentId, getChildren, setChildren));
            }
        }
        return parent;
    }
}
