优化了构造器的逻辑可以实现简单的条件过滤查询
This commit is contained in:
parent
f3787af369
commit
5bb367146e
@ -1,16 +1,18 @@
|
||||
package com.yfd.platform.modules.algorithm.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.yfd.platform.config.ResponseResult;
|
||||
import com.yfd.platform.modules.algorithm.domain.AlgorithmLogs;
|
||||
import com.yfd.platform.modules.algorithm.service.IAlgorithmLogsService;
|
||||
import com.yfd.platform.utils.wrapper.QueryCondition;
|
||||
import com.yfd.platform.utils.wrapper.QueryWrapperBuilder;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -31,7 +33,7 @@ public class AlgorithmLogsController {
|
||||
|
||||
// 算法分类,区域,间隔,部件
|
||||
@GetMapping("/getAlgorithmLogsPage")
|
||||
@ApiOperation("Page<Map<String,Object>>")
|
||||
@ApiOperation("分页查询算法分析日志")
|
||||
public ResponseResult getAlgorithmLogsPage(String stationCode, String algorithmId, String areaId, String bayId,
|
||||
String mainDeviceId, String componentId, String componentName,
|
||||
Page<AlgorithmLogs> page) {
|
||||
@ -40,4 +42,13 @@ public class AlgorithmLogsController {
|
||||
return ResponseResult.successData(pageMaps);
|
||||
}
|
||||
|
||||
// 算法分类,区域,间隔,部件
|
||||
@PostMapping("/testQueryWrapperBuilder")
|
||||
@ApiOperation("测试构造器")
|
||||
public ResponseResult testQueryWrapperBuilder(@RequestBody QueryCondition queryCondition) {
|
||||
QueryWrapper<AlgorithmLogs> queryWrapper = QueryWrapperBuilder.build(queryCondition, AlgorithmLogs.class);
|
||||
List<AlgorithmLogs> list = algorithmLogsService.list(queryWrapper);
|
||||
return ResponseResult.successData(list);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,17 @@
|
||||
package com.yfd.platform.utils;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
public class TestFileDir {
|
||||
public static void main(String[] args) throws Exception {
|
||||
double resultvalue = 50;
|
||||
double baseValue = 100;
|
||||
double abs = Math.abs((resultvalue - baseValue) / baseValue * 100);
|
||||
System.out.println(abs);
|
||||
|
||||
String decode = URLDecoder.decode("%E5%95%86%E4%B8%98%E5%B8%82%2F500Kv%E6%A0%87%E5%87%86%E5%8F%98%E7%94%B5%E7%AB%99%E7%AE%A1%E7%90%86%E5%8C%BA%E5%9F%9F%2F500V%E5%8F%98%E7%94%B5%E7%AB%99%2F%E7%BC%BA%E9%99%B7%2F202505%2F20250519_160142_500kV%E6%B5%8B%E8%AF%95%E9%97%B4%E9%9A%94_500kV%E6%B5%8B%E8%AF%95%E4%B8%BB%E8%AE%BE%E5%A4%87_%E5%A3%B0%E7%BA%B9%E7%82%B9%E4%BD%8D.wav", "utf-8");
|
||||
System.out.println(decode);
|
||||
String encode = URLEncoder.encode("商丘市\\500Kv标准变电站管理区域\\500V变电站\\缺陷\\202505\\test.mp4", "UTF-8");
|
||||
System.out.println(encode);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.yfd.platform.utils.wrapper;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@ -10,6 +10,6 @@ public class QueryCondition {
|
||||
private Integer page;
|
||||
private Integer size;
|
||||
private Map<String, Object> filters;
|
||||
private Sort sort;
|
||||
private List<SortOrder> sortOrders; // 修改为 SortOrder 列表
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.yfd.platform.utils.wrapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
@ -23,40 +22,36 @@ public class QueryWrapperBuilder {
|
||||
*/
|
||||
public static <T> QueryWrapper<T> build(QueryCondition condition, Class<T> entityClass) {
|
||||
QueryWrapper<T> wrapper = new QueryWrapper<>();
|
||||
|
||||
|
||||
// 修改 build 方法中的过滤条件处理部分
|
||||
condition.getFilters().forEach((field, value) -> {
|
||||
if (isValidField(field, entityClass)) {
|
||||
String column = camelToUnderline(field);
|
||||
|
||||
// 解析运算符(示例格式:age_gt=30)
|
||||
if (field.contains("__")) { // 使用双下划线分隔符更安全
|
||||
String[] parts = field.split("__");
|
||||
if (parts.length == 2) {
|
||||
String realField = parts[0];
|
||||
String operator = parts[1];
|
||||
column = camelToUnderline(realField);
|
||||
applyOperator(wrapper, column, operator, value);
|
||||
if (!field.contains("__")) { // 使用双下划线分隔符更安全
|
||||
return;
|
||||
}
|
||||
String[] parts = field.split("__");
|
||||
if (parts.length != 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 默认等值查询
|
||||
wrapper.eq(column, value);
|
||||
String realField = parts[0];
|
||||
String operator = parts[1];
|
||||
String column = camelToUnderline(realField);
|
||||
if (isValidField(column, entityClass)) {
|
||||
applyOperator(wrapper, column, operator, value);
|
||||
}
|
||||
});
|
||||
|
||||
// 处理排序
|
||||
if (condition.getSort() != null) {
|
||||
condition.getSort().forEach(order -> {
|
||||
if (condition.getSortOrders() != null) {
|
||||
for (SortOrder order : condition.getSortOrders()) {
|
||||
String column = camelToUnderline(order.getProperty());
|
||||
if (order.isAscending()) {
|
||||
if (!isValidField(column, entityClass)) {
|
||||
continue;
|
||||
}
|
||||
if (order.getDirection() == SortDirection.ASC) {
|
||||
wrapper.orderByAsc(column);
|
||||
} else {
|
||||
wrapper.orderByDesc(column);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return wrapper;
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.yfd.platform.utils.wrapper;
|
||||
|
||||
public enum SortDirection {
|
||||
// 升序
|
||||
ASC,
|
||||
// 降序
|
||||
DESC;
|
||||
|
||||
public static SortDirection fromString(String direction) {
|
||||
try {
|
||||
return SortDirection.valueOf(direction.toUpperCase());
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 默认升序
|
||||
return ASC;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.yfd.platform.utils.wrapper;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SortOrder {
|
||||
/**
|
||||
* 排序字段(对应实体类属性名)
|
||||
*/
|
||||
private String property;
|
||||
|
||||
/**
|
||||
* 排序方向
|
||||
*/
|
||||
private SortDirection direction = SortDirection.ASC; // 默认升序
|
||||
|
||||
/**
|
||||
* 快速构建方法
|
||||
*/
|
||||
public static SortOrder of(String property, SortDirection direction) {
|
||||
return new SortOrder(property, direction);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user