|
@@ -7,6 +7,8 @@ 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.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.iotdevicebom.IotDeviceBomDO;
|
|
|
import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
|
|
@@ -14,6 +16,7 @@ import cn.iocoder.yudao.module.pms.dal.mysql.iotbom.IotBomMapper;
|
|
|
import cn.iocoder.yudao.module.pms.dal.mysql.iotdevicebom.IotDeviceBomMapper;
|
|
|
import cn.iocoder.yudao.module.pms.service.IotProductClassifyService;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
@@ -62,12 +65,54 @@ public class IotBomServiceImpl implements IotBomService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public void updateIotBom(IotBomSaveReqVO updateReqVO) {
|
|
|
// 校验存在
|
|
|
validateIotBomExists(updateReqVO.getId());
|
|
|
// 更新
|
|
|
IotBomDO updateObj = BeanUtils.toBean(updateReqVO, IotBomDO.class);
|
|
|
iotBomMapper.updateById(updateObj);
|
|
|
+ // 选择了相同设备分类的设备,如果未同步过设备分类BOM, 应该将设备分类BOM同步复制到设备BOM。
|
|
|
+ // 设备分类BOM已经挂载的物料 也要拷贝到 设备BOM关联物料表
|
|
|
+ // 1. 根据当前BOM节点关联的设备分类查询所有已经关联了此分类但是未同步过 设备分类BOM 的设备
|
|
|
+ IotDevicePageReqVO reqVO = new IotDevicePageReqVO();
|
|
|
+ reqVO.setAssetClass(updateObj.getDeviceCategoryId());
|
|
|
+ reqVO.setBomSyncStatus(0);
|
|
|
+ List<IotDeviceDO> devices = iotDeviceMapper.selectList(reqVO);
|
|
|
+ if (CollUtil.isNotEmpty(devices)) {
|
|
|
+ // 2. 批量将当前设备分类BOM复制到每个设备
|
|
|
+ List<IotBomDO> bomList = deviceCategoryBom(updateObj.getDeviceCategoryId());
|
|
|
+ // 3. 构建树形结构并获取层级顺序
|
|
|
+ List<IotBomDO> orderedBoms = buildHierarchyOrder(bomList);
|
|
|
+ List<IotDeviceBomDO> resultDeviceBomList = new ArrayList<>();
|
|
|
+ List<Long> deviceIds = new ArrayList<>();
|
|
|
+ devices.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());
|
|
|
+ resultDeviceBomList.addAll(targetBoms);
|
|
|
+ });
|
|
|
+ if (CollUtil.isNotEmpty(resultDeviceBomList)) {
|
|
|
+ // 批量插入设备BOM
|
|
|
+ iotDeviceBomMapper.insertBatch(resultDeviceBomList);
|
|
|
+ // 批量设置 设备 的BOM同步标识 bom_sync_status = 1
|
|
|
+ batchUpdateBomSyncStatus(deviceIds);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量更新设备的 bom_sync_status = 1
|
|
|
+ * @param ids
|
|
|
+ */
|
|
|
+ private void batchUpdateBomSyncStatus(List<Long> ids) {
|
|
|
+ LambdaUpdateWrapper<IotDeviceDO> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.in(IotDeviceDO::getId, ids) // WHERE id IN (ids)
|
|
|
+ .set(IotDeviceDO::getBomSyncStatus, 1); // SET bom_sync_status = 1
|
|
|
+ iotDeviceMapper.update(wrapper);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -124,10 +169,7 @@ public class IotBomServiceImpl implements IotBomService {
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void copyBomByCategory(Long deviceCategoryId, Long deviceId) {
|
|
|
// 1. 查询源数据(按创建时间排序保证基础顺序)
|
|
|
- List<IotBomDO> sourceBoms = iotBomMapper.selectList(new LambdaQueryWrapper<IotBomDO>()
|
|
|
- .eq(IotBomDO::getDeviceCategoryId, deviceCategoryId)
|
|
|
- .eq(IotBomDO::getDeleted, false)
|
|
|
- .orderByAsc(IotBomDO::getCreateTime));
|
|
|
+ List<IotBomDO> sourceBoms = deviceCategoryBom(deviceCategoryId);
|
|
|
if (CollUtil.isEmpty(sourceBoms)) {
|
|
|
throw exception(IOT_BOM_NOT_EXISTS);
|
|
|
}
|
|
@@ -144,6 +186,18 @@ public class IotBomServiceImpl implements IotBomService {
|
|
|
iotDeviceMapper.updateBomSyncStatus(deviceId);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询某个设备分类下所有BOM节点
|
|
|
+ */
|
|
|
+ private List<IotBomDO> deviceCategoryBom(Long deviceCategoryId){
|
|
|
+ // 1. 查询源数据(按创建时间排序保证基础顺序)
|
|
|
+ List<IotBomDO> sourceBoms = iotBomMapper.selectList(new LambdaQueryWrapper<IotBomDO>()
|
|
|
+ .eq(IotBomDO::getDeviceCategoryId, deviceCategoryId)
|
|
|
+ .eq(IotBomDO::getDeleted, false)
|
|
|
+ .orderByAsc(IotBomDO::getCreateTime));
|
|
|
+ return sourceBoms;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 转换目标对象
|
|
|
* @param orderedBoms
|