package com.ard.agent.tool;
|
|
|
import com.ard.common.core.constant.SecurityConstants;
|
import com.ard.common.core.domain.R;
|
import com.ard.system.api.RemoteDeptService;
|
import com.ard.system.api.RemoteUserService;
|
import com.ard.system.api.domain.SysDept;
|
import com.ard.system.api.domain.SysUser;
|
import com.ard.work.api.domian.ArdCamera;
|
import jakarta.annotation.Resource;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.ai.tool.annotation.Tool;
|
import org.springframework.ai.tool.annotation.ToolParam;
|
import org.springframework.stereotype.Component;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
@Component
|
@Slf4j
|
public class SystemTools {
|
|
@Resource
|
private RemoteUserService userService;
|
|
@Resource
|
private RemoteDeptService deptService;
|
|
|
|
/**
|
* 搜索用户信息
|
*/
|
@Tool(description = """
|
检索系统用户信息。根据用户名、手机号、真实姓名等条件查询用户。
|
返回用户的详细信息,包括所属部门、角色等。
|
适用场景:
|
- 查询某个用户的基本信息
|
- 根据手机号查找用户
|
- 列出某部门下的所有用户
|
""")
|
public String searchUsers(
|
@ToolParam(description = "真实姓名(模糊匹配)", required = false) String nickName,
|
@ToolParam(description = "用户名(模糊匹配)", required = false) String userName,
|
@ToolParam(description = "手机号(模糊匹配)", required = false) String phone,
|
@ToolParam(description = "部门名称(模糊匹配)", required = false) String deptName,
|
@ToolParam(description = "最大返回数量,默认10") Integer limit) {
|
|
log.info("AI调用searchUsers工具,参数: nickName={}, userName={}, phone={}, deptName={}, limit={}",
|
nickName, userName, phone, deptName, limit);
|
|
if (limit == null) limit = 10;
|
|
// 构造查询条件
|
SysUser query = new SysUser();
|
if (userName != null && !userName.isEmpty()) {
|
query.setUserName(userName);
|
}
|
if (phone != null && !phone.isEmpty()) {
|
query.setPhonenumber(phone);
|
}
|
if (deptName != null && !deptName.isEmpty()) {
|
SysDept dept = new SysDept();
|
dept.setDeptName(deptName);
|
query.setDept(dept);
|
}
|
|
|
// 调用若依的 Service 查询
|
R<List<SysUser>> result = userService.list(query, SecurityConstants.INNER);
|
if (result == null) {
|
return "错误:服务返回空结果";
|
}
|
|
if (result.getCode() != R.SUCCESS) {
|
return String.format("获取用户列表失败:%s", result.getMsg());
|
}
|
|
List<SysUser> users = result.getData();
|
// 限制返回数量
|
if (users.size() > limit) {
|
users = users.subList(0, limit);
|
}
|
|
if (users.isEmpty()) {
|
return "未找到匹配的用户";
|
}
|
|
// 格式化返回结果
|
return users.stream()
|
.map(user -> String.format("""
|
用户ID: %s
|
用户名: %s
|
真实姓名: %s
|
手机号: %s
|
邮箱: %s
|
部门: %s
|
状态: %s
|
创建时间: %s
|
""",
|
user.getUserId(),
|
user.getUserName(),
|
user.getNickName(),
|
maskPhone(user.getPhonenumber()),
|
user.getEmail(),
|
user.getDept() != null ? user.getDept().getDeptName() : "未分配",
|
"0".equals(user.getStatus()) ? "正常" : "禁用",
|
user.getCreateTime()))
|
.collect(Collectors.joining("\n---\n"));
|
}
|
|
/**
|
* 搜索部门信息
|
*/
|
@Tool(description = """
|
检索系统部门信息。根据部门名称或部门ID查询部门详情。
|
返回部门的层级结构、负责人、状态等信息。
|
适用场景:
|
- 查询某个部门的详细信息
|
- 列出所有部门
|
- 查询部门的层级关系
|
""")
|
public String searchDepts(
|
@ToolParam(description = "部门名称(支持模糊搜索),留空则返回所有部门") String deptName,
|
@ToolParam(description = "最大返回数量,默认20") Integer limit) {
|
|
log.info("AI调用searchDepts工具,参数: deptName={}, limit={}", deptName, limit);
|
|
if (limit == null) limit = 20;
|
|
SysDept query = new SysDept();
|
if (deptName != null && !deptName.isEmpty()) {
|
query.setDeptName(deptName);
|
}
|
|
R<List<SysDept>> result = deptService.list(query, SecurityConstants.INNER);
|
if (result == null) {
|
return "错误:服务返回空结果";
|
}
|
|
if (result.getCode() != R.SUCCESS) {
|
return String.format("获取部门列表失败:%s", result.getMsg());
|
}
|
|
List<SysDept> depts = result.getData();
|
if (depts.size() > limit) {
|
depts = depts.subList(0, limit);
|
}
|
|
if (depts.isEmpty()) {
|
return "未找到匹配的部门";
|
}
|
|
return depts.stream()
|
.map(dept -> String.format("""
|
部门ID: %s
|
部门名称: %s
|
父部门ID: %s
|
负责人: %s
|
联系电话: %s
|
邮箱: %s
|
状态: %s
|
""",
|
dept.getDeptId(),
|
dept.getDeptName(),
|
dept.getParentId(),
|
dept.getLeader(),
|
dept.getPhone(),
|
dept.getEmail(),
|
"0".equals(dept.getStatus()) ? "正常" : "停用"))
|
.collect(Collectors.joining("\n---\n"));
|
}
|
|
|
/**
|
* 手机号脱敏
|
*/
|
private String maskPhone(String phone) {
|
if (phone == null || phone.length() < 7) {
|
return phone;
|
}
|
return phone.substring(0, 3) + "****" + phone.substring(7);
|
}
|
|
}
|