top.summerboot.orm.association.AssociationQuery Maven / Gradle / Ivy
The newest version!
package top.summerboot.orm.association;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import top.summerboot.orm.association.annotation.MDS;
import top.summerboot.orm.association.util.JoinUtil;
import top.summerboot.orm.association.util.QueryUtil;
import top.summerboot.orm.association.util.ReflexUtil;
import top.summerboot.orm.dto.PageDTO;
import top.summerboot.orm.service.BaseDao;
import top.summerboot.orm.util.SpringBeanUtil;
import top.summerboot.orm.wrapper.WrapperFactory;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author xieshuang
* @date 2020-11-22 11:44
*/
public class AssociationQuery {
private Object query;
public AssociationQuery(Class eClass){
this.eClass = eClass;
}
private final Class eClass;
public IPage voPage(PageDTO pageQuery){
return voPage(pageQuery, new WrapperFactory<>().create(pageQuery, eClass));
}
public IPage voPage(PageDTO pageQuery, Object query){
return voPage(pageQuery, new WrapperFactory<>().create(query, eClass));
}
public IPage voPage(PageDTO pageQuery, Object query, boolean association){
return voPage(pageQuery, new WrapperFactory<>().create(query, eClass), association);
}
public IPage voPage(PageDTO pageQuery, boolean association){
return voPage(pageQuery, new WrapperFactory<>().create(pageQuery, eClass), association);
}
public IPage voPage(PageDTO pageQuery, QueryWrapper queryWrapper, boolean association){
return voPage(pageQuery, queryWrapper, QueryUtil.generateSql(eClass, pageQuery), association);
}
public IPage voPage(PageDTO pageQuery, QueryWrapper queryWrapper){
return voPage(pageQuery, queryWrapper, QueryUtil.generateSql(eClass, pageQuery), true);
}
public IPage voPage(PageDTO pageQuery, QueryWrapper queryWrapper, String sql, boolean association){
this.query = pageQuery;
BaseDao baseDao;
MDS ds = eClass.getAnnotation(MDS.class);
if (ds == null){
baseDao = SpringBeanUtil.getBean("defaultDaoImpl", BaseDao.class);
}else {
baseDao = SpringBeanUtil.getBean(ds.value() + "DaoImpl", BaseDao.class);
}
IPage selectPage = baseDao.selectPageBySql(PageDTO.page(pageQuery), sql, queryWrapper);
IPage voPage = new Page<>();
BeanUtils.copyProperties(selectPage, voPage);
List voList = selectPage.getRecords().stream().map(jsonObject -> JSON.toJavaObject(jsonObject, eClass)).collect(Collectors.toList());
if (association){
JoinUtil joinUtil = new JoinUtil(voList, query);
joinUtil.relationObjProcessing();
}
voPage.setRecords(voList);
return voPage;
}
public List voList(Object query){
return voList(new WrapperFactory<>().create(query, eClass), QueryUtil.generateSql(eClass, query), true);
}
public List voList(Object query, boolean association){
return voList(new WrapperFactory<>().create(query, eClass), QueryUtil.generateSql(eClass, query), association);
}
public List voList(QueryWrapper queryWrapper){
return voList(null, queryWrapper);
}
public List voList(QueryWrapper queryWrapper, boolean association){
return voList(null, queryWrapper, association);
}
public List voList(Object query, QueryWrapper queryWrapper){
this.query = query;
return voList(queryWrapper, QueryUtil.generateSql(eClass, query), true);
}
public List voList(Object query, QueryWrapper queryWrapper, boolean association){
this.query = query;
return voList(queryWrapper, QueryUtil.generateSql(eClass, query), association);
}
public List voList(Collection> ids){
String tableName;
TableName annotation = ReflexUtil.getAnnotation(eClass, TableName.class, Model.class);
if (annotation == null){
tableName = StrUtil.toUnderlineCase(eClass.getSimpleName());
}else {
tableName = annotation.value();
}
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.in(tableName + ".id", ids);
return voList(queryWrapper);
}
public List voList(QueryWrapper queryWrapper, String sql, boolean association){
BaseDao baseDao;
MDS ds = eClass.getAnnotation(MDS.class);
if (ds == null){
baseDao = SpringBeanUtil.getBean("defaultDaoImpl", BaseDao.class);
}else {
baseDao = SpringBeanUtil.getBean(ds.value() + "DaoImpl", BaseDao.class);
}
List objectList = baseDao.selectListBySql(sql, queryWrapper);
List voList = objectList.stream().map(jsonObject -> JSON.toJavaObject(jsonObject, eClass)).collect(Collectors.toList());
if (association){
JoinUtil joinUtil = new JoinUtil(voList, query);
joinUtil.relationObjProcessing();
}
return voList;
}
public E getVo(QueryWrapper queryWrapper, String sql, boolean association){
List list = voList(queryWrapper, sql, association);
if (CollUtil.isNotEmpty(list)){
return list.get(0);
}
return null;
}
public E getVo(QueryWrapper queryWrapper){
return getVo(queryWrapper, QueryUtil.generateSql(eClass, null),true);
}
public E getVo(QueryWrapper queryWrapper, boolean association){
return getVo(queryWrapper, QueryUtil.generateSql(eClass, null), association);
}
public E getVo(QueryWrapper queryWrapper, Object query){
return getVo(queryWrapper, QueryUtil.generateSql(eClass, query), true);
}
public E getVo(QueryWrapper queryWrapper, Object query, boolean association){
return getVo(queryWrapper, QueryUtil.generateSql(eClass, query), association);
}
public E getVo(Object id, Object query){
return getVo(id, query, true);
}
public E getVo(Object id, Object query, boolean association){
this.query = query;
String tableName;
TableName annotation = ReflexUtil.getAnnotation(eClass, TableName.class, Model.class);
if (annotation == null){
tableName = StrUtil.toUnderlineCase(eClass.getSimpleName());
}else {
tableName = annotation.value();
}
return getVo(id, tableName + ".id", query, association);
}
public E getVo(Object id){
String tableName;
TableName annotation = ReflexUtil.getAnnotation(eClass, TableName.class, Model.class);
if (annotation == null){
tableName = StrUtil.toUnderlineCase(eClass.getSimpleName());
}else {
tableName = annotation.value();
}
return getVo(id, tableName + ".id");
}
public E getVo(Object id, boolean association){
String tableName;
TableName annotation = ReflexUtil.getAnnotation(eClass, TableName.class, Model.class);
if (annotation == null){
tableName = StrUtil.toUnderlineCase(eClass.getSimpleName());
}else {
tableName = annotation.value();
}
return getVo(id, tableName + ".id", association);
}
public E getVo(Object id, String idName){
return getVo(id, idName, null);
}
public E getVo(Object id, String idName, boolean association){
return getVo(id, idName, null, association);
}
public E getVo(Object id, String idName, Object query){
QueryWrapper