對象數據自動替換方法

/**
     * 迭代處理對象屬性值替換
     * @param vo
     * @param attriMap
     * @param unchangeNameSet
     */
    private void changeBaseInfo(Object vo,Map attriMap,Set<String> unchangeNameSet) {
        try {
            if(vo == null || attriMap == null || attriMap.size() == 0) return;
            //map 數據中內容替換
            if (vo instanceof Map) {
                Map map = (Map) vo;
                if (map != null && map.size() > 0) {
                    Set set = map.keySet();
                    Iterator iterator = set.iterator();
                    //獲取map中所有value值
                    while (iterator.hasNext()) {
                        Object key = iterator.next();
                        if(key == null) continue;
                        Object value = map.get(key);
                        //map中String類型內容可直接替換
                        if (value != null && value instanceof String) {
                            ZhuanquDataMultilingualDTO dtoObjB = (ZhuanquDataMultilingualDTO) attriMap.get(value.toString().toLowerCase());
                            if (dtoObjB != null) {
                                String otherB = dtoObjB.getTextValue();
                                if (StringUtils.isNotBlank(otherB)) {
                                    //不需要替換內容校驗
                                    if(unchangeNameSet.contains(key.toString())) continue;
                                    map.put(key, otherB);
                                }
                            }
                        }
                        //其他類型需要迭代類型處理
                        else {
                            //迭代分析
                            changeBaseInfo(value, attriMap,unchangeNameSet);
                        }
                    }
                }
            }
            //列表中數據替換
            else if (vo instanceof List) {
                List list = (List) vo;
                if (!CollectionUtils.isEmpty(list)) {
                    for (Object value : list) {
                        //迭代分析(字符類型暫不做處理,只處理對象類型)
                        changeBaseInfo(value, attriMap,unchangeNameSet);
                    }
                }
            }
            //對象數據替換
            else {
                Field[] declaredFields = vo.getClass().getDeclaredFields();
                //只處理有屬性值得對象數據
                if (declaredFields != null && declaredFields.length > 0) {
                    for (Field field : declaredFields) {
                        //過濾掉配置中不需要修改的值
                        if(field == null && unchangeNameSet.contains(field.getName())){
                            continue;
                        }
                        //對象中字符類型數據使用get\set方法替換
                        if (field.getType() == String.class) {
                            String methodName = com.dhgate.mobile.util.StringUtils.toUpperCaseFirstOne(field.getName());
                            Method getMethod = vo.getClass().getMethod("get" + methodName);
                            if (getMethod != null) {
                                Object value = getMethod.invoke(vo);
                                if (value != null && attriMap != null) {
                                    ZhuanquDataMultilingualDTO dtoObjB = (ZhuanquDataMultilingualDTO) attriMap.get(value.toString().toLowerCase());
                                    if (dtoObjB != null) {
                                        String otherB = dtoObjB.getTextValue();
                                        if (StringUtils.isNotBlank(otherB)) {
                                            Method setMethod = vo.getClass().getMethod("set" + methodName, String.class);
                                            setMethod.invoke(vo, otherB);
                                        }
                                    }
                                }
                            }
                        }
                        //對象中map和list對象繼續迭代處理
                        else if(field.getType() == Map.class || field.getType() == List.class){
                            String methodName = com.dhgate.mobile.util.StringUtils.toUpperCaseFirstOne(field.getName());
                            Method getMethod = vo.getClass().getMethod("get" + methodName);
                            Object value = getMethod.invoke(vo);
                            if (value != null) {
                                //迭代分析
                                changeBaseInfo(value, attriMap,unchangeNameSet);
                            }
                        }

                    }
                }
            }
        } catch (Exception e) {
            logger.error("changeBaseInfo error ",e);
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章