fix(mcp): scope 绑定查找工具时兼容 soft_delete 为 NULL 的历史记录

现象:sync 作用域绑定了 4 个工具,硬件统计/设备日均/换纸明细都通了,
但 district.paper.statistics.query 仍 PERMISSION_DENIED。

原因:工具查找用 .eq(Tool::getSoftDelete, 0),而历史记录该列可能为 NULL
(建表默认 0,早期插入未显式写),于是被判"工具不存在"跳过绑定。

修复:条件改为 soft_delete = 0 OR IS NULL;新增单测覆盖 NULL 场景。
This commit is contained in:
xiaoqu-ai
2026-10-21 12:49:54 +08:00
parent 0d43b6af23
commit b62ba1ca37
2 changed files with 24 additions and 1 deletions
@@ -82,8 +82,12 @@ public class ScopeToolSyncService implements ApplicationRunner {
if (toolName.isEmpty()) {
continue;
}
// soft_delete 在历史数据里可能是 NULL(建表默认 0 但早期插入未写),
// 只按 =0 过滤会把已注册的工具误判为"不存在"而跳过绑定
// (2026-10-21 district.paper.statistics.query 即因此未绑上)。
Tool tool = toolMapper.selectOne(new LambdaQueryWrapper<Tool>()
.eq(Tool::getName, toolName).eq(Tool::getSoftDelete, 0));
.eq(Tool::getName, toolName)
.and(w -> w.eq(Tool::getSoftDelete, 0).or().isNull(Tool::getSoftDelete)));
if (tool == null) {
log.warn("scope-tool binding 跳过:工具不存在 name={}", toolName);
skipped++;
@@ -65,6 +65,25 @@ class ScopeToolSyncServiceTest {
verify(scopeToolMapper, times(1)).insert(any(ScopeTool.class));
}
@Test
void bindsToolWhoseSoftDeleteColumnIsNull() {
service.rawBindings = "sync=tool.c";
AgentScope scope = new AgentScope();
scope.setId(4L);
scope.setCode("sync");
when(scopeMapper.selectOne(any())).thenReturn(scope);
Tool toolC = new Tool();
toolC.setId(12L);
toolC.setName("tool.c");
// softDelete 为 NULL 的历史记录也要能绑上
when(toolMapper.selectOne(any())).thenReturn(toolC);
when(scopeToolMapper.selectCount(any())).thenReturn(0L);
service.run(null);
verify(scopeToolMapper, times(1)).insert(any(ScopeTool.class));
}
@Test
void skipsUnknownScopeWithoutFailing() {
service.rawBindings = "nope=tool.a";