refactor(property): 优化能源趋势查询接口返回值类型

This commit is contained in:
2025-08-28 15:05:01 +08:00
parent 76c0aa74f1
commit cfff5df7d0
7 changed files with 89 additions and 50 deletions

View File

@@ -1,6 +1,7 @@
package org.dromara.property.controller.smartDevicesController;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
@@ -115,13 +116,13 @@ public class TbMeterRecordController extends BaseController {
* @param year 年份
*/
@GetMapping("/trend")
public R<Void> getEnergyTrend(@RequestParam(required = false) String floorId,
public R<Map<String, Object>> getEnergyTrend(@RequestParam(required = false) String floorId,
@RequestParam(required = false) String meterId,
@RequestParam Long meterType,
@RequestParam String day,
@RequestParam String month,
@RequestParam String year) {
tbMeterRecordService.getEnergyTrend(floorId, meterId, meterType, day, month, year);
return R.ok();
Map<String, Object> resultMap = tbMeterRecordService.getEnergyTrend(floorId, meterId, meterType, day, month, year);
return R.ok(resultMap);
}
}

View File

@@ -133,6 +133,11 @@ public class TbMeterInfoVo implements Serializable {
@ExcelProperty(value = "采集器IP")
private String hostIp;
/**
* 租户编号
*/
private String tenantId;
/**
* 备注
*/

View File

@@ -194,6 +194,7 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService {
record.setMeterType(info.getMeterType());
record.setReaderId(1L);
record.setReadingTime(DateUtil.parse(result.getRecordTime(), "yyyy-MM-dd HH:mm:ss"));
record.setTenantId(info.getTenantId());
// 获取当前读数
BigDecimal currentReading = BigDecimal.valueOf(
@@ -234,16 +235,50 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService {
* @param year 年份
*/
@Override
public void getEnergyTrend(String floorId, String meterId, Long meterType, String day, String month, String year) {
public Map<String, Object> getEnergyTrend(String floorId, String meterId, Long meterType, String day, String month, String year) {
Map<String, Object> resultMap = new HashMap<>();
// hour
List<Map<String, Object>> hourList = baseMapper.getHourTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, day);
log.info("小时数据:{}", hourList);
Map<String, Object> hourMap = new HashMap<>();
String[] hourCategories = hourList.stream()
.map(map -> map.get("hour").toString())
.toArray(String[]::new);
BigDecimal[] hourData = hourList.stream()
.map(map -> new BigDecimal(map.get("total_consumption").toString()))
.toArray(BigDecimal[]::new);
hourMap.put("categories", hourCategories);
hourMap.put("data", hourData);
// day
String[] monthArr = month.split("-");
List<Map<String, Object>> dayList = baseMapper.getDayTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, monthArr[0], monthArr[1]);
log.info("天数据:{}", dayList);
Map<String, Object> dayMap = new HashMap<>();
String[] dayCategories = dayList.stream()
.map(map -> map.get("day").toString())
.toArray(String[]::new);
BigDecimal[] dayData = dayList.stream()
.map(map -> new BigDecimal(map.get("total_consumption").toString()))
.toArray(BigDecimal[]::new);
dayMap.put("categories", dayCategories);
dayMap.put("data", dayData);
// month
List<Map<String, Object>> monthList = baseMapper.getMonthTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year);
log.info("月数据:{}", monthList);
Map<String, Object> monthMap = new HashMap<>();
String[] monthCategories = monthList.stream()
.map(map -> map.get("month").toString())
.toArray(String[]::new);
BigDecimal[] monthData = monthList.stream()
.map(map -> new BigDecimal(map.get("total_consumption").toString()))
.toArray(BigDecimal[]::new);
monthMap.put("categories", monthCategories);
monthMap.put("data", monthData);
resultMap.put("hour", hourMap);
resultMap.put("day", dayMap);
resultMap.put("month", monthMap);
return resultMap;
}
}

View File

@@ -8,6 +8,7 @@ import org.dromara.property.rocketmq.domain.MeterResult;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* 抄记录Service接口
@@ -84,5 +85,5 @@ public interface ITbMeterRecordService {
* @param month 月份
* @param year 年份
*/
void getEnergyTrend(String floorId, String meterId, Long meterType, String day, String month, String year);
Map<String, Object> getEnergyTrend(String floorId, String meterId, Long meterType, String day, String month, String year);
}

View File

@@ -15,8 +15,8 @@
<if test="meterId != '' and meterId != null">
AND a.meter_id = #{meterId}
</if>
GROUP BY CONCAT_WS(':', HOUR(reading_time), '00')
ORDER BY hour
GROUP BY CONCAT_WS(':', HOUR(reading_time), '00'), HOUR(reading_time)
ORDER BY HOUR(reading_time)
</select>