refactor(iot): 重构设备和产品查询接口
- 修改设备查询接口,使用分页查询替代全量查询 - 优化设备查询结果的产品名称填充逻辑 - 修改产品查询接口,支持模糊查询返回最多五条数据
This commit is contained in:
parent
0170600ff7
commit
b6c66f7157
@ -68,8 +68,8 @@ public class DeviceController {
|
|||||||
@DataPermission
|
@DataPermission
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@PreAuthorize("hasAuthority('iot:device:query')")
|
@PreAuthorize("hasAuthority('iot:device:query')")
|
||||||
public List<Device> query(DeviceQuery query) {
|
public IPage<Device> query(Page<Device> page, DeviceQuery query) {
|
||||||
return service.list(query);
|
return service.query(page, query);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,5 +26,5 @@ public interface DeviceService extends IService<Device> {
|
|||||||
|
|
||||||
Boolean serve(DeviceServeDto dto);
|
Boolean serve(DeviceServeDto dto);
|
||||||
|
|
||||||
List<Device> list(DeviceQuery query);
|
IPage<Device> query(Page<Device> page, DeviceQuery query);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
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,28 +161,27 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询设备列表
|
* 查询设备列表
|
||||||
*
|
|
||||||
* @param query
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<Device> list(DeviceQuery query) {
|
public IPage<Device> query(Page<Device> page, DeviceQuery query) {
|
||||||
List<Device> devices = baseMapper.selectList(query.wrapper());
|
IPage<Device> devicePage = this.page(page, query.wrapper());
|
||||||
Set<Long> productIds = devices.stream()
|
List<Long> productIds = devicePage.getRecords().stream()
|
||||||
.map(Device::getProductId)
|
.map(Device::getProductId)
|
||||||
.collect(Collectors.toSet());
|
.filter(Objects::nonNull)
|
||||||
Map<Long, String> productMap = productRepo.selectByIds(productIds).stream()
|
.distinct()
|
||||||
.collect(Collectors.toMap(Product::getId, Product::getName));
|
|
||||||
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());
|
.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));
|
||||||
|
devicePage.getRecords().forEach(device -> {
|
||||||
|
if (device.getProductId() != null) {
|
||||||
|
device.setProductName(productMap.get(device.getProductId()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return devicePage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhuang
|
* @author zhuang
|
||||||
@ -74,8 +75,8 @@ public class ProductController {
|
|||||||
*/
|
*/
|
||||||
@GetMapping("fuzzy")
|
@GetMapping("fuzzy")
|
||||||
@PreAuthorize("hasAuthority('iot:product:query')")
|
@PreAuthorize("hasAuthority('iot:product:query')")
|
||||||
public Page<Product> fuzzyQuery(Page<Product> page, ProductQuery query) {
|
public List<Product> list(ProductQuery query) {
|
||||||
return service.page(page, query.fuzzyWrapper());
|
return service.list(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,6 +7,8 @@ import com.zsc.edu.gateway.modules.iot.product.dto.ProductDto;
|
|||||||
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
|
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
|
||||||
import com.zsc.edu.gateway.modules.iot.product.query.ProductQuery;
|
import com.zsc.edu.gateway.modules.iot.product.query.ProductQuery;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhuang
|
* @author zhuang
|
||||||
*/
|
*/
|
||||||
@ -18,4 +20,6 @@ public interface ProductService extends IService<Product> {
|
|||||||
Product detail(Long id);
|
Product detail(Long id);
|
||||||
|
|
||||||
boolean delete(Long id);
|
boolean delete(Long id);
|
||||||
|
|
||||||
|
List<Product> list(ProductQuery query);
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import com.zsc.edu.gateway.exception.ConstraintException;
|
|||||||
import com.zsc.edu.gateway.modules.iot.product.dto.ProductDto;
|
import com.zsc.edu.gateway.modules.iot.product.dto.ProductDto;
|
||||||
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
|
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
|
||||||
import com.zsc.edu.gateway.modules.iot.product.mapper.ProductMapper;
|
import com.zsc.edu.gateway.modules.iot.product.mapper.ProductMapper;
|
||||||
|
import com.zsc.edu.gateway.modules.iot.product.query.ProductQuery;
|
||||||
import com.zsc.edu.gateway.modules.iot.product.repo.ProductRepository;
|
import com.zsc.edu.gateway.modules.iot.product.repo.ProductRepository;
|
||||||
import com.zsc.edu.gateway.modules.iot.product.service.ProductService;
|
import com.zsc.edu.gateway.modules.iot.product.service.ProductService;
|
||||||
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
|
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
|
||||||
@ -13,6 +14,8 @@ import lombok.AllArgsConstructor;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaung
|
* @author zhaung
|
||||||
@ -77,4 +80,14 @@ public class ProductServiceImpl extends ServiceImpl<ProductRepository, Product>
|
|||||||
paramService.delete(id);
|
paramService.delete(id);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模糊返回五条数据
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<Product> list(ProductQuery query) {
|
||||||
|
List<Product> products = baseMapper.selectList(query.fuzzyWrapper());
|
||||||
|
return products.subList(0, Math.min(5, products.size()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user