mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-28 19:43:53 +08:00
简化代码
This commit is contained in:
+3
-14
@@ -39,7 +39,6 @@ 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;
|
||||||
@@ -79,19 +78,9 @@ public class AgentController {
|
|||||||
@RequestParam(value = "searchType", defaultValue = "name") String searchType) {
|
@RequestParam(value = "searchType", defaultValue = "name") String searchType) {
|
||||||
UserDetail user = SecurityUser.getUser();
|
UserDetail user = SecurityUser.getUser();
|
||||||
|
|
||||||
// 如果有搜索关键词,则使用搜索功能
|
// 直接调用整合后的getUserAgents方法,无需再区分搜索和普通查询
|
||||||
if (StringUtils.isNotBlank(keyword)) {
|
List<AgentDTO> agents = agentService.getUserAgents(user.getId(), keyword, searchType);
|
||||||
AgentSearchDTO searchDTO = new AgentSearchDTO();
|
return new Result<List<AgentDTO>>().ok(agents);
|
||||||
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")
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
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,7 +7,6 @@ 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;
|
||||||
@@ -55,9 +54,11 @@ public interface AgentService extends BaseService<AgentEntity> {
|
|||||||
* 获取用户智能体列表
|
* 获取用户智能体列表
|
||||||
*
|
*
|
||||||
* @param userId 用户ID
|
* @param userId 用户ID
|
||||||
|
* @param keyword 搜索关键词
|
||||||
|
* @param searchType 搜索类型(name - 按名称搜索,mac - 按MAC地址搜索)
|
||||||
* @return 智能体列表
|
* @return 智能体列表
|
||||||
*/
|
*/
|
||||||
List<AgentDTO> getUserAgents(Long userId);
|
List<AgentDTO> getUserAgents(Long userId, String keyword, String searchType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据智能体ID获取设备数量
|
* 根据智能体ID获取设备数量
|
||||||
@@ -100,11 +101,5 @@ public interface AgentService extends BaseService<AgentEntity> {
|
|||||||
*/
|
*/
|
||||||
String createAgent(AgentCreateDTO dto);
|
String createAgent(AgentCreateDTO dto);
|
||||||
|
|
||||||
/**
|
|
||||||
* 搜索智能体
|
|
||||||
*
|
|
||||||
* @param searchDTO 搜索条件DTO
|
|
||||||
* @return 智能体列表
|
|
||||||
*/
|
|
||||||
List<AgentDTO> searchAgent(AgentSearchDTO searchDTO);
|
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-47
@@ -31,7 +31,6 @@ 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;
|
||||||
@@ -129,11 +128,42 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<AgentDTO> getUserAgents(Long userId) {
|
public List<AgentDTO> getUserAgents(Long userId, String keyword, String searchType) {
|
||||||
QueryWrapper<AgentEntity> wrapper = new QueryWrapper<>();
|
QueryWrapper<AgentEntity> queryWrapper = new QueryWrapper<>();
|
||||||
wrapper.eq("user_id", userId);
|
queryWrapper.eq("user_id", userId);
|
||||||
List<AgentEntity> agents = agentDao.selectList(wrapper);
|
|
||||||
return agents.stream().map(this::buildAgentDTO).collect(Collectors.toList());
|
// 如果有搜索关键词,根据搜索类型添加相应的查询条件
|
||||||
|
if (StringUtils.isNotBlank(keyword)) {
|
||||||
|
if ("mac".equals(searchType)) {
|
||||||
|
// 按MAC地址搜索:先搜索设备,再获取对应的智能体
|
||||||
|
List<DeviceEntity> devices = deviceService.searchDevicesByMacAddress(keyword, userId);
|
||||||
|
|
||||||
|
if (!devices.isEmpty()) {
|
||||||
|
// 获取设备对应的智能体ID列表
|
||||||
|
List<String> agentIds = devices.stream()
|
||||||
|
.map(DeviceEntity::getAgentId)
|
||||||
|
.distinct()
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
if (!agentIds.isEmpty()) {
|
||||||
|
queryWrapper.in("id", agentIds);
|
||||||
|
} else {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 按名称搜索
|
||||||
|
queryWrapper.like("agent_name", keyword);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行查询
|
||||||
|
List<AgentEntity> agentEntities = baseDao.selectList(queryWrapper);
|
||||||
|
|
||||||
|
// 转换为DTO并设置所有必要字段
|
||||||
|
return agentEntities.stream().map(this::buildAgentDTO).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -477,45 +507,4 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
|
|||||||
return entity.getId();
|
return entity.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<AgentDTO> searchAgent(AgentSearchDTO searchDTO) {
|
|
||||||
if (StringUtils.isBlank(searchDTO.getKeyword())) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
QueryWrapper<AgentEntity> queryWrapper = new QueryWrapper<>();
|
|
||||||
queryWrapper.eq("user_id", searchDTO.getUserId());
|
|
||||||
|
|
||||||
if ("mac".equals(searchDTO.getSearchType())) {
|
|
||||||
// 按MAC地址搜索:先搜索设备,再获取对应的智能体
|
|
||||||
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", searchDTO.getKeyword());
|
|
||||||
}
|
|
||||||
|
|
||||||
// 执行查询
|
|
||||||
List<AgentEntity> agentEntities = baseDao.selectList(queryWrapper);
|
|
||||||
|
|
||||||
// 转换为DTO并设置所有必要字段
|
|
||||||
return agentEntities.stream().map(this::buildAgentDTO).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user