feat(账号到期+公司超管): 级联过期判定、过期停任务巡检与两端续费提醒弹窗
需求两条:站点超管可开设其他公司的超级管理员(站点超管看全站、公司超管只看本公司、 组内管理员仍只看本组);账号要有过期时间,管理员过期后名下普通账户一并停用、 正在跑的任务也要停掉。落库只有 users.expires_at(V146,NULL=永不过期),级联一律现算, 不写"上级过期就把下级置成过期"那种续费也救不回来的数据。 - AccountExpiryService:生效到期 = min(自身, 本公司公司超管, 成员表组长, 创建人)。 登录拦 403(文案给续费提示)、每次请求鉴权拦 401(前端按下线踢回登录页),super_admin 永不适用。级联口径与数据可见范围同源(成员表基本未用、实际按 created_by_id 派生)。 - ExpiredAccountTaskStopService:每分钟把过期账号(含级联成员)名下 biz_file_task PENDING/SCHEDULED/RUNNING → FAILED,含未领取的 PENDING(防内部通道继续派发)。原因文案 刻意不以「客户端异常中断」开头,否则会被 TaskResumeService 自动复活。品牌采集与跟价循环 不在那张表上,经 ExpiredAccountTaskStopSpi 由各自模块停:品牌 pending/running→cancelled 且 UPDATE 的 WHERE 二次核对状态(抢到行后被客户端推进成完成的行不得误杀),跟价循环只置 stop_requested=1、沿用用户点「停止循环」那条收尾路径,绝不动 status。任务心跳接口保持匿名 不走 requireUser,所以过期账号的客户端仍能拿到 alive=false 协作停止。 - 角色 company_super_admin:由站点超管开设(唯一要显式选公司的建号场景),建号/授菜单/改用户/ 看数据四条链路都按"本公司"收口;公司超管之间、以及公司超管与站点超管互相碰不得。 - 续费提醒:剩余 <5 天在数富后台与用户端弹窗,只能收起不能关闭(无关闭按钮,收起态是右下角 浮标),续费到 ≥5 天自动消失。后台读 /api/admin/current-user 的 expiresAt/daysToExpire; 用户端无 Pinia,走 localStorage 的 account_expires_at(登录/check_login 落键、被顶下线清键, 换账号不残留)。只反映账号自身到期,弹窗不做任何级联推断。 - V146 撞掉了 V145 那批的一条护栏:它按"db/ 里不许出现 V146__ 前缀"编死号位,被一条正常的 前向迁移占用就红。改成钉动作——剥注释后的可执行 SQL 不得写 flyway_schema_history(回滚 一律是扫描路径外的人工补丁),并把盘点文档改 V146 时丢掉的 V145 文件名补回去。 验证(本会话亲跑,不是转述):后端定向 14 类 123/0(同一轮 mvn 里连批次③的 4 类 53 例、以及既有 ShopDataCrawl/SimilarAsin 两条回归对照 21 例一起跑,合计 197 例 0 红);admin-frontend-vue 本批定向 44/0(弹窗 7 + 公司建号/列表/入口/日期默认 21 + 角色 task-48/task-251 16),全量 1726 例里那 1 支红是另一条 会话刻意留的「公司 tab 接线缺口」尺子(点名 admin_task_center 等 6 页,与本批无关); frontend-vue 弹窗 9/0、全量 710/0。变异自证两批共 34 点:弹窗语义 16 点全杀(阈值 5、只能收起 不能关闭、挂载点含"注释掉挂载"、落键/清键链路),本批 SPI+迁移护栏 17 点全杀(含 G2 反向: 前向迁移只在注释里提 flyway_schema_history 必须照样绿),注入文件还原 dirty=0、探针残留 0。
This commit is contained in:
+182
@@ -0,0 +1,182 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
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 com.nanri.aiimage.common.service.DistributedJobLockService;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.spi.ExpiredAccountTaskStopSpi;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 过期停任务巡检(V146)口径钉:
|
||||
* 边界账号(expires_at ≤ now 且非超管)→ 级联成员集合与登录侧一致;
|
||||
* biz_file_task 的 PENDING/SCHEDULED/RUNNING 全部置终态(PENDING 也要,防内部通道继续派发);
|
||||
* 置终态原因绝不以「客户端异常中断」开头(否则被 TaskResumeService 自动复活);
|
||||
* SPI 由业务模块各自实现,一个用户都没有时全链路零写库。
|
||||
*/
|
||||
class ExpiredAccountTaskStopServiceTest {
|
||||
|
||||
private AdminUserMapper adminUserMapper;
|
||||
private ShopManageGroupMapper groupMapper;
|
||||
private CompanyScopeSupport companyScopeSupport;
|
||||
private FileTaskMapper fileTaskMapper;
|
||||
private DistributedJobLockService jobLockService;
|
||||
private RecordingSpi spi;
|
||||
private ExpiredAccountTaskStopService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
AdminUserEntity.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
FileTaskEntity.class);
|
||||
adminUserMapper = mock(AdminUserMapper.class);
|
||||
groupMapper = mock(ShopManageGroupMapper.class);
|
||||
companyScopeSupport = mock(CompanyScopeSupport.class);
|
||||
fileTaskMapper = mock(FileTaskMapper.class);
|
||||
jobLockService = mock(DistributedJobLockService.class);
|
||||
when(jobLockService.tryLock(anyString(), any()))
|
||||
.thenReturn(mock(DistributedJobLockService.LockHandle.class));
|
||||
when(adminUserMapper.selectList(any())).thenReturn(List.of());
|
||||
when(groupMapper.selectManagedMemberUserIds(any())).thenReturn(List.of());
|
||||
spi = new RecordingSpi();
|
||||
service = new ExpiredAccountTaskStopService(adminUserMapper, groupMapper, companyScopeSupport,
|
||||
fileTaskMapper, jobLockService, List.of(spi));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noExpiredAccountsMeansZeroWrites() {
|
||||
service.runOnce();
|
||||
verify(fileTaskMapper, never()).update(any(), any());
|
||||
assertThat(spi.calls).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredAdminCollectsCreatedNormalsAndGroupMembers() {
|
||||
AdminUserEntity admin = boundary(2L, "admin");
|
||||
AdminUserEntity createdNormal = new AdminUserEntity();
|
||||
createdNormal.setId(30L);
|
||||
// 第一次调用是边界扫描(返回过期的管理员),第二次是「名下子账户」查询
|
||||
AtomicInteger call = new AtomicInteger();
|
||||
when(adminUserMapper.selectList(any())).thenAnswer(inv -> {
|
||||
int n = call.incrementAndGet();
|
||||
return n == 1 ? List.of(admin) : List.of(createdNormal);
|
||||
});
|
||||
when(groupMapper.selectManagedMemberUserIds(2L)).thenReturn(List.of(31L));
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(fileTask(11L)));
|
||||
when(fileTaskMapper.update(any(), any())).thenReturn(1);
|
||||
|
||||
service.runOnce();
|
||||
|
||||
assertThat(spi.lastUserIds).containsExactlyInAnyOrder(2L, 30L, 31L);
|
||||
verify(fileTaskMapper).update(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredCompanySuperAdminCollectsWholeCompany() {
|
||||
AdminUserEntity csa = boundary(9L, "company_super_admin");
|
||||
csa.setCompanyId(2L);
|
||||
when(adminUserMapper.selectList(any())).thenReturn(List.of(csa));
|
||||
when(companyScopeSupport.memberUserIds(2L)).thenReturn(List.of(101L, 102L));
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
service.runOnce();
|
||||
|
||||
assertThat(spi.lastUserIds).containsExactlyInAnyOrder(9L, 101L, 102L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fileTaskUpdateCarriesTerminalStateWithNonResumeReason() {
|
||||
AdminUserEntity admin = boundary(2L, "admin");
|
||||
when(adminUserMapper.selectList(any())).thenReturn(List.of(admin));
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(fileTask(11L)));
|
||||
when(fileTaskMapper.update(any(), any())).thenReturn(1);
|
||||
|
||||
service.runOnce();
|
||||
|
||||
var wrapperCaptor = org.mockito.ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
|
||||
verify(fileTaskMapper).update(org.mockito.ArgumentMatchers.isNull(), wrapperCaptor.capture());
|
||||
LambdaUpdateWrapper<FileTaskEntity> wrapper = wrapperCaptor.getValue();
|
||||
// SET 子句在 getSqlSet()(只有列名=占位符),写入值在参数表里——两边一起断
|
||||
String sqlSet = wrapper.getSqlSet();
|
||||
assertThat(sqlSet).contains("status").contains("error_message").contains("finished_at");
|
||||
// WHERE 条件更新钉住状态集合(user_id 在 SELECT 侧过滤、UPDATE 按 id 收口),
|
||||
// 避免并发下把刚续跑的新任务误杀
|
||||
String where = wrapper.getSqlSegment();
|
||||
assertThat(where).contains("status").contains("id IN");
|
||||
Collection<Object> params = wrapper.getParamNameValuePairs().values();
|
||||
assertThat(params).contains("FAILED", "PENDING", "SCHEDULED", "RUNNING",
|
||||
ExpiredAccountTaskStopService.STOP_REASON);
|
||||
// 原因文案绝不以「客户端异常中断」开头,否则会被 TaskResumeService 自动续跑复活
|
||||
assertThat(ExpiredAccountTaskStopService.STOP_REASON).doesNotStartWith("客户端异常中断");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nothingActiveLeavesModuleSpisWithoutWorkButStillNotified() {
|
||||
// file_task 全是终态 → fileTaskMapper.update 不发生;SPI 仍会被询问一次(幂等、0 行)
|
||||
AdminUserEntity admin = boundary(2L, "admin");
|
||||
when(adminUserMapper.selectList(any())).thenReturn(List.of(admin));
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
service.runOnce();
|
||||
|
||||
verify(fileTaskMapper, never()).update(any(), any());
|
||||
assertThat(spi.calls).isEqualTo(1);
|
||||
assertThat(spi.lastUserIds).containsExactly(2L);
|
||||
}
|
||||
|
||||
// ---------- helpers ----------
|
||||
|
||||
private static AdminUserEntity boundary(Long id, String role) {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(id);
|
||||
user.setRole(role);
|
||||
user.setCompanyId(1L);
|
||||
user.setExpiresAt(LocalDateTime.now().minusMinutes(1));
|
||||
return user;
|
||||
}
|
||||
|
||||
private static FileTaskEntity fileTask(Long id) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(id);
|
||||
return task;
|
||||
}
|
||||
|
||||
private static final class RecordingSpi implements ExpiredAccountTaskStopSpi {
|
||||
private int calls;
|
||||
private Collection<Long> lastUserIds = List.of();
|
||||
|
||||
@Override
|
||||
public String moduleType() {
|
||||
return "RECORDING";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int stopActiveTasks(Collection<Long> userIds, String reason) {
|
||||
calls++;
|
||||
lastUserIds = userIds == null ? List.of() : List.copyOf(userIds);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user