feat(公司tab): 任务看板五个端点按公司筛(一处 resolve 收窄,detail 靠 scope.visible 同宽)

This commit is contained in:
2026-10-05 19:14:51 +08:00
parent b80addfdb4
commit 965e969076
7 changed files with 348 additions and 35 deletions
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.taskcenter.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.DataReadScope;
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskQuery;
import com.nanri.aiimage.modules.taskcenter.model.vo.TaskCenterOverviewVo;
@@ -13,6 +14,7 @@ import com.nanri.aiimage.modules.taskcenter.service.TaskCenterQueryService;
import com.nanri.aiimage.modules.taskcenter.service.TaskCenterScopeService;
import com.nanri.aiimage.modules.taskcenter.service.TaskCenterVariantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
@@ -29,6 +31,13 @@ import java.util.List;
*
* <p>仅超管默认可见;管理员需超管在「用户管理 → 菜单权限」授权 {@code admin_task_center}
* 后才能访问。数据范围 = 本人 + 本组用户 + 自己管理的成员(超管不裁剪)。</p>
*
* <p>公司 tab(spec §4):五个端点各收一个可选 {@code company_id},都在 handler 体内过唯一出口
* {@link DataReadScope#readScope} 算成员集,再交给 {@code TaskCenterScopeService.resolve} 与既有口径
* 求交——一处收窄,五个出口天然同宽(详情走 {@code scope.visible()},用的就是同一条集合)。
* 那一行之所以在五个 handler 里各写一遍而不是抽进私有方法:护栏
* {@code CompanyIdParamGoesThroughReadScopeTest} 的断言 A/C/D 认的就是「handler 体内」出现过出口、
* 结果被接住当实参传出去、且 {@code company_id} 那个值真进了出口实参列表,抽走即三只都红。</p>
*/
@RestController
@RequiredArgsConstructor
@@ -42,20 +51,35 @@ public class AdminTaskCenterController {
private final TaskCenterScopeService scopeService;
private final TaskCenterQueryService queryService;
private final TaskCenterVariantService variantService;
private final DataReadScope dataReadScope;
@GetMapping("/overview")
@Operation(summary = "汇总卡", description = "总店铺/在跑店铺数、进行中任务数、今日完成任务数、"
+ "疑似卡住数(进行中且 >10 分钟无上报)。")
public ApiResponse<TaskCenterOverviewVo> overview(HttpServletRequest request) {
TaskCenterScopeService.ScopeContext scope = resolveScope(request);
public ApiResponse<TaskCenterOverviewVo> overview(
HttpServletRequest request,
@Parameter(description = "公司 ID(超管 tab;不传=不筛)")
@RequestParam(name = "company_id", required = false) Long companyId) {
AdminUserEntity operator = authorizedOperator(request);
boolean superAdmin = isSuper(operator);
Long operatorId = operator.getId();
List<Long> companyUserIds = superAdmin ? dataReadScope.readScope(operator, companyId).userIdsOr(null) : null;
TaskCenterScopeService.ScopeContext scope = resolveScope(operatorId, companyUserIds);
return ApiResponse.success(queryService.overview(scope));
}
@GetMapping("/shop-options")
@Operation(summary = "店铺筛选候选",
description = "店铺库(biz_shop_manage)在可见范围内的店铺名,供筛选下拉精确匹配;只出店铺名。")
public ApiResponse<List<String>> shopOptions(HttpServletRequest request) {
TaskCenterScopeService.ScopeContext scope = resolveScope(request);
public ApiResponse<List<String>> shopOptions(
HttpServletRequest request,
@Parameter(description = "公司 ID(超管 tab;不传=不筛)")
@RequestParam(name = "company_id", required = false) Long companyId) {
AdminUserEntity operator = authorizedOperator(request);
boolean superAdmin = isSuper(operator);
Long operatorId = operator.getId();
List<Long> companyUserIds = superAdmin ? dataReadScope.readScope(operator, companyId).userIdsOr(null) : null;
TaskCenterScopeService.ScopeContext scope = resolveScope(operatorId, companyUserIds);
return ApiResponse.success(queryService.shopOptions(scope));
}
@@ -64,8 +88,16 @@ public class AdminTaskCenterController {
description = "biz_file_task(12 个客户端模块)∪ brand_crawl_tasks 按创建时间倒序;"
+ "店铺按规范化名精确匹配(来源=上架结果文件 + 逐店落行的结果文件名);"
+ "进行中且 >10 分钟无上报标记 stuck。")
public ApiResponse<TaskCenterTaskPageVo> tasks(HttpServletRequest request, TaskCenterTaskQuery query) {
TaskCenterScopeService.ScopeContext scope = resolveScope(request);
public ApiResponse<TaskCenterTaskPageVo> tasks(
HttpServletRequest request,
TaskCenterTaskQuery query,
@Parameter(description = "公司 ID(超管 tab;不传=不筛)")
@RequestParam(name = "company_id", required = false) Long companyId) {
AdminUserEntity operator = authorizedOperator(request);
boolean superAdmin = isSuper(operator);
Long operatorId = operator.getId();
List<Long> companyUserIds = superAdmin ? dataReadScope.readScope(operator, companyId).userIdsOr(null) : null;
TaskCenterScopeService.ScopeContext scope = resolveScope(operatorId, companyUserIds);
return ApiResponse.success(queryService.tasks(scope, query));
}
@@ -74,8 +106,14 @@ public class AdminTaskCenterController {
description = "参数 taskNo(品牌任务为 brand-<id>):基础信息 + 进度快照 + 分组明细 + 错误信息;"
+ "任务不存在或不在可见范围时 data 为空。")
public ApiResponse<TaskCenterTaskDetailVo> taskDetail(HttpServletRequest request,
@RequestParam String taskNo) {
TaskCenterScopeService.ScopeContext scope = resolveScope(request);
@RequestParam String taskNo,
@Parameter(description = "公司 ID(超管 tab;不传=不筛)")
@RequestParam(name = "company_id", required = false) Long companyId) {
AdminUserEntity operator = authorizedOperator(request);
boolean superAdmin = isSuper(operator);
Long operatorId = operator.getId();
List<Long> companyUserIds = superAdmin ? dataReadScope.readScope(operator, companyId).userIdsOr(null) : null;
TaskCenterScopeService.ScopeContext scope = resolveScope(operatorId, companyUserIds);
TaskCenterTaskDetailVo detail = queryService.detail(scope, taskNo);
if (detail == null) {
return ApiResponse.success("任务不存在或无权查看", null);
@@ -92,14 +130,39 @@ public class AdminTaskCenterController {
@RequestParam(required = false) Integer status,
@RequestParam(required = false) String taskType,
@RequestParam(required = false) Integer page,
@RequestParam(required = false) Integer pageSize) {
TaskCenterScopeService.ScopeContext scope = resolveScope(request);
@RequestParam(required = false) Integer pageSize,
@Parameter(description = "公司 ID(超管 tab;不传=不筛)")
@RequestParam(name = "company_id", required = false) Long companyId) {
AdminUserEntity operator = authorizedOperator(request);
boolean superAdmin = isSuper(operator);
Long operatorId = operator.getId();
List<Long> companyUserIds = superAdmin ? dataReadScope.readScope(operator, companyId).userIdsOr(null) : null;
TaskCenterScopeService.ScopeContext scope = resolveScope(operatorId, companyUserIds);
return ApiResponse.success(variantService.variantTasks(scope, status, taskType, page, pageSize));
}
private TaskCenterScopeService.ScopeContext resolveScope(HttpServletRequest request) {
/** 会话闸门(登录态 + 菜单权限),五个端点同一把;service 只认算好的 operatorId。 */
private AdminUserEntity authorizedOperator(HttpServletRequest request) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
permissionMenuService.requireClientTaskCenterAccess(operator);
return scopeService.resolve(operator.getId());
return operator;
}
/**
* 是否站点超管——用的是 {@code AdminAuthSupport.currentRole}(与 {@code DataReadScope} 内部同一把尺子,
* 含「role 为空 + isAdmin=1 + createdById 为空」的历史超管推断),不是这里新造的判定。
* 只有超管才按公司 tab 收窄:非超管的可见集里含「靠分组可见」的行,给它叠成员集会把那批行
* 从每一个 tab 里静默删掉;它的公司维度本来就由会话独家决定(spec §4)。
*/
private boolean isSuper(AdminUserEntity operator) {
return "super_admin".equals(adminAuthSupport.currentRole(operator));
}
/**
* 五个端点共用的收窄出口:{@code companyUserIds} 必须是 handler 体内 {@code readScope} 算出来的那个值
* ({@code null} = 不收窄),不许在这一层重新取公司值——那样护栏 A/C/D 看见的就不是这条链了。
*/
private TaskCenterScopeService.ScopeContext resolveScope(Long operatorId, List<Long> companyUserIds) {
return scopeService.resolve(operatorId, companyUserIds);
}
}
@@ -17,6 +17,9 @@ import java.util.Set;
* 客户端任务管理的数据范围:超管看全部,管理员看「本人 + 本组用户 + 自己管理的成员」。
* 口径与 {@code DedupeTotalDataService.resolveAccessScope} 一致——范围按任务发起账号
* (biz_file_task.user_id)判定,不按设备归属,设备跨组使用也不会串组。
*
* <p>公司 tab(spec §4)叠在这条既有口径之上:{@link #resolve(Long, List)} 只与上面的集合求交,
* 超管选了公司才有收窄,非超管的范围仍由会话独家决定。</p>
*/
@Service
@RequiredArgsConstructor
@@ -27,6 +30,38 @@ public class TaskCenterScopeService {
private final CompanyScopeSupport companyScopeSupport;
public ScopeContext resolve(Long operatorId) {
return resolve(operatorId, null);
}
/**
* 公司 tab 的收窄点:在 {@link #resolveBase(Long)} 的既有可见集上**只叠一层交集**,一律不放大
* (spec §4)。客户端任务管理的五个端点共用这一处,所以「列表筛了但按 id 能读」在这一页不存在
* ——详情走 {@code scope.visible(userId)},与列表同一条集合。
*
* <p>{@code companyUserIds} 由 controller 过唯一出口 {@code DataReadScope.readScope} 算好
* (本 service 手上只有 operatorId,没有操作者实体之外的会话信息,交不出这个集合)。
* <b>{@code null} 与空集不是一回事</b>:{@code null} = 不收窄(非超管、或超管没选公司),
* 逐字退回今天的行为;空集 = 选了公司但这家一个账号都没有,必须收窄成恒不命中
* ({@link ScopeContext#sqlUserIds()} 的 {@code [-1]} 兜底)。这里要是把空集也当「没选」放过,
* 症状是「切到空公司 tab 反而看见全量」——越权,且不报错。</p>
*
* @param companyUserIds 公司成员账号集;{@code null} 表示不按公司收窄
*/
public ScopeContext resolve(Long operatorId, List<Long> companyUserIds) {
ScopeContext base = resolveBase(operatorId);
if (companyUserIds == null) {
return base;
}
LinkedHashSet<Long> visible = new LinkedHashSet<>();
addAll(visible, companyUserIds);
if (!base.allUsers()) {
visible.retainAll(base.visibleUserIds());
}
return new ScopeContext(false, Set.copyOf(visible));
}
/** 今天的三条腿,逐字保留(超管直通 / 公司超管按会话 / 管理员按分组),不动口径、不换 mapper 方法。 */
private ScopeContext resolveBase(Long operatorId) {
AdminUserEntity operator = requireOperator(operatorId);
if (isSuperAdmin(operator)) {
return new ScopeContext(true, Set.of());
@@ -4,6 +4,8 @@ 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 com.nanri.aiimage.common.service.CompanyScopeSupport;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
@@ -15,6 +17,7 @@ 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.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyList;
@@ -25,12 +28,18 @@ import static org.mockito.Mockito.when;
/**
* 客户端任务管理的组范围裁剪:超管直通,管理员 = 本人 + 本组用户 + 自己管理的成员。
* 口径与 DedupeTotalDataService.resolveAccessScope 一致。
*
* <p>公司 tab 的收窄走第二个入参 {@code companyUserIds}(controller 已经过 {@code DataReadScope}
* 把成员集算好,见 {@code AdminTaskCenterController} 五个 handler),所以本类只喂 List、不 mock
* {@code DataReadScope}:{@code null} = 不收窄,空集 = 「选了公司但这家一个账号都没有」,
* 两者绝不许混为一谈(后者必须是恒不命中,不是全量)。</p>
*/
@ExtendWith(MockitoExtension.class)
class TaskCenterScopeServiceTest {
@Mock private AdminUserMapper adminUserMapper;
@Mock private ShopManageGroupMapper shopManageGroupMapper;
@Mock private CompanyScopeSupport companyScopeSupport;
@InjectMocks private TaskCenterScopeService service;
private AdminUserEntity user(Long id, String role) {
@@ -120,4 +129,86 @@ class TaskCenterScopeServiceTest {
assertThrows(BusinessException.class, () -> service.resolve(404L));
assertThrows(BusinessException.class, () -> service.resolve(null));
}
@Test
@DisplayName("超管选了公司:allUsers 必须翻成 false,否则集合根本不进 SQL,tab 是装饰品")
void superAdminWithCompanyStopsBeingUnfiltered() {
when(adminUserMapper.selectById(1L)).thenReturn(user(1L, "super_admin"));
TaskCenterScopeService.ScopeContext scope = service.resolve(1L, List.of(11L, 12L));
assertFalse(scope.allUsers(), "选了公司还 allUsers=true ⇒ sqlUserIds() 交 null ⇒ SQL 整段条件不渲染");
assertEquals(Set.of(11L, 12L), Set.copyOf(scope.sqlUserIds()));
assertTrue(scope.visible(11L));
assertFalse(scope.visible(13L), "别家账号连 tasks/detail 也进不去(详情走 scope.visible,不走 SQL)");
}
@Test
@DisplayName("第二参 null(非超管 / 超管没选公司):今天的行为一分不变")
void nullCompanyUserIdsKeepsTodayBehavior() {
when(adminUserMapper.selectById(1L)).thenReturn(user(1L, "super_admin"));
TaskCenterScopeService.ScopeContext scope = service.resolve(1L, null);
assertTrue(scope.allUsers(), "没选公司的超管仍然直通");
assertNull(scope.sqlUserIds(), "allUsers=true 时交给 SQL 的是 null = 整段 user_id 条件不加");
}
@Test
@DisplayName("历史超管(role 为空 + isAdmin=1 + createdById 为空)选了公司,也得跟着窄")
void legacySuperAdminIsNotLeftUnfiltered() {
AdminUserEntity legacy = new AdminUserEntity();
legacy.setId(2L);
legacy.setIsAdmin(1);
legacy.setCreatedById(null);
when(adminUserMapper.selectById(2L)).thenReturn(legacy);
TaskCenterScopeService.ScopeContext scope = service.resolve(2L, List.of(31L));
assertFalse(scope.allUsers(), "controller 那侧认这把的是 AdminAuthSupport.currentRole→resolveRole,"
+ "与本类的 isSuperAdmin 必须同一条:哪一天单边改了,红的是这条而不是某个历史超管看见全量");
assertEquals(Set.of(31L), scope.visibleUserIds());
}
@Test
@DisplayName("成员集为空时交给 SQL 的是 [-1] 不是空集:IN () 是语法错,兜底在 ScopeContext 里")
void emptyMemberSetFallsBackToSentinelNotEmptyList() {
when(adminUserMapper.selectById(1L)).thenReturn(user(1L, "super_admin"));
TaskCenterScopeService.ScopeContext scope = service.resolve(1L, List.of());
assertTrue(scope.visibleUserIds().isEmpty());
assertEquals(List.of(-1L), scope.sqlUserIds(), "本任务不许改 :100 那条兜底");
assertFalse(scope.visible(11L), "空公司连详情也不能看见任何人");
}
@Test
@DisplayName("非超管的既有分组集:公司集只与之求交,一律不放大")
void adminIntersectsWithGroupScope() {
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(6L));
when(shopManageGroupMapper.selectManagedMemberUserIds(5L)).thenReturn(List.of());
// 分组集 = 本人 5 + 组员 6(:42-48 的既有口径),公司集 = 6、7 ⇒ 只剩 6
TaskCenterScopeService.ScopeContext scope = service.resolve(5L, List.of(6L, 7L));
assertFalse(scope.allUsers());
assertEquals(Set.of(6L), scope.visibleUserIds(), "公司集里的 7 不在本组可见集内,不许被放大进来");
assertFalse(scope.visible(5L), "连本人都被公司维度筛掉时就是筛掉,不保留全部");
}
@Test
@DisplayName("第三条腿(公司超管按会话定本公司成员)逐字留在 resolveBase:没选公司时一分不变")
void companySuperAdminLegStillDecidedBySession() {
AdminUserEntity operator = user(9L, "company_super_admin");
operator.setCompanyId(7L);
when(adminUserMapper.selectById(9L)).thenReturn(operator);
when(companyScopeSupport.memberUserIds(7L)).thenReturn(List.of(41L, 42L));
TaskCenterScopeService.ScopeContext scope = service.resolve(9L, null);
assertFalse(scope.allUsers(), "公司超管不是超管,不能直通");
assertEquals(Set.of(9L, 41L, 42L), scope.visibleUserIds(), "本人 + 本公司成员(既有口径)");
}
}