2021-06-18 object转map


public enum FeedBackEnum {
    NAME("NAME", "分类标签"),
    PHONE_NO("PHONE_NO", "电话号码"),
    CUSTOMER_COMPANY("CUSTOMER_COMPANY", "客户单位"),
    SR_NO("SR_NO", "问题单号"),
    REMARK("REMARK", "备注"),
    USER_NAME("USER_NAME", "客户姓名"),
    CREATE_TIME("CREATE_TIME", "反馈时间");
    private String code;
    private String desc;

    FeedBackEnum(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }


    public String getCode() {
        return code;
    }


    public String getDesc() {
        return desc;
    }


    public static FeedBackEnum byCode(String code) throws RuntimeException {
        FeedBackEnum[] values = values();
        for (FeedBackEnum value : values) {
            if (code.equals(value.getCode())) {
                return value;
            }
        }
        throw new RuntimeException("未找到对应操作日志类型");
    }


    public static String toDesc(String code) throws RuntimeException {
        try {
            FeedBackEnum en = byCode(code);
            return en.getDesc();
        } catch (RuntimeException e) {
            FeedBackEnum wde = FeedBackEnum.byName(code);
            if (Objects.nonNull(wde)) {
                return wde.getDesc();
            }
            throw e;
        }
    }
    public static FeedBackEnum byName(String name) {
        return Enum.valueOf(FeedBackEnum.class, name);
    }
}


public class MapUtil {
    

    public MapUtil() {
    }

    public static Map<String, Object> objectToMap(Object obj) {
        if (obj == null) {
            return null;
        } else {
            Map<String, Object> map = new HashMap();
            Method[] methods = obj.getClass().getMethods();
            Method[] var3 = methods;
            int var4 = methods.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Method method = var3[var5];

                try {
                    if (method.getName().startsWith("get")) {
                        String field = toFirstLower(method.getName().replace("get", ""));
                        Object objValue = method.invoke(obj, (Object[])null);
                        map.put(field, objValue);
                    }
                } catch (Exception var12) {
                    log.error(var12);
                }
            }

            for(Class superClass = obj.getClass().getSuperclass(); !superClass.getName().equals("java.lang.Object"); superClass = superClass.getSuperclass()) {
                Method[] superMethods = superClass.getMethods();
                Method[] var15 = superMethods;
                int var16 = superMethods.length;

                for(int var17 = 0; var17 < var16; ++var17) {
                    Method method = var15[var17];

                    try {
                        if (method.getName().startsWith("get")) {
                            String field = toFirstLower(method.getName().replace("get", ""));
                            Object objValue = method.invoke(obj, (Object[])null);
                            map.put(field, objValue);
                        }
                    } catch (Exception var11) {
                        log.error(var11);
                    }
                }
            }

            return map;
        }
    }

    private static String toFirstLower(String str) {
        if (str != null && !"".equals(str)) {
            char[] chars = new char[]{str.charAt(0)};
            String temp = new String(chars);
            return str.replaceFirst(temp, temp.toLowerCase());
        } else {
            return "";
        }
    }
}
  public static void excleCommonUtil(List<Object> objectList, String title, String... exclusions) throws Exception {
        if (!org.springframework.util.CollectionUtils.isEmpty(objectList)) {
            ArrayList<Map> listMap = (ArrayList<Map>) objectList.get(0);
            Map<String, Object> objectMap = com.huawei.it.jalor5.core.util.MapUtil.objectToMap(listMap.get(0));
            List<String> rowNameList = new ArrayList<>();

            if (!Objects.isNull(exclusions)) {
                Iterator<Map.Entry<String, Object>> it = objectMap.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, Object> entry = it.next();
                    rowNameList.add(entry.getKey());
                    for (String exclusion : exclusions) {
                        if (exclusion.equals(entry.getKey())) {
                            it.remove();
                            rowNameList.remove(entry.getKey());
                        }
                    }
                }
            }

            String[] rowName = rowNameList.toArray(new String[0]);
            List<Object[]> dataList = new ArrayList<>();
            for (Object list : listMap) {
                Object[] rowList = new String[rowNameList.size()];
                Map<String, Object> objMap = MapUtil.objectToMap(list);
                for (int i = 0; i < rowNameList.size(); i++) {
                    Object objectValue = objMap.get(rowNameList.get(i));
                    if (objectValue instanceof Date) {
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        objectValue = formatter.format(objectValue);
                    }
                    rowList[i] = objectValue;
                }
                dataList.add(rowList);
            }

            Message message = PhaseInterceptorChain.getCurrentMessage();
            HttpServletResponse response = null;
            if (message.get(AbstractHTTPDestination.HTTP_RESPONSE) instanceof HttpServletResponse) {
                response = (HttpServletResponse) message.get(AbstractHTTPDestination.HTTP_RESPONSE);
            }
            if (response != null) {
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                response.setCharacterEncoding("utf-8");
            }
            ExportExcelUtil excelUtil = new ExportExcelUtil(title, rowName, dataList);
            excelUtil.export(null, response);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值