feat(jd): 商品上架数量上限配置(默认 5000,管理员+名下员工合计口径,2026-11-15 用户口径)——aiimage.jd.max-products-per-admin(env AIIMAGE_JD_MAX_PRODUCTS_PER_ADMIN,0=不限,yml 与 @Value 同默认、application-local.example.yml 同步)+ 新增 JdPublishQuotaService 配额原语:计数源=biz_jd_product 按 owner uid 集计行数(JdProductMapper.countByUserIds,SQL 收口 XML,空集服务侧按 0 不进 SQL);uid 集复用 UserDataScopeSupport.resolveVisibleUserIds(管理员=本人+名下员工/分组成员+created_by_id 兜底,公司超管=全公司),普通账号先锚定 created_by_id 指向的管理员(读不到回退自己);站点超管豁免(走 DeviceSessionPolicy.isSuperAdmin 单出口)。出口两个:quotaOf 只读快照(供上架界面展示余量)+ assertWithinQuota 建上架单前的配额闸(超限抛 BusinessException,文案含占用/上限数字)。⚠ 本笔无生产调用方——商品上架功能目前是用户端占位卡(建设中、后端零代码),本笔只落配置与可测配额服务,不改任何现有链路行为;上架功能落地时在其建单入口调用 assertWithinQuota。判据:JdPublishQuotaServiceTest 7 例全绿(默认 5000/0=不限不查库/锚定到管理员/边界恰好等于上限放行/站点超管豁免/空集 used=0/读不到主管回退);后端 mvn -o test 编译零错,Jd*Test 108 例 107 绿(唯一红=另一路会话在飞件 JdProductUpdateServiceTest:182 与其 Service:130 的 never() 冲突,两文件均属在飞改动、与本笔零交集,未代改);ops 配置棘轮 test_no_implicit_config_switches 7 passed
This commit is contained in:
@@ -54,6 +54,14 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
|
||||
@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<Long> userIds);
|
||||
|
||||
/**
|
||||
* 后管只读页分页查询(synced_at 倒序)。
|
||||
*
|
||||
|
||||
+103
@@ -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})。
|
||||
*
|
||||
* <p>计数口径=团队在京东数据库({@code biz_jd_product})占有的行数:上架功能的数据源
|
||||
* 就是京东数据库,团队占有量即「可上架量」的上游闸。归属列 {@code user_id}=任务 owner
|
||||
* (V193 物化列),uid 集由 {@link UserDataScopeSupport#resolveVisibleUserIds} 统一解析——
|
||||
* 管理员=本人+名下员工(数据权限分组成员,无分组回退 {@code created_by_id} 兜底);
|
||||
* 公司超管=全公司成员;普通账号先锚定到 {@code created_by_id} 指向的管理员再取团队集,
|
||||
* 读不到主管回退按自己计。站点超管不限额。</p>
|
||||
*
|
||||
* <p>两个出口:{@link #quotaOf} 只读快照(供上架界面展示余量);{@link #assertWithinQuota}
|
||||
* 建上架单前的配额闸,超限抛 {@link BusinessException}。上限 ≤0(0=不限)整体放行。
|
||||
* 商品上架建单入口落地时调用 {@link #assertWithinQuota}——本服务是配额原语,
|
||||
* 不自行改变任何现有链路行为。</p>
|
||||
*/
|
||||
@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<Long> 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<Long> 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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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 口径一致)。
|
||||
|
||||
@@ -148,4 +148,14 @@
|
||||
GROUP BY p.category_path
|
||||
ORDER BY p.category_path
|
||||
</select>
|
||||
|
||||
<!-- 商品上架配额计数(JdPublishQuotaService):按 owner uid 集计 biz_jd_product 行数;
|
||||
uid 集=管理员本人+名下员工(公司超管=全公司成员),由服务侧保证非空
|
||||
(空集按 0 处理,不进 SQL)。user_id=任务 owner(V193 物化归属列)。 -->
|
||||
<select id="countByUserIds" resultType="long">
|
||||
SELECT COUNT(*)
|
||||
FROM biz_jd_product
|
||||
WHERE user_id IN
|
||||
<foreach collection="userIds" item="uid" open="(" separator="," close=")">#{uid}</foreach>
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
+152
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user