优化抄表记录

This commit is contained in:
2023-11-16 09:42:10 +08:00
parent 3bbcccff16
commit fd3ff2616a
3 changed files with 26 additions and 13 deletions

View File

@@ -25,7 +25,11 @@ import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@Slf4j
@@ -41,7 +45,7 @@ public class MeterLogService extends BaseService<MeterLogEntity, MeterLogReposit
MeterLogEntity entity = mapper.toEntity(request);
this.repository.save(entity);
syncMeterLog(entity.getMeter().getId());
syncMeterLog(entity.getMeter().getId(), request.getTime());
return getById(entity.getId());
}
@@ -50,18 +54,22 @@ public class MeterLogService extends BaseService<MeterLogEntity, MeterLogReposit
this.mapper.updateEntity(entity, request);
this.repository.save(entity);
syncMeterLog(entity.getMeter().getId());
syncMeterLog(entity.getMeter().getId(), request.getTime());
return getById(entity.getId());
}
public void delete(IdRequest request) {
List<MeterLogEntity> all = this.repository.findAllById(request.getIds());
Map<String, List<MeterLogEntity>> groupByMeterId = all.stream().collect(Collectors.groupingBy(l -> l.getMeter().getId()));
this.repository.deleteAllById(request.getIds());
for (MeterLogEntity entity : all) {
syncMeterLog(entity.getMeter().getId());
}
groupByMeterId.forEach((meterId, meterLogs) -> {
syncMeterLog(meterId, meterLogs.stream().sorted(Comparator.comparing(MeterLogEntity::getTime)).findFirst().map(MeterLogEntity::getTime).orElse(LocalDateTime.now()));
});
}
public MeterLogDto getById(String id) {
@@ -84,7 +92,8 @@ public class MeterLogService extends BaseService<MeterLogEntity, MeterLogReposit
@Autowired MeterMonthLogService meterMonthLogService;
public void syncMeterLog(String meterId) {
public void syncMeterLog(String meterId, LocalDateTime time) {
log.info("syncMeterLog {} {} 开始同步", meterId, time);
Query nativeQuery =
entityManager.createNativeQuery(
@@ -99,7 +108,7 @@ public class MeterLogService extends BaseService<MeterLogEntity, MeterLogReposit
from (
select id, time, value, COALESCE(lag(value) over (order by time), value ) as previous_value from t_meter_log where meter_id = :meterId
select id, time, value, COALESCE(lag(value) over (order by time), value ) as previous_value from t_meter_log where meter_id = :meterId and time >= :time
) tb
where tb.id = ta.id
@@ -108,6 +117,7 @@ public class MeterLogService extends BaseService<MeterLogEntity, MeterLogReposit
""");
nativeQuery.setParameter("meterId", meterId);
nativeQuery.setParameter("time", time);
StopWatch stopWatch = new StopWatch();
stopWatch.start("syncMeterLog");
@@ -117,7 +127,7 @@ public class MeterLogService extends BaseService<MeterLogEntity, MeterLogReposit
stopWatch.start("syncMeterMonthLog");
meterMonthLogService.syncMeter(meterId);
meterMonthLogService.syncMeter(meterId, time);
stopWatch.stop();
log.info("syncMeterLog {} 更新了 {} 条数据\n {}", meterId, count, stopWatch.prettyPrint());

View File

@@ -8,5 +8,5 @@ import org.springframework.stereotype.Repository;
@Repository
public interface MeterMonthLogRepository extends BaseRepository<MeterMonthLogEntity> {
long deleteByMeter(MeterEntity meter);
long deleteByMeterAndTimeGreaterThanEqual(MeterEntity meter, java.time.LocalDate time);
}

View File

@@ -25,6 +25,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Service
@Slf4j
@@ -76,11 +77,11 @@ public class MeterMonthLogService
}
public void syncMeter(String meterId) {
public void syncMeter(String meterId, LocalDateTime time) {
MeterEntity meter = new MeterEntity();
meter.setId(meterId);
repository.deleteByMeter(meter);
repository.deleteByMeterAndTimeGreaterThanEqual(meter, time.toLocalDate());
Query nativeQuery =
entityManager.createNativeQuery(
@@ -90,10 +91,11 @@ public class MeterMonthLogService
meter_id,
sum(usage) as value
from t_meter_log
where meter_id = :meterId group by cast(DATE_TRUNC('month', time) as date), meter_id
where meter_id = :meterId and time >= :time group by cast(DATE_TRUNC('month', time) as date), meter_id
""");
nativeQuery.setParameter("meterId", meterId);
nativeQuery.setParameter("time", time);
nativeQuery
.getResultList()
@@ -120,7 +122,7 @@ public class MeterMonthLogService
from (
select id, time, value, COALESCE(lag(value) over (order by time), value ) as previous_value from t_meter_month_log where meter_id = :meterId
select id, time, value, COALESCE(lag(value) over (order by time), value ) as previous_value from t_meter_month_log where meter_id = :meterId and time >=:time
) tb
where tb.id = ta.id
@@ -129,6 +131,7 @@ public class MeterMonthLogService
""");
nativeQuery2.setParameter("meterId", meterId);
nativeQuery2.setParameter("time", time.toLocalDate());
nativeQuery2.executeUpdate();
}