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.

152 lines
6.3 KiB

package common.ecommerce.yqn.adapter;
import common.ecommerce.shared.support.Money;
import common.ecommerce.warehouse.model.InboundBatch;
import common.ecommerce.warehouse.model.InboundCarton;
import common.ecommerce.warehouse.model.InboundCommand;
import common.ecommerce.warehouse.model.InboundLine;
import common.ecommerce.yqn.dto.YqnDtos;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
/**
* 运去哪入库计划报文映射(API 97)。
*/
public final class YqnInboundMapper {
private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd");
/** 容器号:4 位大写字母 + 7 位数字。 */
private static final java.util.regex.Pattern CONTAINER_NO =
java.util.regex.Pattern.compile("^[A-Z]{4}\\d{7}$");
private YqnInboundMapper() {
}
/** 统一命令 → 运去哪创建入库计划请求。 */
public static YqnDtos.CreateInboundPlanRequest toRequest(InboundCommand cmd, String warehouseCode) {
requireContainerNo(cmd.containerNo());
YqnDtos.CreateInboundPlanRequest req = new YqnDtos.CreateInboundPlanRequest();
req.warehouseCode = warehouseCode;
req.orderNo = cmd.inboundRequestNo();
req.inventoryType = cmd.inventoryType();
req.batchNumber = cmd.factoryBatchNo();
req.currency = cmd.currency();
req.estimateArriveTime = formatDate(cmd.estimateArriveDate());
req.expectArriveDate = formatDate(cmd.expectArriveDate());
req.predictionShipDate = formatDate(cmd.predictionShipDate());
req.originCountry = cmd.originCountry();
req.shipperName = cmd.shipperName();
req.shipperAddress = cmd.shipperAddress();
req.shipperContact = cmd.shipperContact();
req.shipperPhone = cmd.shipperPhone();
req.containerNo = cmd.containerNo();
req.remark = cmd.remark();
// openapi 只支持自发头程;给 1 会被仓拒绝,这里前置挡掉
req.logisticsType = 0;
req.productItems = toProductItems(cmd.lines());
req.cartonItems = toCartonItems(cmd.cartons());
req.batchItems = toBatchItems(cmd.batches());
return req;
}
private static List<YqnDtos.CreateInboundPlanRequest.InboundProductItem> toProductItems(
List<InboundLine> lines) {
List<YqnDtos.CreateInboundPlanRequest.InboundProductItem> items =
new ArrayList<YqnDtos.CreateInboundPlanRequest.InboundProductItem>(lines.size());
for (InboundLine line : lines) {
YqnDtos.CreateInboundPlanRequest.InboundProductItem item =
new YqnDtos.CreateInboundPlanRequest.InboundProductItem();
item.sku = skuOf(line.providerSku(), line.omsSku());
item.qty = line.quantity();
Money cost = line.cost();
item.cost = cost == null ? null : cost.amount().doubleValue();
items.add(item);
}
return items;
}
private static List<YqnDtos.CreateInboundPlanRequest.InboundCartonItem> toCartonItems(
List<InboundCarton> cartons) {
if (cartons == null || cartons.isEmpty()) {
return null;
}
List<YqnDtos.CreateInboundPlanRequest.InboundCartonItem> items =
new ArrayList<YqnDtos.CreateInboundPlanRequest.InboundCartonItem>(cartons.size());
for (InboundCarton carton : cartons) {
YqnDtos.CreateInboundPlanRequest.InboundCartonItem item =
new YqnDtos.CreateInboundPlanRequest.InboundCartonItem();
item.sku = carton.providerSku();
item.qty = carton.quantity();
item.cartonQty = carton.cartonQuantity() > 0 ? carton.cartonQuantity() : null;
item.cartonNo = carton.cartonNo();
item.sid = carton.sid();
item.cartonWeight = positiveOrNull(carton.weightGrams());
item.cartonLength = positiveOrNull(carton.lengthCm());
item.cartonWidth = positiveOrNull(carton.widthCm());
item.cartonHeight = positiveOrNull(carton.heightCm());
items.add(item);
}
return items;
}
private static List<YqnDtos.CreateInboundPlanRequest.InboundBatchItem> toBatchItems(
List<InboundBatch> batches) {
if (batches == null || batches.isEmpty()) {
return null;
}
List<YqnDtos.CreateInboundPlanRequest.InboundBatchItem> items =
new ArrayList<YqnDtos.CreateInboundPlanRequest.InboundBatchItem>(batches.size());
for (InboundBatch batch : batches) {
YqnDtos.CreateInboundPlanRequest.InboundBatchItem item =
new YqnDtos.CreateInboundPlanRequest.InboundBatchItem();
item.sku = batch.providerSku();
item.cartonNo = batch.cartonNo();
item.qty = batch.quantity();
List<YqnDtos.CreateInboundPlanRequest.BatchAttr> params =
new ArrayList<YqnDtos.CreateInboundPlanRequest.BatchAttr>(batch.params().size());
for (InboundBatch.BatchParam param : batch.params()) {
YqnDtos.CreateInboundPlanRequest.BatchAttr attr =
new YqnDtos.CreateInboundPlanRequest.BatchAttr();
attr.batchAttriKey = param.key();
attr.batchAttriValue = param.value();
params.add(attr);
}
item.batchParam = params;
items.add(item);
}
return items;
}
/** 运去哪对集装箱号有格式要求,传错会被仓拒绝;本地先挡,省一个来回。 */
static void requireContainerNo(String containerNo) {
if (containerNo == null || containerNo.trim().isEmpty()) {
return;
}
if (!CONTAINER_NO.matcher(containerNo.trim()).matches()) {
throw new IllegalArgumentException(
"yqn containerNo must be 4 uppercase letters + 7 digits, got: " + containerNo);
}
}
static String formatDate(LocalDate date) {
return date == null ? null : DATE.format(date);
}
static String skuOf(String providerSku, String omsSku) {
if (providerSku != null && !providerSku.trim().isEmpty()) {
return providerSku.trim();
}
return omsSku;
}
private static Double positiveOrNull(double v) {
return v > 0 ? v : null;
}
}