| | |
| | | import com.ruoyi.other.emind.dto.EmindDashboardQuery; |
| | | import com.ruoyi.other.emind.mapper.EmindStorageDashboardMapper; |
| | | import org.springframework.stereotype.Service; |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | if (start==null) start="YEAR".equals(period)?end.withDayOfYear(1):("MONTH".equals(period)?end.withDayOfMonth(1):end.minusDays(6)); |
| | | Map<String,Object> result=new LinkedHashMap<>(); |
| | | result.put("summary", mapper.selectOverview(query.getStationId(), LocalDate.now())); |
| | | result.put("history", mapper.selectHistory(query.getStationId(), period, start, end)); |
| | | result.put("history", completeHistory(mapper.selectHistory(query.getStationId(), period, start, end), period, start, end)); |
| | | result.put("curve", mapper.selectCurve(query.getStationId())); |
| | | return result; |
| | | } |
| | | |
| | | private List<Map<String, Object>> completeHistory(List<Map<String, Object>> history, String period, |
| | | LocalDate start, LocalDate end) { |
| | | Map<String, Map<String, Object>> historyByLabel = new HashMap<>(); |
| | | if (history != null) { |
| | | for (Map<String, Object> row : history) { |
| | | if (row != null && row.get("label") != null) { |
| | | historyByLabel.put(String.valueOf(row.get("label")), row); |
| | | } |
| | | } |
| | | } |
| | | List<Map<String, Object>> completed = new ArrayList<>(); |
| | | DateTimeFormatter formatter = "YEAR".equals(period) |
| | | ? DateTimeFormatter.ofPattern("yyyy-MM") : DateTimeFormatter.ISO_LOCAL_DATE; |
| | | LocalDate cursor = "YEAR".equals(period) ? start.withDayOfMonth(1) : start; |
| | | LocalDate last = "YEAR".equals(period) ? end.withDayOfMonth(1) : end; |
| | | while (!cursor.isAfter(last)) { |
| | | String label = cursor.format(formatter); |
| | | Map<String, Object> row = historyByLabel.get(label); |
| | | if (row == null) { |
| | | row = new LinkedHashMap<>(); |
| | | row.put("label", label); |
| | | row.put("income", BigDecimal.ZERO); |
| | | row.put("chargedEnergy", BigDecimal.ZERO); |
| | | row.put("dischargedEnergy", BigDecimal.ZERO); |
| | | } |
| | | completed.add(row); |
| | | cursor = "YEAR".equals(period) ? cursor.plusMonths(1) : cursor.plusDays(1); |
| | | } |
| | | return completed; |
| | | } |
| | | } |
| | |
| | | import com.ruoyi.other.emind.dto.EmindDashboardQuery; |
| | | import com.ruoyi.other.emind.mapper.EmindStorageDashboardMapper; |
| | | import org.junit.jupiter.api.Test; |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import static org.junit.jupiter.api.Assertions.assertEquals; |
| | | import static org.mockito.ArgumentMatchers.*; |
| | | import static org.mockito.Mockito.*; |
| | |
| | | assertEquals(3, new EmindStorageDashboardService(mapper).overview(query).size()); |
| | | verify(mapper).selectHistory(isNull(), eq("DAY"), eq(LocalDate.now().minusDays(6)), eq(LocalDate.now())); |
| | | } |
| | | |
| | | @Test |
| | | void overviewFillsMissingDailyHistoryWithZeroValues() { |
| | | EmindStorageDashboardMapper mapper = mock(EmindStorageDashboardMapper.class); |
| | | LocalDate start = LocalDate.of(2026, 7, 20); |
| | | LocalDate end = LocalDate.of(2026, 7, 22); |
| | | Map<String, Object> recordedDay = new HashMap<>(); |
| | | recordedDay.put("label", "2026-07-21"); |
| | | recordedDay.put("income", 12); |
| | | recordedDay.put("chargedEnergy", 5); |
| | | recordedDay.put("dischargedEnergy", 3); |
| | | when(mapper.selectOverview(isNull(), any())).thenReturn(Collections.emptyMap()); |
| | | when(mapper.selectHistory(isNull(), eq("DAY"), eq(start), eq(end))).thenReturn(Collections.singletonList(recordedDay)); |
| | | when(mapper.selectCurve(isNull())).thenReturn(Collections.emptyList()); |
| | | EmindDashboardQuery query = new EmindDashboardQuery(); |
| | | query.setPeriod("DAY"); |
| | | query.setStartDate(start); |
| | | query.setEndDate(end); |
| | | |
| | | Map<String, Object> result = new EmindStorageDashboardService(mapper).overview(query); |
| | | List<Map<String, Object>> history = (List<Map<String, Object>>) result.get("history"); |
| | | |
| | | List<Object> labels = new ArrayList<>(); |
| | | for (Map<String, Object> row : history) labels.add(row.get("label")); |
| | | assertEquals(Arrays.asList("2026-07-20", "2026-07-21", "2026-07-22"), labels); |
| | | assertEquals(BigDecimal.ZERO, history.get(0).get("income")); |
| | | assertEquals(12, history.get(1).get("income")); |
| | | assertEquals(BigDecimal.ZERO, history.get(2).get("chargedEnergy")); |
| | | } |
| | | } |