Prechádzať zdrojové kódy

pms 同步SAP库存 逻辑优化 工厂+库存地点 确定唯一值

zhangcl 2 mesiacov pred
rodič
commit
bd54000eeb

+ 28 - 20
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/sap/service/IotSapServiceImpl.java

@@ -414,7 +414,7 @@ public class IotSapServiceImpl implements IotSapService {
 
     @Override
     public void processSapStock(String factoryCode, List<IotSapStockVO> sapStocks) {
-        // 查询出所有工厂 对应的 deptId 新增本地库存时 可以匹配
+        // 查询出所有库存地点 对应的 deptId 新增本地库存时 可以匹配  工厂+库存地点 确定唯一值 因为 不同工厂的库存地点code可能重复
         List<SapOrgRespDTO> storageLocations = TenantUtils.execute(1L, () -> sapOrgApi.getSapOrgByType(3));
         // 查询所有工厂
         List<SapOrgRespDTO> factories = TenantUtils.execute(1L, () -> sapOrgApi.getSapOrgByType(1));
@@ -437,9 +437,14 @@ public class IotSapServiceImpl implements IotSapService {
         });
         // 库存地点集合
         storageLocations.forEach(location -> {
-            stockLocationCodeIdPair.put(location.getId(), location.getStorageLocationCode());
-            storageLocationIdPair.put(location.getStorageLocationCode(), location.getId());
-            stockLocationNamePair.put(location.getStorageLocationCode(), location.getStorageLocationName());
+            System.out.println("当前库存地点信息:" + location.getFactoryCode() + " - " + location.getStorageLocationCode());
+            String uniqueKey = StrUtil.join("-", location.getFactoryCode(), location.getStorageLocationCode());
+            // stockLocationCodeIdPair.put(location.getId(), location.getStorageLocationCode());
+            stockLocationCodeIdPair.put(location.getId(), uniqueKey);
+            // storageLocationIdPair.put(location.getStorageLocationCode(), location.getId());
+            storageLocationIdPair.put(uniqueKey, location.getId());
+            // stockLocationNamePair.put(location.getStorageLocationCode(), location.getStorageLocationName());
+            stockLocationNamePair.put(uniqueKey, location.getStorageLocationName());
         });
         // 查询所有部门
         List<DeptRespDTO> depts = TenantUtils.execute(1L, () -> deptApi.getDeptList());
@@ -448,11 +453,11 @@ public class IotSapServiceImpl implements IotSapService {
             Set<Long> stockLocationIds = dept.getStockLocationIds();
             // 循环遍历 stockLocationIds
             if (CollUtil.isNotEmpty(stockLocationIds)) {
-                stockLocationIds.forEach(location -> {
-                    if (stockLocationCodeIdPair.containsKey(location)) {
-                        // 当前部门设置的 库存地点
-                        String locationCode = stockLocationCodeIdPair.get(location);
-                        locationCodeDeptIdPair.put(locationCode, dept.getId());
+                stockLocationIds.forEach(locationId -> {
+                    if (stockLocationCodeIdPair.containsKey(locationId)) {
+                        // 当前部门设置的 库存地点 uniqueKey = 工厂编码+库存地点编码
+                        String uniqueKey = stockLocationCodeIdPair.get(locationId);
+                        locationCodeDeptIdPair.put(uniqueKey, dept.getId());
                     }
                 });
             }
@@ -462,7 +467,8 @@ public class IotSapServiceImpl implements IotSapService {
         List<IotSapStockDO> existStocks = TenantUtils.execute(1L, () -> iotSapStockMapper.selectList());
         if (CollUtil.isNotEmpty(existStocks)) {
             existStockKeys = existStocks.stream()
-                    .filter(stk -> ObjUtil.isNotEmpty(stk) && StrUtil.isNotBlank(stk.getMaterialCode()) && ObjUtil.isNotEmpty(stk.getStorageLocationId()))  // 过滤非空对象和非空code
+                    // 过滤非空对象和非空code
+                    .filter(stk -> ObjUtil.isNotEmpty(stk) && StrUtil.isNotBlank(stk.getMaterialCode()) && ObjUtil.isNotEmpty(stk.getStorageLocationId()))
                     .map(stk -> StrUtil.join("-", String.valueOf(stk.getStorageLocationId()), stk.getMaterialCode()))
                     .collect(Collectors.toSet());
         }
@@ -483,12 +489,12 @@ public class IotSapServiceImpl implements IotSapService {
         Set<String> finalExistStockKeys = existStockKeys;
         List<IotSapStockVO> newStocks = sapStocks.stream()
                 .filter(stk -> StrUtil.isNotBlank(stk.getMATNR()) && StrUtil.isNotBlank(stk.getLGORT())
-                        && storageLocationIdPair.containsKey(stk.getLGORT()))
+                        && storageLocationIdPair.containsKey(StrUtil.join("-", stk.getWERKS(), stk.getLGORT())))
                 .filter(stk -> {
                     // 处理前导零:移除MATNR前的 00000000
                     String processedCode = stk.getMATNR().replaceFirst("^0+", "");
                     // 本地已经配置过库存地点 包含SAP库存接口返回的库存地点
-                    Long storageLocationId = storageLocationIdPair.get(stk.getLGORT());
+                    Long storageLocationId = storageLocationIdPair.get(StrUtil.join("-", stk.getWERKS(), stk.getLGORT()));
                     return !finalExistStockKeys.contains(StrUtil.join("-", storageLocationId, processedCode));
                 })
                 .collect(Collectors.toList());
@@ -498,10 +504,11 @@ public class IotSapServiceImpl implements IotSapService {
         if (CollUtil.isNotEmpty(newStocks)) {
             newStocks.forEach(stock -> {
                 // 只保存pms维护过库存地点的数据
-                if (locationCodeDeptIdPair.containsKey(stock.getLGORT())) {
+                String uniqueKey = StrUtil.join("-", stock.getWERKS(), stock.getLGORT());
+                if (locationCodeDeptIdPair.containsKey(uniqueKey)) {
                     IotSapStockDO sapStock = new IotSapStockDO();
                     // 部门id
-                    sapStock.setDeptId(locationCodeDeptIdPair.get(stock.getLGORT()));
+                    sapStock.setDeptId(locationCodeDeptIdPair.get(uniqueKey));
                     // 工厂id
                     if (factoryIdPair.containsKey(stock.getWERKS())) {
                         sapStock.setFactoryId(factoryIdPair.get(stock.getWERKS()));
@@ -511,12 +518,12 @@ public class IotSapServiceImpl implements IotSapService {
                         sapStock.setFactory(factoryNamePair.get(stock.getWERKS()));
                     }
                     // 库存地点id
-                    if (storageLocationIdPair.containsKey(stock.getLGORT())) {
-                        sapStock.setStorageLocationId(storageLocationIdPair.get(stock.getLGORT()));
+                    if (storageLocationIdPair.containsKey(uniqueKey)) {
+                        sapStock.setStorageLocationId(storageLocationIdPair.get(uniqueKey));
                     }
                     // 库存地点名称
-                    if (stockLocationNamePair.containsKey(stock.getLGORT())) {
-                        sapStock.setProjectDepartment(stockLocationNamePair.get(stock.getLGORT()));
+                    if (stockLocationNamePair.containsKey(uniqueKey)) {
+                        sapStock.setProjectDepartment(stockLocationNamePair.get(uniqueKey));
                     }
                     // 物料编码 需要去掉前缀 00000000
                     sapStock.setMaterialCode(stock.getMATNR().replaceFirst("^0+", ""));
@@ -638,12 +645,13 @@ public class IotSapServiceImpl implements IotSapService {
     private Map<String, IotSapStockVO> createExistStockMap(List<IotSapStockVO> sapStocks, Set<String> existStockKeys, Map<String, Long> storageLocationIdPair) {
         return sapStocks.stream()
                 .filter(sapStock -> StrUtil.isNotBlank(sapStock.getMATNR()) &&
-                        ObjUtil.isNotEmpty(sapStock.getLGORT()) && storageLocationIdPair.containsKey(sapStock.getLGORT()))
+                        ObjUtil.isNotEmpty(sapStock.getLGORT()) &&
+                        storageLocationIdPair.containsKey(StrUtil.join("-", sapStock.getWERKS(), sapStock.getLGORT())))
                 .map(sapStock -> {
                     // 处理前导零:移除MATNR前的 00000000
                     String processedCode = sapStock.getMATNR().replaceFirst("^0+", "");
                     // 本地已经配置过库存地点 包含SAP库存接口返回的库存地点
-                    Long storageLocationId = storageLocationIdPair.get(sapStock.getLGORT());
+                    Long storageLocationId = storageLocationIdPair.get(StrUtil.join("-", sapStock.getWERKS(), sapStock.getLGORT()));
                     return new AbstractMap.SimpleEntry<>(StrUtil.join("-", storageLocationId, processedCode), sapStock);
                 })
                 .filter(entry -> existStockKeys.contains(entry.getKey()))

+ 0 - 23
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/service/iotmainworkorder/IotMainWorkOrderServiceImpl.java

@@ -385,29 +385,6 @@ public class IotMainWorkOrderServiceImpl implements IotMainWorkOrderService {
      * @return
      */
     private List<Long> sortByNumericValue(Map<Long, String> map) {
-        /* return map.entrySet().stream()
-                // 转换数值并保留原始entry
-                .map(entry -> {
-                    try {
-                        // 移除所有字母后缀并转换为double
-                        String cleanValue = entry.getValue()
-                                .replaceAll("[A-Za-z]+$", "");
-                        double numericValue = Double.parseDouble(cleanValue);
-                        return new AbstractMap.SimpleEntry<>(entry.getKey(), numericValue);
-                    } catch (NumberFormatException e) {
-                        // 数值转换失败处理(返回最大值确保排在最后)
-                        return new AbstractMap.SimpleEntry<>(entry.getKey(), Double.MAX_VALUE);
-                    }
-                })
-                // 双级排序:先按数值升序,数值相同再按key升序
-                // 显式指定泛型类型
-                .sorted(Comparator
-                        .<AbstractMap.SimpleEntry<Long, Double>>comparingDouble(AbstractMap.SimpleEntry::getValue)
-                        .thenComparingLong(AbstractMap.SimpleEntry::getKey)
-                )
-                // 提取排序后的key
-                .map(AbstractMap.SimpleEntry::getKey)
-                .collect(Collectors.toList()); */
         return map.entrySet().stream()
                 .map(entry -> {
                     String[] parts = entry.getValue().split("\\s+");