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
|
||||
@GetMapping
|
||||
@PreAuthorize("hasAuthority('iot:device:query')")
|
||||
public IPage<Device> query(Page<Device> page, DeviceQuery query) {
|
||||
return service.query(page, query);
|
||||
public List<Device> query(DeviceQuery 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.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.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
@ -16,6 +19,8 @@ import java.util.Objects;
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class DeviceQuery {
|
||||
@Resource
|
||||
private ProductRepository productRepository;
|
||||
/**
|
||||
* 设备名称
|
||||
*/
|
||||
@ -38,7 +43,10 @@ public class DeviceQuery {
|
||||
* 设态是否在线
|
||||
*/
|
||||
public Boolean isOnline;
|
||||
|
||||
/**
|
||||
* 产品名称
|
||||
*/
|
||||
public String productName;
|
||||
|
||||
public LambdaQueryWrapper<Device> wrapper() {
|
||||
LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
|
||||
|
@ -26,5 +26,5 @@ public interface DeviceService extends IService<Device> {
|
||||
|
||||
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;
|
||||
|
||||
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.zsc.edu.gateway.exception.ConstraintException;
|
||||
import com.zsc.edu.gateway.exception.OutlineException;
|
||||
@ -159,24 +157,30 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询设备列表
|
||||
*
|
||||
* @param query
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public IPage<Device> query(Page<Device> page, DeviceQuery query) {
|
||||
IPage<Device> devicePage = this.page(page, query.wrapper());
|
||||
List<Long> productIds = devicePage.getRecords().stream()
|
||||
public List<Device> list(DeviceQuery query) {
|
||||
List<Device> devices = baseMapper.selectList(query.wrapper());
|
||||
Set<Long> productIds = devices.stream()
|
||||
.map(Device::getProductId)
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.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.toSet());
|
||||
Map<Long, String> productMap = productRepo.selectByIds(productIds).stream()
|
||||
.collect(Collectors.toMap(Product::getId, Product::getName));
|
||||
devicePage.getRecords().forEach(device -> {
|
||||
if (device.getProductId() != null) {
|
||||
device.setProductName(productMap.get(device.getProductId()));
|
||||
}
|
||||
});
|
||||
return devicePage;
|
||||
return devices.stream()
|
||||
.peek(device -> device.setProductName(productMap.getOrDefault(device.getProductId(), null)))
|
||||
.filter(device -> query.getProductName() == null || query.getProductName().equals(device.getProductName()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user