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.

198 lines
8.6 KiB

package common.ecommerce.xincang.adapter;
import common.ecommerce.shared.error.ChannelException;
import common.ecommerce.shared.error.ValidationException;
import common.ecommerce.warehouse.enums.WarehouseProvider;
import common.ecommerce.warehouse.model.TransferAttachment;
import common.ecommerce.warehouse.model.TransferCommand;
import common.ecommerce.warehouse.model.TransferOrder;
import common.ecommerce.warehouse.enums.TransferType;
import common.ecommerce.warehouse.sdk.WarehouseContext;
import common.ecommerce.xincang.dto.XincangDtos;
import common.ecommerce.xincang.enums.XincangTransferState;
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.expressPrintCopies = copies(cmd.expressLabelCopies());
req.palletPrintCopies = copies(cmd.palletLabelCopies());
req.productLabelAtt = attachment(cmd.productLabelAttachment());
req.ctnLabelAtt = attachment(cmd.cartonLabelAttachment());
req.expressLabelAtt = attachment(cmd.expressLabelAttachment());
req.palletLabelAtt = attachment(cmd.palletLabelAttachment());
req.otherAtt = attachments(cmd.otherAttachments());
req.bolAtt = attachments(cmd.bolAttachments());
return req;
}
/** 统一附件 → 信仓附件报文;null 透传(不发)。 */
private static XincangDtos.Attachment attachment(TransferAttachment source) {
if (source == null) {
return null;
}
XincangDtos.Attachment target = new XincangDtos.Attachment();
target.type = source.type();
target.bucket = source.bucket();
target.fileName = source.fileName();
target.fileUrl = source.fileUrl();
target.fileType = source.fileType();
target.name = source.name();
return target;
}
/** 附件列表;空列表返回 null(不发空数组)。 */
private static List<XincangDtos.Attachment> attachments(List<TransferAttachment> sources) {
if (sources == null || sources.isEmpty()) {
return null;
}
List<XincangDtos.Attachment> out = new ArrayList<XincangDtos.Attachment>(sources.size());
for (TransferAttachment source : sources) {
XincangDtos.Attachment target = attachment(source);
if (target != null) {
out.add(target);
}
}
return out.isEmpty() ? null : out;
}
/** 转运单创建结果 → 统一模型;信仓只回 {@code order_no},其余字段留空。 */
public static TransferOrder toCreated(TransferCommand cmd, String providerTransferNo) {
// 新增成功后处于待审核(state=0);审核走 approve
String rawState = XincangTransferState.PENDING_REVIEW.rawCode();
return TransferOrder.builder()
.transferRequestNo(cmd.transferRequestNo())
.providerTransferNo(providerTransferNo)
.fromWarehouseCode(cmd.fromWarehouseCode())
.toWarehouseCode(cmd.toWarehouseCode())
.type(cmd.type())
.status(XincangTransferState.toCore(rawState))
.rawStatus(rawState)
.sourceProvider(WarehouseProvider.XINCANG)
.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());
product.fnSku = blankToNull(line.fulfillmentSku());
product.ctnSku = blankToNull(line.cartonSku());
product.remark = blankToNull(line.remark());
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));
}
}