feat(taskcenter): 客户端任务管理模块骨架、菜单授权与汇总接口
新增 modules/taskcenter:组范围裁剪(超管直通 / 非超管取本组用户)、 两表 UNION 统计(biz_file_task 12 模块 + brand_crawl_tasks)、设备花名册 (任务设备近 30 天 + 客户端日志设备近 7 天且 uid 非空)。overview 返回 设备总数/在线数/进行中/今日完成/疑似卡住五项。 权限复用既有菜单授权:PermissionMenuService.requireClientTaskCenterAccess (超管直通,非超管要求 admin_task_center 授权,无记录即 403)。 测试:ScopeService 7 项 + Mapper 动态 SQL 渲染 7 项(不连库,守住 XML 实体转义与可见账号占位符数量)。
This commit is contained in:
+145
@@ -0,0 +1,145 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import org.apache.ibatis.mapping.BoundSql;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* 由 MyBatis 真正解析 {@code @Select} 脚本,验证动态 SQL 模板可渲染、范围占位符数量正确。
|
||||
* 不连库:只断言生成的 SQL 文本与参数绑定,防住「脚本写坏到启动期才炸」。
|
||||
*
|
||||
* <p>注意:{@code <} 必须写成 XML 实体、{@code <>} 不能直接出现在脚本里(非法 XML),
|
||||
* 本类同时是这两条约束的回归网。</p>
|
||||
*/
|
||||
class TaskCenterQueryMapperSqlTest {
|
||||
|
||||
private static MybatisConfiguration configuration;
|
||||
|
||||
@BeforeAll
|
||||
static void setUp() {
|
||||
configuration = new MybatisConfiguration();
|
||||
configuration.addMapper(TaskCenterQueryMapper.class);
|
||||
}
|
||||
|
||||
private static BoundSql render(String method, Map<String, Object> params) {
|
||||
MappedStatement statement = configuration.getMappedStatement(
|
||||
TaskCenterQueryMapper.class.getName() + "." + method);
|
||||
return statement.getBoundSql(params);
|
||||
}
|
||||
|
||||
private static Map<String, Object> params(Object... keyValues) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (int i = 0; i < keyValues.length; i += 2) {
|
||||
map.put((String) keyValues[i], keyValues[i + 1]);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/** 折叠空白:脚本渲染会保留缩进与 foreach 内补的空白,断言只看语义,不看排版。 */
|
||||
private static String sqlOf(BoundSql boundSql) {
|
||||
return boundSql.getSql().replaceAll("\\s+", " ").trim()
|
||||
.replaceAll("\\s*\\(\\s*", "(")
|
||||
.replaceAll("\\s*\\)", ")")
|
||||
.replaceAll("\\s*,\\s*", ",");
|
||||
}
|
||||
|
||||
@Test
|
||||
void runningCountWithoutScopeFiltersNothing() {
|
||||
BoundSql boundSql = render("countRunningTasks", params("userIds", null));
|
||||
String sql = sqlOf(boundSql);
|
||||
|
||||
assertTrue(sql.contains("biz_file_task"));
|
||||
assertTrue(sql.contains("brand_crawl_tasks"));
|
||||
assertTrue(sql.contains("AND t.status IN('PENDING','RUNNING','SCHEDULED')"));
|
||||
assertTrue(sql.contains("WHERE b.status IN('pending','running')"), "品牌表状态小写");
|
||||
assertFalse(sql.contains("user_id IN"), "超管不按账号裁剪");
|
||||
assertEquals(0, boundSql.getParameterMappings().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void runningCountScopedBindsEveryVisibleUser() {
|
||||
BoundSql boundSql = render("countRunningTasks", params("userIds", List.of(5L, 11L)));
|
||||
String sql = sqlOf(boundSql);
|
||||
|
||||
assertTrue(sql.contains("AND t.user_id IN(?,?)"));
|
||||
assertTrue(sql.contains("AND b.user_id IN(?,?)"));
|
||||
assertEquals(4, boundSql.getParameterMappings().size(), "两张表各绑一次可见账号集合");
|
||||
assertEquals(5L, boundSql.getAdditionalParameter("__frch_uid_0"), "第一个可见账号");
|
||||
assertEquals(11L, boundSql.getAdditionalParameter("__frch_uid_1"), "第二个可见账号");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stuckCountAppliesIdleThresholdToBothSources() {
|
||||
LocalDateTime idleBefore = LocalDateTime.of(2026, 9, 28, 10, 0);
|
||||
BoundSql boundSql = render("countStuckTasks",
|
||||
params("userIds", List.of(7L), "idleBefore", idleBefore));
|
||||
String sql = sqlOf(boundSql);
|
||||
|
||||
assertTrue(sql.contains("AND t.updated_at < ?"), "卡住判定按 updated_at 阈值");
|
||||
assertTrue(sql.contains("AND b.updated_at < ?"));
|
||||
assertEquals(4, boundSql.getParameterMappings().size(), "阈值在两张表各绑一次 + 两张表各绑一次可见账号");
|
||||
assertEquals(7L, boundSql.getAdditionalParameter("__frch_uid_0"), "可见账号集合生效");
|
||||
}
|
||||
|
||||
@Test
|
||||
void finishedCountUsesFinishedAtWithUpdatedAtFallback() {
|
||||
BoundSql boundSql = render("countFinishedTasksSince",
|
||||
params("userIds", List.of(5L), "since", LocalDateTime.of(2026, 9, 28, 0, 0)));
|
||||
String sql = sqlOf(boundSql);
|
||||
|
||||
assertTrue(sql.contains("AND COALESCE(t.finished_at,t.updated_at) >= ?"),
|
||||
"biz_file_task 优先 finished_at");
|
||||
assertTrue(sql.contains("AND b.updated_at >= ?"), "品牌表无 finished_at 用 updated_at");
|
||||
assertEquals(4, boundSql.getParameterMappings().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rosterExcludesBlankDeviceAndUngroupedLogDevices() {
|
||||
LocalDateTime since = LocalDateTime.of(2026, 9, 1, 0, 0);
|
||||
BoundSql boundSql = render("countRosterDevices",
|
||||
params("userIds", List.of(5L), "taskSince", since, "logSince", since));
|
||||
String sql = sqlOf(boundSql);
|
||||
|
||||
assertTrue(sql.contains("SELECT COUNT(DISTINCT u.device_id)"));
|
||||
assertTrue(sql.contains("d.device_id != ''"), "空设备号不参与");
|
||||
assertTrue(sql.contains("d.uid IS NOT NULL"), "无归属账号的日志设备不显示");
|
||||
assertTrue(sql.contains("d.source = 'client'"), "只看客户端日志设备");
|
||||
assertTrue(sql.contains("AND d.last_upload_at >= ?"), "日志设备回看窗口");
|
||||
assertEquals(6, boundSql.getParameterMappings().size(), "任务窗口×2 + 日志窗口 + 账号集合×3");
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlineCountGroupsByDeviceAndKeepsOnlyFreshOnes() {
|
||||
LocalDateTime since = LocalDateTime.of(2026, 9, 1, 0, 0);
|
||||
BoundSql boundSql = render("countOnlineDevices",
|
||||
params("userIds", null, "taskSince", since, "logSince", since,
|
||||
"onlineSince", LocalDateTime.of(2026, 9, 28, 10, 0)));
|
||||
String sql = sqlOf(boundSql);
|
||||
|
||||
assertTrue(sql.contains("GROUP BY u.device_id"));
|
||||
assertTrue(sql.contains("HAVING MAX(u.activity_at) >= ?"));
|
||||
assertFalse(sql.contains("__frch"), "超管不渲染账号占位符");
|
||||
assertEquals(4, boundSql.getParameterMappings().size(),
|
||||
"任务窗口×2 + 日志窗口 + 在线阈值");
|
||||
}
|
||||
|
||||
@Test
|
||||
void excludedServerSideModulesNeverAppear() {
|
||||
String sql = sqlOf(render("countRunningTasks", params("userIds", null)));
|
||||
|
||||
assertFalse(sql.contains("'DEDUPE'"), "去重为服务端执行,无客户端设备归属");
|
||||
assertFalse(sql.contains("'SPLIT'"));
|
||||
assertFalse(sql.contains("'CONVERT'"));
|
||||
}
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.service;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.mapper.ShopManageGroupMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 客户端任务管理的组范围裁剪:超管直通,管理员 = 本人 + 本组用户 + 自己管理的成员。
|
||||
* 口径与 DedupeTotalDataService.resolveAccessScope 一致。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class TaskCenterScopeServiceTest {
|
||||
|
||||
@Mock private AdminUserMapper adminUserMapper;
|
||||
@Mock private ShopManageGroupMapper shopManageGroupMapper;
|
||||
@InjectMocks private TaskCenterScopeService service;
|
||||
|
||||
private AdminUserEntity user(Long id, String role) {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(id);
|
||||
user.setRole(role);
|
||||
user.setIsAdmin(1);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Test
|
||||
void superAdminSeesAll() {
|
||||
when(adminUserMapper.selectById(1L)).thenReturn(user(1L, "super_admin"));
|
||||
|
||||
TaskCenterScopeService.ScopeContext scope = service.resolve(1L);
|
||||
|
||||
assertTrue(scope.allUsers(), "超管不裁剪");
|
||||
assertTrue(scope.visibleUserIds().isEmpty(), "超管无需可见用户集合");
|
||||
assertTrue(scope.visible(999L), "超管对任意用户可见");
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyAdminWithoutRoleCountsAsSuperAdmin() {
|
||||
AdminUserEntity legacy = new AdminUserEntity();
|
||||
legacy.setId(2L);
|
||||
legacy.setIsAdmin(1);
|
||||
legacy.setCreatedById(null);
|
||||
when(adminUserMapper.selectById(2L)).thenReturn(legacy);
|
||||
|
||||
assertTrue(service.resolve(2L).allUsers(), "历史超管(无 role)直通");
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminGetsSelfPlusGroupUsersPlusManaged() {
|
||||
when(adminUserMapper.selectById(5L)).thenReturn(user(5L, "admin"));
|
||||
when(shopManageGroupMapper.selectAccessibleGroupIds(5L)).thenReturn(List.of(3L));
|
||||
when(shopManageGroupMapper.selectUserIdsByGroupIds(List.of(3L))).thenReturn(List.of(11L, 12L));
|
||||
when(shopManageGroupMapper.selectManagedMemberUserIds(5L)).thenReturn(List.of(13L));
|
||||
|
||||
TaskCenterScopeService.ScopeContext scope = service.resolve(5L);
|
||||
|
||||
assertFalse(scope.allUsers());
|
||||
assertEquals(Set.of(5L, 11L, 12L, 13L), scope.visibleUserIds());
|
||||
assertTrue(scope.visible(11L));
|
||||
assertFalse(scope.visible(99L), "组外用户不可见");
|
||||
assertFalse(scope.visible(null), "空用户号不可见");
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminWithNoGroupHasOnlySelf() {
|
||||
when(adminUserMapper.selectById(5L)).thenReturn(user(5L, "admin"));
|
||||
when(shopManageGroupMapper.selectAccessibleGroupIds(5L)).thenReturn(List.of());
|
||||
when(shopManageGroupMapper.selectManagedMemberUserIds(5L)).thenReturn(List.of());
|
||||
|
||||
TaskCenterScopeService.ScopeContext scope = service.resolve(5L);
|
||||
|
||||
assertEquals(Set.of(5L), scope.visibleUserIds(), "无组无成员时不越权");
|
||||
assertFalse(scope.allUsers());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidAndDuplicateIdsAreDropped() {
|
||||
when(adminUserMapper.selectById(5L)).thenReturn(user(5L, "admin"));
|
||||
when(shopManageGroupMapper.selectAccessibleGroupIds(5L)).thenReturn(List.of(3L));
|
||||
when(shopManageGroupMapper.selectUserIdsByGroupIds(anyList()))
|
||||
.thenReturn(java.util.Arrays.asList(5L, null, 0L, -1L, 11L));
|
||||
when(shopManageGroupMapper.selectManagedMemberUserIds(anyLong())).thenReturn(List.of(11L));
|
||||
|
||||
TaskCenterScopeService.ScopeContext scope = service.resolve(5L);
|
||||
|
||||
assertEquals(Set.of(5L, 11L), scope.visibleUserIds(), "空号/负数丢弃,重复去重");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullMapperResultDegradesToSelf() {
|
||||
lenient().when(adminUserMapper.selectById(5L)).thenReturn(user(5L, "admin"));
|
||||
lenient().when(shopManageGroupMapper.selectAccessibleGroupIds(5L)).thenReturn(List.of(3L));
|
||||
lenient().when(shopManageGroupMapper.selectUserIdsByGroupIds(anyList())).thenReturn(null);
|
||||
lenient().when(shopManageGroupMapper.selectManagedMemberUserIds(5L)).thenReturn(null);
|
||||
|
||||
assertEquals(Set.of(5L), service.resolve(5L).visibleUserIds());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownOperatorRejected() {
|
||||
when(adminUserMapper.selectById(404L)).thenReturn(null);
|
||||
assertThrows(BusinessException.class, () -> service.resolve(404L));
|
||||
assertThrows(BusinessException.class, () -> service.resolve(null));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user