|
|
@@ -3,15 +3,16 @@ package com.usky.ems.service.impl;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.usky.ems.domain.EmsDevice;
|
|
|
import com.usky.ems.domain.EmsDeviceFunction;
|
|
|
+import com.usky.ems.enums.TemporalTypeEnum;
|
|
|
import com.usky.ems.mapper.EmsDeviceFunctionMapper;
|
|
|
import com.usky.ems.mapper.EmsDeviceMapper;
|
|
|
import com.usky.ems.service.EmsDeviceReportService;
|
|
|
import com.usky.ems.service.EmsSpaceService;
|
|
|
-import com.usky.ems.service.vo.DeviceEnergyReportItemVO;
|
|
|
-import com.usky.ems.service.vo.DeviceEnergyReportRequest;
|
|
|
+import com.usky.ems.service.vo.*;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -106,6 +107,151 @@ public class EmsDeviceReportServiceImpl implements EmsDeviceReportService {
|
|
|
return deviceList;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Object showEnergyReportData(DeviceFunctionValueReportDTO dto) {
|
|
|
+ checkParams(dto);
|
|
|
+ TemporalTypeEnum temporalTypeEnum = TemporalTypeEnum.get(dto.getTimeType());
|
|
|
+ LocalDateTime startLocalTime = dto.getStartTime();
|
|
|
+ LocalDateTime endLocalTime = dto.getEndTime();
|
|
|
+ String identifier = getDeviceFunctionIdentifierByEnergyType(dto.getEnergyType());
|
|
|
+ if (temporalTypeEnum != null) {
|
|
|
+ checkDateTime(startLocalTime, endLocalTime, temporalTypeEnum);
|
|
|
+ startLocalTime = temporalTypeEnum.normalizeStart(startLocalTime);
|
|
|
+ endLocalTime = temporalTypeEnum.normalizeEnd(endLocalTime);
|
|
|
+ DeviceFunctionLastValueDTO paramDTO = convertParamDTO(dto, startLocalTime, endLocalTime, temporalTypeEnum, identifier);
|
|
|
+ return asyncProcessData(dto, paramDTO, temporalTypeEnum);
|
|
|
+ } else {
|
|
|
+ DeviceFunctionOneValueDTO paramDTO = convertParamOneLastValueDTO(dto, startLocalTime, endLocalTime, identifier);
|
|
|
+ return getCustomizeEnergyReportVOList(dto, paramDTO, startLocalTime);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkParams(DeviceFunctionValueReportDTO dto) {
|
|
|
+ if (dto == null) {
|
|
|
+ throw new IllegalArgumentException("请求参数不能为空");
|
|
|
+ }
|
|
|
+ if (dto.getProjectId() == null) {
|
|
|
+ throw new IllegalArgumentException("projectId 不能为空");
|
|
|
+ }
|
|
|
+ if (dto.getStartTime() == null || dto.getEndTime() == null) {
|
|
|
+ throw new IllegalArgumentException("开始时间和结束时间不能为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkDateTime(LocalDateTime start, LocalDateTime end, TemporalTypeEnum type) {
|
|
|
+ if (start.isAfter(end)) {
|
|
|
+ throw new IllegalArgumentException("开始时间不能晚于结束时间");
|
|
|
+ }
|
|
|
+ // 如需限制时间跨度,可在此扩展逻辑(例如:按日最多查询31天)。
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getDeviceFunctionIdentifierByEnergyType(Integer energyType) {
|
|
|
+ if (energyType == null) {
|
|
|
+ return "total_kwh";
|
|
|
+ }
|
|
|
+ switch (energyType) {
|
|
|
+ case 1:
|
|
|
+ return "total_kwh";
|
|
|
+ case 2:
|
|
|
+ return "total_water";
|
|
|
+ case 3:
|
|
|
+ return "total_gas";
|
|
|
+ default:
|
|
|
+ return "total_kwh";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private DeviceFunctionLastValueDTO convertParamDTO(DeviceFunctionValueReportDTO src,
|
|
|
+ LocalDateTime start,
|
|
|
+ LocalDateTime end,
|
|
|
+ TemporalTypeEnum temporalTypeEnum,
|
|
|
+ String identifier) {
|
|
|
+ DeviceFunctionLastValueDTO dto = new DeviceFunctionLastValueDTO();
|
|
|
+ dto.setProjectId(src.getProjectId());
|
|
|
+ dto.setDeviceIds(src.getDeviceIds());
|
|
|
+ dto.setAttributePointIds(src.getAttributePointIds());
|
|
|
+ dto.setTimeType(temporalTypeEnum != null ? temporalTypeEnum.getCode() : null);
|
|
|
+ dto.setStartTime(start);
|
|
|
+ dto.setEndTime(end);
|
|
|
+ dto.setIdentifier(identifier);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ private DeviceFunctionOneValueDTO convertParamOneLastValueDTO(DeviceFunctionValueReportDTO src,
|
|
|
+ LocalDateTime start,
|
|
|
+ LocalDateTime end,
|
|
|
+ String identifier) {
|
|
|
+ DeviceFunctionOneValueDTO dto = new DeviceFunctionOneValueDTO();
|
|
|
+ dto.setProjectId(src.getProjectId());
|
|
|
+ dto.setDeviceIds(src.getDeviceIds());
|
|
|
+ dto.setAttributePointIds(src.getAttributePointIds());
|
|
|
+ dto.setStartTime(start);
|
|
|
+ dto.setEndTime(end);
|
|
|
+ dto.setIdentifier(identifier);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object asyncProcessData(DeviceFunctionValueReportDTO origin,
|
|
|
+ DeviceFunctionLastValueDTO paramDTO,
|
|
|
+ TemporalTypeEnum temporalTypeEnum) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("timeType", temporalTypeEnum != null ? temporalTypeEnum.getCode() : null);
|
|
|
+ result.put("startTime", paramDTO.getStartTime());
|
|
|
+ result.put("endTime", paramDTO.getEndTime());
|
|
|
+
|
|
|
+ // 模拟数据:根据 deviceIds 与 attributePointIds 生成简单的时间序列数据
|
|
|
+ List<Map<String, Object>> items = new ArrayList<>();
|
|
|
+ List<String> deviceIds = paramDTO.getDeviceIds() != null ? paramDTO.getDeviceIds() : Collections.emptyList();
|
|
|
+ List<Long> attrIds = paramDTO.getAttributePointIds() != null ? paramDTO.getAttributePointIds() : Collections.emptyList();
|
|
|
+
|
|
|
+ int index = 0;
|
|
|
+ for (String deviceId : deviceIds) {
|
|
|
+ for (Long attrId : attrIds) {
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
+ item.put("deviceId", deviceId);
|
|
|
+ item.put("attributePointId", attrId);
|
|
|
+ item.put("identifier", paramDTO.getIdentifier());
|
|
|
+ // 简单模拟一个数值:100 + 10 * index
|
|
|
+ item.put("value", 100 + 10 * index);
|
|
|
+ item.put("unit", "kWh");
|
|
|
+ items.add(item);
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ result.put("items", items);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object getCustomizeEnergyReportVOList(DeviceFunctionValueReportDTO origin,
|
|
|
+ DeviceFunctionOneValueDTO paramDTO,
|
|
|
+ LocalDateTime startLocalTime) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("time", startLocalTime);
|
|
|
+
|
|
|
+ List<Map<String, Object>> items = new ArrayList<>();
|
|
|
+ List<String> deviceIds = paramDTO.getDeviceIds() != null ? paramDTO.getDeviceIds() : Collections.emptyList();
|
|
|
+ List<Long> attrIds = paramDTO.getAttributePointIds() != null ? paramDTO.getAttributePointIds() : Collections.emptyList();
|
|
|
+
|
|
|
+ int index = 0;
|
|
|
+ for (String deviceId : deviceIds) {
|
|
|
+ for (Long attrId : attrIds) {
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
+ item.put("deviceId", deviceId);
|
|
|
+ item.put("attributePointId", attrId);
|
|
|
+ item.put("identifier", paramDTO.getIdentifier());
|
|
|
+ // 单点场景模拟一个数值:200 + 5 * index
|
|
|
+ item.put("value", 200 + 5 * index);
|
|
|
+ item.put("unit", "kWh");
|
|
|
+ items.add(item);
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ result.put("items", items);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
private DeviceEnergyReportItemVO deviceToVo(EmsDevice d) {
|
|
|
DeviceEnergyReportItemVO vo = new DeviceEnergyReportItemVO();
|
|
|
vo.setId(d.getId());
|