liusuyi
2026-05-18 0b8c8d8986a35c3e36db1503125e2dff79d6d10e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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);
    }
 
}