com.circustar.mybatis_accessor.service.SelectService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-accessor Show documentation
Show all versions of mybatis-accessor Show documentation
enhancement based on mybatis plus
The newest version!
package com.circustar.mybatis_accessor.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.circustar.mybatis_accessor.class_info.DtoField;
import com.circustar.mybatis_accessor.response.PageInfo;
import com.circustar.mybatis_accessor.class_info.DtoClassInfo;
import com.circustar.mybatis_accessor.class_info.DtoClassInfoHelper;
import com.circustar.mybatis_accessor.class_info.DtoFields;
import com.circustar.mybatis_accessor.mapper.CommonMapper;
import com.circustar.mybatis_accessor.relation.EntityDtoServiceRelation;
import com.circustar.common_utils.reflection.FieldUtils;
import com.circustar.common_utils.parser.SPELParser;
import org.springframework.util.StringUtils;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
public class SelectService implements ISelectService {
private final DtoClassInfoHelper dtoClassInfoHelper;
public SelectService(DtoClassInfoHelper dtoClassInfoHelper) {
this.dtoClassInfoHelper = dtoClassInfoHelper;
}
@Override
public T getEntityByAnnotation(EntityDtoServiceRelation relationInfo
, Object object
) {
DtoClassInfo dtoClassInfo = this.dtoClassInfoHelper.getDtoClassInfo(relationInfo);
QueryWrapper queryWrapper = dtoClassInfo.createQueryWrapper(object);
return getEntityByQueryWrapper(relationInfo, object, queryWrapper);
}
@Override
public T getDtoByAnnotation(EntityDtoServiceRelation relationInfo
, Object object, boolean includeAllChildren, List children
){
Object oriEntity = this.getEntityByAnnotation(relationInfo, object);
if (oriEntity == null) {
return null;
}
DtoClassInfo dtoClassInfo = dtoClassInfoHelper.getDtoClassInfo(relationInfo);
List subFields = DtoClassInfo.getDtoFieldsByName(dtoClassInfo, includeAllChildren, false, children);
T result = (T) this.dtoClassInfoHelper.convertFromEntity(oriEntity, dtoClassInfo);
if(dtoClassInfo.getEntityClassInfo().getKeyField() != null) {
Serializable id = (Serializable) FieldUtils.getFieldValue(oriEntity
, dtoClassInfo.getEntityClassInfo().getKeyField().getPropertyDescriptor().getReadMethod());
if (id != null) {
setDtoChildren(relationInfo, dtoClassInfo, result, id, subFields);
}
}
return result;
}
@Override
public T getEntityByQueryWrapper(EntityDtoServiceRelation relationInfo
, Object dto
, QueryWrapper queryWrapper) {
DtoClassInfo dtoClassInfo = dtoClassInfoHelper.getDtoClassInfo(relationInfo);
IService service = dtoClassInfo.getServiceBean();
String joinExpression = getJoinExpression(dtoClassInfo, dto);
T result;
if (!StringUtils.isEmpty(joinExpression)) {
result = (T) ((CommonMapper)service.getBaseMapper()).selectOneWithJoin(queryWrapper
, joinExpression, dtoClassInfo.getJoinColumns());
} else {
result = (T) service.getOne(queryWrapper);
}
return result;
}
@Override
public T getDtoByQueryWrapper(EntityDtoServiceRelation relationInfo
, Object dto
, QueryWrapper queryWrapper
, boolean includeAllChildren
, List children) {
Object oriEntity = getEntityByQueryWrapper(relationInfo, dto, queryWrapper);
if (oriEntity == null) {
return null;
}
DtoClassInfo dtoClassInfo = dtoClassInfoHelper.getDtoClassInfo(relationInfo);
List subFields = DtoClassInfo.getDtoFieldsByName(dtoClassInfo, includeAllChildren, false, children);
T result = (T) this.dtoClassInfoHelper.convertFromEntity(oriEntity, dtoClassInfo);
if(dtoClassInfo.getEntityClassInfo().getKeyField() != null) {
Serializable id = (Serializable) FieldUtils.getFieldValue(oriEntity
, dtoClassInfo.getEntityClassInfo().getKeyField().getPropertyDescriptor().getReadMethod());
if(id != null) {
setDtoChildren(relationInfo, dtoClassInfo, result, id, subFields);
}
}
return result;
}
private void setDtoChildren(EntityDtoServiceRelation relationInfo
, DtoClassInfo dtoClassInfo
, Object dto
, Serializable id
, List subFields) {
Map> dtoFieldMap = subFields.stream().filter(x -> x.getQueryJoin() == null)
.collect(Collectors.partitioningBy(x -> x.getSelectors() == null || x.getSelectors().size() == 0));
List fieldsWithNoSelector = dtoFieldMap.get(true);
if(fieldsWithNoSelector != null && !fieldsWithNoSelector.isEmpty()) {
DtoFields.queryAndAssignDtoFieldById(dtoClassInfo
, relationInfo, fieldsWithNoSelector, dto, id);
}
List fieldsWithSelector = dtoFieldMap.get(false);
if(fieldsWithSelector != null && !fieldsWithSelector.isEmpty()) {
DtoFields.queryAndAssignDtoFieldBySelector(dtoClassInfo.getDtoClassInfoHelper()
, relationInfo
, dto
, fieldsWithSelector);
}
}
@Override
public T getEntityById(EntityDtoServiceRelation relationInfo
, Serializable id){
DtoClassInfo dtoClassInfo = dtoClassInfoHelper.getDtoClassInfo(relationInfo);
IService service = dtoClassInfo.getServiceBean();
String joinExpression = getJoinExpression(dtoClassInfo, null);
T result;
if (!StringUtils.isEmpty(joinExpression)) {
QueryWrapper qw = new QueryWrapper();
qw.eq(dtoClassInfo.getEntityClassInfo().getTableInfo().getTableName()
+ "." + dtoClassInfo.getEntityClassInfo().getTableInfo().getKeyColumn(), id);
result = (T) ((CommonMapper)service.getBaseMapper()).selectOneWithJoin(qw
, joinExpression, dtoClassInfo.getJoinColumns());
} else {
result = (T) service.getById(id);
}
return result;
}
@Override
public T getDtoById(EntityDtoServiceRelation relationInfo
, Serializable id
, boolean includeAllChildren
, List children) {
Object oriEntity = getEntityById(relationInfo, id);
if (oriEntity == null) {
return null;
}
DtoClassInfo dtoClassInfo = dtoClassInfoHelper.getDtoClassInfo(relationInfo);
List subFields = DtoClassInfo.getDtoFieldsByName(dtoClassInfo, includeAllChildren, false, children);
T result = (T) this.dtoClassInfoHelper.convertFromEntity(oriEntity, dtoClassInfo);
setDtoChildren(relationInfo, dtoClassInfo, result, id , subFields);
return result;
}
@Override
public PageInfo getEntityPageByAnnotation(EntityDtoServiceRelation relationInfo
, Object object
, Integer pageIndex
, Integer pageSize
) {
DtoClassInfo dtoClassInfo = this.dtoClassInfoHelper.getDtoClassInfo(relationInfo);
QueryWrapper queryWrapper = dtoClassInfo.createQueryWrapper(object);
return getEntityPageByQueryWrapper(relationInfo, object, queryWrapper, pageIndex, pageSize);
}
@Override
public PageInfo getDtoPageByAnnotation(EntityDtoServiceRelation relationInfo
, Object object
, Integer pageIndex
, Integer pageSize
) {
PageInfo entityPage = getEntityPageByAnnotation(relationInfo, object, pageIndex, pageSize);
DtoClassInfo dtoClassInfo = dtoClassInfoHelper.getDtoClassInfo(relationInfo);
List dtoList = (List) this.dtoClassInfoHelper.convertFromEntityList(entityPage.getRecords(), dtoClassInfo);
return new PageInfo<>(entityPage.getTotal(), entityPage.getSize(), entityPage.getCurrent(), dtoList); }
@Override
public PageInfo getEntityPageByQueryWrapper(EntityDtoServiceRelation relationInfo
, Object dto
, QueryWrapper queryWrapper
, Integer pageIndex
, Integer pageSize
) {
DtoClassInfo dtoClassInfo = this.dtoClassInfoHelper.getDtoClassInfo(relationInfo);
IService service = dtoClassInfo.getServiceBean();
Page page = new Page(pageIndex, pageSize);
IPage pageResult;
String joinExpression = getJoinExpression(dtoClassInfo, dto);
if (!StringUtils.isEmpty(joinExpression)) {
pageResult = ((CommonMapper) service.getBaseMapper()).selectPageWithJoin(page, queryWrapper
, joinExpression, dtoClassInfo.getJoinColumns());
} else {
pageResult = service.page(page, queryWrapper);
}
return new PageInfo(pageResult.getTotal(), pageResult.getSize(), pageResult.getCurrent(), pageResult.getRecords());
}
@Override
public PageInfo getDtoPageByQueryWrapper(EntityDtoServiceRelation relationInfo, Object object
, QueryWrapper queryWrapper
, Integer pageIndex
, Integer pageSize
) {
PageInfo entityPage = getEntityPageByQueryWrapper(relationInfo, object, queryWrapper, pageIndex, pageSize);
DtoClassInfo dtoClassInfo = dtoClassInfoHelper.getDtoClassInfo(relationInfo);
List dtoList = (List) this.dtoClassInfoHelper.convertFromEntityList(entityPage.getRecords(), dtoClassInfo);
return new PageInfo<>(entityPage.getTotal(), entityPage.getSize(), entityPage.getCurrent(), dtoList);
}
@Override
public List getEntityListByAnnotation(EntityDtoServiceRelation relationInfo
, Object object
) {
DtoClassInfo dtoClassInfo = this.dtoClassInfoHelper.getDtoClassInfo(relationInfo);
QueryWrapper queryWrapper = dtoClassInfo.createQueryWrapper(object);
return getEntityListByQueryWrapper(relationInfo, object, queryWrapper);
}
@Override
public List getDtoListByAnnotation(EntityDtoServiceRelation relationInfo
, Object object
) {
List entityList = getEntityListByAnnotation(relationInfo, object);
DtoClassInfo dtoClassInfo = dtoClassInfoHelper.getDtoClassInfo(relationInfo);
return (List) this.dtoClassInfoHelper.convertFromEntityList(entityList, dtoClassInfo);
}
@Override
public List getEntityListByQueryWrapper(EntityDtoServiceRelation relationInfo
, Object dto, QueryWrapper queryWrapper
) {
DtoClassInfo dtoClassInfo = this.dtoClassInfoHelper.getDtoClassInfo(relationInfo);
IService service = dtoClassInfo.getServiceBean();
List entityList;
String joinExpression = getJoinExpression(dtoClassInfo, dto);
if (!StringUtils.isEmpty(joinExpression)) {
entityList = ((CommonMapper)service.getBaseMapper()).selectListWithJoin(queryWrapper
, joinExpression, dtoClassInfo.getJoinColumns());
} else {
entityList = service.list(queryWrapper);
}
return entityList;
}
@Override
public List getDtoListByQueryWrapper(EntityDtoServiceRelation relationInfo, Object object
, QueryWrapper queryWrapper
) {
List entityList = getEntityListByQueryWrapper(relationInfo, object, queryWrapper);
DtoClassInfo dtoClassInfo = dtoClassInfoHelper.getDtoClassInfo(relationInfo);
return (List) this.dtoClassInfoHelper.convertFromEntityList(entityList, dtoClassInfo);
}
@Override
public Long getCountByAnnotation(EntityDtoServiceRelation relationInfo
, Object object
){
DtoClassInfo dtoClassInfo = this.dtoClassInfoHelper.getDtoClassInfo(relationInfo);
QueryWrapper queryWrapper = dtoClassInfo.createQueryWrapper(object);
return this.getCountByQueryWrapper(relationInfo, object, queryWrapper);
}
@Override
public Long getCountByQueryWrapper(EntityDtoServiceRelation relationInfo
, Object dto, QueryWrapper queryWrapper
) {
DtoClassInfo dtoClassInfo = this.dtoClassInfoHelper.getDtoClassInfo(relationInfo);
IService service = dtoClassInfo.getServiceBean();
Long result;
String joinExpression = getJoinExpression(dtoClassInfo, dto);
if (!StringUtils.isEmpty(joinExpression)) {
result = ((CommonMapper)service.getBaseMapper()).selectCountWithJoin(queryWrapper
, joinExpression, dtoClassInfo.getJoinColumns());
} else {
result = service.count(queryWrapper);
}
return result;
}
private String getJoinExpression(DtoClassInfo dtoClassInfo, Object dto) {
return SPELParser.parseStringExpression(dto, dtoClassInfo.getJoinTables());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy