longz
2026-08-04 07287305466a0695d1310c6e50ffefd4b15c054c
fix: use daily records for storage income (#23884)
2个文件已删除
2个文件已修改
121 ■■■■■ 已修改文件
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/emind/service/EmindIncomeCalculator.java 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/emind/service/EmindStorageDashboardSyncService.java 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-other/src/test/java/com/ruoyi/other/emind/service/EmindIncomeCalculatorTest.java 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-other/src/test/java/com/ruoyi/other/emind/service/EmindStorageDashboardSyncServiceTest.java 39 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/emind/service/EmindIncomeCalculator.java
File was deleted
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/emind/service/EmindStorageDashboardSyncService.java
@@ -28,13 +28,16 @@
    public int syncDashboardData() {
        int success=0;
        LocalDateTime now=LocalDateTime.now();
        LocalDate today=LocalDate.now();
        for (EmindStation station: stationMapper.selectRemoteActiveStations()) {
            try {
                EmindMainTotalDto main=api.getMainTotal(station.getStationId());
                BigDecimal incomeToday=EmindIncomeCalculator.calculate(
                        api.getMeterTotals(station.getStationId()), api.getCurrentPrice(station.getStationId()));
                List<EmindIncomeHistoryDto> daily=api.getIncomeHistory(station.getStationId(),
                        LocalDate.now().withDayOfYear(1).atStartOfDay(), LocalDate.now().atTime(23,59,59));
                        today.withDayOfYear(1).atStartOfDay(), today.atTime(23,59,59));
                BigDecimal incomeToday=daily.stream()
                        .filter(row -> today.equals(row.getDate()) && row.getIncome()!=null)
                        .map(EmindIncomeHistoryDto::getIncome)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                mapper.upsertSnapshot(station, main, incomeToday, now);
                mapper.deleteCurve(station.getStationId());
                if (main.getHistoryDataList()!=null && !main.getHistoryDataList().isEmpty())
ruoyi-service/ruoyi-other/src/test/java/com/ruoyi/other/emind/service/EmindIncomeCalculatorTest.java
File was deleted
ruoyi-service/ruoyi-other/src/test/java/com/ruoyi/other/emind/service/EmindStorageDashboardSyncServiceTest.java
@@ -1,9 +1,8 @@
package com.ruoyi.other.emind.service;
import com.ruoyi.other.emind.domain.EmindStation;
import com.ruoyi.other.emind.dto.EmindCurrentPriceDto;
import com.ruoyi.other.emind.dto.EmindIncomeHistoryDto;
import com.ruoyi.other.emind.dto.EmindMainTotalDto;
import com.ruoyi.other.emind.dto.EmindMeterTotalDto;
import com.ruoyi.other.emind.mapper.EmindStationMapper;
import com.ruoyi.other.emind.mapper.EmindStorageDashboardMapper;
import com.ruoyi.other.emind.util.EmindCloudApiUtil;
@@ -14,6 +13,8 @@
import org.mockito.junit.jupiter.MockitoExtension;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -30,28 +31,28 @@
    @Mock private EmindStorageDashboardMapper mapper;
    @Test
    void storesCalculatedTodayIncomeInSnapshot() {
    void storesTodayIncomeFromDailyHistoryInSnapshot() {
        EmindStation station = station();
        when(stationMapper.selectRemoteActiveStations()).thenReturn(Collections.singletonList(station));
        EmindMainTotalDto main = new EmindMainTotalDto();
        when(api.getMainTotal(758L)).thenReturn(main);
        when(api.getMeterTotals(758L)).thenReturn(Collections.singletonList(meter()));
        when(api.getCurrentPrice(758L)).thenReturn(price());
        when(api.getIncomeHistory(eq(758L), any(), any())).thenReturn(Collections.emptyList());
        when(api.getIncomeHistory(eq(758L), any(), any())).thenReturn(Arrays.asList(
                income(LocalDate.now(), "12.34"), income(LocalDate.now().minusDays(1), "130.00")));
        assertEquals(1, new EmindStorageDashboardSyncService(api, stationMapper, mapper).syncDashboardData());
        ArgumentCaptor<BigDecimal> income = ArgumentCaptor.forClass(BigDecimal.class);
        verify(mapper).upsertSnapshot(eq(station), eq(main), income.capture(), any());
        assertEquals(new BigDecimal("130.00"), income.getValue());
        assertEquals(new BigDecimal("12.34"), income.getValue());
        verify(api, never()).getMeterTotals(any());
        verify(api, never()).getCurrentPrice(any());
    }
    @Test
    void priceFailureDoesNotWritePartialSnapshot() {
    void historyFailureDoesNotWritePartialSnapshot() {
        when(stationMapper.selectRemoteActiveStations()).thenReturn(Collections.singletonList(station()));
        when(api.getMainTotal(758L)).thenReturn(new EmindMainTotalDto());
        when(api.getMeterTotals(758L)).thenReturn(Collections.singletonList(meter()));
        when(api.getCurrentPrice(758L)).thenThrow(new IllegalStateException("price unavailable"));
        when(api.getIncomeHistory(eq(758L), any(), any())).thenThrow(new IllegalStateException("history unavailable"));
        assertEquals(0, new EmindStorageDashboardSyncService(api, stationMapper, mapper).syncDashboardData());
@@ -65,18 +66,10 @@
        return station;
    }
    private EmindMeterTotalDto meter() {
        EmindMeterTotalDto meter = new EmindMeterTotalDto();
        meter.setMeterType(3);
        meter.setChargedPeak(new BigDecimal("100"));
        meter.setDischargedPeak(new BigDecimal("200"));
        return meter;
    }
    private EmindCurrentPriceDto price() {
        EmindCurrentPriceDto price = new EmindCurrentPriceDto();
        price.setInPeakPrice(new BigDecimal("0.30"));
        price.setOutPeakPrice(new BigDecimal("0.80"));
        return price;
    private EmindIncomeHistoryDto income(LocalDate date, String value) {
        EmindIncomeHistoryDto income = new EmindIncomeHistoryDto();
        income.setDate(date);
        income.setIncome(new BigDecimal(value));
        return income;
    }
}