|
@@ -9,6 +9,7 @@ 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.iotdevicebom.vo.IotDeviceBomListReqVO;
|
|
|
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;
|
|
@@ -88,6 +89,17 @@ public class IotBomServiceImpl implements IotBomService {
|
|
|
syncDeviceBomAndMaterials(updateObj.getDeviceCategoryId());
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateIotBomAlone(IotBomSaveReqVO updateReqVO) {
|
|
|
+ // 校验存在
|
|
|
+ // validateIotBomExists(updateReqVO.getId());
|
|
|
+ // 更新
|
|
|
+ // IotBomDO updateObj = BeanUtils.toBean(updateReqVO, IotBomDO.class);
|
|
|
+ // iotBomMapper.updateById(updateObj);
|
|
|
+ syncDeviceBomAndMaterialsAlone(updateReqVO.getDeviceCategoryId());
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 批量更新设备的 bom_sync_status = 1
|
|
|
* @param ids
|
|
@@ -169,6 +181,104 @@ public class IotBomServiceImpl implements IotBomService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将设备分类BOM及bom节点挂载的物料同步到 设备bom
|
|
|
+ */
|
|
|
+ public void syncDeviceBomAndMaterialsAlone(Long deviceCategoryId){
|
|
|
+ // 只同步没有BOM的设备
|
|
|
+ // 查找当前分类 deviceCategoryId 下 没有BOM的设备
|
|
|
+ IotDevicePageReqVO deviceReqVO = new IotDevicePageReqVO();
|
|
|
+ deviceReqVO.setAssetClass(deviceCategoryId);
|
|
|
+ List<IotDeviceDO> devicesUnderCategories = iotDeviceMapper.selectList(deviceReqVO);
|
|
|
+ Set<Long> deviceIds = new HashSet<>();
|
|
|
+ if (CollUtil.isNotEmpty(devicesUnderCategories)) {
|
|
|
+ devicesUnderCategories.forEach(device -> {
|
|
|
+ deviceIds.add(device.getId());
|
|
|
+ });
|
|
|
+ }
|
|
|
+ // 查找设备BOM表 筛选出没有BOM的设备
|
|
|
+ Map<Long, List<IotDeviceBomDO>> deviceBomPair = new HashMap<>();
|
|
|
+ if (CollUtil.isNotEmpty(deviceIds)) {
|
|
|
+ IotDeviceBomListReqVO reqVO = new IotDeviceBomListReqVO();
|
|
|
+ reqVO.setDeviceIds(deviceIds);
|
|
|
+ List<IotDeviceBomDO> deviceBoms = iotDeviceBomMapper.selectList(reqVO);
|
|
|
+ if (CollUtil.isNotEmpty(deviceBoms)) {
|
|
|
+ deviceBoms.forEach(bom -> {
|
|
|
+ if (deviceBomPair.containsKey(bom.getDeviceId())) {
|
|
|
+ List<IotDeviceBomDO> tempBoms = deviceBomPair.get(bom.getDeviceId());
|
|
|
+ tempBoms.add(bom);
|
|
|
+ deviceBomPair.put(bom.getDeviceId(), tempBoms);
|
|
|
+ } else {
|
|
|
+ List<IotDeviceBomDO> tempBoms = new ArrayList<>();
|
|
|
+ tempBoms.add(bom);
|
|
|
+ deviceBomPair.put(bom.getDeviceId(), tempBoms);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<IotDeviceDO> tobeSyncBomDevices = new ArrayList<>();
|
|
|
+ if (CollUtil.isNotEmpty(devicesUnderCategories)) {
|
|
|
+ devicesUnderCategories.forEach(device -> {
|
|
|
+ if (!deviceBomPair.containsKey(device.getId())) {
|
|
|
+ // 设备没有同步过分类BOM 可以更新同步 与已经同步过分类BOM的设备区分开
|
|
|
+ tobeSyncBomDevices.add(device);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 选择了相同设备分类的设备,如果未同步过设备分类BOM, 应该将设备分类BOM同步复制到设备BOM。
|
|
|
+ // 设备分类BOM已经挂载的物料 也要拷贝到 设备BOM关联物料表
|
|
|
+ // 1. 根据当前BOM节点关联的设备分类查询所有已经关联了此分类但是未同步过 设备分类BOM 的设备
|
|
|
+ if (CollUtil.isNotEmpty(tobeSyncBomDevices)) {
|
|
|
+ // 2. 批量将当前设备分类BOM复制到每个设备
|
|
|
+ List<IotBomDO> bomList = deviceCategoryBom(deviceCategoryId);
|
|
|
+ // 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> tobeDelDeviceIds = new ArrayList<>();
|
|
|
+ tobeSyncBomDevices.forEach(device -> {
|
|
|
+ deviceIds.add(device.getId());
|
|
|
+ // 4. 生成ID映射关系
|
|
|
+ Map<Long, Long> idMapping = new LinkedHashMap<>();
|
|
|
+ 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 及 BOM节点已经关联的物料 (如果存在)
|
|
|
+ if (CollUtil.isNotEmpty(tobeDelDeviceIds)) {
|
|
|
+ Integer count = iotDeviceBomMapper.deleteByDeviceIds(tobeDelDeviceIds);
|
|
|
+ Integer materialCount = iotDeviceMaterialMapper.deleteByDeviceIds(tobeDelDeviceIds);
|
|
|
+ }
|
|
|
+ // 批量插入设备BOM
|
|
|
+ iotDeviceBomMapper.insertBatch(resultDeviceBomList);
|
|
|
+ if (CollUtil.isNotEmpty(deviceMaterialList)) {
|
|
|
+ // 批量插入设备关联的物料
|
|
|
+ iotDeviceMaterialMapper.insertBatch(deviceMaterialList);
|
|
|
+ }
|
|
|
+ // 单独修改设备BOM后再将 bom_sync_status 设置为 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private IotBomDO validateIotBomExists(Long id) {
|
|
|
IotBomDO bom = iotBomMapper.selectById(id);
|
|
|
if (ObjUtil.isEmpty(bom)) {
|