feat(公司隔离): 加 CompanyScopeSupport 与二期实施计划

取不到公司一律抛 BusinessException,不返回 null:null 进实体会被 MyBatis-Plus
跳过该列,落库时由 V145 的列 DEFAULT=1 静默兜住,第二家公司开通后每个漏传都
会归错公司且不报错。同时把 18 号实施计划入库(docs/plans 已不再 gitignore)。
This commit is contained in:
2026-10-04 15:09:05 +08:00
parent c9d5cb6c2d
commit 0311f9fa2a
3 changed files with 1698 additions and 0 deletions
@@ -0,0 +1,49 @@
package com.nanri.aiimage.common.service;
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 org.springframework.stereotype.Service;
/**
* 按账号解析归属公司(二期设计文档 §3.1)——数据出口只认这一个入口。
*
* <p>一期"业务表零 DDL"靠 {@code user_id → users.company_id} 派生;本期两张规则库表破例物化了
* {@code company_id}(§2.2),但<b>归属仍然从账号解析</b>,物化列只是把它固定下来、好让唯一键与
* 消费侧的集合查询用得上。</p>
*
* <p>取不到公司一律抛,不返回 null:调用方一旦把 null 传进实体,MyBatis-Plus 会跳过该列,
* 落库时被 V145 的 {@code DEFAULT 1} 静默兜住——那是"归属失真且不报错"的成因(§6.3)。</p>
*/
@Service
@RequiredArgsConstructor
public class CompanyScopeSupport {
private final AdminUserMapper adminUserMapper;
public Long requireCompanyId(Long userId) {
if (userId == null || userId <= 0) {
throw new BusinessException("归属账号不能为空");
}
return requireCompanyId(adminUserMapper.selectById(userId));
}
public Long requireCompanyId(AdminUserEntity user) {
if (user == null) {
throw new BusinessException("归属账号不存在");
}
Long companyId = user.getCompanyId();
if (companyId == null || companyId <= 0) {
throw new BusinessException(403, "当前账号没有所属公司,无法按公司读写数据");
}
return companyId;
}
/** 行级归属闸门:跨公司写/删的第一道,消息由调用方给(前端按它显示,别在这里编文案)。 */
public void assertSameCompany(Long operatorCompanyId, Long rowCompanyId, String deniedMessage) {
if (rowCompanyId == null || !rowCompanyId.equals(operatorCompanyId)) {
throw new BusinessException(403, deniedMessage);
}
}
}