优化了构造器的逻辑可以实现简单的条件过滤查询
This commit is contained in:
parent
f3787af369
commit
5bb367146e
@ -1,16 +1,18 @@
|
|||||||
package com.yfd.platform.modules.algorithm.controller;
|
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.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.yfd.platform.config.ResponseResult;
|
import com.yfd.platform.config.ResponseResult;
|
||||||
import com.yfd.platform.modules.algorithm.domain.AlgorithmLogs;
|
import com.yfd.platform.modules.algorithm.domain.AlgorithmLogs;
|
||||||
import com.yfd.platform.modules.algorithm.service.IAlgorithmLogsService;
|
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.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,13 +33,22 @@ public class AlgorithmLogsController {
|
|||||||
|
|
||||||
// 算法分类,区域,间隔,部件
|
// 算法分类,区域,间隔,部件
|
||||||
@GetMapping("/getAlgorithmLogsPage")
|
@GetMapping("/getAlgorithmLogsPage")
|
||||||
@ApiOperation("Page<Map<String,Object>>")
|
@ApiOperation("分页查询算法分析日志")
|
||||||
public ResponseResult getAlgorithmLogsPage(String stationCode, String algorithmId, String areaId, String bayId,
|
public ResponseResult getAlgorithmLogsPage(String stationCode, String algorithmId, String areaId, String bayId,
|
||||||
String mainDeviceId, String componentId, String componentName,
|
String mainDeviceId, String componentId, String componentName,
|
||||||
Page<AlgorithmLogs> page) {
|
Page<AlgorithmLogs> page) {
|
||||||
Page<Map<String,Object>> pageMaps = algorithmLogsService.getAlgorithmLogsPage(stationCode, algorithmId, areaId,
|
Page<Map<String, Object>> pageMaps = algorithmLogsService.getAlgorithmLogsPage(stationCode, algorithmId, areaId,
|
||||||
bayId, mainDeviceId, componentId, componentName, page);
|
bayId, mainDeviceId, componentId, componentName, page);
|
||||||
return ResponseResult.successData(pageMaps);
|
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;
|
package com.yfd.platform.utils;
|
||||||
|
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
public class TestFileDir {
|
public class TestFileDir {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
double resultvalue = 50;
|
double resultvalue = 50;
|
||||||
double baseValue = 100;
|
double baseValue = 100;
|
||||||
double abs = Math.abs((resultvalue - baseValue) / baseValue * 100);
|
double abs = Math.abs((resultvalue - baseValue) / baseValue * 100);
|
||||||
System.out.println(abs);
|
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;
|
package com.yfd.platform.utils.wrapper;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.data.domain.Sort;
|
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@ -10,6 +10,6 @@ public class QueryCondition {
|
|||||||
private Integer page;
|
private Integer page;
|
||||||
private Integer size;
|
private Integer size;
|
||||||
private Map<String, Object> filters;
|
private Map<String, Object> filters;
|
||||||
private Sort sort;
|
private List<SortOrder> sortOrders; // 修改为 SortOrder 列表
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.yfd.platform.utils.wrapper;
|
package com.yfd.platform.utils.wrapper;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import org.springframework.data.domain.Sort;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -23,40 +22,36 @@ public class QueryWrapperBuilder {
|
|||||||
*/
|
*/
|
||||||
public static <T> QueryWrapper<T> build(QueryCondition condition, Class<T> entityClass) {
|
public static <T> QueryWrapper<T> build(QueryCondition condition, Class<T> entityClass) {
|
||||||
QueryWrapper<T> wrapper = new QueryWrapper<>();
|
QueryWrapper<T> wrapper = new QueryWrapper<>();
|
||||||
|
|
||||||
|
|
||||||
// 修改 build 方法中的过滤条件处理部分
|
// 修改 build 方法中的过滤条件处理部分
|
||||||
condition.getFilters().forEach((field, value) -> {
|
condition.getFilters().forEach((field, value) -> {
|
||||||
if (isValidField(field, entityClass)) {
|
if (!field.contains("__")) { // 使用双下划线分隔符更安全
|
||||||
String column = camelToUnderline(field);
|
return;
|
||||||
|
}
|
||||||
// 解析运算符(示例格式:age_gt=30)
|
String[] parts = field.split("__");
|
||||||
if (field.contains("__")) { // 使用双下划线分隔符更安全
|
if (parts.length != 2) {
|
||||||
String[] parts = field.split("__");
|
return;
|
||||||
if (parts.length == 2) {
|
}
|
||||||
String realField = parts[0];
|
String realField = parts[0];
|
||||||
String operator = parts[1];
|
String operator = parts[1];
|
||||||
column = camelToUnderline(realField);
|
String column = camelToUnderline(realField);
|
||||||
applyOperator(wrapper, column, operator, value);
|
if (isValidField(column, entityClass)) {
|
||||||
return;
|
applyOperator(wrapper, column, operator, value);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 默认等值查询
|
|
||||||
wrapper.eq(column, value);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 处理排序
|
// 处理排序
|
||||||
if (condition.getSort() != null) {
|
if (condition.getSortOrders() != null) {
|
||||||
condition.getSort().forEach(order -> {
|
for (SortOrder order : condition.getSortOrders()) {
|
||||||
String column = camelToUnderline(order.getProperty());
|
String column = camelToUnderline(order.getProperty());
|
||||||
if (order.isAscending()) {
|
if (!isValidField(column, entityClass)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (order.getDirection() == SortDirection.ASC) {
|
||||||
wrapper.orderByAsc(column);
|
wrapper.orderByAsc(column);
|
||||||
} else {
|
} else {
|
||||||
wrapper.orderByDesc(column);
|
wrapper.orderByDesc(column);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return wrapper;
|
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