|
|
|
|
@@ -3,6 +3,8 @@ package org.nl.wms.bi_manage.service.impl;
|
|
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
|
|
|
import cn.hutool.poi.excel.ExcelWriter;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
@@ -19,10 +21,10 @@ import org.nl.wms.basedata_manage.service.dao.MdMeMaterialbase;
|
|
|
|
|
import org.nl.wms.bi_manage.service.IBiStockService;
|
|
|
|
|
import org.nl.wms.bi_manage.service.dto.BiStockReconcileResult;
|
|
|
|
|
import org.nl.wms.bi_manage.service.dto.BiStockReconcileRow;
|
|
|
|
|
import org.nl.wms.ext_manage.api.EasApi;
|
|
|
|
|
import org.nl.wms.ext_manage.api.request.QueryEasStockRequest;
|
|
|
|
|
import org.nl.wms.ext_manage.api.response.QueryEasStockResponse;
|
|
|
|
|
import org.nl.wms.bi_manage.service.dto.SapStockQueryResult;
|
|
|
|
|
import org.nl.wms.ext_manage.enums.EXTConstant;
|
|
|
|
|
import org.nl.wms.ext_manage.sap.api.SapApi;
|
|
|
|
|
import org.nl.wms.ext_manage.sap.enums.SapConstant;
|
|
|
|
|
import org.nl.wms.ext_manage.service.WmsToZDWmdService;
|
|
|
|
|
import org.nl.wms.ext_manage.service.dto.ZDInventory;
|
|
|
|
|
import org.nl.wms.system_manage.service.param.ISysParamService;
|
|
|
|
|
@@ -53,10 +55,8 @@ import java.util.Set;
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class BiStockServiceImpl implements IBiStockService {
|
|
|
|
|
|
|
|
|
|
private static final String EAS_ORG_NO = "01.09.14";
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private EasApi easApi;
|
|
|
|
|
private SapApi sapApi;
|
|
|
|
|
@Autowired
|
|
|
|
|
private WmsToZDWmdService wmsToZDWmdService;
|
|
|
|
|
@Autowired
|
|
|
|
|
@@ -76,16 +76,9 @@ public class BiStockServiceImpl implements IBiStockService {
|
|
|
|
|
materialCode = materialCode.trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QueryEasStockRequest easRequest = new QueryEasStockRequest();
|
|
|
|
|
easRequest.setOrgno(EAS_ORG_NO);
|
|
|
|
|
easRequest.setWarehouseno(sectCode);
|
|
|
|
|
if (StringUtils.isNotBlank(materialCode)) {
|
|
|
|
|
easRequest.setMaterialno(materialCode);
|
|
|
|
|
}
|
|
|
|
|
List<QueryEasStockResponse> easStocks = easApi.queryStock(easRequest);
|
|
|
|
|
if (easStocks == null) {
|
|
|
|
|
easStocks = new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
JSONObject sapResult = sapApi.stockQuery(materialCode, SapConstant.WERKS, sectCode);
|
|
|
|
|
SapStockQueryResult sapStock = toSapStock(sapResult);
|
|
|
|
|
List<SapStockQueryResult.Item> sapStocks = sapStock.getBody();
|
|
|
|
|
|
|
|
|
|
Param zdStorParam = iSysParamService.findByCode(EXTConstant.ZD_STOR_SET);
|
|
|
|
|
List ZD_STOR_SET;
|
|
|
|
|
@@ -134,40 +127,40 @@ public class BiStockServiceImpl implements IBiStockService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, BigDecimal> easQtyMap = new HashMap<>();
|
|
|
|
|
Map<String, String> easNameMap = new HashMap<>();
|
|
|
|
|
Map<String, String> easUnitMap = new HashMap<>();
|
|
|
|
|
for (QueryEasStockResponse eas : easStocks) {
|
|
|
|
|
String skuCode = StringUtils.trimToEmpty(eas.getMaterialno());
|
|
|
|
|
Map<String, BigDecimal> sapQtyMap = new HashMap<>();
|
|
|
|
|
Map<String, String> sapNameMap = new HashMap<>();
|
|
|
|
|
Map<String, String> sapUnitMap = new HashMap<>();
|
|
|
|
|
for (SapStockQueryResult.Item sap : sapStocks) {
|
|
|
|
|
String skuCode = StringUtils.trimToEmpty(sap.getMatnr());
|
|
|
|
|
if (StringUtils.isBlank(skuCode) || !matchMaterial(skuCode, materialCode)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
easQtyMap.merge(skuCode, parseQty(eas.getQty()), BigDecimal::add);
|
|
|
|
|
if (StringUtils.isNotBlank(eas.getMaterialname())) {
|
|
|
|
|
easNameMap.putIfAbsent(skuCode, eas.getMaterialname());
|
|
|
|
|
sapQtyMap.merge(skuCode, parseQty(sap.stockQty()), BigDecimal::add);
|
|
|
|
|
if (StringUtils.isNotBlank(sap.getMaktx())) {
|
|
|
|
|
sapNameMap.putIfAbsent(skuCode, sap.getMaktx().trim());
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isNotBlank(eas.getUnitname())) {
|
|
|
|
|
easUnitMap.putIfAbsent(skuCode, eas.getUnitname());
|
|
|
|
|
if (StringUtils.isNotBlank(sap.getMeins())) {
|
|
|
|
|
sapUnitMap.putIfAbsent(skuCode, sap.getMeins().trim());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Set<String> materialCodes = new LinkedHashSet<>();
|
|
|
|
|
materialCodes.addAll(easQtyMap.keySet());
|
|
|
|
|
materialCodes.addAll(sapQtyMap.keySet());
|
|
|
|
|
materialCodes.addAll(localQtyMap.keySet());
|
|
|
|
|
|
|
|
|
|
List<BiStockReconcileRow> rows = new ArrayList<>();
|
|
|
|
|
for (String skuCode : materialCodes) {
|
|
|
|
|
BigDecimal easQty = normalize(easQtyMap.getOrDefault(skuCode, BigDecimal.ZERO));
|
|
|
|
|
BigDecimal sapQty = normalize(sapQtyMap.getOrDefault(skuCode, BigDecimal.ZERO));
|
|
|
|
|
BigDecimal compareQty = normalize(localQtyMap.getOrDefault(skuCode, BigDecimal.ZERO));
|
|
|
|
|
BiStockReconcileRow row = new BiStockReconcileRow();
|
|
|
|
|
row.setWarehouseCode(sectCode);
|
|
|
|
|
row.setMaterialCode(skuCode);
|
|
|
|
|
row.setMaterialName(firstNotBlank(easNameMap.get(skuCode), localNameMap.get(skuCode)));
|
|
|
|
|
row.setUnitName(firstNotBlank(easUnitMap.get(skuCode), localUnitMap.get(skuCode)));
|
|
|
|
|
row.setEasQty(easQty);
|
|
|
|
|
row.setMaterialName(firstNotBlank(sapNameMap.get(skuCode), localNameMap.get(skuCode)));
|
|
|
|
|
row.setUnitName(firstNotBlank(sapUnitMap.get(skuCode), localUnitMap.get(skuCode)));
|
|
|
|
|
row.setSapQty(sapQty);
|
|
|
|
|
row.setCompareQty(compareQty);
|
|
|
|
|
row.setDiffQty(normalize(easQty.subtract(compareQty)));
|
|
|
|
|
row.setDiff(easQty.compareTo(compareQty) != 0);
|
|
|
|
|
row.setDiffQty(normalize(sapQty.subtract(compareQty)));
|
|
|
|
|
row.setDiff(sapQty.compareTo(compareQty) != 0);
|
|
|
|
|
rows.add(row);
|
|
|
|
|
}
|
|
|
|
|
if (Boolean.TRUE.equals(onlyDiff)) {
|
|
|
|
|
@@ -183,8 +176,8 @@ public class BiStockServiceImpl implements IBiStockService {
|
|
|
|
|
result.setCompareSource(zdStor ? "ZD" : "WMS");
|
|
|
|
|
result.setCompareSourceName(zdStor ? "中鼎库存" : "WMS库存");
|
|
|
|
|
result.setRows(rows);
|
|
|
|
|
log.info("库存对账 sectCode={}, source={}, easSize={}, localSize={}, rowSize={}",
|
|
|
|
|
sectCode, result.getCompareSource(), easStocks.size(), localQtyMap.size(), rows.size());
|
|
|
|
|
log.info("库存对账 sectCode={}, source={}, sapSize={}, localSize={}, rowSize={}",
|
|
|
|
|
sectCode, result.getCompareSource(), sapStocks.size(), localQtyMap.size(), rows.size());
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -200,14 +193,14 @@ public class BiStockServiceImpl implements IBiStockService {
|
|
|
|
|
excelRow.put("物料编码", row.getMaterialCode());
|
|
|
|
|
excelRow.put("物料名称", row.getMaterialName());
|
|
|
|
|
excelRow.put("单位", row.getUnitName());
|
|
|
|
|
excelRow.put("EAS库存", row.getEasQty());
|
|
|
|
|
excelRow.put("SAP库存", row.getSapQty());
|
|
|
|
|
excelRow.put(compareName, row.getCompareQty());
|
|
|
|
|
excelRow.put("差异", row.getDiffQty());
|
|
|
|
|
excelRows.add(excelRow);
|
|
|
|
|
}
|
|
|
|
|
ExcelWriter writer = ExcelUtil.getWriter(true);
|
|
|
|
|
if (excelRows.isEmpty()) {
|
|
|
|
|
writer.writeHeadRow(Arrays.asList("仓库编号", "物料编码", "物料名称", "单位", "EAS库存", compareName, "差异"));
|
|
|
|
|
writer.writeHeadRow(Arrays.asList("仓库编号", "物料编码", "物料名称", "单位", "SAP库存", compareName, "差异"));
|
|
|
|
|
} else {
|
|
|
|
|
writer.write(excelRows, true);
|
|
|
|
|
}
|
|
|
|
|
@@ -314,4 +307,84 @@ public class BiStockServiceImpl implements IBiStockService {
|
|
|
|
|
private String firstNotBlank(String first, String second) {
|
|
|
|
|
return StringUtils.isNotBlank(first) ? first : second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 把 MM027 响应转成实体。body 可能是数组,也可能是带 data 的对象。
|
|
|
|
|
* status=E 且没有物料号时视为查询失败。
|
|
|
|
|
*/
|
|
|
|
|
private SapStockQueryResult toSapStock(JSONObject result) {
|
|
|
|
|
SapStockQueryResult response = new SapStockQueryResult();
|
|
|
|
|
if (result == null) {
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
JSONObject headerJson = result.getJSONObject("header");
|
|
|
|
|
if (headerJson != null) {
|
|
|
|
|
SapStockQueryResult.Header header = headerJson.toJavaObject(SapStockQueryResult.Header.class);
|
|
|
|
|
response.setHeader(header);
|
|
|
|
|
if (header != null && "E".equalsIgnoreCase(StringUtils.trimToEmpty(header.getStatus()))) {
|
|
|
|
|
throw new BadRequestException(StringUtils.defaultIfBlank(header.getMessage(), "SAP库存查询失败"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
JSONArray rows = resolveBodyRows(result.get("body"));
|
|
|
|
|
if (rows == null || rows.isEmpty()) {
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
List<SapStockQueryResult.Item> items = JSON.parseArray(rows.toJSONString(), SapStockQueryResult.Item.class);
|
|
|
|
|
if (items == null) {
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
for (SapStockQueryResult.Item item : items) {
|
|
|
|
|
if (item == null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ("E".equalsIgnoreCase(StringUtils.trimToEmpty(item.getStatus())) && StringUtils.isBlank(item.getMatnr())) {
|
|
|
|
|
throw new BadRequestException(StringUtils.defaultIfBlank(item.getMessage(), "SAP库存查询失败"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
response.setBody(items);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JSONArray resolveBodyRows(Object body) {
|
|
|
|
|
if (body == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (body instanceof JSONArray) {
|
|
|
|
|
return (JSONArray) body;
|
|
|
|
|
}
|
|
|
|
|
if (body instanceof JSONObject) {
|
|
|
|
|
JSONObject bodyObj = (JSONObject) body;
|
|
|
|
|
JSONArray rows = firstArray(bodyObj, "data", "item", "items", "result");
|
|
|
|
|
if (rows != null) {
|
|
|
|
|
return rows;
|
|
|
|
|
}
|
|
|
|
|
JSONArray single = new JSONArray();
|
|
|
|
|
single.add(bodyObj);
|
|
|
|
|
return single;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JSONArray firstArray(JSONObject obj, String... keys) {
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
for (String key : keys) {
|
|
|
|
|
JSONArray array = obj.getJSONArray(key);
|
|
|
|
|
if (array != null) {
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (String actual : obj.keySet()) {
|
|
|
|
|
for (String key : keys) {
|
|
|
|
|
if (actual.equalsIgnoreCase(key)) {
|
|
|
|
|
JSONArray array = obj.getJSONArray(actual);
|
|
|
|
|
if (array != null) {
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|