zjk
2026-09-29 54dcf0a82e7490fa58b1c056d5c0b9cb739c2931
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
package com.tcm.training.admin.statistics.service;
 
import com.tcm.training.admin.statistics.mapper.RevenueStatisticsMapper;
import com.tcm.training.admin.statistics.row.RevenueStatisticsDailyRow;
import com.tcm.training.admin.statistics.vo.RevenueStatisticsVO;
import com.tcm.training.admin.trade.service.PaymentFlowAppService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collections;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
@ExtendWith(MockitoExtension.class)
class RevenueStatisticsAppServiceTest {
 
    @Mock
    private RevenueStatisticsMapper revenueStatisticsMapper;
 
    @Mock
    private PaymentFlowAppService paymentFlowAppService;
 
    @InjectMocks
    private RevenueStatisticsAppService revenueStatisticsAppService;
 
    @Test
    void statisticsShouldBuildOverviewAndFillMissingDates() {
        RevenueStatisticsDailyRow row = new RevenueStatisticsDailyRow();
        row.setStatDate(LocalDate.of(2026, 3, 1));
        row.setHealthIncome(new BigDecimal("100.00"));
        row.setMemberIncome(new BigDecimal("50.00"));
        row.setHealthRefundAmount(new BigDecimal("12.00"));
        row.setMemberRefundAmount(new BigDecimal("8.00"));
        when(revenueStatisticsMapper.selectDailyStatistics(
                eq(null),
                eq(LocalDateTime.of(2026, 3, 1, 0, 0, 0)),
                eq(LocalDateTime.of(2026, 3, 3, LocalTime.MAX.getHour(), LocalTime.MAX.getMinute(), LocalTime.MAX.getSecond(), LocalTime.MAX.getNano()))
        )).thenReturn(Collections.singletonList(row));
 
        RevenueStatisticsVO result = revenueStatisticsAppService.statistics(
                "ALL",
                LocalDate.of(2026, 3, 1),
                LocalDate.of(2026, 3, 3)
        );
 
        verify(paymentFlowAppService).syncFlows();
        assertEquals(3, result.getTrend().size());
        assertEquals(new BigDecimal("150.00"), result.getOverview().getTotalIncome());
        assertEquals(new BigDecimal("100.00"), result.getOverview().getHealthIncome());
        assertEquals(new BigDecimal("50.00"), result.getOverview().getMemberIncome());
        assertEquals(new BigDecimal("20.00"), result.getOverview().getRefundAmount());
        assertEquals(new BigDecimal("130.00"), result.getOverview().getProfit());
        assertEquals(new BigDecimal("0"), result.getTrend().get(1).getTotalIncome());
        assertEquals(new BigDecimal("12.00"), result.getTrend().get(0).getHealthRefundAmount());
        assertEquals(new BigDecimal("8.00"), result.getTrend().get(0).getMemberRefundAmount());
    }
 
    @Test
    void statisticsShouldUseHealthTypeFilter() {
        RevenueStatisticsDailyRow row = new RevenueStatisticsDailyRow();
        row.setStatDate(LocalDate.of(2026, 3, 5));
        row.setHealthIncome(new BigDecimal("88.00"));
        row.setMemberIncome(BigDecimal.ZERO);
        row.setHealthRefundAmount(new BigDecimal("8.00"));
        row.setMemberRefundAmount(BigDecimal.ZERO);
        when(revenueStatisticsMapper.selectDailyStatistics(
                eq("COURSE_PURCHASE"),
                eq(LocalDateTime.of(2026, 3, 5, 0, 0, 0)),
                eq(LocalDateTime.of(2026, 3, 5, LocalTime.MAX.getHour(), LocalTime.MAX.getMinute(), LocalTime.MAX.getSecond(), LocalTime.MAX.getNano()))
        )).thenReturn(Collections.singletonList(row));
 
        RevenueStatisticsVO result = revenueStatisticsAppService.statistics(
                "HEALTH",
                LocalDate.of(2026, 3, 5),
                LocalDate.of(2026, 3, 5)
        );
 
        assertEquals(new BigDecimal("88.00"), result.getOverview().getTotalIncome());
        assertEquals(new BigDecimal("88.00"), result.getOverview().getHealthIncome());
        assertEquals(new BigDecimal("0"), result.getOverview().getMemberIncome());
        assertEquals(new BigDecimal("8.00"), result.getOverview().getRefundAmount());
        assertEquals(new BigDecimal("80.00"), result.getOverview().getProfit());
    }
 
    @Test
    void statisticsShouldRejectInvalidType() {
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
                () -> revenueStatisticsAppService.statistics("OTHER", LocalDate.of(2026, 3, 1), LocalDate.of(2026, 3, 1)));
 
        assertEquals("统计类型不合法", exception.getMessage());
    }
 
    @Test
    void statisticsShouldRejectInvalidDateRange() {
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
                () -> revenueStatisticsAppService.statistics("ALL", LocalDate.of(2026, 3, 10), LocalDate.of(2026, 3, 1)));
 
        assertEquals("开始日期不能晚于结束日期", exception.getMessage());
    }
}