puzhibing
2026-09-02 54b8d039c03b2a94b7e6dcd74b8bd18ee979f55d
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
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.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
class TCardChargingOrderSummaryTest {
 
    @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);
    }
}