fix:需求单下推校验eas库存

This commit is contained in:
zhaoyf
2026-10-14 10:36:17 +08:00
parent 3f95aedfeb
commit a43c7ce075

View File

@@ -19,6 +19,8 @@ import org.nl.wms.basedata_manage.service.dao.MdMeMaterialbase;
import org.nl.wms.basedata_manage.service.dao.Structattr;
import org.nl.wms.ext_manage.api.EasApi;
import org.nl.wms.ext_manage.api.MesApi;
import org.nl.wms.ext_manage.api.request.QueryEasStockRequest;
import org.nl.wms.ext_manage.api.response.QueryEasStockResponse;
import org.nl.wms.ext_manage.enums.EXTConstant;
import org.nl.wms.ext_manage.service.WmsToZDWmdService;
import org.nl.wms.ext_manage.service.dto.ZDInventory;
@@ -397,6 +399,8 @@ public class PmDemandServiceImpl extends ServiceImpl<PmDemandMapper, PmDemand> i
List<PmDemandDto> zdDemands = partitioned.get(true);
List<PmDemandDto> otherDemands = partitioned.get(false);
if (!CollectionUtils.isEmpty(otherDemands)) {
validateEasInventory(otherDemands);
JSONObject billParam = convertDemandsToBillParam(otherDemands);
iOutBillService.saveBill(billParam);
this.update(new LambdaUpdateWrapper<PmDemand>()
@@ -459,6 +463,72 @@ public class PmDemandServiceImpl extends ServiceImpl<PmDemandMapper, PmDemand> i
}
/**
* 下发前按分配仓库和物料汇总需求数量,并校验 EAS 实际库存。
* 任一物料库存不足时直接终止整个批次,避免已创建部分下发单据。
*/
private void validateEasInventory(List<PmDemandDto> demands) {
Map<String, Map<String, BigDecimal>> requiredQtyByWarehouse = new LinkedHashMap<>();
for (PmDemandDto demand : demands) {
String warehouseCode = StringUtils.trimToNull(demand.getInventoryDis());
String materialCode = StringUtils.trimToNull(demand.getSkuCode());
if (warehouseCode == null) {
throw new BadRequestException("下推失败,需求单" + demand.getWorkOrder() + "未分配仓库");
}
if (materialCode == null) {
throw new BadRequestException("下推失败,需求单" + demand.getWorkOrder() + "物料编码为空");
}
if (demand.getQty() == null || demand.getQty().compareTo(BigDecimal.ZERO) <= 0) {
throw new BadRequestException("下推失败,物料" + materialCode + "需求数量必须大于 0");
}
requiredQtyByWarehouse
.computeIfAbsent(warehouseCode, key -> new LinkedHashMap<>())
.merge(materialCode, demand.getQty(), BigDecimal::add);
}
for (Map.Entry<String, Map<String, BigDecimal>> warehouseEntry : requiredQtyByWarehouse.entrySet()) {
String warehouseCode = warehouseEntry.getKey();
QueryEasStockRequest request = new QueryEasStockRequest();
request.setOrgno("01.09.14");
request.setWarehouseno(warehouseCode);
List<QueryEasStockResponse> easStocks;
try {
easStocks = easApi.queryStock(request);
} catch (Exception e) {
log.error("下推需求单前查询 EAS 库存失败,仓库:{}", warehouseCode, e);
throw new BadRequestException("下推失败,查询EAS仓库" + warehouseCode + "库存失败");
}
Map<String, BigDecimal> easQtyByMaterial = new HashMap<>();
if (easStocks != null) {
for (QueryEasStockResponse easStock : easStocks) {
String materialCode = StringUtils.trimToNull(easStock.getMaterialno());
if (materialCode == null || !warehouseEntry.getValue().containsKey(materialCode)) {
continue;
}
try {
BigDecimal stockQty = new BigDecimal(StringUtils.defaultIfBlank(easStock.getQty(), "0").trim());
easQtyByMaterial.merge(materialCode, stockQty, BigDecimal::add);
} catch (NumberFormatException e) {
log.error("下推需求单前读取 EAS 库存数量失败,仓库:{},物料:{},数量:{}",
warehouseCode, materialCode, easStock.getQty());
throw new BadRequestException("下推失败,EAS仓库" + warehouseCode + "物料" + materialCode + "库存数量异常");
}
}
}
for (Map.Entry<String, BigDecimal> requiredEntry : warehouseEntry.getValue().entrySet()) {
BigDecimal easQty = easQtyByMaterial.getOrDefault(requiredEntry.getKey(), BigDecimal.ZERO);
if (easQty.compareTo(requiredEntry.getValue()) < 0) {
throw new BadRequestException("下推失败,EAS仓库" + warehouseCode + "物料" + requiredEntry.getKey()
+ "库存不足:可用" + easQty.stripTrailingZeros().toPlainString()
+ ",需求" + requiredEntry.getValue().stripTrailingZeros().toPlainString());
}
}
}
}
private JSONObject convertDemandsToBillParam(List<PmDemandDto> demands) {
if (CollectionUtils.isEmpty(demands)) {
throw new BadRequestException("需求单数据为空");