From b9b66bcb0b8ab2c8ff08edfb2dc0f10d6a4d858c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Sun, 15 Nov 2026 20:18:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(jd):=20=E5=95=86=E5=93=81=E4=B8=8A?= =?UTF-8?q?=E6=9E=B6=E6=95=B0=E9=87=8F=E4=B8=8A=E9=99=90=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=88=E9=BB=98=E8=AE=A4=205000=EF=BC=8C=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=91=98+=E5=90=8D=E4=B8=8B=E5=91=98=E5=B7=A5=E5=90=88?= =?UTF-8?q?=E8=AE=A1=E5=8F=A3=E5=BE=84=EF=BC=8C2026-11-15=20=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=8F=A3=E5=BE=84=EF=BC=89=E2=80=94=E2=80=94aiimage.j?= =?UTF-8?q?d.max-products-per-admin=EF=BC=88env=20AIIMAGE=5FJD=5FMAX=5FPRO?= =?UTF-8?q?DUCTS=5FPER=5FADMIN=EF=BC=8C0=3D=E4=B8=8D=E9=99=90=EF=BC=8Cyml?= =?UTF-8?q?=20=E4=B8=8E=20@Value=20=E5=90=8C=E9=BB=98=E8=AE=A4=E3=80=81app?= =?UTF-8?q?lication-local.example.yml=20=E5=90=8C=E6=AD=A5=EF=BC=89+=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20JdPublishQuotaService=20=E9=85=8D=E9=A2=9D?= =?UTF-8?q?=E5=8E=9F=E8=AF=AD=EF=BC=9A=E8=AE=A1=E6=95=B0=E6=BA=90=3Dbiz=5F?= =?UTF-8?q?jd=5Fproduct=20=E6=8C=89=20owner=20uid=20=E9=9B=86=E8=AE=A1?= =?UTF-8?q?=E8=A1=8C=E6=95=B0=EF=BC=88JdProductMapper.countByUserIds?= =?UTF-8?q?=EF=BC=8CSQL=20=E6=94=B6=E5=8F=A3=20XML=EF=BC=8C=E7=A9=BA?= =?UTF-8?q?=E9=9B=86=E6=9C=8D=E5=8A=A1=E4=BE=A7=E6=8C=89=200=20=E4=B8=8D?= =?UTF-8?q?=E8=BF=9B=20SQL=EF=BC=89=EF=BC=9Buid=20=E9=9B=86=E5=A4=8D?= =?UTF-8?q?=E7=94=A8=20UserDataScopeSupport.resolveVisibleUserIds=EF=BC=88?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98=3D=E6=9C=AC=E4=BA=BA+=E5=90=8D?= =?UTF-8?q?=E4=B8=8B=E5=91=98=E5=B7=A5/=E5=88=86=E7=BB=84=E6=88=90?= =?UTF-8?q?=E5=91=98+created=5Fby=5Fid=20=E5=85=9C=E5=BA=95=EF=BC=8C?= =?UTF-8?q?=E5=85=AC=E5=8F=B8=E8=B6=85=E7=AE=A1=3D=E5=85=A8=E5=85=AC?= =?UTF-8?q?=E5=8F=B8=EF=BC=89=EF=BC=8C=E6=99=AE=E9=80=9A=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E5=85=88=E9=94=9A=E5=AE=9A=20created=5Fby=5Fid=20=E6=8C=87?= =?UTF-8?q?=E5=90=91=E7=9A=84=E7=AE=A1=E7=90=86=E5=91=98=EF=BC=88=E8=AF=BB?= =?UTF-8?q?=E4=B8=8D=E5=88=B0=E5=9B=9E=E9=80=80=E8=87=AA=E5=B7=B1=EF=BC=89?= =?UTF-8?q?=EF=BC=9B=E7=AB=99=E7=82=B9=E8=B6=85=E7=AE=A1=E8=B1=81=E5=85=8D?= =?UTF-8?q?=EF=BC=88=E8=B5=B0=20DeviceSessionPolicy.isSuperAdmin=20?= =?UTF-8?q?=E5=8D=95=E5=87=BA=E5=8F=A3=EF=BC=89=E3=80=82=E5=87=BA=E5=8F=A3?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA=EF=BC=9AquotaOf=20=E5=8F=AA=E8=AF=BB?= =?UTF-8?q?=E5=BF=AB=E7=85=A7=EF=BC=88=E4=BE=9B=E4=B8=8A=E6=9E=B6=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E5=B1=95=E7=A4=BA=E4=BD=99=E9=87=8F=EF=BC=89+=20asser?= =?UTF-8?q?tWithinQuota=20=E5=BB=BA=E4=B8=8A=E6=9E=B6=E5=8D=95=E5=89=8D?= =?UTF-8?q?=E7=9A=84=E9=85=8D=E9=A2=9D=E9=97=B8=EF=BC=88=E8=B6=85=E9=99=90?= =?UTF-8?q?=E6=8A=9B=20BusinessException=EF=BC=8C=E6=96=87=E6=A1=88?= =?UTF-8?q?=E5=90=AB=E5=8D=A0=E7=94=A8/=E4=B8=8A=E9=99=90=E6=95=B0?= =?UTF-8?q?=E5=AD=97=EF=BC=89=E3=80=82=E2=9A=A0=20=E6=9C=AC=E7=AC=94?= =?UTF-8?q?=E6=97=A0=E7=94=9F=E4=BA=A7=E8=B0=83=E7=94=A8=E6=96=B9=E2=80=94?= =?UTF-8?q?=E2=80=94=E5=95=86=E5=93=81=E4=B8=8A=E6=9E=B6=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E7=9B=AE=E5=89=8D=E6=98=AF=E7=94=A8=E6=88=B7=E7=AB=AF=E5=8D=A0?= =?UTF-8?q?=E4=BD=8D=E5=8D=A1=EF=BC=88=E5=BB=BA=E8=AE=BE=E4=B8=AD=E3=80=81?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E9=9B=B6=E4=BB=A3=E7=A0=81=EF=BC=89=EF=BC=8C?= =?UTF-8?q?=E6=9C=AC=E7=AC=94=E5=8F=AA=E8=90=BD=E9=85=8D=E7=BD=AE=E4=B8=8E?= =?UTF-8?q?=E5=8F=AF=E6=B5=8B=E9=85=8D=E9=A2=9D=E6=9C=8D=E5=8A=A1=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E6=94=B9=E4=BB=BB=E4=BD=95=E7=8E=B0=E6=9C=89=E9=93=BE?= =?UTF-8?q?=E8=B7=AF=E8=A1=8C=E4=B8=BA=EF=BC=9B=E4=B8=8A=E6=9E=B6=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E8=90=BD=E5=9C=B0=E6=97=B6=E5=9C=A8=E5=85=B6=E5=BB=BA?= =?UTF-8?q?=E5=8D=95=E5=85=A5=E5=8F=A3=E8=B0=83=E7=94=A8=20assertWithinQuo?= =?UTF-8?q?ta=E3=80=82=E5=88=A4=E6=8D=AE=EF=BC=9AJdPublishQuotaServiceTest?= =?UTF-8?q?=207=20=E4=BE=8B=E5=85=A8=E7=BB=BF=EF=BC=88=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=205000/0=3D=E4=B8=8D=E9=99=90=E4=B8=8D=E6=9F=A5=E5=BA=93/?= =?UTF-8?q?=E9=94=9A=E5=AE=9A=E5=88=B0=E7=AE=A1=E7=90=86=E5=91=98/?= =?UTF-8?q?=E8=BE=B9=E7=95=8C=E6=81=B0=E5=A5=BD=E7=AD=89=E4=BA=8E=E4=B8=8A?= =?UTF-8?q?=E9=99=90=E6=94=BE=E8=A1=8C/=E7=AB=99=E7=82=B9=E8=B6=85?= =?UTF-8?q?=E7=AE=A1=E8=B1=81=E5=85=8D/=E7=A9=BA=E9=9B=86=20used=3D0/?= =?UTF-8?q?=E8=AF=BB=E4=B8=8D=E5=88=B0=E4=B8=BB=E7=AE=A1=E5=9B=9E=E9=80=80?= =?UTF-8?q?=EF=BC=89=EF=BC=9B=E5=90=8E=E7=AB=AF=20mvn=20-o=20test=20?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E9=9B=B6=E9=94=99=EF=BC=8CJd*Test=20108=20?= =?UTF-8?q?=E4=BE=8B=20107=20=E7=BB=BF=EF=BC=88=E5=94=AF=E4=B8=80=E7=BA=A2?= =?UTF-8?q?=3D=E5=8F=A6=E4=B8=80=E8=B7=AF=E4=BC=9A=E8=AF=9D=E5=9C=A8?= =?UTF-8?q?=E9=A3=9E=E4=BB=B6=20JdProductUpdateServiceTest:182=20=E4=B8=8E?= =?UTF-8?q?=E5=85=B6=20Service:130=20=E7=9A=84=20never()=20=E5=86=B2?= =?UTF-8?q?=E7=AA=81=EF=BC=8C=E4=B8=A4=E6=96=87=E4=BB=B6=E5=9D=87=E5=B1=9E?= =?UTF-8?q?=E5=9C=A8=E9=A3=9E=E6=94=B9=E5=8A=A8=E3=80=81=E4=B8=8E=E6=9C=AC?= =?UTF-8?q?=E7=AC=94=E9=9B=B6=E4=BA=A4=E9=9B=86=EF=BC=8C=E6=9C=AA=E4=BB=A3?= =?UTF-8?q?=E6=94=B9=EF=BC=89=EF=BC=9Bops=20=E9=85=8D=E7=BD=AE=E6=A3=98?= =?UTF-8?q?=E8=BD=AE=20test=5Fno=5Fimplicit=5Fconfig=5Fswitches=207=20pass?= =?UTF-8?q?ed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/jd/mapper/JdProductMapper.java | 8 + .../jd/service/JdPublishQuotaService.java | 103 ++++++++++++ .../resources/application-local.example.yml | 2 + .../src/main/resources/application.yml | 5 + .../resources/mapper/jd/JdProductMapper.xml | 10 ++ .../jd/service/JdPublishQuotaServiceTest.java | 152 ++++++++++++++++++ 6 files changed, 280 insertions(+) create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdPublishQuotaService.java create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdPublishQuotaServiceTest.java 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)); + } +}