This commit is contained in:
2026-08-04 21:01:53 +08:00
parent 7e544780b4
commit 9b7b08665e
21 changed files with 268 additions and 32 deletions
@@ -35,7 +35,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/** 平台库位管理接口;当前数据模型中企业边界即目标 tenantId。 */
/** 平台库位管理接口;分页支持跨企业查询,写操作仍以目标 tenantId 隔离。 */
@Validated
@RestController
@RequestMapping("/api/platform/v1/locations")
@@ -49,14 +49,16 @@ public class PlatformLocationController {
@GetMapping
@PreAuthorize("hasAuthority('platform:location:list')")
@Operation(summary = "分页查询库位", description = "按目标企业租户、仓库、库区、编码、类型和状态分页查询")
@Operation(summary = "分页查询库位",
description = "默认分页查询全部企业库位;可按目标企业租户、仓库、库区、编码、类型和状态筛选")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "需要 platform:location:list 权限,且仓库须属于目标企业租户")
})
public ApiResponse<PageResult<LocationEntity>> page(
@AuthenticationPrincipal AuthenticatedUser user,
@RequestParam @Min(1) long tenantId,
@Parameter(description = "可选目标企业租户ID;不传时查询全部企业")
@RequestParam(required = false) @Min(1) Long tenantId,
@Valid @ParameterObject PageQuery query) {
requirePlatform(user);
return ApiResponse.success(service.page(tenantId, query));
@@ -8,6 +8,7 @@ import com.baiyue.common.security.AuthenticatedUser;
import com.baiyue.warehouse.dto.PalletRequests;
import com.baiyue.warehouse.service.PalletApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
@@ -40,10 +41,11 @@ public class PlatformPalletController {
@GetMapping
@PreAuthorize("hasAuthority('platform:pallet:list')")
@Operation(summary = "分页查询托盘")
@Operation(summary = "分页查询托盘", description = "默认查询全部企业;可按企业、仓库、托盘编码和类型筛选")
@ApiResponses(@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "需要 platform:pallet:list 权限"))
public ApiResponse<PageResult<PalletRequests.View>> page(@AuthenticationPrincipal AuthenticatedUser user,
@RequestParam @Min(1) long tenantId,
@Parameter(description = "目标企业租户ID;不传时查询全部企业")
@RequestParam(required = false) @Min(1) Long tenantId,
@Valid @ParameterObject PalletRequests.PageQuery query) {
requirePlatform(user);
return ApiResponse.success(service.page(tenantId, query));
@@ -23,9 +23,9 @@ public final class LocationDtos {
@Schema(name = "LocationPageQuery", description = "库位分页查询条件")
public record PageQuery(
@Min(1) long page,
@Min(1) @Max(200) long size,
@NotNull @Min(1) Long warehouseId,
@Min(1) @Schema(description = "页码", defaultValue = "1") long page,
@Min(1) @Max(200) @Schema(description = "每页条数", defaultValue = "20") long size,
@Min(1) @Schema(description = "可选仓库ID;不传时不限制仓库") Long warehouseId,
@Size(max = 50) String zoneCode,
@Size(max = 50) String locationCode,
@Pattern(regexp = "^(SHELF|PALLET|FLOOR|FLOW_RACK)$") String locationType,
@@ -41,7 +41,7 @@ public final class PalletRequests {
public record PageQuery(
@Min(1) @Schema(description = "页码", defaultValue = "1") long page,
@Min(1) @Max(200) @Schema(description = "每页条数", defaultValue = "20") long size,
@Min(1) @Schema(description = "仓库ID") Long warehouseId,
@Min(1) @Schema(description = "仓库ID;不传时不限制仓库") Long warehouseId,
@Size(max = 50) @Schema(description = "托盘编码,支持模糊查询") String palletCode,
@Size(max = 32) @Schema(description = "托盘类型") String palletType
) {
@@ -20,9 +20,13 @@ public interface LocationMapper extends BaseMapper<LocationEntity> {
FROM wh_location l
JOIN wh_warehouse w
ON w.id = l.warehouse_id AND w.tenant_id = l.tenant_id AND w.deleted = 0
WHERE l.tenant_id = #{tenantId}
WHERE l.deleted = 0
<if test="tenantId != null">
AND l.tenant_id = #{tenantId}
</if>
<if test="warehouseId != null">
AND l.warehouse_id = #{warehouseId}
AND l.deleted = 0
</if>
<if test="zoneCode != null and zoneCode != ''">
AND l.zone_code = #{zoneCode}
</if>
@@ -39,8 +43,8 @@ public interface LocationMapper extends BaseMapper<LocationEntity> {
</script>
""")
Page<LocationEntity> selectLocationPage(Page<LocationEntity> page,
@Param("tenantId") long tenantId,
@Param("warehouseId") long warehouseId,
@Param("tenantId") Long tenantId,
@Param("warehouseId") Long warehouseId,
@Param("zoneCode") String zoneCode,
@Param("locationCode") String locationCode,
@Param("locationType") String locationType,
@@ -18,7 +18,7 @@ import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
/** 强制使用租户、仓库联合条件的库位持久化边界。 */
/** 库位持久化边界;平台分页条件可选,资源读写继续强制租户、仓库联合条件。 */
@Repository
public class LocationRepository {
private final LocationMapper locations;
@@ -47,7 +47,7 @@ public class LocationRepository {
.eq(LocationEntity::getId, id));
}
public PageResult<LocationEntity> page(long tenantId, PageQuery query) {
public PageResult<LocationEntity> page(Long tenantId, PageQuery query) {
Page<LocationEntity> result = locations.selectLocationPage(new Page<>(query.page(), query.size()),
tenantId, query.warehouseId(), query.zoneCode(), query.locationCode(), query.locationType(), query.status());
return PageResult.from(result);
@@ -13,7 +13,7 @@ import org.springframework.util.StringUtils;
import java.util.Collection;
import java.util.List;
/** 托盘持久化仓储,所有业务查询均显式包含租户和仓库边界。 */
/** 托盘持久化仓储;平台分页支持跨租户筛选,资源操作始终包含租户和仓库边界。 */
@Repository
public class PalletRepository {
private final PalletMapper mapper;
@@ -22,9 +22,9 @@ public class PalletRepository {
this.mapper = mapper;
}
public PageResult<PalletEntity> page(long tenantId, PalletRequests.PageQuery query) {
public PageResult<PalletEntity> page(Long tenantId, PalletRequests.PageQuery query) {
LambdaQueryWrapper<PalletEntity> wrapper = new LambdaQueryWrapper<PalletEntity>()
.eq(PalletEntity::getTenantId, tenantId)
.eq(tenantId != null, PalletEntity::getTenantId, tenantId)
.eq(query.warehouseId() != null, PalletEntity::getWarehouseId, query.warehouseId())
.like(StringUtils.hasText(query.palletCode()), PalletEntity::getPalletCode, query.palletCode())
.eq(StringUtils.hasText(query.palletType()), PalletEntity::getPalletType, query.palletType())
@@ -16,7 +16,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/** 库位基础资料应用服务,负责 CRUD、租户仓库隔离和启停/删除约束。 */
/** 库位基础资料应用服务,负责平台分页、CRUD、租户仓库隔离和启停/删除约束。 */
@Service
public class LocationApplicationService {
private final LocationRepository repository;
@@ -25,8 +25,7 @@ public class LocationApplicationService {
this.repository = repository;
}
public PageResult<LocationEntity> page(long tenantId, PageQuery query) {
requireWarehouse(tenantId, query.warehouseId(), false);
public PageResult<LocationEntity> page(Long tenantId, PageQuery query) {
return repository.page(tenantId, query);
}
@@ -23,7 +23,7 @@ public class PalletApplicationService {
this.warehouseRepository = warehouseRepository;
}
public PageResult<PalletRequests.View> page(long tenantId, PalletRequests.PageQuery query) {
public PageResult<PalletRequests.View> page(Long tenantId, PalletRequests.PageQuery query) {
PageResult<PalletEntity> source = repository.page(tenantId, query);
return new PageResult<>(source.records().stream().map(PalletApplicationService::view).toList(),
source.page(), source.size(), source.total(), source.pages());
@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -29,13 +30,30 @@ class PlatformLocationControllerContractTest {
assertMethod("delete", DeleteMapping.class, "platform:location:delete");
}
@Test
void 分页查询的企业租户参数可选但写接口仍然必填() {
Method page = method("page");
assertThat(page.getParameterTypes()[1]).isEqualTo(Long.class);
assertThat(page.getParameters()[1].getAnnotation(RequestParam.class).required()).isFalse();
for (String writeMethod : new String[] {"create", "update", "changeStatus", "delete"}) {
Method method = method(writeMethod);
assertThat(method.getParameterTypes()[1]).isEqualTo(long.class);
assertThat(method.getParameters()[1].getAnnotation(RequestParam.class).required()).isTrue();
}
}
private static void assertMethod(String name, Class<? extends Annotation> mapping, String permission) {
Method method = java.util.Arrays.stream(PlatformLocationController.class.getDeclaredMethods())
.filter(candidate -> candidate.getName().equals(name))
.findFirst().orElseThrow();
Method method = method(name);
assertThat(method.getAnnotation(mapping)).isNotNull();
assertThat(method.getAnnotation(PreAuthorize.class).value())
.isEqualTo("hasAuthority('" + permission + "')");
assertThat(method.getAnnotation(Operation.class).summary()).isNotBlank();
}
private static Method method(String name) {
return java.util.Arrays.stream(PlatformLocationController.class.getDeclaredMethods())
.filter(candidate -> candidate.getName().equals(name))
.findFirst().orElseThrow();
}
}
@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -24,15 +25,28 @@ class PlatformPalletControllerContractTest {
assertMethod("create", PostMapping.class, "platform:pallet:create");
assertMethod("update", PutMapping.class, "platform:pallet:edit");
assertMethod("delete", DeleteMapping.class, "platform:pallet:delete");
Method page = method("page");
assertThat(page.getParameterTypes()[1]).isEqualTo(Long.class);
assertThat(page.getParameters()[1].getAnnotation(RequestParam.class).required()).isFalse();
for (String writeMethod : new String[]{"detail", "create", "update", "delete"}) {
Method method = method(writeMethod);
assertThat(method.getParameterTypes()[1]).isEqualTo(long.class);
assertThat(method.getParameters()[1].getAnnotation(RequestParam.class).required()).isTrue();
}
assertThat(Arrays.stream(PlatformPalletController.class.getDeclaredMethods())
.noneMatch(method -> method.getName().equals("status"))).isTrue();
}
private static void assertMethod(String name, Class<? extends Annotation> mapping, String authority) {
Method method = Arrays.stream(PlatformPalletController.class.getDeclaredMethods())
.filter(candidate -> candidate.getName().equals(name)).findFirst().orElseThrow();
Method method = method(name);
assertThat(method.getAnnotation(mapping)).isNotNull();
assertThat(method.getAnnotation(PreAuthorize.class).value())
.isEqualTo("hasAuthority('" + authority + "')");
}
private static Method method(String name) {
return Arrays.stream(PlatformPalletController.class.getDeclaredMethods())
.filter(candidate -> candidate.getName().equals(name)).findFirst().orElseThrow();
}
}
@@ -1,5 +1,6 @@
package com.baiyue.warehouse.mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.junit.jupiter.api.Test;
@@ -9,6 +10,22 @@ import java.lang.reflect.Method;
import static org.assertj.core.api.Assertions.assertThat;
class LocationMapperContractTest {
@Test
void 平台分页的企业和仓库条件均为可选且仍返回仓库快照() throws NoSuchMethodException {
Method page = LocationMapper.class.getMethod("selectLocationPage", Page.class,
Long.class, Long.class, String.class, String.class, String.class, String.class);
String pageSql = sql(page.getAnnotation(Select.class).value());
assertThat(pageSql)
.contains("SELECT l.*, w.warehouse_code, w.warehouse_name")
.contains("w.id = l.warehouse_id AND w.tenant_id = l.tenant_id")
.contains("<if test=\"tenantId != null\">")
.contains("AND l.tenant_id = #{tenantId}")
.contains("<if test=\"warehouseId != null\">")
.contains("AND l.warehouse_id = #{warehouseId}")
.contains("ORDER BY l.created_at DESC, l.id DESC");
}
@Test
void 可用库位按库区和库位数字自然顺序临近分配() throws NoSuchMethodException {
String selectSql = sql(LocationMapper.class.getMethod(
@@ -0,0 +1,78 @@
package com.baiyue.warehouse.repository;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baiyue.warehouse.dto.PalletRequests;
import com.baiyue.warehouse.entity.PalletEntity;
import com.baiyue.warehouse.mapper.PalletMapper;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.Collection;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class PalletRepositoryTest {
@BeforeAll
static void 初始化实体字段缓存() {
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new Configuration(), "test"), PalletEntity.class);
}
@Test
void 平台分页默认不限制企业和仓库() {
Collection<Object> values = queryValues(null, null);
assertThat(values).isEmpty();
}
@Test
void 平台分页可单独按企业筛选() {
Collection<Object> values = queryValues(10001L, null);
assertThat(values).containsExactly(10001L);
}
@Test
void 平台分页可同时按企业和仓库筛选() {
Collection<Object> values = queryValues(10001L, 20L);
assertThat(values).containsExactlyInAnyOrder(10001L, 20L);
}
@Test
void 平台分页可按全局唯一仓库筛选且写查询仍保留联合边界() {
Collection<Object> pageValues = queryValues(null, 20L);
assertThat(pageValues).containsExactly(20L);
PalletMapper mapper = mapper();
new PalletRepository(mapper).findOwned(10001L, 20L, 30L);
@SuppressWarnings("unchecked")
ArgumentCaptor<LambdaQueryWrapper<PalletEntity>> wrapper = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(mapper).selectOne(wrapper.capture());
wrapper.getValue().getSqlSegment();
assertThat(wrapper.getValue().getParamNameValuePairs().values())
.containsExactlyInAnyOrder(10001L, 20L, 30L);
}
private static Collection<Object> queryValues(Long tenantId, Long warehouseId) {
PalletMapper mapper = mapper();
new PalletRepository(mapper).page(tenantId,
new PalletRequests.PageQuery(1, 20, warehouseId, null, null));
@SuppressWarnings("unchecked")
ArgumentCaptor<LambdaQueryWrapper<PalletEntity>> wrapper = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(mapper).selectPage(any(Page.class), wrapper.capture());
wrapper.getValue().getSqlSegment();
return wrapper.getValue().getParamNameValuePairs().values();
}
private static PalletMapper mapper() {
PalletMapper mapper = mock(PalletMapper.class);
when(mapper.selectPage(any(Page.class), any())).thenAnswer(invocation -> invocation.getArgument(0));
return mapper;
}
}
@@ -1,6 +1,7 @@
package com.baiyue.warehouse.service;
import com.baiyue.common.core.exception.BizException;
import com.baiyue.warehouse.dto.LocationDtos.PageQuery;
import com.baiyue.warehouse.dto.LocationDtos.SaveRequest;
import com.baiyue.warehouse.entity.LocationEntity;
import com.baiyue.warehouse.entity.WarehouseEntity;
@@ -14,9 +15,44 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
class LocationApplicationServiceTest {
@Test
void 平台分页不传企业和仓库时查询全部库位() {
LocationRepository repository = mock(LocationRepository.class);
LocationApplicationService service = new LocationApplicationService(repository);
PageQuery query = pageQuery(null);
service.page(null, query);
verify(repository).page(null, query);
verifyNoMoreInteractions(repository);
}
@Test
void 平台分页可只按企业筛选全部仓库() {
LocationRepository repository = mock(LocationRepository.class);
LocationApplicationService service = new LocationApplicationService(repository);
PageQuery query = pageQuery(null);
service.page(10001L, query);
verify(repository).page(10001L, query);
}
@Test
void 平台分页可同时按企业和仓库筛选() {
LocationRepository repository = mock(LocationRepository.class);
LocationApplicationService service = new LocationApplicationService(repository);
PageQuery query = pageQuery(88L);
service.page(10001L, query);
verify(repository).page(10001L, query);
}
@Test
void 新增时仓库必须属于当前企业租户() {
LocationRepository repository = mock(LocationRepository.class);
@@ -79,6 +115,10 @@ class LocationApplicationServiceTest {
"PALLET", new BigDecimal("500"), status, "备注", version);
}
private static PageQuery pageQuery(Long warehouseId) {
return new PageQuery(1, 20, warehouseId, null, null, null, null);
}
private static WarehouseEntity warehouse(String status) {
WarehouseEntity warehouse = new WarehouseEntity();
warehouse.setId(88L);
@@ -1,6 +1,8 @@
package com.baiyue.warehouse.service;
import com.baiyue.common.core.exception.BizException;
import com.baiyue.common.core.api.PageResult;
import com.baiyue.warehouse.dto.PalletRequests;
import com.baiyue.warehouse.entity.PalletEntity;
import com.baiyue.warehouse.repository.PalletRepository;
import com.baiyue.warehouse.repository.WarehouseRepository;
@@ -8,6 +10,7 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class PalletApplicationServiceTest {
@@ -28,6 +31,17 @@ class PalletApplicationServiceTest {
assertThatThrownBy(() -> service.detail(10001, 21, 30)).isInstanceOf(BizException.class);
}
@Test
void 平台分页允许不指定企业并保留分页结果() {
PalletRequests.PageQuery query = new PalletRequests.PageQuery(1, 20, null, null, null);
when(repository.page(null, query)).thenReturn(new PageResult<>(java.util.List.of(pallet("AVAILABLE")),
1, 20, 1, 1));
org.assertj.core.api.Assertions.assertThat(service.page(null, query).records())
.singleElement().extracting("tenantId", "warehouseId").containsExactly(10001L, 20L);
verify(repository).page(null, query);
}
private static PalletEntity pallet(String status) {
PalletEntity entity = new PalletEntity();
entity.setId(30L);