diff --git a/src/main/java/cn/lihongjie/coal/meterLog/service/MeterLogService.java b/src/main/java/cn/lihongjie/coal/meterLog/service/MeterLogService.java index 8729edc8..90f7b000 100644 --- a/src/main/java/cn/lihongjie/coal/meterLog/service/MeterLogService.java +++ b/src/main/java/cn/lihongjie/coal/meterLog/service/MeterLogService.java @@ -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 all = this.repository.findAllById(request.getIds()); + + Map> 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= :time ) tb where tb.id = ta.id @@ -108,6 +117,7 @@ public class MeterLogService extends BaseService { - long deleteByMeter(MeterEntity meter); + long deleteByMeterAndTimeGreaterThanEqual(MeterEntity meter, java.time.LocalDate time); } diff --git a/src/main/java/cn/lihongjie/coal/meterMonthLog/service/MeterMonthLogService.java b/src/main/java/cn/lihongjie/coal/meterMonthLog/service/MeterMonthLogService.java index a39579a0..fac2357f 100644 --- a/src/main/java/cn/lihongjie/coal/meterMonthLog/service/MeterMonthLogService.java +++ b/src/main/java/cn/lihongjie/coal/meterMonthLog/service/MeterMonthLogService.java @@ -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(); }