puzhibing
2026-09-02 bf82d9f70479a5ea99ff2ea00b35cb3b6b7d500f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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<com.ruoyi.order.api.vo.ChargingOrderVO> 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<String, Object> 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<String, Object> baseSummary = new HashMap<>();
        baseSummary.put("orderCount", 501L);
        List<Long> 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<String, Object> share = new HashMap<>();
        share.put("platformIncome", new BigDecimal("10.25"));
        share.put("partnerIncome", new BigDecimal("2.50"));
        when(mapper.shareIncomeSummaryByOrderIds(anyList())).thenReturn(share);
 
        Map<String, Object> 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<String, Object> 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<String, Object> 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<String, BigDecimal> 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<String, BigDecimal> summary = ReflectionTestUtils.invokeMethod(service, "emptyAccountingSummary");
 
        String proportion = ReflectionTestUtils.invokeMethod(service, "buildProportion", summary, "electric");
 
        assertEquals("0%-0%-0%-0%", proportion);
    }
}