完善报表

This commit is contained in:
2024-04-22 21:12:31 +08:00
parent 368093b799
commit 4cff327321

View File

@@ -89,15 +89,34 @@ public class PdcDeviceDataService
public List<Map> getReport(GetReportRequest request) {
Tuple minMaxTime = em.createQuery(
"select min(d.time) as minTime, max(d.time) as maxTime from PdcDeviceDataEntity d where d.time >= :startTime and d.time<=:endTime and d.device.deviceGroup = :deviceGroup and d.organizationId = :organizationId", Tuple.class)
.setParameter("startTime", request.getStartTime())
.setParameter(
"endTime",
request.getEndTime().isAfter(LocalDateTime.now())
? LocalDateTime.now()
: request.getEndTime())
.setParameter("deviceGroup", request.getDeviceGroup())
.setParameter("organizationId", Ctx.currentUser().getOrganizationId())
.getSingleResult();
if (minMaxTime == null) {
return List.of();
}
LocalDateTime startTime = minMaxTime.get("minTime", LocalDateTime.class);
LocalDateTime endTime = minMaxTime.get("maxTime", LocalDateTime.class);
Query nativeQuery =
em.createNativeQuery(
"""
with tmp as (select d.device_id,
public.time_bucket_gapfill(cast(:bucket as interval), d.time) as tb,
locf(max(d.total_data), (select max(total_data) from t_pdc_device_data dx where dx.device_id = d.device_id and dx.time < :startTime
) , true) as total_data
time_bucket_gapfill(cast(:bucket as interval), d.time) as tb,
interpolate(max(d.total_data)) as total_data
from t_pdc_device_data d
left join t_pdc_device pdc on d.device_id = pdc.id
@@ -112,8 +131,8 @@ public class PdcDeviceDataService
select tmp.*, d.coal_type, d.code, d.name from tmp inner join t_pdc_device d on tmp.device_id = d.id
),
tmp2 as (select *,
round(cast(tmp.total_data - lag(tmp.total_data, 1, null)
over (partition by tmp.device_id order by tmp.tb ) as numeric),
round(cast(tmp.total_data - first_value(tmp.total_data)
over (partition by tmp.device_id order by tmp.tb asc ) as numeric),
3) as diff
from tmp1 tmp),
@@ -136,11 +155,11 @@ public class PdcDeviceDataService
""",
Tuple.class);
nativeQuery.setParameter("startTime", request.getStartTime());
nativeQuery.setParameter("endTime", request.getEndTime().isAfter(LocalDateTime.now())? LocalDateTime.now(): request.getEndTime());
nativeQuery.setParameter("startTime", startTime);
nativeQuery.setParameter("endTime", endTime);
nativeQuery.setParameter("deviceGroup", request.getDeviceGroup());
Duration duration = Duration.between(request.getStartTime(), request.getEndTime());
Duration duration = Duration.between(startTime, endTime);
// 最多返回100条数据用于绘图, 最少10s一个点
var maxBucket = Math.max(10, duration.getSeconds() / 100);