Преглед на файлове

开发单位综合能耗、综合累计能耗、分类能耗占比(总览顶部)接口

james преди 5 дни
родител
ревизия
999bbc7488

+ 1 - 1
service-ems/service-ems-biz/src/main/java/com/usky/ems/enums/EnergyTypeEnum.java

@@ -5,7 +5,7 @@ import com.usky.common.core.exception.BusinessException;
 import java.util.Objects;
 
 public enum EnergyTypeEnum {
-    AMMETER("电", 1, "10000000"),
+    AMMETER("电", 1, "totalactiveenergyf"),
     WATER("水", 2, "20000000"),
     COOL("冷", 3, "60000000"),
     HOT("热", 4, "50000000"),

+ 55 - 93
service-ems/service-ems-biz/src/main/java/com/usky/ems/service/impl/EmsOverviewServiceImpl.java

@@ -8,6 +8,7 @@ import com.usky.demo.RemoteTsdbProxyService;
 import com.usky.demo.domain.EnergyItemSumQueryVO;
 import com.usky.demo.domain.EnergyItemSumResultVO;
 import com.usky.ems.domain.*;
+import com.usky.ems.enums.EnergyTypeEnum;
 import com.usky.ems.mapper.DmpProductMapper;
 import com.usky.ems.mapper.EmsDeviceMapper;
 import com.usky.ems.mapper.EmsEnergyItemCodeMapper;
@@ -243,11 +244,11 @@ public class EmsOverviewServiceImpl implements EmsOverviewService {
             return null;
         }
 
-        BigDecimal data = sumEnergyByProductCodes(productCodeList, identifier, startTime, endTime);
+        BigDecimal data = sumEnergyByProductCodes(productCodeList, identifier.toLowerCase(), startTime, endTime);
 
         LocalDateTime pariPassuEndTime = endTime.minusYears(1);
         LocalDateTime pariPassuStartTime = getStartTime(dateType, pariPassuEndTime);
-        BigDecimal pariPassuData = sumEnergyByProductCodes(productCodeList, identifier, pariPassuStartTime, pariPassuEndTime);
+        BigDecimal pariPassuData = sumEnergyByProductCodes(productCodeList, identifier.toLowerCase(), pariPassuStartTime, pariPassuEndTime);
 
         LocalDateTime sequentialEndTime;
         if (dateType == 1) {
@@ -258,7 +259,7 @@ public class EmsOverviewServiceImpl implements EmsOverviewService {
             sequentialEndTime = endTime.minusYears(1);
         }
         LocalDateTime sequentialStartTime = getStartTime(dateType, sequentialEndTime);
-        BigDecimal sequentialData = sumEnergyByProductCodes(productCodeList, identifier, sequentialStartTime, sequentialEndTime);
+        BigDecimal sequentialData = sumEnergyByProductCodes(productCodeList, identifier.toLowerCase(), sequentialStartTime, sequentialEndTime);
 
         return buildClassificationResult(data, sequentialData, pariPassuData, coalFactor, co2Factor);
     }
@@ -582,7 +583,6 @@ public class EmsOverviewServiceImpl implements EmsOverviewService {
      * 单位综合能耗、综合累计能耗、分类能耗占比(总览顶部)
      * - 能源类型来自 EmsModelService.getEnergyTypeList(电/水/气)
      * - 折算系数来自 leo.ems_project_conversion_factor(优先取项目专属,其次 project_id=0 的公共配置)
-     * - 真实 TSDB 能耗数据暂不可用,当前按规则模拟各能源类型的能耗值。
      */
     @Override
     public Map<String, Object> queryOverviewTop(Integer dateType, Long projectId) {
@@ -599,8 +599,8 @@ public class EmsOverviewServiceImpl implements EmsOverviewService {
         }
 
         // 项目信息(含面积、根空间 ID)
-        Integer resolvedProjectId = projectId != null ? projectId.intValue() : null;
-        EmsProjectResponse project = getProject(resolvedProjectId);
+        Long resolvedProjectId = resolveProjectId(projectId);
+        EmsProjectResponse project = getProject(Math.toIntExact(resolvedProjectId));
         if (project == null || project.getSpaceId() == null) {
             return resultMap;
         }
@@ -620,102 +620,62 @@ public class EmsOverviewServiceImpl implements EmsOverviewService {
         }
 
         // 2. 查询折算系数(项目专属 + 公共)
-//        List<EmsProjectConversionFactor> factorList = emsProjectConversionFactorMapper.selectList(
-//                new LambdaQueryWrapper<EmsProjectConversionFactor>()
-//                        .in(EmsProjectConversionFactor::getEnergyType, typeIds)
-//                        .in(EmsProjectConversionFactor::getProjectId, pid, 0L)
-//        );
-//        if (factorList == null || factorList.isEmpty()) {
-//            return resultMap;
-//        }
-//
-//        // 优先使用项目专属配置;若没有则退回公共配置
-//        Map<Integer, List<EmsProjectConversionFactor>> groupedByType = factorList.stream()
-//                .collect(Collectors.groupingBy(EmsProjectConversionFactor::getEnergyType));
-
-        // 3. 模拟各能源类型能耗值(真实实现应从 TSDB 或历史统计表获取)
-        Map<Integer, BigDecimal> consumeMap = new HashMap<>();
-        BigDecimal base = new BigDecimal("1000");
-        BigDecimal step = new BigDecimal("200");
-        BigDecimal factor;
-        if (dateType == 1) {
-            factor = BigDecimal.ONE;
-        } else if (dateType == 2) {
-            factor = new BigDecimal("1.5");
-        } else if (dateType == 3) {
-            factor = new BigDecimal("2.0");
-        } else {
-            factor = BigDecimal.ONE;
+        List<EmsProjectConversionFactor> factorList = emsProjectConversionFactorMapper.selectList(
+                new LambdaQueryWrapper<EmsProjectConversionFactor>()
+                        .in(EmsProjectConversionFactor::getEnergyType, typeIds)
+                        .in(EmsProjectConversionFactor::getProjectId, resolvedProjectId, 0L)
+        );
+        if (factorList == null || factorList.isEmpty()) {
+            return resultMap;
         }
 
-        int index = 0;
-        for (Integer typeId : typeIds) {
-            BigDecimal value = base.subtract(step.multiply(BigDecimal.valueOf(index))).multiply(factor);
-            if (value.compareTo(BigDecimal.ZERO) < 0) {
-                value = BigDecimal.ZERO;
+        Map<Integer, BigDecimal> map = new HashMap();
+
+        for(Integer energyType : typeIds) {
+            if (energyType != 5) {
+                EnergyTypeEnum typeEnum = EnergyTypeEnum.get(energyType);
+
+
+                List<String> productCodeList = dmpProductMapper.selectProductCodesByEnergyType(
+                        SecurityUtils.getTenantId(), energyType);
+                if (productCodeList == null || productCodeList.isEmpty()) {
+                    return resultMap;
+                }
+
+                BigDecimal data = sumEnergyByProductCodes(productCodeList, typeEnum.getCode(), startTime, endTime);
+                map.put(energyType, data);
             }
-            consumeMap.put(typeId, value);
-            index++;
         }
 
-        BigDecimal totalCon = consumeMap.values().stream()
-                .reduce(BigDecimal.ZERO, BigDecimal::add);
-
-        // 4. 计算各能源类型折算标煤与碳排放、占比
+        Map<Integer, List<EmsProjectConversionFactor>> listMap = (Map)factorList.stream().collect(Collectors.groupingBy(EmsProjectConversionFactor::getEnergyType));
         BigDecimal coalTotal = BigDecimal.ZERO;
         BigDecimal co2Total = BigDecimal.ZERO;
-        List<Map<String, Object>> ratioList = new ArrayList<>();
-
-//        for (EmsEnergyTypeVO typeVO : energyTypes) {
-//            if (typeVO.getId() == null) {
-//                continue;
-//            }
-//            Integer typeId = typeVO.getId().intValue();
-//            BigDecimal con = consumeMap.get(typeId);
-//            if (con == null) {
-//                continue;
-//            }
-//
-//            List<EmsProjectConversionFactor> conversionFactorList = groupedByType.get(typeId);
-//            if (conversionFactorList == null || conversionFactorList.isEmpty()) {
-//                continue;
-//            }
-//
-//            // 先筛出项目专属配置
-//            List<EmsProjectConversionFactor> projectFactors = conversionFactorList.stream()
-//                    .filter(f -> pid.equals(f.getProjectId()))
-//                    .collect(Collectors.toList());
-//            List<EmsProjectConversionFactor> effectiveFactors = projectFactors.isEmpty()
-//                    ? conversionFactorList
-//                    : projectFactors;
-//
-//            Map<String, BigDecimal> factorMap = effectiveFactors.stream()
-//                    .filter(f -> f.getName() != null && f.getValue() != null)
-//                    .collect(Collectors.toMap(EmsProjectConversionFactor::getName, EmsProjectConversionFactor::getValue,
-//                            (oldVal, newVal) -> oldVal));
-//
-//            BigDecimal coal = factorMap.get("coal");
-//            if (coal == null) {
-//                continue;
-//            }
-//
-//            BigDecimal coalAmount = coal.multiply(con).setScale(2, RoundingMode.UP);
-//            BigDecimal co2Amount = coalAmount.multiply(new BigDecimal("2.4589")).setScale(2, RoundingMode.UP);
-//            coalTotal = coalTotal.add(coalAmount);
-//            co2Total = co2Total.add(co2Amount);
-//
-//            Map<String, Object> ratioMap = new HashMap<>();
-//            ratioMap.put("name", typeVO.getName());
-//            ratioMap.put("consume", coalAmount);
-//            ratioMap.put("totalConsume", totalCon.multiply(coal).setScale(2, RoundingMode.UP));
-//            ratioList.add(ratioMap);
-//        }
+        BigDecimal totalCon = (BigDecimal)map.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
+        List<Map<String, Object>> ratioList = new ArrayList();
+
+        for(Integer energyType : typeIds) {
+            if (energyType != 5) {
+                BigDecimal con = (BigDecimal)map.get(energyType);
+                List<EmsProjectConversionFactor> conversionFactorList = (List)listMap.get(energyType);
+                if (conversionFactorList != null && !conversionFactorList.isEmpty()) {
+                    Map<String, BigDecimal> factorMap = (Map)conversionFactorList.stream().collect(Collectors.toMap(EmsProjectConversionFactor::getName, EmsProjectConversionFactor::getValue));
+                    BigDecimal coal = (BigDecimal)factorMap.get("coal");
+                    BigDecimal coalAmount = coal.multiply(con).setScale(2, RoundingMode.UP);
+                    BigDecimal co2Amount = coalAmount.multiply(new BigDecimal("2.4589")).setScale(2, RoundingMode.UP);
+                    coalTotal = coalTotal.add(coalAmount);
+                    co2Total = co2Total.add(co2Amount);
+                    EnergyTypeEnum typeEnum = EnergyTypeEnum.get(energyType);
+                    Map<String, Object> ratioMap = new HashMap();
+                    ratioMap.put("name", typeEnum.getName());
+                    ratioMap.put("consume", con.multiply(coal).setScale(2, RoundingMode.UP));
+                    ratioMap.put("totalConsume", totalCon.multiply(coal).setScale(2, RoundingMode.UP));
+                    ratioList.add(ratioMap);
+                }
+            }
+        }
 
-        // 5. 单位综合能耗(每平米标煤量)
         BigDecimal unitCoal = BigDecimal.ZERO;
-        if (project.getArea() != null
-                && project.getArea().compareTo(BigDecimal.ZERO) != 0
-                && coalTotal.compareTo(BigDecimal.ZERO) != 0) {
+        if (project.getArea() != null && project.getArea().compareTo(BigDecimal.ZERO) != 0) {
             unitCoal = coalTotal.divide(project.getArea(), 2, RoundingMode.UP);
         }
 
@@ -724,6 +684,8 @@ public class EmsOverviewServiceImpl implements EmsOverviewService {
         resultMap.put("coalTotal", coalTotal);
         resultMap.put("co2Total", co2Total);
         return resultMap;
+
+
     }
 
     /**

+ 3 - 1
service-ems/service-ems-biz/src/main/resources/mapper/ems/DmpProductMapper.xml

@@ -8,9 +8,11 @@
         INNER JOIN ems_product_energy_type ep ON dp.id = ep.product_id
         WHERE dp.tenant_id = #{tenantId}
           AND dp.delete_flag = 0
-          AND ep.energy_type = #{energyType}
           AND dp.product_code IS NOT NULL
           AND dp.product_code != ''
+        <if test="energyType != null">
+            AND ep.energy_type = #{energyType}
+        </if>
     </select>
 
 </mapper>