refactor(iot-device): 重构设备查询接口
- 将 query 方法改为 list 方法,返回类型从 IPage<Device> 改为 List<Device> - 在 DeviceQuery 中添加 productName 字段,用于产品名称查询 - 优化设备列表查询逻辑,增加产品名称查询功能 - 调整 Service接口和实现类以适应新的查询逻辑
This commit is contained in:
parent
4a48907215
commit
0170600ff7
@ -68,8 +68,8 @@ public class DeviceController {
|
|||||||
@DataPermission
|
@DataPermission
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@PreAuthorize("hasAuthority('iot:device:query')")
|
@PreAuthorize("hasAuthority('iot:device:query')")
|
||||||
public IPage<Device> query(Page<Device> page, DeviceQuery query) {
|
public List<Device> query(DeviceQuery query) {
|
||||||
return service.query(page, query);
|
return service.list(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,6 +2,9 @@ package com.zsc.edu.gateway.modules.iot.device.query;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.zsc.edu.gateway.modules.iot.device.entity.Device;
|
import com.zsc.edu.gateway.modules.iot.device.entity.Device;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.product.repo.ProductRepository;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
@ -16,6 +19,8 @@ import java.util.Objects;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class DeviceQuery {
|
public class DeviceQuery {
|
||||||
|
@Resource
|
||||||
|
private ProductRepository productRepository;
|
||||||
/**
|
/**
|
||||||
* 设备名称
|
* 设备名称
|
||||||
*/
|
*/
|
||||||
@ -38,7 +43,10 @@ public class DeviceQuery {
|
|||||||
* 设态是否在线
|
* 设态是否在线
|
||||||
*/
|
*/
|
||||||
public Boolean isOnline;
|
public Boolean isOnline;
|
||||||
|
/**
|
||||||
|
* 产品名称
|
||||||
|
*/
|
||||||
|
public String productName;
|
||||||
|
|
||||||
public LambdaQueryWrapper<Device> wrapper() {
|
public LambdaQueryWrapper<Device> wrapper() {
|
||||||
LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
@ -26,5 +26,5 @@ public interface DeviceService extends IService<Device> {
|
|||||||
|
|
||||||
Boolean serve(DeviceServeDto dto);
|
Boolean serve(DeviceServeDto dto);
|
||||||
|
|
||||||
IPage<Device> query(Page<Device> page, DeviceQuery query);
|
List<Device> list(DeviceQuery query);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package com.zsc.edu.gateway.modules.iot.device.service.impl;
|
package com.zsc.edu.gateway.modules.iot.device.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
||||||
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.zsc.edu.gateway.exception.ConstraintException;
|
import com.zsc.edu.gateway.exception.ConstraintException;
|
||||||
import com.zsc.edu.gateway.exception.OutlineException;
|
import com.zsc.edu.gateway.exception.OutlineException;
|
||||||
@ -159,24 +157,30 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询设备列表
|
||||||
|
*
|
||||||
|
* @param query
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public IPage<Device> query(Page<Device> page, DeviceQuery query) {
|
public List<Device> list(DeviceQuery query) {
|
||||||
IPage<Device> devicePage = this.page(page, query.wrapper());
|
List<Device> devices = baseMapper.selectList(query.wrapper());
|
||||||
List<Long> productIds = devicePage.getRecords().stream()
|
Set<Long> productIds = devices.stream()
|
||||||
.map(Device::getProductId)
|
.map(Device::getProductId)
|
||||||
.filter(Objects::nonNull)
|
.collect(Collectors.toSet());
|
||||||
.distinct()
|
Map<Long, String> productMap = productRepo.selectByIds(productIds).stream()
|
||||||
.collect(Collectors.toList());
|
|
||||||
LambdaQueryWrapper<Product> productQueryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
productQueryWrapper.in(Product::getId, productIds);
|
|
||||||
List<Product> products = productRepo.selectList(productQueryWrapper);
|
|
||||||
Map<Long, String> productMap = products.stream()
|
|
||||||
.collect(Collectors.toMap(Product::getId, Product::getName));
|
.collect(Collectors.toMap(Product::getId, Product::getName));
|
||||||
devicePage.getRecords().forEach(device -> {
|
return devices.stream()
|
||||||
if (device.getProductId() != null) {
|
.peek(device -> device.setProductName(productMap.getOrDefault(device.getProductId(), null)))
|
||||||
device.setProductName(productMap.get(device.getProductId()));
|
.filter(device -> query.getProductName() == null || query.getProductName().equals(device.getProductName()))
|
||||||
}
|
.collect(Collectors.toList());
|
||||||
});
|
|
||||||
return devicePage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user