package cn.com.ty.lift.common.utils; import cn.com.ty.lift.common.model.StatisticsKVModel; import cn.hutool.core.util.StrUtil; import java.lang.reflect.Method; import java.util.*; /** * @author huangyuan * @date 2019-12-02 * @description 项目工具类 */ public class ProjectUtils { /** * @param * @return * @description 通过id字符串,获取long型id列表 * @date 2019/11/27 10:03 AM */ public static List getLongIdFromIds(String ids) { List longIdList = new ArrayList<>(); if (StrUtil.isNotBlank(ids)) { String[] idArray = ids.split(","); for (String id : idArray) { longIdList.add(Long.parseLong(id)); } } return longIdList; } /** * 将对象集合转化为attr->List的map形式,通过特定属性给对象归类 * * @param objList * @param attrName:属性名 * @param parameterTypes * @return Map * @throws * @author huangy * @date 2018年12月19日 */ public static Map attrToListMap(List objList, String attrName, Class... parameterTypes) { Map map = new HashMap<>(); try { if (objList != null && objList.size() > 0) { for (Object o : objList) { Class classObj = o.getClass(); //获取属性get方法 String methodName = PojoUtils.attrGetMethodName(attrName); Method method = classObj.getMethod(methodName, parameterTypes); Object attr = method.invoke(o, parameterTypes); //在map中获取对象集合 List objInfoList = (List) map.get(attr); if (objInfoList == null) { objInfoList = new ArrayList(); } objInfoList.add(o); map.put(attr, objInfoList); } } } catch (Exception e) { e.printStackTrace(); } return map; } /** * 将对象集合转化为attribute->Object的map * * @param objList * @param attrName:属性名称 * @param parameterTypes * @return Map * @throws * @author huangy * @date 2018年12月19日 */ public static Map attrToObjMap(List objList, String attrName, Class... parameterTypes) { Map map = new HashMap<>(); try { if (objList != null && objList.size() > 0) { for (Object o : objList) { Class classObj = o.getClass(); Method method = classObj.getMethod(PojoUtils.attrGetMethodName(attrName), parameterTypes); Object attr = method.invoke(o, parameterTypes); map.put(attr, o); } } } catch (Exception e) { e.printStackTrace(); } return map; } /** * 通过集合对象集合获取对象id集合 * * @param objList * @param attrName * @param parameterTypes * @return List * @throws * @author huangy */ public static List getAttrList(List objList, String attrName, Class... parameterTypes) { List idList = new ArrayList(); if (objList != null && objList.size() > 0) { for (Object o : objList) { try { Class classObj = o.getClass(); Method method = classObj.getMethod(PojoUtils.attrGetMethodName(attrName), parameterTypes); Object id = method.invoke(o, parameterTypes); idList.add(id); } catch (Exception e) { e.printStackTrace(); } } } return idList; } /** * 列表去重 * * @param list void * @throws */ public static void removeDuplicateWithOrder(List list) { Set set = new HashSet<>(); List newList = new ArrayList<>(); if (list != null && list.size() > 0) { for (Iterator iter = list.iterator(); iter.hasNext(); ) { Object element = iter.next(); if (set.add(element)) { newList.add(element); } } list.clear(); list.addAll(newList); } } /** * @param * @return * @description 将map值转化为 key -> key值 value -> value值 * @date 2020/2/24 7:37 下午 */ public static List transReturnMapToStatisticsKVModel(Map dataMap) { List statisticsKVModelList = new ArrayList<>(); if (dataMap != null && dataMap.size() > 0) { dataMap.forEach((key, value) -> { StatisticsKVModel outStatisticsKVModel = new StatisticsKVModel(); //设置键 outStatisticsKVModel.setKey(key); //判断值是否为也为map如果是就行封装 目前只涉及一层封装 if (value != null && value instanceof Map) { outStatisticsKVModel.setValue(transReturnMapToStatisticsKVModel((Map)value)); } else { outStatisticsKVModel.setValue(value); } statisticsKVModelList.add(outStatisticsKVModel); }); } return statisticsKVModelList; } }