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.

155 lines
6.6 KiB

package common.ecommerce.xincang.adapter;
import common.ecommerce.shared.error.ChannelException;
import common.ecommerce.shared.error.ValidationException;
import common.ecommerce.warehouse.WarehouseProvider;
import common.ecommerce.warehouse.model.TransferCommand;
import common.ecommerce.warehouse.model.TransferOrder;
import common.ecommerce.warehouse.model.TransferStatus;
import common.ecommerce.warehouse.model.TransferType;
import common.ecommerce.warehouse.sdk.WarehouseContext;
import common.ecommerce.xincang.dto.XincangDtos;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
/**
* 信仓转运 / 调拨报文映射({@code POST fba.do_fba})。
*
* <p>信仓的「转运」= 从信仓发货到平台仓(FBA / 其它),在统一模型里对应
* {@link TransferType#OUTBOUND}:发货仓是信仓自己,目的仓是平台仓。</p>
*
* <p>字典字段({@code order_type} / {@code delivery_mode} / {@code delivery_way} /
* {@code label_requirement})信仓只认它自己字典的原值,调用方需从字典接口取;
* SDK 不做枚举归一,也不猜测取值。</p>
*/
public final class XincangTransferMapper {
private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd");
/** 目的仓不在字典里时的固定写法(文档要求)。 */
static final String OTHER_WAREHOUSE_NAME = "其他仓库";
/** 标签需求多选分隔符。 */
private static final String LABEL_SEPARATOR = ",";
private XincangTransferMapper() {
}
/** 统一命令 → 信仓新增转运请求。 */
public static XincangDtos.TransferRequest toRequest(TransferCommand cmd, WarehouseContext ctx,
String productsJson) {
if (cmd.type() != TransferType.OUTBOUND) {
throw validation(ctx, "xincang transfer only supports OUTBOUND (warehouse -> platform), got "
+ cmd.type());
}
XincangDtos.TransferRequest req = new XincangDtos.TransferRequest();
req.fbaNo = cmd.transferRequestNo();
req.po = firstNonBlank(cmd.po(), cmd.transferRequestNo());
req.fromWarehouseName = require(cmd.fromWarehouseCode(),
"fromWarehouseCode (信仓 from_warehouse_name)", ctx);
req.destWarehouseName = require(cmd.toWarehouseCode(),
"toWarehouseCode (信仓 dest_warehouse_name)", ctx);
req.otherWarehouse = blankToNull(cmd.otherWarehouseName());
req.orderType = blankToNull(cmd.orderType());
req.deliveryMode = require(cmd.deliveryMode(), "deliveryMode (信仓转运类型字典值)", ctx);
req.deliveryWay = require(cmd.deliveryWay(), "deliveryWay (信仓转运方式字典值)", ctx);
req.preSendTime = formatDate(cmd.estimateOutboundDate());
if (req.preSendTime == null) {
throw validation(ctx, "estimateOutboundDate is required (信仓 pre_send_time)");
}
if (cmd.palletRequired() == null) {
throw validation(ctx, "palletRequired is required (信仓 is_pallet)");
}
req.isPallet = booleanText(cmd.palletRequired());
req.labelRequirement = cmd.labelRequirements().isEmpty() ? null : join(cmd.labelRequirements());
req.products = productsJson;
req.remark = blankToNull(cmd.remark());
req.productPrintCopies = copies(cmd.productLabelCopies());
req.ctnPrintCopies = copies(cmd.cartonLabelCopies());
req.palletPrintCopies = copies(cmd.palletLabelCopies());
return req;
}
/** 转运单创建结果 → 统一模型;信仓只回 {@code order_no},其余字段留空。 */
public static TransferOrder toCreated(TransferCommand cmd, String providerTransferNo) {
return TransferOrder.builder()
.transferRequestNo(cmd.transferRequestNo())
.providerTransferNo(providerTransferNo)
.fromWarehouseCode(cmd.fromWarehouseCode())
.toWarehouseCode(cmd.toWarehouseCode())
.type(cmd.type())
// 新增成功后处于待审核;审核走 approve
.status(TransferStatus.PENDING_REVIEW)
.rawStatus("0")
.build();
}
/** 产品明细:{@code [{"product_no":"SKU","sku_count":"1"}]}。 */
public static List<XincangDtos.TransferProduct> products(TransferCommand cmd) {
List<XincangDtos.TransferProduct> products =
new ArrayList<XincangDtos.TransferProduct>(cmd.lines().size());
for (TransferCommand.TransferLine line : cmd.lines()) {
XincangDtos.TransferProduct product = new XincangDtos.TransferProduct();
product.productNo = line.providerSku();
product.skuCount = String.valueOf(line.quantity());
products.add(product);
}
return products;
}
/** 打托:true → 是 / false → 否 / null → 不传。 */
private static String booleanText(Boolean value) {
if (value == null) {
return null;
}
return value ? "是" : "否";
}
private static String copies(Integer copies) {
return copies == null ? null : String.valueOf(copies);
}
private static String formatDate(LocalDate date) {
return date == null ? null : DATE.format(date);
}
private static String join(List<String> values) {
StringBuilder sb = new StringBuilder();
for (String value : values) {
if (sb.length() > 0) {
sb.append(LABEL_SEPARATOR);
}
sb.append(value);
}
return sb.toString();
}
private static String require(String value, String field, WarehouseContext ctx) {
if (value == null || value.trim().isEmpty()) {
throw validation(ctx, field + " is required for xincang transfer");
}
return value.trim();
}
private static String firstNonBlank(String a, String fallback) {
if (a != null && !a.trim().isEmpty()) {
return a.trim();
}
return fallback;
}
private static String blankToNull(String v) {
return v == null || v.trim().isEmpty() ? null : v.trim();
}
private static ValidationException validation(WarehouseContext ctx, String message) {
return new ValidationException(ChannelException.builder()
.message(message)
.operation("warehouse.transfer.create")
.provider(WarehouseProvider.XINCANG.name())
.requestId(ctx.requestId())
.retryable(false));
}
}