Kaynağa Gözat

pms 设备BOM 物料

zhangcl 3 ay önce
ebeveyn
işleme
0ba6b4d7eb

+ 75 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/service/iotbom/IotBomServiceImpl.java

@@ -7,13 +7,18 @@ import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.module.pms.controller.admin.iotbom.vo.IotBomListReqVO;
 import cn.iocoder.yudao.module.pms.controller.admin.iotbom.vo.IotBomPageReqVO;
 import cn.iocoder.yudao.module.pms.controller.admin.iotbom.vo.IotBomSaveReqVO;
+import cn.iocoder.yudao.module.pms.controller.admin.iotcommonbommaterial.vo.IotCommonBomMaterialSaveReqVO;
 import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDevicePageReqVO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.iotbom.IotBomDO;
+import cn.iocoder.yudao.module.pms.dal.dataobject.iotcommonbommaterial.IotCommonBomMaterialDO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicebom.IotDeviceBomDO;
+import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicematerial.IotDeviceMaterialDO;
 import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
 import cn.iocoder.yudao.module.pms.dal.mysql.iotbom.IotBomMapper;
+import cn.iocoder.yudao.module.pms.dal.mysql.iotcommonbommaterial.IotCommonBomMaterialMapper;
 import cn.iocoder.yudao.module.pms.dal.mysql.iotdevicebom.IotDeviceBomMapper;
+import cn.iocoder.yudao.module.pms.dal.mysql.iotdevicematerial.IotDeviceMaterialMapper;
 import cn.iocoder.yudao.module.pms.service.IotProductClassifyService;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@@ -24,8 +29,11 @@ import org.springframework.validation.annotation.Validated;
 import javax.annotation.Resource;
 import java.time.LocalDateTime;
 import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertListByFlatMap;
 import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_BOM_NOT_EXISTS;
 
 /**
@@ -45,6 +53,10 @@ public class IotBomServiceImpl implements IotBomService {
     private IotDeviceBomMapper iotDeviceBomMapper;
     @Resource
     private IotDeviceMapper iotDeviceMapper;
+    @Resource
+    private IotCommonBomMaterialMapper iotCommonBomMaterialMapper;
+    @Resource
+    private IotDeviceMaterialMapper iotDeviceMaterialMapper;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -84,7 +96,20 @@ public class IotBomServiceImpl implements IotBomService {
             List<IotBomDO> bomList = deviceCategoryBom(updateObj.getDeviceCategoryId());
             // 3. 构建树形结构并获取层级顺序
             List<IotBomDO> orderedBoms = buildHierarchyOrder(bomList);
+            // 当前设备分类下所有bom节点id
+            List<Long> bomIds = convertListByFlatMap(orderedBoms, bom -> Stream.of(bom.getId()));
+            IotCommonBomMaterialSaveReqVO saveReqVO = new IotCommonBomMaterialSaveReqVO();
+            saveReqVO.setBomNodeIds(bomIds);
+            List<IotCommonBomMaterialDO> bomMaterials = iotCommonBomMaterialMapper.selectList(saveReqVO);
+            Map<Long, List<IotCommonBomMaterialDO>> bomNodeMaterialMap = bomMaterials.stream()
+                    .collect(Collectors.groupingBy(
+                            IotCommonBomMaterialDO::getBomNodeId,  // 分组键:提取 bomNodeId
+                            Collectors.toList()                      // 分组值:保存为列表
+                    ));
+            // 设备bom列表
             List<IotDeviceBomDO> resultDeviceBomList = new ArrayList<>();
+            // 设备bom关联物料列表
+            List<IotDeviceMaterialDO> deviceMaterialList = new ArrayList<>();
             List<Long> deviceIds = new ArrayList<>();
             devices.forEach(device -> {
                 deviceIds.add(device.getId());
@@ -93,11 +118,18 @@ public class IotBomServiceImpl implements IotBomService {
                 orderedBoms.forEach(bom -> idMapping.put(bom.getId(), IdGenerator.nextId()));
                 // 5. 转换目标对象
                 List<IotDeviceBomDO> targetBoms = convertBoms(orderedBoms, idMapping, device.getId());
+                // 转换 bom关联的物料 目标对象
+                List<IotDeviceMaterialDO> targetDeviceMaterials = convertBomMaterials(idMapping, device.getAssetClass(), device.getId(), bomNodeMaterialMap);
                 resultDeviceBomList.addAll(targetBoms);
+                deviceMaterialList.addAll(targetDeviceMaterials);
             });
             if (CollUtil.isNotEmpty(resultDeviceBomList)) {
                 // 批量插入设备BOM
                 iotDeviceBomMapper.insertBatch(resultDeviceBomList);
+                if (CollUtil.isNotEmpty(deviceMaterialList)) {
+                    // 批量插入设备关联的物料
+                    iotDeviceMaterialMapper.insertBatch(deviceMaterialList);
+                }
                 // 批量设置 设备 的BOM同步标识 bom_sync_status = 1
                 batchUpdateBomSyncStatus(deviceIds);
             }
@@ -175,11 +207,25 @@ public class IotBomServiceImpl implements IotBomService {
         }
         // 2. 构建树形结构并获取层级顺序
         List<IotBomDO> orderedBoms = buildHierarchyOrder(sourceBoms);
+        // 查询当前所有BOM节点关联的所有物料
+        List<Long> bomIds = convertListByFlatMap(orderedBoms, bom -> Stream.of(bom.getId()));
+        IotCommonBomMaterialSaveReqVO reqVO = new IotCommonBomMaterialSaveReqVO();
+        reqVO.setBomNodeIds(bomIds);
+        List<IotCommonBomMaterialDO> bomMaterials = iotCommonBomMaterialMapper.selectList(reqVO);
+        Map<Long, List<IotCommonBomMaterialDO>> bomNodeMaterialMap = bomMaterials.stream()
+                .collect(Collectors.groupingBy(
+                        IotCommonBomMaterialDO::getBomNodeId,  // 分组键:提取 bomNodeId
+                        Collectors.toList()                      // 分组值:保存为列表
+                ));
         // 3. 生成ID映射关系
         Map<Long, Long> idMapping = new LinkedHashMap<>();
         orderedBoms.forEach(bom -> idMapping.put(bom.getId(), IdGenerator.nextId()));
         // 4. 转换目标对象
         List<IotDeviceBomDO> targetBoms = convertBoms(orderedBoms, idMapping, deviceId);
+        // 转换 bom关联的物料 目标对象
+        List<IotDeviceMaterialDO> targetDeviceMaterials = convertBomMaterials(idMapping, deviceCategoryId, deviceId, bomNodeMaterialMap);
+        // 批量插入 设备bom关联的物料  // 设备分类BOM已经挂载的物料 也要拷贝到 设备BOM关联物料表
+        iotDeviceMaterialMapper.insertBatch(targetDeviceMaterials);
         // 批量插入设备BOM
         iotDeviceBomMapper.insertBatch(targetBoms);
         // 拷贝BOM成功后 设置设备 bom_sync_status 字段 为 1已同步
@@ -198,6 +244,35 @@ public class IotBomServiceImpl implements IotBomService {
         return sourceBoms;
     }
 
+    /**
+     * 转换目标对象
+     * @param
+     * @param idMapping
+     * @return
+     */
+    private List<IotDeviceMaterialDO> convertBomMaterials(Map<Long, Long> idMapping, Long deviceCategoryId,
+                                                          Long deviceId, Map<Long, List<IotCommonBomMaterialDO>> bomNodeMaterialMap) {
+        List<IotDeviceMaterialDO> targetDeviceMaterials = new ArrayList<>();
+        bomNodeMaterialMap.forEach((k,v) -> {
+            // k bomNodeId    v bomNodeId关联的物料集合
+            // 获取 bomNodeId 对应的 设备bom nodeId
+            Long deviceBomNodeId = idMapping.get(k);
+            v.forEach(bomMaterial -> {
+                IotDeviceMaterialDO deviceMaterial = new IotDeviceMaterialDO();
+                deviceMaterial.setDeviceCategoryId(deviceCategoryId);
+                deviceMaterial.setDeviceId(deviceId);
+                deviceMaterial.setBomNodeId(deviceBomNodeId);
+                deviceMaterial.setName(bomMaterial.getName());
+                deviceMaterial.setCode(bomMaterial.getCode());
+                deviceMaterial.setMaterialId(bomMaterial.getMaterialId());
+                deviceMaterial.setCreator(bomMaterial.getCreator());
+                deviceMaterial.setCreateTime(LocalDateTime.now());
+                targetDeviceMaterials.add(deviceMaterial);
+            });
+        });
+        return targetDeviceMaterials;
+    }
+
     /**
      * 转换目标对象
      * @param orderedBoms