You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
8.0 KiB

package common.ecommerce.edayun.adapter;
import common.ecommerce.edayun.dto.EdayunRentDetailDtos;
import common.ecommerce.shared.support.Money;
import common.ecommerce.warehouse.enums.WarehouseFeeType;
import common.ecommerce.warehouse.enums.WarehouseProvider;
import common.ecommerce.warehouse.model.BillingDocument;
import common.ecommerce.warehouse.model.FeeQuery;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 易达云仓租费用明细映射
* ({@code POST /webApi/order/cost/v1/selectUserRentDetailList})。
*
* <p>与另外两个费用接口的区别:</p>
* <ul>
* <li>{@code accountFlow/list} = 账户流水(充值 / 扣费 / 退费 + 余额)</li>
* <li>{@code selectOrderCostInfoList} = 按<b>订单</b>拆的尾程 / 操作费</li>
* <li>本接口 = <b>仓租</b>:按 SKU + 计费日逐条,带租期天数、库存件数、计费体积与单价</li>
* </ul>
*
* <p>金额口径:{@code rentPrice} 是原价减折扣后的实收;缺失时用
* {@code originalPrice - discountPrice} 兜底,两者都没有才记 0。</p>
*/
public final class EdayunRentDetailMapper {
/** 计费时间窗上限 3 个月。 */
static final int MAX_RANGE_MONTHS = 3;
/** 单页上限。 */
static final int MAX_PAGE_SIZE = 200;
/**
* 翻页上限:{@value #MAX_PAGES} 页 × {@value #MAX_PAGE_SIZE} 条。
*
* <p>仓租按 SKU + 天出账,3 个月窗口下条数可能上万;这里设上限防止一次拉爆,
* 超出时适配器<b>显式失败</b>而不是截断——见 docs-warehouse/edayun/TODO.md。</p>
*/
static final int MAX_PAGES = 20;
/** 一次拉取的最大行数;超过即失败。 */
static final int MAX_ROWS = MAX_PAGES * MAX_PAGE_SIZE;
private EdayunRentDetailMapper() {
}
/**
* 统一查询条件 → 仓租明细查询请求。
*
* <p>文档里 {@code billDateBegin} / {@code billDateEnd} 都是必填,所以没有
* 时间窗时<b>本地直接拒绝</b>——仓租没有按单号单独查的口径。</p>
*
* @param providerWarehouseId 仓方仓 id;非空时作为 {@code warehouseIds} 唯一元素
*/
public static EdayunRentDetailDtos.SelectRentDetailRequest toRequest(
FeeQuery query, String providerWarehouseId) {
if (!query.hasSettledRange()) {
throw new IllegalArgumentException(
"edayun rent detail query requires a billing time range (billDateBegin/End);"
+ " refusing to pull all rents");
}
requireStorageOnly(query);
EdayunRentDetailDtos.SelectRentDetailRequest req =
new EdayunRentDetailDtos.SelectRentDetailRequest();
req.pageNum = 1;
req.pageSize = MAX_PAGE_SIZE;
EdayunQueryWindows.requireWithinMonths(query.settledAfter(), query.settledBefore(),
MAX_RANGE_MONTHS, "rent detail");
req.billDateBegin = query.settledAfter().toEpochMilli();
req.billDateEnd = query.settledBefore().toEpochMilli();
Long warehouseId = warehouseIdOf(providerWarehouseId);
req.warehouseIds = warehouseId == null ? null
: Collections.singletonList(warehouseId);
String businessNum = firstNonBlank(query.externalOrderNo(), query.saleOrderNo());
req.businessNums = businessNum == null ? null
: Collections.singletonList(businessNum);
// 仓租是 SKU 维度;只给条码时用条码查,别把条码当 SKU 传
req.skuCodes = hasText(query.providerSku())
? Collections.singletonList(query.providerSku().trim()) : null;
return req;
}
/**
* 本接口只出仓租。
*
* <p>宿主若明确要求别的费用类型,说明用错了接口——显式拒绝而不是返回空集合,
* 否则调用方会误以为「该期间没有作业费」。</p>
*/
private static void requireStorageOnly(FeeQuery query) {
if (query.costType() != null && query.costType() != WarehouseFeeType.STORAGE) {
throw new IllegalArgumentException(
"edayun rent detail query only returns STORAGE cost, got " + query.costType()
+ "; use settlements() or orderCosts() for other fee types");
}
if (query.hasDateRange()) {
throw new IllegalArgumentException(
"edayun rent detail query uses settledAfter/settledBefore (billDateBegin/End),"
+ " not snapshot date range");
}
}
/** 仓方仓 id 是数字;非数字直接拒绝,避免仓侧静默查不到。 */
static Long warehouseIdOf(String providerWarehouseId) {
if (!hasText(providerWarehouseId)) {
return null;
}
try {
return Long.valueOf(providerWarehouseId.trim());
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"edayun warehouseIds must be numeric, got: " + providerWarehouseId
+ " (configure the provider warehouse id as edayun's numeric warehouse id)");
}
}
/**
* 一行仓租明细 → 一条 {@link BillingDocument}。
*
* <p>仓租天然就是仓储费,费用类型固定 {@link WarehouseFeeType#STORAGE},
* 不按 {@code billingName} 关键字猜。</p>
*/
public static BillingDocument toDocument(EdayunRentDetailDtos.RentDetailRow row,
String warehouseCode) {
String currency = hasText(row.currencyCode) ? row.currencyCode.trim() : "USD";
return BillingDocument.builder()
.warehouseCode(firstNonBlank(row.warehouseName, warehouseCode))
.sourceProvider(WarehouseProvider.EDAYUN)
.documentNo(firstNonBlank(row.businessNum, row.orderNo))
// billingId 是计费明细唯一标识,可作为重复拉取的去重键
.serialNumber(row.billingId == null ? null : String.valueOf(row.billingId))
.feeName(firstNonBlank(row.billingName, "仓租"))
.feeType(WarehouseFeeType.STORAGE)
.estimated(false)
.settledAt(row.billingDate == null ? null : Instant.ofEpochMilli(row.billingDate))
.amount(moneyOf(row, currency))
.saleOrderNo(row.orderNo)
.providerSku(row.skuCode)
.build();
}
/** 批量映射,跳过 null 行。 */
public static List<BillingDocument> toDocuments(List<EdayunRentDetailDtos.RentDetailRow> rows,
String warehouseCode) {
if (rows == null || rows.isEmpty()) {
return Collections.emptyList();
}
List<BillingDocument> out = new ArrayList<BillingDocument>(rows.size());
for (EdayunRentDetailDtos.RentDetailRow row : rows) {
if (row != null) {
out.add(toDocument(row, warehouseCode));
}
}
return out;
}
/** 实收金额:{@code rentPrice} 优先,缺失时原价减折扣。 */
static Money moneyOf(EdayunRentDetailDtos.RentDetailRow row, String currency) {
BigDecimal amount = null;
if (row.rentPrice != null) {
amount = BigDecimal.valueOf(row.rentPrice);
} else if (row.originalPrice != null) {
BigDecimal discount = row.discountPrice == null
? BigDecimal.ZERO : BigDecimal.valueOf(row.discountPrice);
amount = BigDecimal.valueOf(row.originalPrice).subtract(discount);
}
return amount == null ? Money.zero(currency) : Money.of(amount, currency);
}
private static String firstNonBlank(String... values) {
for (String v : values) {
if (hasText(v)) {
return v.trim();
}
}
return null;
}
private static boolean hasText(String v) {
return v != null && !v.trim().isEmpty();
}
}