dvc890 commited on
Commit
2473642
·
verified ·
1 Parent(s): 6386c92

Upload 64 files

Browse files
Files changed (1) hide show
  1. ai-tools.js +26 -1
ai-tools.js CHANGED
@@ -182,7 +182,7 @@ async function executeMongoTool(functionCall, user, role, schoolId) {
182
  // 2. 🛡️ 安全注入
183
  let safeFilter = injectSecurityFilter(normalizedFilter, user, role, schoolId);
184
 
185
- // 3. 🧠 智能模糊处理 (Value Normalization) - 关键修改
186
  safeFilter = buildFuzzyQuery(safeFilter);
187
 
188
  const safeLimit = Math.min(Math.max(limit, 1), 20);
@@ -199,6 +199,31 @@ async function executeMongoTool(functionCall, user, role, schoolId) {
199
  let result = [];
200
  let fields = "";
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  switch (collection) {
203
  case "Student":
204
  fields = "name studentNo className gender flowerBalance seatNo -_id";
 
182
  // 2. 🛡️ 安全注入
183
  let safeFilter = injectSecurityFilter(normalizedFilter, user, role, schoolId);
184
 
185
+ // 3. 🧠 智能模糊处理 (Value Normalization)
186
  safeFilter = buildFuzzyQuery(safeFilter);
187
 
188
  const safeLimit = Math.min(Math.max(limit, 1), 20);
 
199
  let result = [];
200
  let fields = "";
201
 
202
+ // 🔥 关系查询增强:Score 表没有 className,需要先查 Student 获取 studentNo
203
+ if (collection === 'Score' && safeFilter.className) {
204
+ console.log(`🔄 [Relational] Score query detected className filter. Searching Students first...`);
205
+
206
+ // 构造 Student 查询条件
207
+ const studentQuery = {
208
+ schoolId: safeFilter.schoolId,
209
+ className: safeFilter.className // 这里已经是正则了
210
+ };
211
+
212
+ const studentsInClass = await Student.find(studentQuery).select('studentNo');
213
+ const studentNos = studentsInClass.map(s => s.studentNo);
214
+
215
+ if (studentNos.length === 0) {
216
+ console.log(`⚠️ [Relational] No students found in class: ${safeFilter.className}`);
217
+ return { info: `未找到班级符合条件的学生,无法查询成绩。` };
218
+ }
219
+
220
+ console.log(`✅ [Relational] Found ${studentNos.length} students. Translating to Score query...`);
221
+
222
+ // 替换查询条件:移除 className,添加 studentNo $in
223
+ delete safeFilter.className;
224
+ safeFilter.studentNo = { $in: studentNos };
225
+ }
226
+
227
  switch (collection) {
228
  case "Student":
229
  fields = "name studentNo className gender flowerBalance seatNo -_id";