Prechádzať zdrojové kódy

pms 本地库存筛选条件 SAP同步物料单位

zhangcl 1 mesiac pred
rodič
commit
4232bbbb51

+ 1 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/job/sap/SyncSapMaterialsJob.java

@@ -59,6 +59,7 @@ public class SyncSapMaterialsJob implements JobHandler {
                     tempMaterial.setMATKL(otItabTable.getString("MATKL"));
                     tempMaterial.setWGBEZ(otItabTable.getString("WGBEZ"));
                     tempMaterial.setLVORM(otItabTable.getString("LVORM"));
+                    tempMaterial.setMEINS(otItabTable.getString("MEINS"));
                     sapMaterials.add(tempMaterial);
                 }
                 iotSapService.processMaterials(sapMaterials);

+ 6 - 5
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/sap/SapController.java

@@ -41,7 +41,7 @@ public class SapController {
 
             // 设置输入参数
             JCoParameterList input = function.getImportParameterList();
-            input.setValue("IV_WERKS", "6011");
+            input.setValue("IV_WERKS", "5021");
 
             // 执行 RFC 调用
             function.execute(destination);
@@ -222,7 +222,7 @@ public class SapController {
                     tempMaterial.setWGBEZ(otItabTable.getString("WGBEZ"));
                     tempMaterial.setLVORM(otItabTable.getString("LVORM"));
                     sapMaterials.add(tempMaterial);
-                    /* System.out.println(String.format("行号 %d: 物料编码=%s, 物料描述=%s, 物料类型=%s, 物料类型描述=%s, 物料组=%s, 物料组描述=%s, 集团级的DF=%s\n",
+                    System.out.println(String.format("行号 %d: 物料编码=%s, 物料描述=%s, 物料类型=%s, 物料类型描述=%s, 物料组=%s, 物料组描述=%s, 集团级的DF=%s, 单位=%s\n",
                             i,
                             otItabTable.getString("MATNR"), // 物料编码
                             otItabTable.getString("MAKTX"), // 物料描述
@@ -230,10 +230,11 @@ public class SapController {
                             otItabTable.getString("MTBEZ"), // 物料类型描述
                             otItabTable.getString("MATKL"), // 物料组
                             otItabTable.getString("WGBEZ"), // 物料组描述
-                            otItabTable.getString("LVORM")  // 集团级的DF
-                    )); */
+                            otItabTable.getString("LVORM"),  // 集团级的DF
+                            otItabTable.getString("MEINS")  // 单位
+                    ));
                 }
-                iotSapService.processMaterials(sapMaterials);
+                // iotSapService.processMaterials(sapMaterials);
             }
             return "Material Info: " + output.getString("OT_ITAB");
         } catch (JCoException e) {

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

@@ -123,14 +123,19 @@ public class IotSapServiceImpl implements IotSapService {
             }
         }
 
-        // 2.更新已有物料 物料描述可能会变化
+        // 2.更新已有物料 物料描述可能会变化 (初始化单位)
         // 更新当前库中已经存在的物料的 物料描述
         Map<String, String> sapMaterialPair = createMaterialMap(sapMaterials, existingMaterialCodes);
+
+        Map<String, String> sapMaterialUnitPair = createMaterialUnitMap(sapMaterials, existingMaterialCodes);
         if (CollUtil.isNotEmpty(existMaterials) && CollUtil.isNotEmpty(sapMaterialPair)) {
             existMaterials.forEach(em -> {
                 if (sapMaterialPair.containsKey(em.getCode())) {
                     em.setName(sapMaterialPair.get(em.getCode()));
                 }
+                if (sapMaterialUnitPair.containsKey(em.getCode())) {
+                    em.setUnit(sapMaterialUnitPair.get(em.getCode()));
+                }
             });
             // 分批多线程更新已有物料数据描述  1000条为一组
             int tempBatchSize = 1000;    // 1000条记录为一组 多线程更新
@@ -704,6 +709,29 @@ public class IotSapServiceImpl implements IotSapService {
                 ));
     }
 
+    /**
+     * 创建物料编码到 单位 的映射 (SAP接口返回单位后 初始化数据)
+     * @param sapMaterials SAP返回的物料列表
+     * @param existingMaterialCodes 数据库中已存在的物料编码集合
+     * @return 物料编码到 单位 的映射
+     */
+    private Map<String, String> createMaterialUnitMap(List<IotSapMaterialVO> sapMaterials, Set<String> existingMaterialCodes) {
+        return sapMaterials.stream()
+                .filter(sapMaterial -> StrUtil.isNotBlank(sapMaterial.getMATNR()) &&
+                        StrUtil.isNotBlank(sapMaterial.getMEINS()))
+                .map(sapMaterial -> {
+                    // 处理前导零:移除MATNR前的00000000
+                    String processedCode = sapMaterial.getMATNR().replaceFirst("^0+", "");
+                    return new AbstractMap.SimpleEntry<>(processedCode, sapMaterial.getMEINS());
+                })
+                .filter(entry -> existingMaterialCodes.contains(entry.getKey()))
+                .collect(Collectors.toMap(
+                        Map.Entry::getKey,
+                        Map.Entry::getValue,
+                        (existing, replacement) -> existing // 如果有重复的key,保留第一个值
+                ));
+    }
+
     /**
      * sap库存 更新 创建 唯一key(库存地点id-物料编码) 到 sap库存对象 的映射
      * pms中已有的库存  SAP库存接口也返回了 需要更新

+ 3 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/sap/vo/IotSapMaterialVO.java

@@ -30,4 +30,7 @@ public class IotSapMaterialVO {
     @Schema(description = "集团级的DF")
     private String LVORM;
 
+    @Schema(description = "单位")
+    private String MEINS;
+
 }

+ 12 - 0
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/saporg/SapOrgController.java

@@ -89,6 +89,18 @@ public class SapOrgController {
         return success(BeanUtils.toBean(list, SapOrgSimpleRespVO.class));
     }
 
+    @GetMapping(value ="filteredSimpleSapOrgList")
+    @Parameter(name = "type", description = "类型", required = true, example = "1工厂 2成本中心 3库存地点")
+    @Parameter(name = "deptId", description = "部门id", required = true, example = "225")
+    @Operation(summary = "根据部门过滤 SAP工厂/成本中心/库存地点全列表", description = "只包含被开启的数据,主要用于前端的下拉选项")
+    public CommonResult<List<SapOrgSimpleRespVO>> filteredSimpleSapOrgList(@RequestParam("type") Integer type, @RequestParam("deptId") Long deptId) {
+        // 获得SAP工厂/成本中心/库存地点列表,只要开启状态的
+        List<SapOrgDO> list = sapOrgService.filteredSapOrgList(deptId, type, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
+        // 排序后,返回给前端
+        list.sort(Comparator.comparing(SapOrgDO::getSort));
+        return success(BeanUtils.toBean(list, SapOrgSimpleRespVO.class));
+    }
+
     @PostMapping("/selectedSimpleList")
     @Operation(summary = "根据已经选择的SAP工厂 查询下属的成本中心/库存地点全列表", description = "只包含被开启的数据,主要用于前端的下拉选项")
     public CommonResult<List<SapOrgSimpleRespVO>> selectedSimpleList(@RequestBody SapOrgSimpleReqVO reqVO) {

+ 11 - 0
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/saporg/SapOrgService.java

@@ -12,6 +12,7 @@ import javax.validation.Valid;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * SAP中的组织主数据(工厂 成本中心) Service 接口
@@ -96,4 +97,14 @@ public interface SapOrgService {
      * @return SAP组织信息数组
      */
     List<SapOrgDO> getSapOrgList(Collection<Long> ids);
+
+    /**
+     * 根据部门过滤 SAP工厂/成本中心/库存地点全列表
+     *
+     * @param deptId 部门id
+     * @param type 类型(1工厂 2成本中心 3库存地点)
+     * @param statuses 状态数组。如果为空,不进行筛选
+     * @return 部门列表
+     */
+    List<SapOrgDO> filteredSapOrgList(Long deptId, Integer type, Set<Integer> statuses);
 }

+ 42 - 3
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/saporg/SapOrgServiceImpl.java

@@ -1,20 +1,25 @@
 package cn.iocoder.yudao.module.system.service.saporg;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.ObjUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
+import cn.iocoder.yudao.module.system.api.dept.DeptSapOrgApi;
+import cn.iocoder.yudao.module.system.api.dept.dto.DeptSapOrgRespDTO;
 import cn.iocoder.yudao.module.system.controller.admin.saporg.vo.SapOrgPageReqVO;
 import cn.iocoder.yudao.module.system.controller.admin.saporg.vo.SapOrgSaveReqVO;
 import cn.iocoder.yudao.module.system.controller.admin.saporg.vo.SapOrgSimpleReqVO;
 import cn.iocoder.yudao.module.system.dal.dataobject.saporg.SapOrgDO;
+import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
 import cn.iocoder.yudao.module.system.dal.mysql.saporg.SapOrgMapper;
+import cn.iocoder.yudao.module.system.service.dept.DeptService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.validation.annotation.Validated;
 
 import javax.annotation.Resource;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SAP_ORG_NOT_EXISTS;
@@ -31,6 +36,13 @@ public class SapOrgServiceImpl implements SapOrgService {
     @Resource
     private SapOrgMapper sapOrgMapper;
 
+    @Resource
+    private DeptMapper deptMapper;
+    @Autowired
+    private DeptService deptService;
+    @Autowired
+    private DeptSapOrgApi deptSapOrgApi;
+
     @Override
     public Long createSapOrg(SapOrgSaveReqVO createReqVO) {
         // 插入
@@ -92,4 +104,31 @@ public class SapOrgServiceImpl implements SapOrgService {
         return sapOrgMapper.selectBatchIds(ids);
     }
 
+    @Override
+    public List<SapOrgDO> filteredSapOrgList(Long deptId, Integer type, Set<Integer> statuses) {
+        // 筛选部门id及子部门id关联的所有 SAP组织
+        if (ObjUtil.isEmpty(deptId)) {
+            return CollectionUtil.newArrayList();
+        }
+        Set<Long> ids = new HashSet<>();
+        ids = deptService.getChildDeptIdListFromCache(deptId);
+        ids.add(deptId);
+        // 查询当前部门及所有子部门关联的SAP工厂 成本中心 列表
+        List<DeptSapOrgRespDTO> sapOrgS = deptSapOrgApi.getSapOrgListByDeptIds(ids);
+        if (CollUtil.isEmpty(sapOrgS)) {
+            return CollectionUtil.newArrayList();
+        }
+        Set<Long> sapOrgIds = new HashSet<>();
+        sapOrgS.forEach(sapOrg -> {
+            if (ObjUtil.isNotEmpty(sapOrg.getFactoryId())) {
+                sapOrgIds.add(sapOrg.getFactoryId());
+            }
+            if (ObjUtil.isNotEmpty(sapOrg.getCostCenterId())) {
+                sapOrgIds.add(sapOrg.getCostCenterId());
+            }
+        });
+        List<SapOrgDO> sapOrgList = getSapOrgList(sapOrgIds, type, statuses);
+        return sapOrgList;
+    }
+
 }