|
|
@@ -94,7 +94,13 @@ public class EmsAnalysisServiceImpl implements EmsAnalysisService {
|
|
|
// 查询每个时间点的能耗数据
|
|
|
List<EmsTrendItemVO> trendItems = new ArrayList<>();
|
|
|
for (String timePoint : timePoints) {
|
|
|
- EmsTrendItemVO item = calculateTrendItem(deviceIds, timeDimension, timePoint, energyTypeId);
|
|
|
+ EmsTrendItemVO item;
|
|
|
+ if (energyTypeId == null) {
|
|
|
+ // 当不指定能源类型时,查询项目中所有能源类型设备的总能耗
|
|
|
+ item = calculateTrendItemForAllEnergyTypes(projectId, timeDimension, timePoint);
|
|
|
+ } else {
|
|
|
+ item = calculateTrendItem(deviceIds, timeDimension, timePoint, energyTypeId);
|
|
|
+ }
|
|
|
trendItems.add(item);
|
|
|
}
|
|
|
|
|
|
@@ -358,7 +364,11 @@ public class EmsAnalysisServiceImpl implements EmsAnalysisService {
|
|
|
String yearPart = yearMonth[0];
|
|
|
String monthPart = yearMonth[1];
|
|
|
range[0] = yearPart + "-" + monthPart + "-01 00:00:00";
|
|
|
- range[1] = yearPart + "-" + monthPart + "-31 23:59:59";
|
|
|
+
|
|
|
+ // Calculate last day of the month properly
|
|
|
+ java.time.YearMonth ym = java.time.YearMonth.of(Integer.parseInt(yearPart), Integer.parseInt(monthPart));
|
|
|
+ int lastDay = ym.lengthOfMonth();
|
|
|
+ range[1] = yearPart + "-" + monthPart + "-" + lastDay + " 23:59:59";
|
|
|
} else {
|
|
|
range[0] = timePoint + "-01-01 00:00:00";
|
|
|
range[1] = timePoint + "-12-31 23:59:59";
|
|
|
@@ -1145,4 +1155,58 @@ sql.append("FROM ems_energy_consumption_device_data eed ");
|
|
|
|
|
|
return resp;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算所有能源类型的趋势项
|
|
|
+ * 当energyTypeId为null时使用,查询项目中所有能源类型设备的总能耗
|
|
|
+ */
|
|
|
+ private EmsTrendItemVO calculateTrendItemForAllEnergyTypes(Long projectId, String timeDimension, String timePoint) {
|
|
|
+ EmsTrendItemVO item = new EmsTrendItemVO();
|
|
|
+ item.setTimeLabel(timePoint);
|
|
|
+
|
|
|
+ try {
|
|
|
+ String[] timeRange = getTimeRange(timeDimension, timePoint);
|
|
|
+
|
|
|
+ // 对每种能源类型分别统计
|
|
|
+ BigDecimal totalAllUsage = BigDecimal.ZERO;
|
|
|
+ BigDecimal totalStandardCoal = BigDecimal.ZERO;
|
|
|
+ BigDecimal totalCarbonEmission = BigDecimal.ZERO;
|
|
|
+
|
|
|
+ for (Long energyType : Arrays.asList(1L, 2L, 3L)) {
|
|
|
+ // 获取该能源类型的设备
|
|
|
+ List<String> deviceIds = getDeviceIdsByProject(projectId, energyType);
|
|
|
+ if (!deviceIds.isEmpty()) {
|
|
|
+ // 查询该能源类型的用量
|
|
|
+ BigDecimal usage = queryTotalEnergyConsumption(deviceIds, timeRange[0], timeRange[1], energyType);
|
|
|
+ if (usage != null && usage.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ totalAllUsage = totalAllUsage.add(usage);
|
|
|
+
|
|
|
+ // 计算该能源类型的标准煤和碳排放
|
|
|
+ BigDecimal coalFactor = STANDARD_COAL_FACTORS.get(energyType);
|
|
|
+ BigDecimal carbonFactor = CARBON_EMISSION_FACTORS.get(energyType);
|
|
|
+
|
|
|
+ if (coalFactor != null) {
|
|
|
+ totalStandardCoal = totalStandardCoal.add(usage.multiply(coalFactor));
|
|
|
+ }
|
|
|
+ if (carbonFactor != null) {
|
|
|
+ totalCarbonEmission = totalCarbonEmission.add(usage.multiply(carbonFactor));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置结果
|
|
|
+ item.setUsage(totalAllUsage.setScale(2, RoundingMode.HALF_UP));
|
|
|
+ item.setStandardCoal(totalStandardCoal.setScale(2, RoundingMode.HALF_UP));
|
|
|
+ item.setCarbonEmission(totalCarbonEmission.setScale(2, RoundingMode.HALF_UP));
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println("Error calculating trend item for all energy types: " + e.getMessage());
|
|
|
+ item.setUsage(BigDecimal.ZERO);
|
|
|
+ item.setStandardCoal(BigDecimal.ZERO);
|
|
|
+ item.setCarbonEmission(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+
|
|
|
+ return item;
|
|
|
+ }
|
|
|
}
|