org.zodiac.mybatisplus.binding.binder.CountBinder Maven / Gradle / Ivy
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.BindCount;
import org.zodiac.mybatisplus.binding.binder.remote.RemoteBindingManager;
import org.zodiac.mybatisplus.binding.cache.BindingCacheManager;
import org.zodiac.mybatisplus.binding.helper.ResultAssembler;
import org.zodiac.sdk.toolkit.util.collection.CollAndMapUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
import java.util.List;
import java.util.Map;
/**
* 关联子项count计数绑定。
*
*/
public class CountBinder extends EntityListBinder {
public CountBinder(BindCount annotation, List voList) {
super(annotation.entity(), voList);
}
@Override
public void bind() {
if (Colls.emptyColl(annoObjectList)) {
return;
}
if (Colls.emptyColl(refObjJoinCols)) {
throw new IllegalUsageException("调用错误:无法从condition中解析出字段关联.");
}
Map valueListCountMap;
if (middleTable == null) {
this.simplifySelectColumns();
super.buildQueryWrapperJoinOn();
/*查询条件为空时不进行查询。*/
if (queryWrapper.isEmptyOfNormal()) {
return;
}
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)) {
valueListCountMap = this.buildMatchKey2ListCountMap(entityList);
ResultAssembler.bindPropValue(annoObjectField, super.getMatchedAnnoObjectList(), getAnnoObjJoinFlds(),
valueListCountMap, null);
}
} 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;
}
valueListCountMap = CollAndMapUtil.map();
for (Map.Entry entry : middleTableResultMap.entrySet()) {
/*List*/
List annoObjFKList = entry.getValue();
if (Colls.emptyColl(annoObjFKList)) {
continue;
}
Integer count = entry.getValue() != null ? entry.getValue().size() : 0;
valueListCountMap.put(entry.getKey(), count);
}
/*绑定结果*/
ResultAssembler.bindPropValue(annoObjectField, super.getMatchedAnnoObjectList(), getAnnoObjJoinFlds(),
valueListCountMap, null);
}
}
/**
* 简化select列,仅select主键。
*/
@Override
protected void simplifySelectColumns() {
List selectColumns = CollAndMapUtil.list(8);
String idCol = BindingCacheManager.getPropInfoByClass(referencedEntityClass).getIdColumn();
selectColumns.add(idCol);
selectColumns.addAll(refObjJoinCols);
String[] selectColsArray = Strings.toStringArray(selectColumns);
if (remoteBindDTO != null) {
remoteBindDTO.setSelectColumns(selectColsArray);
}
this.queryWrapper.select(selectColsArray);
}
/**
* 构建匹配key-count目标的map。
*
* @param list 实体列表
* @return 匹配的映射关系
*/
private Map buildMatchKey2ListCountMap(List list) {
Map key2TargetCountMap = CollAndMapUtil.map(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。*/
Integer entityCount = key2TargetCountMap.get(matchKey);
if (entityCount == null) {
entityCount = 0;
}
entityCount++;
key2TargetCountMap.put(matchKey, entityCount);
}
sb.setLength(0);
return key2TargetCountMap;
}
}