|
@@ -200,77 +200,75 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
|
|
|
@Override
|
|
|
public List<Map<String, String>> rollingControl(String modelId) {
|
|
|
List<Map<String, String>> res = new ArrayList<>();
|
|
|
- String leanModelCode = queryLeanModelCodeById(modelId);
|
|
|
|
|
|
String[] names = {"棒一", "棒二", "棒三", "高线", "板带"};
|
|
|
|
|
|
- LambdaQueryWrapper<DeviceRegion> regionWrapper = new LambdaQueryWrapper<>();
|
|
|
- regionWrapper.eq(DeviceRegion::getRegionTitle, "轧钢厂");
|
|
|
- DeviceRegion deviceRegion = deviceRegionService.getOne(regionWrapper);
|
|
|
+ // 查询设备区域和设备信息
|
|
|
+ DeviceRegion deviceRegion = deviceRegionService.getOne(
|
|
|
+ new LambdaQueryWrapper<DeviceRegion>().eq(DeviceRegion::getRegionTitle, "轧钢厂")
|
|
|
+ );
|
|
|
+
|
|
|
+ List<DeviceInformation> deviceInformations = deviceInformationService.list(
|
|
|
+ new LambdaQueryWrapper<DeviceInformation>().eq(DeviceInformation::getDeviceRegionId, deviceRegion.getId())
|
|
|
+ );
|
|
|
+
|
|
|
+ // 查询所有相关的 MongoDB 数据
|
|
|
+ List<String> deviceIds = deviceInformations.stream()
|
|
|
+ .map(DeviceInformation::getId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ Query query = new Query();
|
|
|
+ query.addCriteria(Criteria.where("deviceInformationId").in(deviceIds));
|
|
|
|
|
|
- LambdaQueryWrapper<DeviceInformation> informationWrapper = new LambdaQueryWrapper<>();
|
|
|
- informationWrapper.eq(DeviceInformation::getDeviceRegionId, deviceRegion.getId());
|
|
|
- List<DeviceInformation> deviceInformations = deviceInformationService.list(informationWrapper);
|
|
|
+ //只获取必要字段,减少查询返回的数据量,提升查询速度
|
|
|
+ query.fields().include("deviceInformationId").include("power").include("datestr");
|
|
|
+ List<DeviceStatiscsModelMongodb> allMongoData = mongoTemplate.find(query, DeviceStatiscsModelMongodb.class, "leanmodel_run_realtime");
|
|
|
|
|
|
- // 按 DeviceInformation::getDeviceType 分组
|
|
|
- Map<String, List<DeviceInformation>> groupedByDeviceType = deviceInformations.stream()
|
|
|
- .collect(Collectors.groupingBy(DeviceInformation::getDeviceType));
|
|
|
+ // 按 deviceInformationId 分组
|
|
|
+ Map<String, List<DeviceStatiscsModelMongodb>> mongoDataGrouped = allMongoData.stream()
|
|
|
+ .collect(Collectors.groupingBy(DeviceStatiscsModelMongodb::getDeviceInformationId));
|
|
|
|
|
|
- // 创建一个 Map 用于存储每个 deviceType 的 power 累积
|
|
|
+ // 分组统计数据
|
|
|
Map<String, BigDecimal> powerAccumulation = new HashMap<>();
|
|
|
+ Map<String, Long> statusZeroCountMap = new HashMap<>();
|
|
|
|
|
|
- groupedByDeviceType.forEach((deviceType, devices) -> {
|
|
|
- devices.forEach(device -> {
|
|
|
- Query querylr = new Query();
|
|
|
- querylr.addCriteria(Criteria.where("deviceInformationId").in(device.getId()));
|
|
|
-
|
|
|
- // 执行查询
|
|
|
- List<DeviceStatiscsModelMongodb> deviceStatiscsModelMongodbList = mongoTemplate.find(querylr, DeviceStatiscsModelMongodb.class, "leanmodel_run_realtime");
|
|
|
-
|
|
|
- // 根据 datestr 降序排列,并取最新一条
|
|
|
- if (deviceStatiscsModelMongodbList != null && !deviceStatiscsModelMongodbList.isEmpty()) {
|
|
|
- DeviceStatiscsModelMongodb latestRecord = deviceStatiscsModelMongodbList.stream()
|
|
|
- .sorted((d1, d2) -> d2.getDatestr().compareTo(d1.getDatestr())) // 降序排序
|
|
|
- .findFirst() // 获取第一条数据(即最新的数据)
|
|
|
- .orElse(null); // 如果没有数据,返回 null
|
|
|
-
|
|
|
- // 如果找到最新记录,累加 power
|
|
|
- if (latestRecord != null) {
|
|
|
- BigDecimal power = latestRecord.getPower(); // 假设 power 字段是 BigDecimal 类型
|
|
|
- if (power != null) {
|
|
|
- // 累加到对应的 deviceType 中
|
|
|
- powerAccumulation.merge(deviceType, power, BigDecimal::add);
|
|
|
- }
|
|
|
- }
|
|
|
+ deviceInformations.forEach(device -> {
|
|
|
+ String deviceType = device.getDeviceType();
|
|
|
+
|
|
|
+ // 统计状态为 0 的设备数量
|
|
|
+ statusZeroCountMap.merge(deviceType, "0".equals(device.getStatus()) ? 1L : 0L, Long::sum);
|
|
|
+
|
|
|
+ // 获取最新 MongoDB 数据
|
|
|
+ List<DeviceStatiscsModelMongodb> mongoData = mongoDataGrouped.getOrDefault(device.getId(), Collections.emptyList())
|
|
|
+ .stream()
|
|
|
+ .sorted(Comparator.comparing(DeviceStatiscsModelMongodb::getDatestr).reversed())
|
|
|
+ .limit(1)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ mongoData.forEach(data -> {
|
|
|
+ if (data.getPower() != null) {
|
|
|
+ powerAccumulation.merge(deviceType, data.getPower(), BigDecimal::add);
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- // 计算实时总电压占比
|
|
|
+ // 计算实时总电压
|
|
|
BigDecimal totalPower = powerAccumulation.values().stream()
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add); // 计算所有设备的实时总电压
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
|
for (int i = 0; i < names.length; i++) {
|
|
|
String deviceTypeKey = String.valueOf(i + 1); // deviceType 从 1 开始
|
|
|
BigDecimal currentPower = powerAccumulation.getOrDefault(deviceTypeKey, BigDecimal.ZERO);
|
|
|
|
|
|
BigDecimal proportion = totalPower.compareTo(BigDecimal.ZERO) > 0
|
|
|
- ? currentPower.divide(totalPower, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).stripTrailingZeros() // 转为百分比并保留 4 位小数
|
|
|
+ ? currentPower.divide(totalPower, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).stripTrailingZeros()
|
|
|
: BigDecimal.ZERO;
|
|
|
|
|
|
- // 统计 status 为 0 的设备数量
|
|
|
- long statusZeroCount = groupedByDeviceType
|
|
|
- .getOrDefault(deviceTypeKey, Collections.emptyList())
|
|
|
- .stream()
|
|
|
- .filter(device -> device != null && "0".equals(device.getStatus())) // 双重防护
|
|
|
- .count();
|
|
|
-
|
|
|
-
|
|
|
// 构造返回数据
|
|
|
Map<String, String> data = new HashMap<>();
|
|
|
data.put("name", names[i]);
|
|
|
data.put("proportion", proportion.toString());
|
|
|
- data.put("statusZeroCount", String.valueOf(statusZeroCount));
|
|
|
+ data.put("statusZeroCount", String.valueOf(statusZeroCountMap.getOrDefault(deviceTypeKey, 0L)));
|
|
|
|
|
|
res.add(data);
|
|
|
}
|