package com.ruoyi.order.service.impl; import com.ruoyi.order.api.query.CardChargingOrderQuery; import com.ruoyi.order.mapper.TCardChargingOrderMapper; import org.junit.jupiter.api.Test; import org.springframework.test.util.ReflectionTestUtils; import java.math.BigDecimal; import java.util.ArrayList; 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.any; import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class TCardChargingOrderSummaryTest { @Test void fillsElectronicProportionFromCapacitySummaryInBatches() { TCardChargingOrderServiceImpl service = new TCardChargingOrderServiceImpl(); TCardChargingOrderMapper mapper = mock(TCardChargingOrderMapper.class); ReflectionTestUtils.setField(service, "baseMapper", mapper); List orders = new ArrayList<>(); for (long id = 1; id <= 501; id++) { com.ruoyi.order.api.vo.ChargingOrderVO order = new com.ruoyi.order.api.vo.ChargingOrderVO(); order.setId(id); orders.add(order); } Map capacity = new HashMap<>(); capacity.put("orderId", 1L); capacity.put("capacityTotal", new BigDecimal("10")); capacity.put("capacity1", new BigDecimal("4")); capacity.put("capacity2", new BigDecimal("3")); capacity.put("capacity3", new BigDecimal("2")); capacity.put("capacity4", BigDecimal.ONE); doReturn(Collections.singletonList(capacity), Collections.emptyList()) .when(mapper).capacitySummaryByOrderIds(anyList()); ReflectionTestUtils.invokeMethod(service, "fillElectronicProportion", orders); assertEquals("40%-30%-20%-10%", orders.get(0).getElectronicProportion()); assertEquals("0%-0%-0%-0%", orders.get(500).getElectronicProportion()); verify(mapper, times(2)).capacitySummaryByOrderIds(anyList()); } @Test void mergesShareAndAccountingSummariesInBatches() { TCardChargingOrderServiceImpl service = new TCardChargingOrderServiceImpl(); TCardChargingOrderMapper mapper = mock(TCardChargingOrderMapper.class); CardChargingOrderQuery query = new CardChargingOrderQuery(); query.setSiteId(12); ReflectionTestUtils.setField(service, "baseMapper", mapper); Map baseSummary = new HashMap<>(); baseSummary.put("orderCount", 501L); List orderIds = new ArrayList<>(); for (long id = 1; id <= 501; id++) { orderIds.add(id); } when(mapper.summary(any(), isNull(), isNull(), isNull(), isNull())).thenReturn(baseSummary); when(mapper.summaryOrderIds(any(), isNull(), isNull(), isNull(), isNull())).thenReturn(orderIds); Map share = new HashMap<>(); share.put("platformIncome", new BigDecimal("10.25")); share.put("partnerIncome", new BigDecimal("2.50")); when(mapper.shareIncomeSummaryByOrderIds(anyList())).thenReturn(share); Map accounting = new HashMap<>(); accounting.put("capacityTotal", new BigDecimal("10")); accounting.put("capacity1", new BigDecimal("4")); accounting.put("capacity2", new BigDecimal("3")); accounting.put("capacity3", new BigDecimal("2")); accounting.put("capacity4", BigDecimal.ONE); when(mapper.accountingSummaryByOrderIds(anyList())).thenReturn(accounting); Map result = service.summary(query); assertEquals(new BigDecimal("20.50"), result.get("platformIncome")); assertEquals(new BigDecimal("5.00"), result.get("partnerIncomeTotal")); assertEquals("40%-30%-20%-10%", result.get("capacityProportion")); assertEquals("0%-0%-0%-0%", result.get("serviceProportion")); verify(mapper, times(2)).shareIncomeSummaryByOrderIds(anyList()); verify(mapper, times(2)).accountingSummaryByOrderIds(anyList()); } @Test void doesNotQueryDetailSummariesWhenNoOrdersMatch() { TCardChargingOrderServiceImpl service = new TCardChargingOrderServiceImpl(); TCardChargingOrderMapper mapper = mock(TCardChargingOrderMapper.class); CardChargingOrderQuery query = new CardChargingOrderQuery(); query.setSiteId(12); ReflectionTestUtils.setField(service, "baseMapper", mapper); when(mapper.summary(any(), isNull(), isNull(), isNull(), isNull())).thenReturn(new HashMap<>()); when(mapper.summaryOrderIds(any(), isNull(), isNull(), isNull(), isNull())).thenReturn(Collections.emptyList()); Map result = service.summary(query); assertEquals(BigDecimal.ZERO, result.get("platformIncome")); assertEquals("0%-0%-0%-0%", result.get("electronicProportion")); verify(mapper, times(0)).shareIncomeSummaryByOrderIds(anyList()); verify(mapper, times(0)).accountingSummaryByOrderIds(anyList()); } @Test void buildsFourPeriodProportionWithTwoDecimalRounding() { TCardChargingOrderServiceImpl service = new TCardChargingOrderServiceImpl(); Map summary = new HashMap<>(); summary.put("capacityTotal", new BigDecimal("3")); summary.put("capacity1", BigDecimal.ONE); summary.put("capacity2", BigDecimal.ONE); summary.put("capacity3", BigDecimal.ONE); summary.put("capacity4", BigDecimal.ZERO); String proportion = ReflectionTestUtils.invokeMethod(service, "buildProportion", summary, "capacity"); assertEquals("33.33%-33.33%-33.33%-0%", proportion); } @Test void returnsZeroProportionWhenThereIsNoAccountingData() { TCardChargingOrderServiceImpl service = new TCardChargingOrderServiceImpl(); Map summary = ReflectionTestUtils.invokeMethod(service, "emptyAccountingSummary"); String proportion = ReflectionTestUtils.invokeMethod(service, "buildProportion", summary, "electric"); assertEquals("0%-0%-0%-0%", proportion); } }