com.github.yulichang.extension.mapping.relation.Relation Maven / Gradle / Ivy
package com.github.yulichang.extension.mapping.relation;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import com.github.yulichang.extension.mapping.config.DeepConfig;
import com.github.yulichang.extension.mapping.mapper.MPJTableFieldInfo;
import com.github.yulichang.extension.mapping.mapper.MPJTableInfo;
import com.github.yulichang.extension.mapping.mapper.MPJTableInfoHelper;
import com.github.yulichang.extension.mapping.wrapper.MappingQuery;
import com.github.yulichang.toolkit.LambdaUtils;
import java.util.*;
import java.util.stream.Collectors;
@SuppressWarnings("unchecked")
public class Relation {
/**
* 通过注解实现单表多次查询
*
* @param r BaseMapper执行结果
* @param config 映射配置
* @see com.github.yulichang.annotation.EntityMapping
* @see com.github.yulichang.annotation.FieldMapping
*/
public static R mpjGetRelation(R r, DeepConfig config) {
int start = 1;
if (Objects.isNull(r)) {
return null;
} else if (r instanceof List) {
List data = (List) r;
if (CollectionUtils.isEmpty(data)) {
return r;
} else {
T t = data.get(0);
if (Map.class.isAssignableFrom(t.getClass())) {
throw ExceptionUtils.mpe("暂不支持Map类型映射");
}
if (Object.class == t.getClass()) {
return r;
}
return (R) Relation.list(data, start, config);
}
} else if (r instanceof IPage) {
IPage data = (IPage) r;
if (!CollectionUtils.isEmpty(data.getRecords())) {
T t = data.getRecords().get(0);
if (Map.class.isAssignableFrom(t.getClass())) {
throw ExceptionUtils.mpe("暂不支持Map类型映射");
}
if (Object.class == t.getClass()) {
return r;
}
Relation.list(data.getRecords(), start, config);
}
return r;
} else if (r instanceof Integer) {
return r;
} else if (r instanceof Long) {
return r;
} else if (r instanceof Boolean) {
return r;
} else if (Object.class == r.getClass()) {
return r;
} else {
return (R) Relation.one((T) r, start, config);
}
}
public static List list(List data, int currDeep, DeepConfig config) {
if (CollectionUtils.isEmpty(data)) {
return data;
}
Class> entityClass = data.get(0).getClass();
MPJTableInfo tableInfo = MPJTableInfoHelper.getTableInfo(entityClass);
if (tableInfo.isHasMappingOrField()) {
boolean hasProperty = CollectionUtils.isNotEmpty(config.getProperty());
List listProperty = hasProperty ? config.getProperty().stream().map(i ->
LambdaUtils.getName(i).toUpperCase(Locale.ENGLISH)).collect(Collectors.toList()) : null;
for (MPJTableFieldInfo fieldInfo : tableInfo.getFieldList()) {
if (!hasProperty || listProperty.contains(fieldInfo.getProperty().toUpperCase(Locale.ENGLISH))) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy