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

org.zodiac.mybatisplus.binding.helper.ResultAssembler Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.mybatisplus.binding.helper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.zodiac.commons.support.SpringContextHolder;
import org.zodiac.commons.util.BeanUtil;
import org.zodiac.commons.util.Colls;
import org.zodiac.commons.util.lang.Strings;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

import java.util.*;

/**
 * 绑定关联数据组装器。
 * 
 */
public class ResultAssembler {

    private static Logger log = LoggerFactory.getLogger(ResultAssembler.class);


    /***
     * 从对象集合提取某个属性值到list中。
     * 
     * @param setterFieldName set字段名
     * @param fromList 源列表
     * @param getterFields get字段列表
     * @param valueMatchMap 配置值的map
     * @param splitBy 分隔符
     * @param  类型
     */
    public static  void bindPropValue(String setterFieldName, List fromList, String[] getterFields,
        Map valueMatchMap, String splitBy) {
        if (Colls.emptyColl(fromList) || Colls.emptyMap(valueMatchMap)) {
            return;
        }
        try {
            for (E object : fromList) {
                List matchKeys = null;
                BeanWrapper beanWrapper = SpringContextHolder.getBeanWrapper(object);
                for (int i = 0; i < getterFields.length; i++) {
                    Object fieldValueObj = BeanUtil.getProperty(object, getterFields[i]);
                    if (fieldValueObj == null) {
                        continue;
                    }
                    if (fieldValueObj instanceof Collection) {
                        if (matchKeys == null) {
                            matchKeys = new ArrayList();
                        }
                        matchKeys.addAll((Collection)fieldValueObj);
                    } else {
                        if (matchKeys == null) {
                            matchKeys = new ArrayList(getterFields.length);
                        }
                        matchKeys.add(StrUtil.cleanNonConsts(StrUtil.stringValueOf(fieldValueObj)));
                    }
                }
                if (matchKeys == null) {
                    continue;
                }
                /*查找匹配Key。*/
                String matchKey = Strings.join(matchKeys);
                if (valueMatchMap.containsKey(matchKey)) {
                    /*赋值。*/
                    beanWrapper.setPropertyValue(setterFieldName, valueMatchMap.get(matchKey));
                } else {
                    if (matchKeys.size() == 1 && StrUtil.isNotBlank(splitBy) && getterFields.length == 1
                        && matchKey.contains(splitBy)) {
                        matchKeys = StrUtil.splitToList(matchKey, splitBy);
                    }
                    List matchedValues = new ArrayList(matchKeys.size());
                    for (Object key : matchKeys) {
                        Object value = valueMatchMap.get(StrUtil.stringValueOf(key));
                        if (value != null) {
                            if (value instanceof Collection) {
                                Collection valueList = (Collection)value;
                                for (Object obj : valueList) {
                                    if (!matchedValues.contains(obj)) {
                                        matchedValues.add(obj);
                                    }
                                }
                            } else {
                                if (!matchedValues.contains(value)) {
                                    matchedValues.add(value);
                                }
                            }
                        }
                    }
                    /*赋值。*/
                    beanWrapper.setPropertyValue(setterFieldName, matchedValues);
                }
            }
        } catch (Exception e) {
            log.warn("设置属性值异常, setterFieldName=" + setterFieldName, e);
        }
    }

    /***
     * 从对象集合提取某个属性值到list中。
     * 
     * @param setterFieldName set字段名
     * @param fromList 源列表
     * @param trunkObjColMapping 对象列map
     * @param valueMatchMap 配置值的map
     * @param col2FieldMapping 列到字段的映射map
     * @param  类型
     */
    public static  void bindEntityPropValue(String setterFieldName, List fromList,
        Map trunkObjColMapping, Map valueMatchMap, Map col2FieldMapping) {
        if (Colls.emptyColl(fromList) || Colls.emptyMap(valueMatchMap)) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        try {
            for (E object : fromList) {
                boolean appendComma = false;
                sb.setLength(0);
                for (Map.Entry entry : trunkObjColMapping.entrySet()) {
                    /*转换为字段名。*/
                    String getterField = col2FieldMapping.get(entry.getKey());
                    if (getterField == null) {
                        getterField = StrUtil.toLowerCaseCamel(entry.getKey());
                    }
                    String fieldValue = BeanUtil.getStringProperty(object, getterField);
                    if (appendComma) {
                        sb.append(Strings.COMMA);
                    }
                    sb.append(fieldValue);
                    if (appendComma == false) {
                        appendComma = true;
                    }
                }
                /*查找匹配Key。*/
                String matchKey = sb.toString();
                if (valueMatchMap.containsKey(matchKey)) {
                    /*赋值。*/
                    BeanUtil.setProperty(object, setterFieldName, valueMatchMap.get(matchKey));
                }
            }
            sb.setLength(0);
        } catch (Exception e) {
            log.warn("设置属性值异常, setterFieldName=" + setterFieldName, e);
        }
    }

    public static  void bindFieldListPropValue(List fromList, String[] getterFields,
        Map valueMatchMap, List annoObjSetterPropNameList, List refGetterFieldNameList,
        String splitBy) {
        if (Colls.emptyColl(fromList) || Colls.emptyMap(valueMatchMap)) {
            return;
        }
        try {
            for (E object : fromList) {
                List matchKeys = null;
                for (int i = 0; i < getterFields.length; i++) {
                    Object fieldValueObj = BeanUtil.getProperty(object, getterFields[i]);
                    if (fieldValueObj == null) {
                        continue;
                    }
                    if (fieldValueObj instanceof Collection) {
                        if (matchKeys == null) {
                            matchKeys = new ArrayList();
                        }
                        matchKeys.addAll((Collection)fieldValueObj);
                    } else {
                        if (matchKeys == null) {
                            matchKeys = new ArrayList(getterFields.length);
                        }
                        matchKeys.add(StrUtil.cleanNonConsts(StrUtil.stringValueOf(fieldValueObj)));
                    }
                }
                /*无有效值,跳过。*/
                if (matchKeys == null) {
                    continue;
                }
                /*查找匹配Key。*/
                String matchKey = Strings.join(matchKeys);
                List entityList = valueMatchMap.get(matchKey);
                if (entityList == null) {
                    if (matchKeys.size() == 1 && StrUtil.isNotBlank(splitBy) && matchKey.contains(splitBy)) {
                        matchKeys = StrUtil.splitToList(matchKey, splitBy);
                    }
                    List matchedValues = new ArrayList(matchKeys.size());
                    for (Object key : matchKeys) {
                        Object value = valueMatchMap.get(StrUtil.stringValueOf(key));
                        if (value != null) {
                            if (value instanceof Collection) {
                                Collection valueList = (Collection)value;
                                for (Object obj : valueList) {
                                    if (!matchedValues.contains(obj)) {
                                        matchedValues.add(obj);
                                    }
                                }
                            } else {
                                if (!matchedValues.contains(value)) {
                                    matchedValues.add(value);
                                }
                            }
                        }
                    }
                    if (matchedValues != null) {
                        entityList = matchedValues;
                    }
                }
                /*赋值。*/
                BeanWrapper beanWrapper = SpringContextHolder.getBeanWrapper(object);
                for (int i = 0; i < annoObjSetterPropNameList.size(); i++) {
                    List valObjList = BeanUtil.collectToList(entityList, refGetterFieldNameList.get(i));
                    beanWrapper.setPropertyValue(annoObjSetterPropNameList.get(i), valObjList);
                }
            }
        } catch (Exception e) {
            log.warn("设置属性值异常", e);
        }
    }

    /**
     * 合并为1-1的map结果。
     * 
     * @param resultSetMapList 结果集map列表
     * @param trunkObjColMapping 主干对象列映射map
     * @param branchObjColMapping 分支对象列映射map
     * @param  类型
     * @return 结果
     */
    public static  Map convertToOneToOneResult(List> resultSetMapList,
        Map trunkObjColMapping, Map branchObjColMapping) {
        if (Colls.emptyColl(resultSetMapList)) {
            return Collections.emptyMap();
        }
        /*获取valueName*/
        String valueName = branchObjColMapping.entrySet().iterator().next().getKey();
        /*合并list为map*/
        Map resultMap = new HashMap<>(resultSetMapList.size());
        StringBuilder sb = new StringBuilder();
        for (Map row : resultSetMapList) {
            boolean appendComma = false;
            sb.setLength(0);
            for (Map.Entry entry : trunkObjColMapping.entrySet()) {
                Object keyObj = getValueIgnoreKeyCase(row, entry.getValue());
                if (appendComma) {
                    sb.append(Strings.COMMA);
                }
                sb.append(StrUtil.stringValueOf(keyObj));
                if (appendComma == false) {
                    appendComma = true;
                }
            }
            String matchKeys = sb.toString();
            Object valueObj = row.containsKey(valueName) ? row.get(valueName) : row.get(valueName.toUpperCase());
            resultMap.put(matchKeys, valueObj);
        }
        return resultMap;
    }

    /**
     * 合并为1-n的map结果。
     * 
     * @param resultSetMapList 结果集map列表
     * @param trunkObjColMapping 主干对象列映射map
     * @param branchObjColMapping 分支对象列映射map
     * @param  类型
     * @return 结果
     */
    public static  Map convertToOneToManyResult(List> resultSetMapList,
        Map trunkObjColMapping, Map branchObjColMapping) {
        if (Colls.emptyColl(resultSetMapList)) {
            return Collections.emptyMap();
        }
        /*获取valueName。*/
        String valueName = branchObjColMapping.entrySet().iterator().next().getKey();
        valueName = StrUtil.removeEscape(valueName);
        /*合并list为map。*/
        Map resultMap = new HashMap<>();
        StringBuilder sb = new StringBuilder();
        for (Map row : resultSetMapList) {
            boolean appendComma = false;
            sb.setLength(0);
            for (Map.Entry entry : trunkObjColMapping.entrySet()) {
                Object keyObj = getValueIgnoreKeyCase(row, entry.getValue());
                if (appendComma) {
                    sb.append(Strings.COMMA);
                }
                sb.append(StrUtil.stringValueOf(keyObj));
                if (appendComma == false) {
                    appendComma = true;
                }
            }
            String matchKeys = sb.toString();
            Object valueObj = row.containsKey(valueName) ? row.get(valueName) : row.get(valueName.toUpperCase());
            if (valueObj != null) {
                List valueList = resultMap.get(matchKeys);
                if (valueList == null) {
                    valueList = new ArrayList();
                    resultMap.put(matchKeys, valueList);
                }
                valueList.add(valueObj);
            }
        }
        sb.setLength(0);
        return resultMap;
    }

    /**
     * 从map中取值,如直接取为null尝试转换大写后再取。
* 如:以支持ORACLE等大写命名数据库。 * * @param map 源数据map * @param key 取值的key * @return 结果 */ public static Object getValueIgnoreKeyCase(Map map, String key) { if (map == null || key == null) { return null; } key = StrUtil.removeEscape(key); if (map.containsKey(key)) { return map.get(key); } if (map.containsKey(key.toUpperCase())) { return map.get(key.toUpperCase()); } return null; } /** * 拆解值列表。 * * @param valueList 源列表 * @param splitBy 分隔符 * @param fieldType 字段类型 * @return 结果 */ public static List unpackValueList(List valueList, String splitBy, Class fieldType) { if (Colls.emptyColl(valueList)) { return valueList; } List newValueList = new ArrayList(); for (Object value : valueList) { if (value == null) { continue; } if (value instanceof Collection) { for (Object key : (Collection)value) { if (!newValueList.contains(key)) { newValueList.add(key); } } } else { if (StrUtil.isNotBlank(splitBy)) { boolean isSameType = fieldType == null || fieldType.equals(String.class); String valueStr = StrUtil.cleanNonConsts(StrUtil.stringValueOf(value)); if (valueStr.contains(splitBy)) { for (String oneVal : valueStr.split(splitBy)) { Object oneValueObj = isSameType ? oneVal : BeanUtil.convertValueToFieldType(oneVal, fieldType); if (!newValueList.contains(oneValueObj)) { newValueList.add(oneValueObj); } } } else { Object oneValueObj = isSameType ? value : BeanUtil.convertValueToFieldType(value, fieldType); if (!newValueList.contains(oneValueObj)) { newValueList.add(oneValueObj); } } } else if (!newValueList.contains(value)) { newValueList.add(value); } } } return newValueList; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy