|
@@ -37,6 +37,7 @@ import java.util.stream.Stream;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
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.framework.common.util.collection.CollectionUtils.convertListByFlatMap;
|
|
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_BOM_NOT_EXISTS;
|
|
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_BOM_NOT_EXISTS;
|
|
|
|
+import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_PRODUCT_CLASSIFY_NOT_EXISTS;
|
|
|
|
|
|
/**
|
|
/**
|
|
* PMS BOM 关系 Service 实现类
|
|
* PMS BOM 关系 Service 实现类
|
|
@@ -181,6 +182,57 @@ public class IotBomServiceImpl implements IotBomService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public void copyCategoryBom(IotBomSaveReqVO updateReqVO) {
|
|
|
|
+ // 校验 被拷贝的设备分类ID 与
|
|
|
|
+ if (ObjUtil.isEmpty(updateReqVO.getDeviceCategoryId()) || ObjUtil.isEmpty(updateReqVO.getToDeviceCategoryId())) {
|
|
|
|
+ throw exception(IOT_PRODUCT_CLASSIFY_NOT_EXISTS);
|
|
|
|
+ }
|
|
|
|
+ // 查询 deviceCategoryId 下关联的BOM
|
|
|
|
+ List<IotBomDO> existBomList = deviceCategoryBom(updateReqVO.getDeviceCategoryId());
|
|
|
|
+ if (CollUtil.isEmpty(existBomList)) {
|
|
|
|
+ throw exception(IOT_BOM_NOT_EXISTS);
|
|
|
|
+ }
|
|
|
|
+ // 3. 构建树形结构并获取层级顺序
|
|
|
|
+ List<IotBomDO> orderedBomS = buildHierarchyOrder(existBomList);
|
|
|
|
+ // 当前设备分类下所有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<IotBomDO> resultCategoryBomList = new ArrayList<>();
|
|
|
|
+ // 新 bom 关联物料列表
|
|
|
|
+ List<IotCommonBomMaterialDO> bomMaterialList = new ArrayList<>();
|
|
|
|
+ // 4. 生成ID映射关系
|
|
|
|
+ Map<Long, Long> idMapping = new LinkedHashMap<>();
|
|
|
|
+ orderedBomS.forEach(bom -> idMapping.put(bom.getId(), IdGenerator.nextId()));
|
|
|
|
+ // 5. 转换目标对象
|
|
|
|
+ List<IotBomDO> targetBoms = convertCategoryBoms(orderedBomS, updateReqVO.getToDeviceCategoryId(), idMapping);
|
|
|
|
+ // 转换 bom关联的物料 目标对象
|
|
|
|
+ List<IotCommonBomMaterialDO> targetCommonBomMaterials = convertCategoryBomMaterials(idMapping,
|
|
|
|
+ updateReqVO.getToDeviceCategoryId(), bomNodeMaterialMap);
|
|
|
|
+ resultCategoryBomList.addAll(targetBoms);
|
|
|
|
+ bomMaterialList.addAll(targetCommonBomMaterials);
|
|
|
|
+ // 先删除已有的 新 设备分类id 下的BOM 及关联的设备
|
|
|
|
+ List<Long> deviceCategoryIds = new ArrayList<>();
|
|
|
|
+ deviceCategoryIds.add(updateReqVO.getToDeviceCategoryId());
|
|
|
|
+ iotBomMapper.deleteByDeviceCategoryIds(deviceCategoryIds);
|
|
|
|
+ iotCommonBomMaterialMapper.deleteByDeviceCategoryIds(deviceCategoryIds);
|
|
|
|
+ if (CollUtil.isNotEmpty(resultCategoryBomList)) {
|
|
|
|
+ iotBomMapper.insertBatch(resultCategoryBomList);
|
|
|
|
+ if (CollUtil.isNotEmpty(bomMaterialList)) {
|
|
|
|
+ iotCommonBomMaterialMapper.insertBatch(bomMaterialList);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 将设备分类BOM及bom节点挂载的物料同步到 设备bom
|
|
* 将设备分类BOM及bom节点挂载的物料同步到 设备bom
|
|
*/
|
|
*/
|
|
@@ -399,6 +451,35 @@ public class IotBomServiceImpl implements IotBomService {
|
|
return targetDeviceMaterials;
|
|
return targetDeviceMaterials;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 拷贝已有设备分类BOM 转换目标对象
|
|
|
|
+ * @param
|
|
|
|
+ * @param idMapping
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private List<IotCommonBomMaterialDO> convertCategoryBomMaterials(Map<Long, Long> idMapping, Long deviceCategoryId,
|
|
|
|
+ Map<Long, List<IotCommonBomMaterialDO>> bomNodeMaterialMap) {
|
|
|
|
+ List<IotCommonBomMaterialDO> targetDeviceMaterials = new ArrayList<>();
|
|
|
|
+ bomNodeMaterialMap.forEach((k,v) -> {
|
|
|
|
+ // k bomNodeId v bomNodeId关联的物料集合
|
|
|
|
+ // 获取 bomNodeId 对应的 设备分类bom nodeId
|
|
|
|
+ Long deviceBomNodeId = idMapping.get(k);
|
|
|
|
+ v.forEach(bomMaterial -> {
|
|
|
|
+ IotCommonBomMaterialDO commonBomMaterial = new IotCommonBomMaterialDO();
|
|
|
|
+ commonBomMaterial.setDeviceCategoryId(deviceCategoryId);
|
|
|
|
+ commonBomMaterial.setBomNodeId(deviceBomNodeId);
|
|
|
|
+ commonBomMaterial.setName(bomMaterial.getName());
|
|
|
|
+ commonBomMaterial.setCode(bomMaterial.getCode());
|
|
|
|
+ commonBomMaterial.setQuantity(bomMaterial.getQuantity());
|
|
|
|
+ commonBomMaterial.setMaterialId(bomMaterial.getMaterialId());
|
|
|
|
+ commonBomMaterial.setCreator(bomMaterial.getCreator());
|
|
|
|
+ commonBomMaterial.setCreateTime(LocalDateTime.now());
|
|
|
|
+ targetDeviceMaterials.add(commonBomMaterial);
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ return targetDeviceMaterials;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 转换目标对象
|
|
* 转换目标对象
|
|
* @param orderedBoms
|
|
* @param orderedBoms
|
|
@@ -427,6 +508,32 @@ public class IotBomServiceImpl implements IotBomService {
|
|
return targetBoms;
|
|
return targetBoms;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 拷贝已有的设备分类的BOM
|
|
|
|
+ * @param orderedBoms
|
|
|
|
+ * @param idMapping
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private List<IotBomDO> convertCategoryBoms(List<IotBomDO> orderedBoms, Long deviceCategoryId, Map<Long, Long> idMapping) {
|
|
|
|
+ List<IotBomDO> targetBoms = new ArrayList<>();
|
|
|
|
+ for (IotBomDO source : orderedBoms) {
|
|
|
|
+ IotBomDO target = new IotBomDO();
|
|
|
|
+
|
|
|
|
+ // 基础字段拷贝
|
|
|
|
+ BeanUtils.copyProperties(source, target);
|
|
|
|
+ target.setDeviceCategoryId(deviceCategoryId);
|
|
|
|
+ // ID映射处理
|
|
|
|
+ target.setId(idMapping.get(source.getId()));
|
|
|
|
+ target.setParentId(idMapping.getOrDefault(source.getParentId(), 0L));
|
|
|
|
+
|
|
|
|
+ target.setCreateTime(LocalDateTime.now());
|
|
|
|
+ target.setUpdateTime(LocalDateTime.now());
|
|
|
|
+
|
|
|
|
+ targetBoms.add(target);
|
|
|
|
+ }
|
|
|
|
+ return targetBoms;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 拷贝设备分类bom时构建层级结构
|
|
* 拷贝设备分类bom时构建层级结构
|
|
* @param sourceBoms
|
|
* @param sourceBoms
|