Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package com.njcn.device.device.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.njcn.common.pojo.enums.common.DataStateEnum;
|
||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.device.device.service.DeviceProcessService;
|
||||
import com.njcn.device.device.service.IDeviceService;
|
||||
@@ -28,10 +30,7 @@ import org.apache.commons.collections4.ListUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -73,31 +72,59 @@ public class NodeDeviceServiceImpl implements NodeDeviceService {
|
||||
Integer nodeDevNum = node.getNodeDevNum();
|
||||
Integer maxProcessNum = node.getMaxProcessNum();
|
||||
List<Device> list = iDeviceService.lambdaQuery().eq(Device::getNodeId, nodeId).list();
|
||||
if(CollectionUtils.isEmpty(list)){
|
||||
if(CollectionUtils.isEmpty(list)){
|
||||
throw new BusinessException(PvDeviceResponseEnum.NO_DEVICE);
|
||||
|
||||
}
|
||||
List<String> deviceIdList = list.stream().map(Device::getId).collect(Collectors.toList());
|
||||
List<DeviceProcess> deviceProcessList = deviceProcessService.lambdaQuery().in(DeviceProcess::getId, deviceIdList).list();
|
||||
Integer devNum = list.size();
|
||||
if(nodeDevNum<devNum){
|
||||
if(nodeDevNum<list.size()){
|
||||
throw new BusinessException(PvDeviceResponseEnum.OVER_LIMIT);
|
||||
}
|
||||
//单个进程支持最大装置数
|
||||
int maxDevNum = nodeDevNum / maxProcessNum;
|
||||
|
||||
List<List<DeviceProcess>> partition = ListUtils.partition(deviceProcessList, maxProcessNum);
|
||||
for (int i = 0; i < partition.size(); i++) {
|
||||
int processNo = i+1;
|
||||
partition.get(i).forEach(temp->{
|
||||
temp.setProcessNo(processNo);
|
||||
});
|
||||
List<String> deviceIdList = list.stream().map(Device::getId).collect(Collectors.toList());
|
||||
List<DeviceProcess> existingProcesses = deviceProcessService.lambdaQuery().in(DeviceProcess::getId, deviceIdList).list();
|
||||
if(existingProcesses.size() >= nodeDevNum){
|
||||
throw new BusinessException(PvDeviceResponseEnum.OVER_LIMIT);
|
||||
}
|
||||
List<String> hasProcess = existingProcesses.stream().map(DeviceProcess::getId).distinct().collect(Collectors.toList());
|
||||
|
||||
//过滤掉已经分配的装置
|
||||
List<String> needDevice = deviceIdList.stream().filter(it->!hasProcess.contains(it)).collect(Collectors.toList());
|
||||
// 无待分配设备直接抛出异常(保留你的原判断)
|
||||
if (CollUtil.isEmpty(needDevice)) {
|
||||
throw new BusinessException(CommonResponseEnum.FAIL, "不存在未分配的进程的装置");
|
||||
}
|
||||
//单个进程支持最大装置数
|
||||
int maxDevNumPerProcess = (int) Math.ceil((double) nodeDevNum / maxProcessNum);
|
||||
Map<Integer, Long> mapCount = existingProcesses.stream().collect(Collectors.groupingBy(DeviceProcess::getProcessNo,Collectors.counting()));
|
||||
List<DeviceProcess> poList = new ArrayList<>();
|
||||
Iterator<String> deviceIterator = needDevice.iterator();
|
||||
|
||||
for (int processNo = 1; processNo <= maxProcessNum && deviceIterator.hasNext(); processNo++) {
|
||||
Long usedCount = mapCount.getOrDefault(processNo, 0L);
|
||||
int remainingCapacity = (int) (maxDevNumPerProcess - usedCount);
|
||||
|
||||
if (remainingCapacity <= 0) {
|
||||
continue;
|
||||
}
|
||||
// 分配设备给当前进程
|
||||
for (int i = 0; i < remainingCapacity && deviceIterator.hasNext(); i++) {
|
||||
String deviceId = deviceIterator.next();
|
||||
DeviceProcess deviceProcess = buildDeviceProcess(deviceId, processNo);
|
||||
poList.add(deviceProcess);
|
||||
}
|
||||
}
|
||||
if(CollUtil.isEmpty(poList)){
|
||||
throw new BusinessException(CommonResponseEnum.FAIL,"不存在可以分配的进程或装置");
|
||||
}
|
||||
List<DeviceProcess> collect = partition.stream().flatMap(List::stream).collect(Collectors.toList());
|
||||
|
||||
//更新进程号
|
||||
deviceProcessService.updateBatchById(collect);
|
||||
deviceProcessService.saveBatch(poList,100);
|
||||
}
|
||||
|
||||
|
||||
private DeviceProcess buildDeviceProcess(String deviceId, Integer processNo) {
|
||||
DeviceProcess deviceProcess = new DeviceProcess();
|
||||
deviceProcess.setId(deviceId); // 核心修复:设置设备关联字段,而非主键id
|
||||
deviceProcess.setProcessNo(processNo);
|
||||
return deviceProcess;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user