优化代码

This commit is contained in:
LJH-rgsze
2026-01-05 14:50:23 +08:00
parent d003e66310
commit e9373b59c8
6 changed files with 57 additions and 52 deletions
@@ -39,6 +39,7 @@ import xiaozhi.modules.agent.dto.AgentChatSessionDTO;
import xiaozhi.modules.agent.dto.AgentCreateDTO; import xiaozhi.modules.agent.dto.AgentCreateDTO;
import xiaozhi.modules.agent.dto.AgentDTO; import xiaozhi.modules.agent.dto.AgentDTO;
import xiaozhi.modules.agent.dto.AgentMemoryDTO; import xiaozhi.modules.agent.dto.AgentMemoryDTO;
import xiaozhi.modules.agent.dto.AgentSearchDTO;
import xiaozhi.modules.agent.dto.AgentUpdateDTO; import xiaozhi.modules.agent.dto.AgentUpdateDTO;
import xiaozhi.modules.agent.entity.AgentEntity; import xiaozhi.modules.agent.entity.AgentEntity;
import xiaozhi.modules.agent.entity.AgentTemplateEntity; import xiaozhi.modules.agent.entity.AgentTemplateEntity;
@@ -73,10 +74,24 @@ public class AgentController {
@GetMapping("/list") @GetMapping("/list")
@Operation(summary = "获取用户智能体列表") @Operation(summary = "获取用户智能体列表")
@RequiresPermissions("sys:role:normal") @RequiresPermissions("sys:role:normal")
public Result<List<AgentDTO>> getUserAgents() { public Result<List<AgentDTO>> getUserAgents(
@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "searchType", defaultValue = "name") String searchType) {
UserDetail user = SecurityUser.getUser(); UserDetail user = SecurityUser.getUser();
List<AgentDTO> agents = agentService.getUserAgents(user.getId());
return new Result<List<AgentDTO>>().ok(agents); // 如果有搜索关键词,则使用搜索功能
if (StringUtils.isNotBlank(keyword)) {
AgentSearchDTO searchDTO = new AgentSearchDTO();
searchDTO.setKeyword(keyword);
searchDTO.setSearchType(searchType);
searchDTO.setUserId(user.getId());
List<AgentDTO> agents = agentService.searchAgent(searchDTO);
return new Result<List<AgentDTO>>().ok(agents);
} else {
// 否则返回所有智能体
List<AgentDTO> agents = agentService.getUserAgents(user.getId());
return new Result<List<AgentDTO>>().ok(agents);
}
} }
@GetMapping("/all") @GetMapping("/all")
@@ -271,15 +286,6 @@ public class AgentController {
.body(audioData); .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);
}
} }
@@ -46,7 +46,4 @@ public class AgentDTO {
@Schema(description = "设备数量", example = "10") @Schema(description = "设备数量", example = "10")
private Integer deviceCount; private Integer deviceCount;
@Schema(description = "关联设备的MAC地址列表", example = "[\"00:11:22:33:44:55\", \"66:77:88:99:AA:BB\"]")
private List<String> macAddresses;
} }
@@ -0,0 +1,17 @@
package xiaozhi.modules.agent.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
@Schema(description = "智能体搜索查询DTO")
public class AgentSearchDTO {
@Schema(description = "搜索关键词")
private String keyword;
@Schema(description = "搜索类型: mac - 按MAC地址搜索, name - 按名称搜索")
private String searchType;
@Schema(description = "用户ID")
private Long userId;
}
@@ -7,6 +7,7 @@ import xiaozhi.common.page.PageData;
import xiaozhi.common.service.BaseService; import xiaozhi.common.service.BaseService;
import xiaozhi.modules.agent.dto.AgentCreateDTO; import xiaozhi.modules.agent.dto.AgentCreateDTO;
import xiaozhi.modules.agent.dto.AgentDTO; import xiaozhi.modules.agent.dto.AgentDTO;
import xiaozhi.modules.agent.dto.AgentSearchDTO;
import xiaozhi.modules.agent.dto.AgentUpdateDTO; import xiaozhi.modules.agent.dto.AgentUpdateDTO;
import xiaozhi.modules.agent.entity.AgentEntity; import xiaozhi.modules.agent.entity.AgentEntity;
import xiaozhi.modules.agent.vo.AgentInfoVO; import xiaozhi.modules.agent.vo.AgentInfoVO;
@@ -102,10 +103,8 @@ public interface AgentService extends BaseService<AgentEntity> {
/** /**
* 搜索智能体 * 搜索智能体
* *
* @param keyword 搜索关键词 * @param searchDTO 搜索条件DTO
* @param searchType 搜索类型:name(按名称搜索)或mac(按MAC地址搜索)
* @param userId 用户ID
* @return 智能体列表 * @return 智能体列表
*/ */
List<AgentDTO> searchAgent(String keyword, String searchType, Long userId); List<AgentDTO> searchAgent(AgentSearchDTO searchDTO);
} }
@@ -31,6 +31,7 @@ import xiaozhi.common.utils.JsonUtils;
import xiaozhi.modules.agent.dao.AgentDao; import xiaozhi.modules.agent.dao.AgentDao;
import xiaozhi.modules.agent.dto.AgentCreateDTO; import xiaozhi.modules.agent.dto.AgentCreateDTO;
import xiaozhi.modules.agent.dto.AgentDTO; import xiaozhi.modules.agent.dto.AgentDTO;
import xiaozhi.modules.agent.dto.AgentSearchDTO;
import xiaozhi.modules.agent.dto.AgentUpdateDTO; import xiaozhi.modules.agent.dto.AgentUpdateDTO;
import xiaozhi.modules.agent.entity.AgentContextProviderEntity; import xiaozhi.modules.agent.entity.AgentContextProviderEntity;
import xiaozhi.modules.agent.entity.AgentEntity; import xiaozhi.modules.agent.entity.AgentEntity;
@@ -89,13 +90,13 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
agent.setChatHistoryConf(Constant.ChatHistoryConfEnum.RECORD_TEXT_AUDIO.getCode()); agent.setChatHistoryConf(Constant.ChatHistoryConfEnum.RECORD_TEXT_AUDIO.getCode());
} }
} }
// 查询上下文源配置 // 查询上下文源配置
AgentContextProviderEntity contextProviderEntity = agentContextProviderService.getByAgentId(id); AgentContextProviderEntity contextProviderEntity = agentContextProviderService.getByAgentId(id);
if (contextProviderEntity != null) { if (contextProviderEntity != null) {
agent.setContextProviders(contextProviderEntity.getContextProviders()); agent.setContextProviders(contextProviderEntity.getContextProviders());
} }
// 无需额外查询插件列表,已通过SQL查询出来 // 无需额外查询插件列表,已通过SQL查询出来
return agent; return agent;
} }
@@ -158,15 +159,7 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
// 获取设备数量 // 获取设备数量
dto.setDeviceCount(getDeviceCountByAgentId(agent.getId())); dto.setDeviceCount(getDeviceCountByAgentId(agent.getId()));
// 获取关联设备的MAC地址列表
List<DeviceEntity> devices = deviceService.getUserDevices(agent.getUserId(), agent.getId());
List<String> macAddresses = devices.stream()
.map(DeviceEntity::getMacAddress)
.filter(StringUtils::isNotBlank)
.collect(Collectors.toList());
dto.setMacAddresses(macAddresses);
return dto; return dto;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
@@ -480,42 +473,43 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
} }
@Override @Override
public List<AgentDTO> searchAgent(String keyword, String searchType, Long userId) { public List<AgentDTO> searchAgent(AgentSearchDTO searchDTO) {
if (StringUtils.isBlank(keyword)) { if (StringUtils.isBlank(searchDTO.getKeyword())) {
return new ArrayList<>(); return new ArrayList<>();
} }
QueryWrapper<AgentEntity> queryWrapper = new QueryWrapper<>(); QueryWrapper<AgentEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId); queryWrapper.eq("user_id", searchDTO.getUserId());
if ("mac".equals(searchType)) { if ("mac".equals(searchDTO.getSearchType())) {
// 按MAC地址搜索:先搜索设备,再获取对应的智能体 // 按MAC地址搜索:先搜索设备,再获取对应的智能体
List<DeviceEntity> devices = deviceService.searchDevicesByMacAddress(keyword, userId); List<DeviceEntity> devices = deviceService.searchDevicesByMacAddress(searchDTO.getKeyword(),
searchDTO.getUserId());
if (devices.isEmpty()) { if (devices.isEmpty()) {
return new ArrayList<>(); return new ArrayList<>();
} }
// 获取设备对应的智能体ID列表 // 获取设备对应的智能体ID列表
List<String> agentIds = devices.stream() List<String> agentIds = devices.stream()
.map(DeviceEntity::getAgentId) .map(DeviceEntity::getAgentId)
.distinct() .distinct()
.collect(Collectors.toList()); .collect(Collectors.toList());
if (agentIds.isEmpty()) { if (agentIds.isEmpty()) {
return new ArrayList<>(); return new ArrayList<>();
} }
// 查询智能体 // 查询智能体
queryWrapper.in("id", agentIds); queryWrapper.in("id", agentIds);
} else { } else {
// 按名称搜索 // 按名称搜索
queryWrapper.like("agent_name", keyword); queryWrapper.like("agent_name", searchDTO.getKeyword());
} }
// 执行查询 // 执行查询
List<AgentEntity> agentEntities = baseDao.selectList(queryWrapper); List<AgentEntity> agentEntities = baseDao.selectList(queryWrapper);
// 转换为DTO并设置所有必要字段 // 转换为DTO并设置所有必要字段
return agentEntities.stream().map(agent -> { return agentEntities.stream().map(agent -> {
AgentDTO dto = new AgentDTO(); AgentDTO dto = new AgentDTO();
@@ -543,15 +537,7 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
// 获取设备数量 // 获取设备数量
dto.setDeviceCount(getDeviceCountByAgentId(agent.getId())); dto.setDeviceCount(getDeviceCountByAgentId(agent.getId()));
// 获取关联设备的MAC地址列表
List<DeviceEntity> devices = deviceService.getUserDevices(agent.getUserId(), agent.getId());
List<String> macAddresses = devices.stream()
.map(DeviceEntity::getMacAddress)
.filter(StringUtils::isNotBlank)
.collect(Collectors.toList());
dto.setMacAddresses(macAddresses);
return dto; return dto;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
+1 -1
View File
@@ -386,7 +386,7 @@ export default {
// 搜索智能体 // 搜索智能体
searchAgent(keyword, searchType, callback) { searchAgent(keyword, searchType, callback) {
RequestService.sendRequest() RequestService.sendRequest()
.url(`${getServiceUrl()}/agent/search`) .url(`${getServiceUrl()}/agent/list`)
.method('GET') .method('GET')
.data({ .data({
keyword: keyword, keyword: keyword,