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.

179 lines
6.7 KiB

package common.ecommerce.yqn.adapter;
import common.ecommerce.shared.support.Money;
import common.ecommerce.warehouse.model.BillingDocument;
import common.ecommerce.warehouse.model.FeeQuery;
import common.ecommerce.warehouse.model.WarehouseFeeType;
import common.ecommerce.warehouse.sdk.WarehouseContext;
import common.ecommerce.yqn.dto.YqnDtos;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
/**
* 运去哪费用报文映射(API 133)。
*/
public final class YqnFeeMapper {
/** 运去哪所有金额都是 USD。 */
private static final String CURRENCY = "USD";
/** 仓储费按日期查的上限。 */
static final int MAX_SNAPSHOT_DAYS = 30;
private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final DateTimeFormatter TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final ZoneId DEFAULT_ZONE = ZoneId.of("Asia/Shanghai");
private YqnFeeMapper() {
}
/**
* 统一查询条件 → 运去哪请求。
*
* <p>运去哪的约束:</p>
* <ul>
* <li>作业费 / 尾程费按 {@code saleOrderNo} 查,仓储费 / 增值服务费按 {@code billingTransNo} 查;
* 按日期查<b>只支持仓储费</b>,且区间不超过 30 天</li>
* <li>{@code saleOrderNo} 在运去哪是数字,非数字直接拒绝——传错类型仓侧会静默查不到</li>
* </ul>
*/
public static YqnDtos.CostDetailRequest toRequest(FeeQuery query) {
if (query.isEmpty()) {
throw new IllegalArgumentException(
"yqn fee query requires at least one condition; refusing to pull all costs");
}
YqnDtos.CostDetailRequest req = new YqnDtos.CostDetailRequest();
req.saleOrderNo = saleOrderNoOf(query.saleOrderNo());
req.billingTransNo = query.billingTransNo();
req.costType = costTypeOf(query.costType());
req.sku = query.providerSku();
req.externalOrderNo = query.externalOrderNo();
if (query.hasDateRange()) {
requireStorageOnly(query);
req.snapshotStartDate = DATE.format(query.snapshotStartDate());
req.snapshotEndDate = DATE.format(query.snapshotEndDate());
}
return req;
}
/** 运去哪的要求:按时间查只能查仓储费。 */
private static void requireStorageOnly(FeeQuery query) {
if (query.costType() != WarehouseFeeType.STORAGE) {
throw new IllegalArgumentException(
"yqn supports snapshot date range only for STORAGE cost; "
+ "operation / last-mile costs must be queried by saleOrderNo, "
+ "value-added by billingTransNo");
}
long days = java.time.temporal.ChronoUnit.DAYS.between(
query.snapshotStartDate(), query.snapshotEndDate());
if (days < 0 || days > MAX_SNAPSHOT_DAYS) {
throw new IllegalArgumentException(
"yqn snapshot date range must be within " + MAX_SNAPSHOT_DAYS + " days, got " + days);
}
}
private static Long saleOrderNoOf(String raw) {
if (raw == null || raw.trim().isEmpty()) {
return null;
}
try {
return Long.valueOf(raw.trim());
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"yqn saleOrderNo must be numeric, got: " + raw
+ " (use the warehouse order id, not your own order number)");
}
}
/** 业务类型 → 运去哪 costType;null 不限制。 */
static Integer costTypeOf(WarehouseFeeType type) {
if (type == null) {
return null;
}
switch (type) {
case OPERATION:
return 1;
case STORAGE:
return 2;
case VALUE_ADDED:
return 3;
case LAST_MILE:
return 4;
default:
return null;
}
}
/** 一条运去哪费用 → 统一账单。 */
public static BillingDocument toDocument(YqnDtos.CostDetail detail, String warehouseCode, ZoneId zone) {
return BillingDocument.builder()
.warehouseCode(firstNonBlank(detail.warehouseCode, warehouseCode))
.feeName(detail.costName)
.feeType(feeTypeOf(detail.costName))
.settledAt(parseTime(detail.settlementTime, zone))
// 1 实际结算 → estimated=false;2 预估结算 → true
.estimated(detail.settlementType != null && detail.settlementType == 2)
.amount(detail.costAmount == null ? Money.zero(CURRENCY)
: Money.of(java.math.BigDecimal.valueOf(detail.costAmount), CURRENCY))
.saleOrderNo(detail.saleOrderNo == null ? null : String.valueOf(detail.saleOrderNo))
.build();
}
/**
* 按费用名称归类。
*
* <p>运去哪只给中文名,没有类型码(查询用的 {@code costType} 不回报)。
* 认不出来就留 null,不猜——对账时按名字分类比猜错安全。</p>
*/
static WarehouseFeeType feeTypeOf(String costName) {
if (costName == null) {
return null;
}
String name = costName.trim();
if (name.contains("仓储") || name.contains("仓租")) {
return WarehouseFeeType.STORAGE;
}
if (name.contains("尾程") || name.contains("运费") || name.contains("快递")) {
return WarehouseFeeType.LAST_MILE;
}
if (name.contains("增值")) {
return WarehouseFeeType.VALUE_ADDED;
}
if (name.contains("操作") || name.contains("作业")) {
return WarehouseFeeType.OPERATION;
}
return null;
}
static Instant parseTime(String value, ZoneId zone) {
if (value == null || value.trim().isEmpty()) {
return null;
}
String v = value.trim();
try {
return java.time.LocalDateTime.parse(v, TIME)
.atZone(zone == null ? DEFAULT_ZONE : zone).toInstant();
} catch (DateTimeParseException e) {
try {
return Instant.parse(v);
} catch (DateTimeParseException ignored) {
return null;
}
}
}
private static String firstNonBlank(String a, String b) {
if (a != null && !a.trim().isEmpty()) {
return a.trim();
}
return b == null ? null : b.trim();
}
}