feat(公司范围): memberUserIds 按公司枚举成员,只投影 id + 500 成员倒计时哨兵
Step 0 分母(2026-10-04 生产只读取证,controller 实跑;本任务零 SSH、零写库): 全库 users = 142 行 / 最大一家公司 = 140 成员 / company_id IS NULL = 0 行 / 公司数 = 2。 三个数各兑现一件事:140 对阈值 500 是约 3.6 倍余量(哨兵要在 500 元素 user_id IN(...) 劣化之前红在日志里,按 brief 那句"余量小于 5 倍=阈值太高"反过来定到 700+ 等于护栏永不响, 故维持 500 并在 javadoc 写死理由);游离账号 0 行是"按公司等值查不把人从每个 tab 丢掉"的前提; 公司数 2 与 spec §1 对账一致。brief 正文那句"今天全库 140 行"是过期读数,已按实跑改写。 出口 List<Long> memberUserIds(Long companyId):只 select id(users 带 password_hash, 整行投影是 2026-09 那轮优化摘掉的东西);非法 companyId 抛 BusinessException 而非返回空集 (调用方把空集读成"这家公司查不到行",两种语义不能互冒充);越阈值打 WARN marker COMPANY_MEMBER_SET_LARGE。本轮只做后端能力,无 controller/前端/UI 暴露。 测试:CompanyScopeSupportTest 追加 6 支,本类 11/11 绿。G6 按本仓 idiom 用 logback ListAppender 断真实 WARN 事件(不是只断包内布尔),反例里带正对照防空转。 变异自证 5 项,其中"阈值抬到 5000"第一跑 11 支全绿=变异存活(用例都按常量±1 造数据), 补一条字面断言 isEqualTo(500) 后才变红——这条正是"哨兵等于不存在"的合入路径。
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
package com.nanri.aiimage.common.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 按账号解析归属公司(二期设计文档 §3.1)——数据出口只认这一个入口。
|
||||
*
|
||||
@@ -16,10 +21,14 @@ import org.springframework.stereotype.Service;
|
||||
* <p>取不到公司一律抛,不返回 null:调用方一旦把 null 传进实体,MyBatis-Plus 会跳过该列,
|
||||
* 落库时被 V145 的 {@code DEFAULT 1} 静默兜住——那是"归属失真且不报错"的成因(§6.3)。</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class CompanyScopeSupport {
|
||||
|
||||
/** 超过这个成员数就该把 {@code user_id IN (...)} 换成子查询(见 {@link #memberUserIds} 的 warn 与 spec §3.2)。 */
|
||||
static final int MEMBER_WARN_THRESHOLD = 500;
|
||||
|
||||
private final AdminUserMapper adminUserMapper;
|
||||
|
||||
public Long requireCompanyId(Long userId) {
|
||||
@@ -46,4 +55,50 @@ public class CompanyScopeSupport {
|
||||
throw new BusinessException(403, deniedMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按公司枚举成员账号 id(spec §3.2:公司 tab 读范围的唯一来源,消费侧是 Task 6 的 DataReadScope)。
|
||||
*
|
||||
* <p>只投影 id:{@code users} 带 {@code password_hash},整行拉进内存是 2026-09
|
||||
* 「不再整表拉回含密码哈希的全列」那轮优化摘掉的东西,别在这里装回去(测试钉的是
|
||||
* {@code getSqlSelect()} 恰好等于 {@code id},加列就会红)。</p>
|
||||
*
|
||||
* <p>不加 LIMIT:2026-10-04 生产只读取证<b>全库 142 行 / 最大公司 140 成员</b>
|
||||
* ({@code company_id IS NULL} 0 行、公司数 2),今天装得下不代表以后装得下,而截断会让某个公司的
|
||||
* tab 看起来"数据不全"且毫无提示。所以用 {@link #MEMBER_WARN_THRESHOLD}(500,对 140 是约 3.6 倍余量)
|
||||
* 当倒计时哨兵——它要在查询劣化<b>之前</b>红在日志里、可被巡检按 marker
|
||||
* {@code COMPANY_MEMBER_SET_LARGE} 抓到;阈值若按"至少留 5 倍余量"定到 700+,
|
||||
* 等于哨兵响的时候列表早已痛了,那就不是护栏。</p>
|
||||
*
|
||||
* <p>非法 companyId 抛 {@link BusinessException} 而不是返回空集:调用方把空集读成"这家公司查不到行",
|
||||
* 与"参数没给对"是两种语义(spec §9 的静默丢数形态)。</p>
|
||||
*
|
||||
* @return 成员账号 id 列表(顺序不保证;合法但没有成员时返回空集)
|
||||
*/
|
||||
public List<Long> memberUserIds(Long companyId) {
|
||||
if (companyId == null || companyId <= 0) {
|
||||
throw new BusinessException("公司 id 不能为空");
|
||||
}
|
||||
List<AdminUserEntity> rows = adminUserMapper.selectList(
|
||||
new LambdaQueryWrapper<AdminUserEntity>()
|
||||
.select(AdminUserEntity::getId)
|
||||
.eq(AdminUserEntity::getCompanyId, companyId));
|
||||
List<Long> ids = new ArrayList<>(rows.size());
|
||||
for (AdminUserEntity row : rows) {
|
||||
if (row.getId() != null && row.getId() > 0) {
|
||||
ids.add(row.getId());
|
||||
}
|
||||
}
|
||||
if (shouldWarn(ids.size())) {
|
||||
log.warn("COMPANY_MEMBER_SET_LARGE companyId={} size={} threshold={}"
|
||||
+ " —— user_id IN (...) 列表已过长,改成 SELECT id FROM users WHERE company_id = ? 子查询",
|
||||
companyId, ids.size(), MEMBER_WARN_THRESHOLD);
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
/** 哨兵的判定口径,抽出来给测试当接缝用;日志事件本身另有断言,这里不算护栏。 */
|
||||
static boolean shouldWarn(int size) {
|
||||
return size > MEMBER_WARN_THRESHOLD;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user