fix: 优化分页逻辑
This commit is contained in:
parent
a3e08f0db7
commit
daea0c3139
@ -47,11 +47,7 @@ public class FishDraftDataController {
|
|||||||
@Operation(summary = "分页查询过鱼数据")
|
@Operation(summary = "分页查询过鱼数据")
|
||||||
public ResponseResult queryPageList(@RequestBody DataSourceRequest dataSourceRequest) {
|
public ResponseResult queryPageList(@RequestBody DataSourceRequest dataSourceRequest) {
|
||||||
Page page = KendoUtil.getPage(dataSourceRequest);
|
Page page = KendoUtil.getPage(dataSourceRequest);
|
||||||
DataSourceLoadOptionsBase loadOptions = dataSourceRequest.toDevRequest();
|
Page<FishDraftData> result = fishDraftDataService.queryPageList(page, dataSourceRequest);
|
||||||
String stcd = QgcQueryWrapperUtil.getFilterFieldValue(loadOptions, "stcd");
|
|
||||||
String status = QgcQueryWrapperUtil.getFilterFieldValue(loadOptions, "status");
|
|
||||||
String ftp = QgcQueryWrapperUtil.getFilterFieldValue(loadOptions, "ftp");
|
|
||||||
Page<FishDraftData> result = fishDraftDataService.queryPageList(page, stcd, status, ftp);
|
|
||||||
return ResponseResult.successData(result);
|
return ResponseResult.successData(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package com.yfd.platform.data.service;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.yfd.platform.common.DataSourceRequest;
|
||||||
import com.yfd.platform.data.domain.FishDraftData;
|
import com.yfd.platform.data.domain.FishDraftData;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -16,7 +17,7 @@ public interface IFishDraftDataService extends IService<FishDraftData> {
|
|||||||
/**
|
/**
|
||||||
* 分页查询草稿数据
|
* 分页查询草稿数据
|
||||||
*/
|
*/
|
||||||
Page<FishDraftData> queryPageList(Page<FishDraftData> page, String stcd, String status, String ftp);
|
Page<FishDraftData> queryPageList(Page<FishDraftData> page, DataSourceRequest dataSourceRequest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据审批批次ID查询
|
* 根据审批批次ID查询
|
||||||
|
|||||||
@ -2,11 +2,14 @@ package com.yfd.platform.data.service.impl;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.yfd.platform.common.DataSourceLoadOptionsBase;
|
||||||
|
import com.yfd.platform.common.DataSourceRequest;
|
||||||
import com.yfd.platform.data.domain.FishDraftData;
|
import com.yfd.platform.data.domain.FishDraftData;
|
||||||
import com.yfd.platform.data.mapper.FishDraftDataMapper;
|
import com.yfd.platform.data.mapper.FishDraftDataMapper;
|
||||||
import com.yfd.platform.data.service.IApprovalChangeLogService;
|
import com.yfd.platform.data.service.IApprovalChangeLogService;
|
||||||
import com.yfd.platform.data.service.IApprovalMainService;
|
import com.yfd.platform.data.service.IApprovalMainService;
|
||||||
import com.yfd.platform.data.service.IFishDraftDataService;
|
import com.yfd.platform.data.service.IFishDraftDataService;
|
||||||
|
import com.yfd.platform.utils.QgcQueryWrapperUtil;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
@ -33,14 +36,16 @@ public class FishDraftDataServiceImpl extends ServiceImpl<FishDraftDataMapper, F
|
|||||||
private IApprovalChangeLogService approvalChangeLogService;
|
private IApprovalChangeLogService approvalChangeLogService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<FishDraftData> queryPageList(Page<FishDraftData> page, String stcd, String status, String ftp) {
|
public Page<FishDraftData> queryPageList(Page<FishDraftData> page, DataSourceRequest dataSourceRequest) {
|
||||||
return this.page(page, this.lambdaQuery()
|
DataSourceLoadOptionsBase loadOptions = dataSourceRequest.toDevRequest();
|
||||||
.eq(StringUtils.hasText(stcd), FishDraftData::getStcd, stcd)
|
String stcd = QgcQueryWrapperUtil.getFilterFieldValue(loadOptions, "stcd");
|
||||||
.eq(StringUtils.hasText(status), FishDraftData::getStatus, status)
|
String rstcd = QgcQueryWrapperUtil.getFilterFieldValue(loadOptions, "rstcd");
|
||||||
.like(StringUtils.hasText(ftp), FishDraftData::getFtp, ftp)
|
String baseId = QgcQueryWrapperUtil.getFilterFieldValue(loadOptions, "baseId");
|
||||||
.eq(FishDraftData::getDeletedFlag, 0)
|
String direction = QgcQueryWrapperUtil.getFilterFieldValue(loadOptions, "direction");
|
||||||
.orderByDesc(FishDraftData::getTm)
|
String status = QgcQueryWrapperUtil.getFilterFieldValue(loadOptions, "status");
|
||||||
.getWrapper());
|
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -1,17 +1,19 @@
|
|||||||
package com.yfd.platform.env.controller;
|
package com.yfd.platform.env.controller;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.yfd.platform.annotation.Log;
|
import com.yfd.platform.annotation.Log;
|
||||||
|
import com.yfd.platform.common.DataSourceRequest;
|
||||||
import com.yfd.platform.config.ResponseResult;
|
import com.yfd.platform.config.ResponseResult;
|
||||||
import com.yfd.platform.env.domain.SdFishDictoryB;
|
import com.yfd.platform.env.domain.SdFishDictoryB;
|
||||||
import com.yfd.platform.env.service.ISdFishDictoryBService;
|
import com.yfd.platform.env.service.ISdFishDictoryBService;
|
||||||
|
import com.yfd.platform.utils.DataSourceRequestUtil;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,30 +29,35 @@ public class SdFishDictoryBController {
|
|||||||
@Resource
|
@Resource
|
||||||
private ISdFishDictoryBService sdFishDictoryBService;
|
private ISdFishDictoryBService sdFishDictoryBService;
|
||||||
|
|
||||||
@GetMapping("/queryPageList")
|
@PostMapping("/queryPageList")
|
||||||
@Operation(summary = "分页查询鱼类字典列表")
|
@Operation(summary = "分页查询鱼类字典列表(支持动态过滤和排序)")
|
||||||
public ResponseResult queryPageList(
|
public ResponseResult queryPageList(@RequestBody DataSourceRequest request) {
|
||||||
@RequestParam(required = false) String name,
|
Page<SdFishDictoryB> page = DataSourceRequestUtil.executeQuery(
|
||||||
@RequestParam(required = false) String code,
|
request,
|
||||||
@RequestParam(required = false) Integer type,
|
SdFishDictoryB.class,
|
||||||
@RequestParam(required = false) Integer rare,
|
sdFishDictoryBService
|
||||||
@RequestParam(defaultValue = "1") Long pageNum,
|
);
|
||||||
@RequestParam(defaultValue = "10") Long pageSize) {
|
return ResponseResult.successData(page);
|
||||||
Page<SdFishDictoryB> page = new Page<>(pageNum, pageSize);
|
|
||||||
Page<SdFishDictoryB> result = sdFishDictoryBService.selectPage(name, code, type, rare, page);
|
|
||||||
return ResponseResult.successData(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/list")
|
@PostMapping("/list")
|
||||||
@Operation(summary = "查询所有鱼类字典")
|
@Operation(summary = "查询鱼类字典列表(支持动态过滤和排序,不分页)")
|
||||||
public ResponseResult list() {
|
public ResponseResult list(@RequestBody DataSourceRequest request) {
|
||||||
return ResponseResult.successData(sdFishDictoryBService.list());
|
List<SdFishDictoryB> list = DataSourceRequestUtil.executeList(
|
||||||
|
request,
|
||||||
|
SdFishDictoryB.class,
|
||||||
|
sdFishDictoryBService
|
||||||
|
);
|
||||||
|
return ResponseResult.successData(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/listByName")
|
@GetMapping("/listByName")
|
||||||
@Operation(summary = "根据名称查询所有鱼类字典")
|
@Operation(summary = "根据名称查询所有鱼类字典")
|
||||||
public ResponseResult listByName( @RequestParam(required = false) String name) {
|
public ResponseResult listByName(@RequestParam(required = false) String name) {
|
||||||
return ResponseResult.successData(sdFishDictoryBService.list(new LambdaQueryWrapper<SdFishDictoryB>().eq(StrUtil.isNotBlank( name),SdFishDictoryB::getName, name)));
|
return ResponseResult.successData(sdFishDictoryBService.list(
|
||||||
|
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<SdFishDictoryB>()
|
||||||
|
.eq(StrUtil.isNotBlank(name), SdFishDictoryB::getName, name)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/getById")
|
@GetMapping("/getById")
|
||||||
|
|||||||
@ -0,0 +1,302 @@
|
|||||||
|
package com.yfd.platform.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.yfd.platform.common.DataSourceRequest;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class DataSourceRequestUtil {
|
||||||
|
|
||||||
|
private static final Pattern SAFE_IDENTIFIER = Pattern.compile("^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)*$");
|
||||||
|
|
||||||
|
public static <T> Page<T> getPage(DataSourceRequest request) {
|
||||||
|
if (request == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int take = request.getTake();
|
||||||
|
int skip = request.getSkip();
|
||||||
|
if (take <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Page<T> page = new Page<>();
|
||||||
|
page.setSize((long) take);
|
||||||
|
page.setCurrent(skip / take + 1L);
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> QueryWrapper<T> buildWrapper(DataSourceRequest request, Class<T> entityClass) {
|
||||||
|
return buildWrapper(request, entityClass, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> QueryWrapper<T> buildWrapper(DataSourceRequest request, Class<T> entityClass,
|
||||||
|
Map<String, String> fieldMapping,
|
||||||
|
List<String> excludeFields) {
|
||||||
|
QueryWrapper<T> wrapper = new QueryWrapper<>();
|
||||||
|
|
||||||
|
if (request == null || entityClass == null) {
|
||||||
|
return wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> excludes = excludeFields != null ? excludeFields : new ArrayList<>();
|
||||||
|
|
||||||
|
applyFilters(request.getFilter(), wrapper, entityClass, fieldMapping, excludes, "and");
|
||||||
|
|
||||||
|
applySort(request.getSort(), wrapper, entityClass);
|
||||||
|
|
||||||
|
return wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> QueryWrapper<T> buildQueryWrapper(DataSourceRequest request, Class<T> entityClass) {
|
||||||
|
return buildQueryWrapper(request, entityClass, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> QueryWrapper<T> buildQueryWrapper(DataSourceRequest request, Class<T> entityClass,
|
||||||
|
Map<String, String> fieldMapping,
|
||||||
|
List<String> excludeFields) {
|
||||||
|
QueryWrapper<T> wrapper = new QueryWrapper<>();
|
||||||
|
|
||||||
|
if (request == null || entityClass == null) {
|
||||||
|
return wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> excludes = excludeFields != null ? excludeFields : new ArrayList<>();
|
||||||
|
|
||||||
|
applyFilters(request.getFilter(), wrapper, entityClass, fieldMapping, excludes, "and");
|
||||||
|
|
||||||
|
applySort(request.getSort(), wrapper, entityClass);
|
||||||
|
|
||||||
|
return wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Page<T> executeQuery(DataSourceRequest request, Class<T> entityClass,
|
||||||
|
com.baomidou.mybatisplus.extension.service.IService<T> service) {
|
||||||
|
return executeQuery(request, entityClass, service, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Page<T> executeQuery(DataSourceRequest request, Class<T> entityClass,
|
||||||
|
com.baomidou.mybatisplus.extension.service.IService<T> service,
|
||||||
|
Map<String, String> fieldMapping,
|
||||||
|
List<String> excludeFields) {
|
||||||
|
if (request == null || service == null) {
|
||||||
|
return new Page<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
QueryWrapper<T> wrapper = buildQueryWrapper(request, entityClass, fieldMapping, excludeFields);
|
||||||
|
Page<T> page = getPage(request);
|
||||||
|
|
||||||
|
if (page != null) {
|
||||||
|
return service.page(page, wrapper);
|
||||||
|
} else {
|
||||||
|
List<T> list = service.list(wrapper);
|
||||||
|
Page<T> resultPage = new Page<>();
|
||||||
|
resultPage.setRecords(list);
|
||||||
|
resultPage.setTotal(list.size());
|
||||||
|
resultPage.setSize((long) list.size());
|
||||||
|
resultPage.setCurrent(1L);
|
||||||
|
return resultPage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> List<T> executeList(DataSourceRequest request, Class<T> entityClass,
|
||||||
|
com.baomidou.mybatisplus.extension.service.IService<T> service) {
|
||||||
|
return executeList(request, entityClass, service, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> List<T> executeList(DataSourceRequest request, Class<T> entityClass,
|
||||||
|
com.baomidou.mybatisplus.extension.service.IService<T> service,
|
||||||
|
Map<String, String> fieldMapping,
|
||||||
|
List<String> excludeFields) {
|
||||||
|
if (request == null || service == null) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
QueryWrapper<T> wrapper = buildQueryWrapper(request, entityClass, fieldMapping, excludeFields);
|
||||||
|
return service.list(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> void applyFilters(DataSourceRequest.FilterDescriptor filter,
|
||||||
|
QueryWrapper<T> wrapper,
|
||||||
|
Class<T> entityClass,
|
||||||
|
Map<String, String> fieldMapping,
|
||||||
|
List<String> excludeFields,
|
||||||
|
String parentLogic) {
|
||||||
|
if (filter == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<DataSourceRequest.FilterDescriptor> filters = filter.getFilters();
|
||||||
|
if (CollectionUtil.isEmpty(filters)) {
|
||||||
|
String field = filter.getField();
|
||||||
|
if (StrUtil.isBlank(field)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (excludeFields.contains(field)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String columnName = getColumnName(field, entityClass, fieldMapping);
|
||||||
|
Object value = filter.getValue();
|
||||||
|
String operator = filter.getOperator();
|
||||||
|
|
||||||
|
applySingleCondition(wrapper, columnName, operator, value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filters.size() == 1) {
|
||||||
|
applyFilters(filters.get(0), wrapper, entityClass, fieldMapping, excludeFields, parentLogic);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String logic = filter.getLogic();
|
||||||
|
boolean isAnd = "and".equalsIgnoreCase(logic);
|
||||||
|
|
||||||
|
List<QueryWrapper<T>> conditionGroups = new ArrayList<>();
|
||||||
|
List<String> logics = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < filters.size(); i++) {
|
||||||
|
QueryWrapper<T> subWrapper = new QueryWrapper<>();
|
||||||
|
applyFilters(filters.get(i), subWrapper, entityClass, fieldMapping, excludeFields, logic);
|
||||||
|
if (hasConditions(subWrapper)) {
|
||||||
|
conditionGroups.add(subWrapper);
|
||||||
|
if (i > 0) {
|
||||||
|
logics.add(isAnd ? "and" : "or");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < conditionGroups.size(); i++) {
|
||||||
|
if (i == 0) {
|
||||||
|
mergeWrapper(wrapper, conditionGroups.get(i));
|
||||||
|
} else {
|
||||||
|
String lgc = logics.get(i - 1);
|
||||||
|
if ("or".equalsIgnoreCase(lgc)) {
|
||||||
|
wrapper.or();
|
||||||
|
}
|
||||||
|
mergeWrapper(wrapper, conditionGroups.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> boolean hasConditions(QueryWrapper<T> wrapper) {
|
||||||
|
return wrapper instanceof QueryWrapper && !wrapper.getExpression().getNormal().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> void mergeWrapper(QueryWrapper<T> target, QueryWrapper<T> source) {
|
||||||
|
target.getExpression().getNormal().addAll(source.getExpression().getNormal());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> void applySingleCondition(QueryWrapper<T> wrapper, String columnName,
|
||||||
|
String operator, Object value) {
|
||||||
|
if (columnName == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String op = operator != null ? operator.trim().toLowerCase() : "eq";
|
||||||
|
|
||||||
|
switch (op) {
|
||||||
|
case "eq" -> wrapper.eq(columnName, value);
|
||||||
|
case "neq", "<>" -> wrapper.ne(columnName, value);
|
||||||
|
case "gt" -> wrapper.gt(columnName, value);
|
||||||
|
case "gte", ">=" -> wrapper.ge(columnName, value);
|
||||||
|
case "lt" -> wrapper.lt(columnName, value);
|
||||||
|
case "lte", "<=" -> wrapper.le(columnName, value);
|
||||||
|
case "contains" -> wrapper.like(columnName, value);
|
||||||
|
case "notcontains" -> wrapper.notLike(columnName, value);
|
||||||
|
case "startswith" -> wrapper.likeRight(columnName, value);
|
||||||
|
case "endswith" -> wrapper.likeLeft(columnName, value);
|
||||||
|
case "isnull" -> wrapper.isNull(columnName);
|
||||||
|
case "isnotnull" -> wrapper.isNotNull(columnName);
|
||||||
|
case "isempty" -> wrapper.eq(columnName, "");
|
||||||
|
case "isnotempty" -> wrapper.ne(columnName, "");
|
||||||
|
case "in" -> {
|
||||||
|
if (value instanceof Iterable) {
|
||||||
|
wrapper.in(columnName, (Iterable<?>) value);
|
||||||
|
} else if (value instanceof Object[]) {
|
||||||
|
wrapper.in(columnName, (Object[]) value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "ni", "notin" -> {
|
||||||
|
if (value instanceof Iterable) {
|
||||||
|
wrapper.notIn(columnName, (Iterable<?>) value);
|
||||||
|
} else if (value instanceof Object[]) {
|
||||||
|
wrapper.notIn(columnName, (Object[]) value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default -> wrapper.eq(columnName, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> void applySort(List<DataSourceRequest.SortDescriptor> sorts,
|
||||||
|
QueryWrapper<T> wrapper,
|
||||||
|
Class<T> entityClass) {
|
||||||
|
if (CollectionUtil.isEmpty(sorts)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (DataSourceRequest.SortDescriptor sort : sorts) {
|
||||||
|
if (sort == null || StrUtil.isBlank(sort.getField())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String columnName = getColumnName(sort.getField(), entityClass, null);
|
||||||
|
if (columnName == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String dir = sort.getDir();
|
||||||
|
if ("desc".equalsIgnoreCase(dir)) {
|
||||||
|
wrapper.orderByDesc(columnName);
|
||||||
|
} else {
|
||||||
|
wrapper.orderByAsc(columnName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> String getColumnName(String property, Class<T> entityClass,
|
||||||
|
Map<String, String> fieldMapping) {
|
||||||
|
if (property == null || property.isBlank()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fieldMapping != null && fieldMapping.containsKey(property)) {
|
||||||
|
return fieldMapping.get(property);
|
||||||
|
}
|
||||||
|
|
||||||
|
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
|
||||||
|
if (tableInfo == null) {
|
||||||
|
return requireSafeIdentifier(property);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (property.equals(tableInfo.getKeyProperty())) {
|
||||||
|
return tableInfo.getKeyColumn();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream()
|
||||||
|
.filter(f -> property.equals(f.getProperty()))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
if (!fieldInfos.isEmpty()) {
|
||||||
|
return fieldInfos.get(0).getColumn();
|
||||||
|
}
|
||||||
|
|
||||||
|
return requireSafeIdentifier(property);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String requireSafeIdentifier(String identifier) {
|
||||||
|
String id = identifier == null ? "" : identifier.trim();
|
||||||
|
if (id.isEmpty() || !SAFE_IDENTIFIER.matcher(id).matches()) {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user