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

org.zodiac.mybatisplus.binding.binder.EntityListBinder Maven / Gradle / Ivy

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

import org.zodiac.commons.util.BeanUtil;
import org.zodiac.commons.util.Colls;
import org.zodiac.commons.util.lang.Strings;
import org.zodiac.core.exception.IllegalUsageException;
import org.zodiac.mybatisplus.binding.annotation.BindEntityList;
import org.zodiac.mybatisplus.binding.binder.remote.RemoteBindingManager;
import org.zodiac.mybatisplus.binding.helper.ResultAssembler;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

import java.util.*;

/**
 * Entity集合绑定实现。
 * 
 */
public class EntityListBinder extends EntityBinder {

    public EntityListBinder(BindEntityList annotation, List voList) {
        super(annotation.entity(), voList);
        if (StrUtil.isNotBlank(annotation.splitBy())) {
            this.splitBy = annotation.splitBy();
        }
        if (StrUtil.isNotBlank(annotation.orderBy())) {
            this.orderBy = annotation.orderBy();
        }
    }

    public EntityListBinder(Class entityClass, List voList) {
        super(entityClass, voList);
    }

    @Override
    public void bind() {
        if (Colls.emptyColl(annoObjectList)) {
            return;
        }
        if (Colls.emptyColl(refObjJoinCols)) {
            throw new IllegalUsageException("调用错误:无法从condition中解析出字段关联.");
        }
        Map valueEntityListMap = new HashMap<>();
        if (middleTable == null) {
            this.simplifySelectColumns();
            super.buildQueryWrapperJoinOn();
            /*查询条件为空时不进行查询。*/
            if (queryWrapper.isEmptyOfNormal()) {
                return;
            }
            /*处理orderBy,附加排序。*/
            this.appendOrderBy(remoteBindDTO);
            List entityList = null;
            /*查询entity列表: List。*/
            if (StrUtil.isBlank(this.module)) {
                /*本地查询获取匹配结果的entityList。*/
                entityList = getEntityList(queryWrapper);
            } else {
                /*远程调用获取。*/
                entityList = RemoteBindingManager.fetchEntityList(module, remoteBindDTO, referencedEntityClass);
            }
            if (Colls.notEmptyColl(entityList)) {
                valueEntityListMap = this.buildMatchKey2EntityListMap(entityList);
            }
            ResultAssembler.bindPropValue(annoObjectField, super.getMatchedAnnoObjectList(), getAnnoObjJoinFlds(),
                valueEntityListMap, this.splitBy);
        } else {
            if (refObjJoinCols.size() > 1) {
                throw new IllegalUsageException(NOT_SUPPORT_MSG);
            }

            /*提取注解条件中指定的对应的列表。*/
            Map trunkObjCol2ValuesMap = super.buildTrunkObjCol2ValuesMap();
            Map middleTableResultMap = middleTable.executeOneToManyQuery(trunkObjCol2ValuesMap);
            if (Colls.emptyMap(middleTableResultMap)) {
                return;
            }
            this.simplifySelectColumns();
            /*收集查询结果values集合。*/
            List entityIdList = extractIdValueFromMap(middleTableResultMap);
            if (StrUtil.isNotBlank(this.splitBy)) {
                Class fieldType = refObjPropInfo.getFieldTypeByColumn(refObjJoinCols.get(0));
                entityIdList = ResultAssembler.unpackValueList(entityIdList, this.splitBy, fieldType);
            }
            /*处理orderBy,附加排序。*/
            this.appendOrderBy(remoteBindDTO);
            /*构建查询条件。*/
            String refObjJoinOnCol = refObjJoinCols.get(0);
            List entityList = null;
            /*查询entity列表: List。*/
            if (StrUtil.isBlank(this.module)) {
                /*本地查询获取匹配结果的entityList。*/
                queryWrapper.in(refObjJoinOnCol, entityIdList);
                entityList = getEntityList(queryWrapper);
            } else {
                /*远程调用获取。*/
                remoteBindDTO.setRefJoinCol(refObjJoinOnCol).setInConditionValues(entityIdList);
                entityList = RemoteBindingManager.fetchEntityList(module, remoteBindDTO, referencedEntityClass);
            }
            if (Colls.emptyColl(entityList)) {
                return;
            }
            String refObjJoinOnField = toRefObjField(refObjJoinOnCol);
            /*转换entity列表为Map。*/
            Map> entityMap = BeanUtil.convertToStringKeyObjectListMap(entityList, refObjJoinOnField);
            for (Map.Entry entry : middleTableResultMap.entrySet()) {
                /*List*/
                List annoObjFKList = entry.getValue();
                if (Colls.emptyColl(annoObjFKList)) {
                    continue;
                }
                List valueList = new ArrayList();
                for (Object obj : annoObjFKList) {
                    if (obj == null) {
                        continue;
                    }
                    /*兼容JsonArray。*/
                    if (obj instanceof Collection) {
                        for (Object key : (Collection)obj) {
                            List ent = entityMap.get(StrUtil.stringValueOf(key));
                            if (ent != null) {
                                for (T item : ent) {
                                    valueList.add(cloneOrConvertBean(item));
                                }
                            }
                        }
                    } else {
                        String valStr = StrUtil.cleanNonConsts(String.valueOf(obj));
                        List ent = entityMap.get(valStr);
                        if (ent != null) {
                            for (T item : ent) {
                                valueList.add(cloneOrConvertBean(item));
                            }
                        } else if (StrUtil.isNotBlank(splitBy) && valStr.contains(splitBy)) {
                            for (String key : valStr.split(splitBy)) {
                                ent = entityMap.get(key);
                                if (ent != null) {
                                    for (T item : ent) {
                                        valueList.add(cloneOrConvertBean(item));
                                    }
                                }
                            }
                        }
                    }
                }
                valueEntityListMap.put(entry.getKey(), valueList);
            }
            /*绑定结果。*/
            ResultAssembler.bindEntityPropValue(annoObjectField, super.getMatchedAnnoObjectList(),
                middleTable.getTrunkObjColMapping(), valueEntityListMap, getAnnoObjColumnToFieldMap());
        }
    }

    private Map buildMatchKey2EntityListMap(List list) {
        Map key2TargetListMap = new HashMap<>(list.size());
        StringBuilder sb = new StringBuilder();
        for (T entity : list) {
            sb.setLength(0);
            for (int i = 0; i < refObjJoinCols.size(); i++) {
                String refObjJoinOnCol = refObjJoinCols.get(i);
                String pkValue = BeanUtil.getStringProperty(entity, toRefObjField(refObjJoinOnCol));
                if (i > 0) {
                    sb.append(Strings.COMMA);
                }
                sb.append(pkValue);
            }
            /*查找匹配Key。*/
            String matchKey = sb.toString();
            /*获取list。*/
            List entityList = key2TargetListMap.get(matchKey);
            if (entityList == null) {
                entityList = new ArrayList<>();
                key2TargetListMap.put(matchKey, entityList);
            }
            Object target = entity;
            if (target instanceof Map == false) {
                target = cloneOrConvertBean(entity);
            }
            entityList.add(target);
        }
        sb.setLength(0);
        return key2TargetListMap;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy