com.diboot.core.binding.QueryBuilder Maven / Gradle / Ivy
/*
* Copyright (c) 2015-2020, www.dibo.ltd ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.diboot.core.binding;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList;
import com.diboot.core.binding.helper.WrapperHelper;
import com.diboot.core.binding.parser.ParserCache;
import com.diboot.core.binding.query.BindQuery;
import com.diboot.core.binding.query.Comparison;
import com.diboot.core.binding.query.Strategy;
import com.diboot.core.binding.query.dynamic.AnnoJoiner;
import com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper;
import com.diboot.core.binding.query.dynamic.ExtQueryWrapper;
import com.diboot.core.data.protect.DataEncryptHandler;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.ContextHolder;
import com.diboot.core.util.S;
import com.diboot.core.util.V;
import com.diboot.core.vo.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.lang.model.type.NullType;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* QueryWrapper构建器
*
* @author [email protected]
* @version v2.0
* @date 2019/07/27
*/
@SuppressWarnings({"unchecked", "rawtypes", "JavaDoc"})
public class QueryBuilder {
private static Logger log = LoggerFactory.getLogger(QueryBuilder.class);
/**
* Entity或者DTO对象转换为QueryWrapper
*
* @param dto
* @param
* @return
*/
public static QueryWrapper toQueryWrapper(DTO dto) {
return dtoToWrapper(dto, null, null);
}
/**
* Entity或者DTO对象转换为QueryWrapper
*
* @param dto
* @param pagination 分页
* @param
* @return
*/
public static QueryWrapper toQueryWrapper(DTO dto, Pagination pagination) {
return dtoToWrapper(dto, null, pagination);
}
/**
* Entity或者DTO对象转换为QueryWrapper
*
* @param dto
* @param fields 指定参与转换的属性值
* @param
* @return
*/
public static QueryWrapper toQueryWrapper(DTO dto, Collection fields) {
return dtoToWrapper(dto, fields, null);
}
/**
* Entity或者DTO对象转换为QueryWrapper
*
* @param dto
* @param fields 指定参与转换的属性值
* @param pagination 分页
* @param
* @return
*/
public static QueryWrapper toQueryWrapper(DTO dto, Collection fields, Pagination pagination) {
return dtoToWrapper(dto, fields, pagination);
}
/**
* Entity或者DTO对象转换为QueryWrapper
*
* @param dto
* @param
* @return
*/
public static ExtQueryWrapper toDynamicJoinQueryWrapper(DTO dto) {
return toDynamicJoinQueryWrapper(dto, null, null);
}
/**
* Entity或者DTO对象转换为QueryWrapper
*
* @param dto
* @param pagination 分页
* @param
* @return
*/
public static ExtQueryWrapper toDynamicJoinQueryWrapper(DTO dto, Pagination pagination) {
return toDynamicJoinQueryWrapper(dto, null, pagination);
}
/**
* Entity或者DTO对象转换为QueryWrapper
*
* @param dto
* @param
* @return
*/
public static ExtQueryWrapper toDynamicJoinQueryWrapper(DTO dto, Collection fields) {
return toDynamicJoinQueryWrapper(dto, fields, null);
}
/**
* Entity或者DTO对象转换为QueryWrapper
*
* @param dto
* @param fields 指定参与转换的属性值
* @param
* @return
*/
public static ExtQueryWrapper toDynamicJoinQueryWrapper(DTO dto, Collection fields, Pagination pagination) {
QueryWrapper queryWrapper = dtoToWrapper(dto, fields, pagination);
if (!(queryWrapper instanceof DynamicJoinQueryWrapper)) {
return (ExtQueryWrapper) queryWrapper;
}
return (DynamicJoinQueryWrapper) queryWrapper;
}
/**
* 转换具体实现
*
* @param dto
* @return
*/
private static QueryWrapper> dtoToWrapper(DTO dto, Collection fields, Pagination pagination) {
QueryWrapper> wrapper;
// 转换
LinkedHashMap fieldValuesMap = extractNotNullValues(dto, fields, pagination);
if (V.isEmpty(fieldValuesMap)) {
return new QueryWrapper<>();
}
// 只解析有值的
fields = fieldValuesMap.keySet();
// 是否有join联表查询
boolean hasJoinTable = ParserCache.hasJoinTable(dto, fields);
if (hasJoinTable) {
wrapper = new DynamicJoinQueryWrapper<>(dto.getClass(), fields);
} else {
wrapper = new ExtQueryWrapper<>();
}
// 构建 ColumnName
List annoJoinerList = ParserCache.getBindQueryAnnos(dto.getClass());
BiFunction buildColumnName = (bindQuery, field) -> {
if (bindQuery != null) {
String key = field.getName() + bindQuery;
for (AnnoJoiner annoJoiner : annoJoinerList) {
if (key.equals(annoJoiner.getKey())) {
if (V.notEmpty(annoJoiner.getJoin())) {
// 获取注解Table
return annoJoiner.getAlias() + "." + annoJoiner.getColumnName();
} else {
return (hasJoinTable ? "self." : "") + annoJoiner.getColumnName();
}
}
}
}
return (hasJoinTable ? "self." : "") + BeanUtils.getColumnName(field);
};
// 忽略空字符串"",空集合等
BiPredicate