调整搜索实现-从前端过滤改为后端查询

This commit is contained in:
LJH-rgsze
2026-01-05 10:10:17 +08:00
parent 9c111b2d5e
commit e25ca3d1d3
8 changed files with 118 additions and 34 deletions
@@ -271,4 +271,15 @@ public class AgentController {
.body(audioData);
}
@GetMapping("/search")
@Operation(summary = "搜索智能体")
@RequiresPermissions("sys:role:normal")
public Result<List<AgentDTO>> searchAgent(
@RequestParam("keyword") String keyword,
@RequestParam(value = "searchType", defaultValue = "name") String searchType) {
UserDetail user = SecurityUser.getUser();
List<AgentDTO> agents = agentService.searchAgent(keyword, searchType, user.getId());
return new Result<List<AgentDTO>>().ok(agents);
}
}
@@ -98,4 +98,14 @@ public interface AgentService extends BaseService<AgentEntity> {
* @return 创建的智能体ID
*/
String createAgent(AgentCreateDTO dto);
/**
* 搜索智能体
*
* @param keyword 搜索关键词
* @param searchType 搜索类型:name(按名称搜索)或mac(按MAC地址搜索)
* @param userId 用户ID
* @return 智能体列表
*/
List<AgentDTO> searchAgent(String keyword, String searchType, Long userId);
}
@@ -478,4 +478,43 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
agentPluginMappingService.saveBatch(toInsert);
return entity.getId();
}
@Override
public List<AgentDTO> searchAgent(String keyword, String searchType, Long userId) {
if (StringUtils.isBlank(keyword)) {
return new ArrayList<>();
}
QueryWrapper<AgentEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId);
if ("mac".equals(searchType)) {
// 按MAC地址搜索:先搜索设备,再获取对应的智能体
List<DeviceEntity> devices = deviceService.searchDevicesByMacAddress(keyword, userId);
if (devices.isEmpty()) {
return new ArrayList<>();
}
// 获取设备对应的智能体ID列表
List<String> agentIds = devices.stream()
.map(DeviceEntity::getAgentId)
.distinct()
.collect(Collectors.toList());
if (agentIds.isEmpty()) {
return new ArrayList<>();
}
// 查询智能体
queryWrapper.in("id", agentIds);
} else {
// 按名称搜索
queryWrapper.like("agent_name", keyword);
}
// 执行查询
List<AgentEntity> agentEntities = baseDao.selectList(queryWrapper);
return ConvertUtils.sourceToTarget(agentEntities, AgentDTO.class);
}
}
@@ -108,4 +108,13 @@ public interface DeviceService extends BaseService<DeviceEntity> {
*/
String generateWebSocketToken(String clientId, String username) throws Exception;
/**
* 根据MAC地址搜索设备
*
* @param macAddress MAC地址关键词
* @param userId 用户ID
* @return 设备列表
*/
List<DeviceEntity> searchDevicesByMacAddress(String macAddress, Long userId);
}
@@ -496,6 +496,14 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
redisUtils.delete(RedisKeys.getAgentDeviceCountById(dto.getAgentId()));
}
@Override
public List<DeviceEntity> searchDevicesByMacAddress(String macAddress, Long userId) {
QueryWrapper<DeviceEntity> wrapper = new QueryWrapper<>();
wrapper.like("mac_address", macAddress);
wrapper.eq("user_id", userId);
return deviceDao.selectList(wrapper);
}
/**
* 生成MQTT密码签名
*