diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/mapper/JdProductMapper.java b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/mapper/JdProductMapper.java index 4e032456f..23e07c3d3 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/mapper/JdProductMapper.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/mapper/JdProductMapper.java @@ -54,6 +54,14 @@ public interface JdProductMapper extends BaseMapper { @Param("syncedFrom") LocalDateTime syncedFrom, @Param("syncedTo") LocalDateTime syncedTo); + /** + * 商品上架配额计数({@code JdPublishQuotaService}):按 owner uid 集(管理员本人+ + * 名下员工,公司超管=全公司成员)计 {@code biz_jd_product} 行数。归属列 + * {@code user_id}=任务 owner(V193 物化列)。调用方保证列表非空——空集在服务侧 + * 直接按 0 处理,不进 SQL。 + */ + long countByUserIds(@Param("userIds") List userIds); + /** * 后管只读页分页查询(synced_at 倒序)。 * diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdPublishQuotaService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdPublishQuotaService.java new file mode 100644 index 000000000..44e0825da --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdPublishQuotaService.java @@ -0,0 +1,103 @@ +package com.nanri.aiimage.modules.jd.service; + +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.common.mapper.AdminUserMapper; +import com.nanri.aiimage.common.model.entity.AdminUserEntity; +import com.nanri.aiimage.common.security.DeviceSessionPolicy; +import com.nanri.aiimage.modules.jd.mapper.JdProductMapper; +import com.nanri.aiimage.modules.permission.support.UserDataScopeSupport; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 商品上架配额(2026-11-15 用户口径「限制管理员上架的商品数量,包括管理员下的员工 + * 总和不能超过配置的最大值」,默认 5000,{@code aiimage.jd.max-products-per-admin})。 + * + *

计数口径=团队在京东数据库({@code biz_jd_product})占有的行数:上架功能的数据源 + * 就是京东数据库,团队占有量即「可上架量」的上游闸。归属列 {@code user_id}=任务 owner + * (V193 物化列),uid 集由 {@link UserDataScopeSupport#resolveVisibleUserIds} 统一解析—— + * 管理员=本人+名下员工(数据权限分组成员,无分组回退 {@code created_by_id} 兜底); + * 公司超管=全公司成员;普通账号先锚定到 {@code created_by_id} 指向的管理员再取团队集, + * 读不到主管回退按自己计。站点超管不限额。

+ * + *

两个出口:{@link #quotaOf} 只读快照(供上架界面展示余量);{@link #assertWithinQuota} + * 建上架单前的配额闸,超限抛 {@link BusinessException}。上限 ≤0(0=不限)整体放行。 + * 商品上架建单入口落地时调用 {@link #assertWithinQuota}——本服务是配额原语, + * 不自行改变任何现有链路行为。

+ */ +@Service +@Slf4j +@RequiredArgsConstructor +public class JdPublishQuotaService { + + private final AdminUserMapper adminUserMapper; + private final UserDataScopeSupport userDataScopeSupport; + private final JdProductMapper jdProductMapper; + + /** 出厂默认与 application.yml 的 aiimage.jd.max-products-per-admin 逐字相等(棘轮);包级可见供同包测试直设。 */ + @Value("${aiimage.jd.max-products-per-admin:5000}") + long maxProductsPerAdmin = 5000L; + + /** 配额只读快照:used=团队已占有行数,limit=配置上限(≤0=不限)。 */ + public record QuotaSnapshot(long used, long limit) { + public boolean unlimited() { + return limit <= 0; + } + } + + /** 团队已占用与上限的只读快照;uid 集为空(无主管且无 id)按 used=0 处理,不进 SQL。 */ + public QuotaSnapshot quotaOf(AdminUserEntity operator) { + List userIds = teamUserIds(operator); + long used = userIds.isEmpty() ? 0L : jdProductMapper.countByUserIds(userIds); + return new QuotaSnapshot(used, maxProductsPerAdmin); + } + + /** + * 建上架单前的配额闸:按「管理员+名下员工合计」口径判限。站点超管不限额; + * 上限 ≤0(0=不限)放行;{@code additionalCount} 为负按 0 处理(本闸只判限不预占)。 + */ + public void assertWithinQuota(AdminUserEntity operator, int additionalCount) { + if (maxProductsPerAdmin <= 0) { + return; + } + if (operator != null && DeviceSessionPolicy.isSuperAdmin( + operator.getRole(), operator.getIsAdmin(), operator.getCreatedById())) { + return; + } + QuotaSnapshot snapshot = quotaOf(operator); + long requested = Math.max(0, additionalCount); + if (snapshot.used() + requested > snapshot.limit()) { + log.warn("[jd-publish] 商品上架配额不足 operatorId={} used={} requested={} limit={}", + operator == null ? null : operator.getId(), + snapshot.used(), requested, snapshot.limit()); + throw new BusinessException("商品上架数量已达上限:该管理员及其名下员工当前已占用 " + + snapshot.used() + "/上限 " + snapshot.limit() + + ",请清理京东数据库商品或联系管理员调整配额后重试"); + } + } + + /** 普通账号锚定到其所属管理员(created_by_id);管理员/公司超管锚自己;无 id 返回空集。 */ + private List teamUserIds(AdminUserEntity operator) { + if (operator == null || operator.getId() == null) { + return List.of(); + } + return userDataScopeSupport.resolveVisibleUserIds(anchorOf(operator)); + } + + private AdminUserEntity anchorOf(AdminUserEntity operator) { + // resolveRole 对普通账号返回 null(角色域只有 super_admin/company_super_admin/admin/null) + if (DeviceSessionPolicy.resolveRole( + operator.getRole(), operator.getIsAdmin(), operator.getCreatedById()) == null + && operator.getCreatedById() != null) { + AdminUserEntity admin = adminUserMapper.selectById(operator.getCreatedById()); + if (admin != null) { + return admin; + } + } + return operator; + } +} diff --git a/backend-java/src/main/resources/application-local.example.yml b/backend-java/src/main/resources/application-local.example.yml index 91f81e428..950ddd9cc 100644 --- a/backend-java/src/main/resources/application-local.example.yml +++ b/backend-java/src/main/resources/application-local.example.yml @@ -43,6 +43,8 @@ AIIMAGE_JD_COLLECT_IMAGE_MAX_PER_PRODUCT=200 AIIMAGE_JD_COLLECT_STALE_MINUTES=60 # SKU 矩阵单商品上限(V198 详情面):真机一个商品 12 行,超限截断并 WARN。 AIIMAGE_JD_SKU_MAX_PER_PRODUCT=200 +# 商品上架配额:单个管理员(含其名下员工合计)可上架商品数上限,默认 5000,0=不限。 +AIIMAGE_JD_MAX_PRODUCTS_PER_ADMIN=5000 # 京东采集的客户端兜底拉取白名单(v1.4 P0-1):默认值已含 JD_SHOP_COLLECT(见 application.yml)。 # ⚠ 只有真的要覆盖整个白名单时才设 AIIMAGE_CLIENT_TASK_PULL_MODULE_TYPES——env 变量存在即生效, # 设成空串会把白名单清空(兜底拉取对 JD 形同关闭)。打开 AIIMAGE_CLIENT_TASK_PULL_ENABLED diff --git a/backend-java/src/main/resources/application.yml b/backend-java/src/main/resources/application.yml index 108edf41d..2d91ffdab 100644 --- a/backend-java/src/main/resources/application.yml +++ b/backend-java/src/main/resources/application.yml @@ -177,6 +177,11 @@ aiimage: # SKU 矩阵单商品上限(V198 详情面):真机一个商品 12 行,超限截断并 WARN, # 防脏回传把整批撑爆。显式写在这里,不许只活在代码默认值里。 sku-max-per-product: ${AIIMAGE_JD_SKU_MAX_PER_PRODUCT:200} + # 商品上架配额(2026-11-15 用户口径):单个管理员(含其名下员工合计;公司超管按 + # 全公司合计)可上架的商品数上限,默认 5000;0=不限。计数源=biz_jd_product 按 + # owner uid 集计行数(上架功能的数据源就是京东数据库);站点超管不限额。 + # 校验出口=JdPublishQuotaService,商品上架建单入口落地时调用。 + max-products-per-admin: ${AIIMAGE_JD_MAX_PRODUCTS_PER_ADMIN:5000} # 设备日志对象存储:主机B 独立 MinIO(与业务 OSS 隔离,日志只保留 7 天)。 # endpoint 未配置时日志上报/管理接口不可用(宁可失败也不静默落到业务桶)。 # 桶的 7 天过期规则由运维用 mc 配置(与 retention-days 口径一致)。 diff --git a/backend-java/src/main/resources/mapper/jd/JdProductMapper.xml b/backend-java/src/main/resources/mapper/jd/JdProductMapper.xml index 453fa9b33..39702ff0d 100644 --- a/backend-java/src/main/resources/mapper/jd/JdProductMapper.xml +++ b/backend-java/src/main/resources/mapper/jd/JdProductMapper.xml @@ -148,4 +148,14 @@ GROUP BY p.category_path ORDER BY p.category_path + + + diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdPublishQuotaServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdPublishQuotaServiceTest.java new file mode 100644 index 000000000..cfa4dd608 --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdPublishQuotaServiceTest.java @@ -0,0 +1,152 @@ +package com.nanri.aiimage.modules.jd.service; + +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.common.mapper.AdminUserMapper; +import com.nanri.aiimage.common.model.entity.AdminUserEntity; +import com.nanri.aiimage.modules.jd.mapper.JdProductMapper; +import com.nanri.aiimage.modules.permission.support.UserDataScopeSupport; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +/** + * 商品上架配额(JdPublishQuotaService)行为测试:默认 5000、0=不限、普通账号锚定到 + * created_by_id 管理员并按其团队 uid 集计数、站点超管豁免、恰好等于上限的边界放行、 + * 空集按 used=0 不进 SQL。判限口径=「管理员+名下员工合计」(2026-11-15 用户口径)。 + */ +@ExtendWith(MockitoExtension.class) +class JdPublishQuotaServiceTest { + + @Mock + private AdminUserMapper adminUserMapper; + @Mock + private UserDataScopeSupport userDataScopeSupport; + @Mock + private JdProductMapper jdProductMapper; + + private JdPublishQuotaService newService() { + return new JdPublishQuotaService(adminUserMapper, userDataScopeSupport, jdProductMapper); + } + + private AdminUserEntity admin(long id) { + AdminUserEntity user = new AdminUserEntity(); + user.setId(id); + user.setRole("admin"); + return user; + } + + /** 普通账号:role 为空(resolveRole 返回 null),created_by_id 指向所属管理员。 */ + private AdminUserEntity employee(long id, long adminId) { + AdminUserEntity user = new AdminUserEntity(); + user.setId(id); + user.setCreatedById(adminId); + return user; + } + + /** 双重载(Long/实体)消歧:lambda 参数显式标注实体形状(argThat(...))。 */ + + @Test + void 默认上限5000_超限拒绝且文案含占用与上限() { + JdPublishQuotaService service = newService(); + AdminUserEntity employee = employee(9L, 5L); + when(adminUserMapper.selectById(5L)).thenReturn(admin(5L)); + when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L))) + .thenReturn(List.of(5L, 9L, 10L)); + when(jdProductMapper.countByUserIds(List.of(5L, 9L, 10L))).thenReturn(4990L); + + assertThatThrownBy(() -> service.assertWithinQuota(employee, 20)) + .isInstanceOf(BusinessException.class) + .hasMessageContaining("4990") + .hasMessageContaining("5000") + .hasMessageContaining("管理员"); + // 普通账号必须锚定到所属管理员(uid=5)的团队集,而不是只数自己 + verify(userDataScopeSupport).resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L)); + } + + @Test + void 恰好等于上限_放行() { + JdPublishQuotaService service = newService(); + AdminUserEntity operator = admin(5L); + when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L))) + .thenReturn(List.of(5L, 9L)); + when(jdProductMapper.countByUserIds(List.of(5L, 9L))).thenReturn(4990L); + + assertThatCode(() -> service.assertWithinQuota(operator, 10)).doesNotThrowAnyException(); + } + + @Test + void 上限为0_整体放行且不查库() { + JdPublishQuotaService service = newService(); + service.maxProductsPerAdmin = 0L; + + assertThatCode(() -> service.assertWithinQuota(admin(5L), 999_999)).doesNotThrowAnyException(); + verifyNoInteractions(jdProductMapper, userDataScopeSupport, adminUserMapper); + } + + @Test + void 站点超管_豁免不限额() { + JdPublishQuotaService service = newService(); + AdminUserEntity siteSuperAdmin = new AdminUserEntity(); + siteSuperAdmin.setId(1L); + siteSuperAdmin.setRole("super_admin"); + + assertThatCode(() -> service.assertWithinQuota(siteSuperAdmin, 999_999)).doesNotThrowAnyException(); + verifyNoInteractions(jdProductMapper, userDataScopeSupport, adminUserMapper); + } + + @Test + void 团队uid集为空_按used0处理且不进SQL() { + JdPublishQuotaService service = newService(); + AdminUserEntity operator = admin(5L); + when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L))) + .thenReturn(List.of()); + + // used=0:additional=5000 恰好等于上限,放行 + assertThatCode(() -> service.assertWithinQuota(operator, 5000)).doesNotThrowAnyException(); + // additional=5001 超限,但空集不查库 + assertThatThrownBy(() -> service.assertWithinQuota(operator, 5001)) + .isInstanceOf(BusinessException.class) + .hasMessageContaining("0"); + verifyNoInteractions(jdProductMapper); + } + + @Test + void 快照返回占用与上限() { + JdPublishQuotaService service = newService(); + AdminUserEntity operator = admin(5L); + when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L))) + .thenReturn(List.of(5L, 9L)); + when(jdProductMapper.countByUserIds(List.of(5L, 9L))).thenReturn(1234L); + + JdPublishQuotaService.QuotaSnapshot snapshot = service.quotaOf(operator); + assertThat(snapshot.used()).isEqualTo(1234L); + assertThat(snapshot.limit()).isEqualTo(5000L); + assertThat(snapshot.unlimited()).isFalse(); + } + + @Test + void 普通账号读不到主管_回退按自己计() { + JdPublishQuotaService service = newService(); + AdminUserEntity employee = employee(9L, 5L); + when(adminUserMapper.selectById(5L)).thenReturn(null); + when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 9L))) + .thenReturn(List.of(9L)); + when(jdProductMapper.countByUserIds(List.of(9L))).thenReturn(5000L); + + assertThatThrownBy(() -> service.assertWithinQuota(employee, 1)) + .isInstanceOf(BusinessException.class) + .hasMessageContaining("5000"); + verify(userDataScopeSupport).resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 9L)); + } +}