fix(order): load all warehouse template options

This commit is contained in:
2026-09-03 17:04:57 +08:00
parent 3664f036cc
commit 253ca82b99
2 changed files with 102 additions and 8 deletions
@@ -34,6 +34,7 @@ import java.util.function.Function;
@Service
public class MerchantBatchImportService {
private static final int MAX_ROWS = 50;
private static final int WAREHOUSE_PAGE_SIZE = 200;
private static final long MAX_BYTES = 5L * 1024 * 1024;
@Resource private MerchantBatchDraftService drafts;
@Resource private OrderBatchFileClient files;
@@ -216,14 +217,7 @@ public class MerchantBatchImportService {
private Catalog catalog(long tenantId, String authorization) {
if (!StringUtils.hasText(authorization)) throw new BizException(ResultCode.UNAUTHORIZED, "登录凭证缺失");
try {
var warehouseResponse = warehouses.page(authorization, 1, 200, "ACTIVE");
if (warehouseResponse == null || !warehouseResponse.successful() || warehouseResponse.data() == null) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "仓库选项暂不可用");
}
List<MerchantWarehouseCatalogClient.Warehouse> allWarehouses = warehouseResponse.data().records();
if (warehouseResponse.data().pages() > 1) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "可用仓库超过模板支持范围,请联系平台处理");
}
List<MerchantWarehouseCatalogClient.Warehouse> allWarehouses = activeWarehouses(authorization);
var receiving = optionMap(allWarehouses.stream().filter(value -> "DOMESTIC".equals(value.warehouseType())).toList(),
value -> label(value.warehouseName(), value.warehouseCode()));
if (receiving.isEmpty()) throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "暂无可用国内收货仓");
@@ -255,6 +249,57 @@ public class MerchantBatchImportService {
}
}
private List<MerchantWarehouseCatalogClient.Warehouse> activeWarehouses(String authorization) {
Map<Long, MerchantWarehouseCatalogClient.Warehouse> unique = new LinkedHashMap<>();
Long expectedTotal = null;
Long expectedPages = null;
long fetchedRows = 0;
for (long requestedPage = 1; ; requestedPage++) {
var response = warehouses.page(authorization, requestedPage, WAREHOUSE_PAGE_SIZE, "ACTIVE");
if (response == null || !response.successful() || response.data() == null) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "仓库选项第" + requestedPage + "页暂不可用");
}
var page = response.data();
if (page.page() != requestedPage || page.size() != WAREHOUSE_PAGE_SIZE) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "仓库选项分页响应异常");
}
long calculatedPages = page.total() == 0 ? 0 : ((page.total() - 1) / page.size()) + 1;
if (page.pages() != calculatedPages) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "仓库选项分页统计异常");
}
if (expectedTotal == null) {
expectedTotal = page.total();
expectedPages = page.pages();
} else if (expectedTotal != page.total() || expectedPages != page.pages()) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "仓库选项分页数据发生变化,请重试");
}
if (requestedPage > Math.max(1L, expectedPages)) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "仓库选项分页响应异常");
}
List<MerchantWarehouseCatalogClient.Warehouse> records = page.records();
if (expectedPages > 0 && records.isEmpty()) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "仓库选项分页数据不完整");
}
fetchedRows += records.size();
for (var warehouse : records) {
if (warehouse == null || warehouse.id() == null || warehouse.id() <= 0
|| !"ACTIVE".equals(warehouse.status())) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "仓库选项数据异常");
}
var previous = unique.putIfAbsent(warehouse.id(), warehouse);
if (previous != null && !previous.equals(warehouse)) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "仓库选项分页数据发生变化,请重试");
}
}
if (requestedPage >= expectedPages) {
if (fetchedRows != expectedTotal) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "仓库选项分页数据不完整");
}
return List.copyOf(unique.values());
}
}
}
private static Map<String, CustomerAddressDtos.Response> addressOptions(List<CustomerAddressDtos.Response> values) {
return optionMap(values, value -> String.join("|", value.addressName(), value.contactName(), value.city(), value.addressLine()));
}