| | |
| | | package com.tourguide.modules.order.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.MybatisConfiguration; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.tourguide.modules.order.domain.OrderStatus; |
| | | import com.tourguide.modules.order.domain.RefundStatus; |
| | | import com.tourguide.modules.order.dto.GuideEarningsResponse; |
| | | import com.tourguide.modules.order.entity.TourOrder; |
| | | import com.tourguide.modules.order.mapper.TourOrderMapper; |
| | | import com.tourguide.modules.user.entity.GuideUser; |
| | | import com.tourguide.modules.user.service.GuideUserService; |
| | | import org.apache.ibatis.builder.MapperBuilderAssistant; |
| | | import org.junit.jupiter.api.BeforeEach; |
| | | import org.junit.jupiter.api.Test; |
| | | import org.mockito.ArgumentCaptor; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.Collections; |
| | |
| | | import static org.assertj.core.api.Assertions.assertThat; |
| | | import static org.mockito.ArgumentMatchers.any; |
| | | import static org.mockito.Mockito.mock; |
| | | import static org.mockito.Mockito.verify; |
| | | import static org.mockito.Mockito.when; |
| | | |
| | | class GuideEarningsServiceTest { |
| | |
| | | |
| | | @BeforeEach |
| | | void setUp() { |
| | | initializeTableInfo(); |
| | | orderMapper = mock(TourOrderMapper.class); |
| | | GuideUserService guideUserService = mock(GuideUserService.class); |
| | | GuideUser guide = new GuideUser(); |
| | |
| | | } |
| | | |
| | | @Test |
| | | void shouldUseGuideEarningSnapshotForSummaryAndDetails() { |
| | | void shouldUsePaidAmountForSummaryAndDetails() { |
| | | TourOrder order = new TourOrder(); |
| | | order.setId(10L); |
| | | order.setPaymentAmount(new BigDecimal("100.00")); |
| | |
| | | page.setRecords(Collections.singletonList(order)); |
| | | page.setTotal(1); |
| | | when(orderMapper.selectGuideEarningSum(any(), any(), any())) |
| | | .thenReturn(new BigDecimal("90.00")); |
| | | .thenReturn(new BigDecimal("100.00")); |
| | | when(orderMapper.selectPage(any(Page.class), any())).thenReturn(page); |
| | | when(orderMapper.selectGuideEarningScenics(8L)).thenReturn(Collections.emptyList()); |
| | | |
| | | GuideEarningsResponse response = service.get(8L, null, null, null, 1, 50); |
| | | |
| | | assertThat(response.getTodayEarnings()).isEqualByComparingTo("90.00"); |
| | | assertThat(response.getMonthEarnings()).isEqualByComparingTo("90.00"); |
| | | assertThat(response.getTodayEarnings()).isEqualByComparingTo("100.00"); |
| | | assertThat(response.getMonthEarnings()).isEqualByComparingTo("100.00"); |
| | | assertThat(response.getRecords()).hasSize(1); |
| | | assertThat(response.getRecords().get(0).getOrderAmount()) |
| | | .isEqualByComparingTo("100.00"); |
| | | assertThat(response.getRecords().get(0).getEarningAmount()) |
| | | .isEqualByComparingTo("90.00"); |
| | | .isEqualByComparingTo("100.00"); |
| | | } |
| | | |
| | | @Test |
| | | void shouldIncludePartialRefundOrderAndUseRemainingPaidAmount() { |
| | | TourOrder order = new TourOrder(); |
| | | order.setId(11L); |
| | | order.setStatus(OrderStatus.CANCELLED.getCode()); |
| | | order.setRefundStatus(RefundStatus.PARTIAL.getCode()); |
| | | order.setPaymentAmount(new BigDecimal("100.00")); |
| | | order.setRefundedAmount(new BigDecimal("20.00")); |
| | | order.setGuideEarningAmount(new BigDecimal("90.00")); |
| | | |
| | | Page<TourOrder> page = new Page<>(1, 50); |
| | | page.setRecords(Collections.singletonList(order)); |
| | | page.setTotal(1); |
| | | when(orderMapper.selectGuideEarningSum(any(), any(), any())) |
| | | .thenReturn(new BigDecimal("80.00")); |
| | | when(orderMapper.selectPage(any(Page.class), any())).thenReturn(page); |
| | | when(orderMapper.selectGuideEarningScenics(8L)).thenReturn(Collections.emptyList()); |
| | | |
| | | GuideEarningsResponse response = service.get(8L, null, null, null, 1, 50); |
| | | |
| | | assertThat(response.getTodayEarnings()).isEqualByComparingTo("80.00"); |
| | | assertThat(response.getMonthEarnings()).isEqualByComparingTo("80.00"); |
| | | assertThat(response.getRecords()).hasSize(1); |
| | | assertThat(response.getRecords().get(0).getEarningAmount()) |
| | | .isEqualByComparingTo("80.00"); |
| | | |
| | | ArgumentCaptor<LambdaQueryWrapper<TourOrder>> queryCaptor = |
| | | ArgumentCaptor.forClass(LambdaQueryWrapper.class); |
| | | verify(orderMapper).selectPage(any(Page.class), queryCaptor.capture()); |
| | | assertThat(queryCaptor.getValue().getSqlSegment()) |
| | | .contains("status") |
| | | .contains("refund_status") |
| | | .contains("GREATEST("); |
| | | } |
| | | |
| | | private static void initializeTableInfo() { |
| | | if (TableInfoHelper.getTableInfo(TourOrder.class) == null) { |
| | | TableInfoHelper.initTableInfo( |
| | | new MapperBuilderAssistant(new MybatisConfiguration(), ""), TourOrder.class); |
| | | } |
| | | } |
| | | } |