mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 23:23:55 +08:00
优化代码
This commit is contained in:
+19
-13
@@ -39,6 +39,7 @@ import xiaozhi.modules.agent.dto.AgentChatSessionDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentCreateDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentMemoryDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentSearchDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentUpdateDTO;
|
||||
import xiaozhi.modules.agent.entity.AgentEntity;
|
||||
import xiaozhi.modules.agent.entity.AgentTemplateEntity;
|
||||
@@ -73,10 +74,24 @@ public class AgentController {
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "获取用户智能体列表")
|
||||
@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();
|
||||
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")
|
||||
@@ -271,15 +286,6 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -46,7 +46,4 @@ public class AgentDTO {
|
||||
|
||||
@Schema(description = "设备数量", example = "10")
|
||||
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.modules.agent.dto.AgentCreateDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentSearchDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentUpdateDTO;
|
||||
import xiaozhi.modules.agent.entity.AgentEntity;
|
||||
import xiaozhi.modules.agent.vo.AgentInfoVO;
|
||||
@@ -102,10 +103,8 @@ public interface AgentService extends BaseService<AgentEntity> {
|
||||
/**
|
||||
* 搜索智能体
|
||||
*
|
||||
* @param keyword 搜索关键词
|
||||
* @param searchType 搜索类型:name(按名称搜索)或mac(按MAC地址搜索)
|
||||
* @param userId 用户ID
|
||||
* @param searchDTO 搜索条件DTO
|
||||
* @return 智能体列表
|
||||
*/
|
||||
List<AgentDTO> searchAgent(String keyword, String searchType, Long userId);
|
||||
List<AgentDTO> searchAgent(AgentSearchDTO searchDTO);
|
||||
}
|
||||
|
||||
+17
-31
@@ -31,6 +31,7 @@ import xiaozhi.common.utils.JsonUtils;
|
||||
import xiaozhi.modules.agent.dao.AgentDao;
|
||||
import xiaozhi.modules.agent.dto.AgentCreateDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentSearchDTO;
|
||||
import xiaozhi.modules.agent.dto.AgentUpdateDTO;
|
||||
import xiaozhi.modules.agent.entity.AgentContextProviderEntity;
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 查询上下文源配置
|
||||
AgentContextProviderEntity contextProviderEntity = agentContextProviderService.getByAgentId(id);
|
||||
if (contextProviderEntity != null) {
|
||||
agent.setContextProviders(contextProviderEntity.getContextProviders());
|
||||
}
|
||||
|
||||
|
||||
// 无需额外查询插件列表,已通过SQL查询出来
|
||||
return agent;
|
||||
}
|
||||
@@ -158,15 +159,7 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
|
||||
|
||||
// 获取设备数量
|
||||
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;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
@@ -480,42 +473,43 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AgentDTO> searchAgent(String keyword, String searchType, Long userId) {
|
||||
if (StringUtils.isBlank(keyword)) {
|
||||
public List<AgentDTO> searchAgent(AgentSearchDTO searchDTO) {
|
||||
if (StringUtils.isBlank(searchDTO.getKeyword())) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
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地址搜索:先搜索设备,再获取对应的智能体
|
||||
List<DeviceEntity> devices = deviceService.searchDevicesByMacAddress(keyword, userId);
|
||||
|
||||
List<DeviceEntity> devices = deviceService.searchDevicesByMacAddress(searchDTO.getKeyword(),
|
||||
searchDTO.getUserId());
|
||||
|
||||
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);
|
||||
queryWrapper.like("agent_name", searchDTO.getKeyword());
|
||||
}
|
||||
|
||||
// 执行查询
|
||||
List<AgentEntity> agentEntities = baseDao.selectList(queryWrapper);
|
||||
|
||||
|
||||
// 转换为DTO并设置所有必要字段
|
||||
return agentEntities.stream().map(agent -> {
|
||||
AgentDTO dto = new AgentDTO();
|
||||
@@ -543,15 +537,7 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
|
||||
|
||||
// 获取设备数量
|
||||
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;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user