package com.ruoyi.web.modules.trading.service.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.math.BigDecimal;
import java.util.Collections;

import com.ruoyi.web.modules.trading.domain.dto.AppSupplierSettlementSummaryDto;
import com.ruoyi.web.modules.trading.domain.dto.AppSupplierTradeSummaryDto;
import com.ruoyi.web.modules.trading.domain.vo.AppSupplierHomeDashboardVo;
import com.ruoyi.web.modules.trading.mapper.AppSupplierHomeMapper;
import com.ruoyi.web.modules.trading.support.AppSupplierHomeRules;
import com.ruoyi.web.modules.trading.support.AppSupplierSessionSupport;
import org.junit.jupiter.api.DisplayName;
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;

@ExtendWith(MockitoExtension.class)
@DisplayName("AppSupplierHomeServiceImpl")
class AppSupplierHomeServiceImplTest
{
    @Mock
    private AppSupplierSessionSupport appSupplierSessionSupport;

    @Mock
    private AppSupplierHomeMapper appSupplierHomeMapper;

    @InjectMocks
    private AppSupplierHomeServiceImpl service;

    @Test
    @DisplayName("今日看板")
    void loadDashboardToday()
    {
        when(appSupplierSessionSupport.requireSupplierId()).thenReturn(5L);

        AppSupplierTradeSummaryDto trade = new AppSupplierTradeSummaryDto();
        trade.setOrderCount(2L);
        trade.setTotalAmount(new BigDecimal("74300"));
        trade.setTotalHeads(6L);
        trade.setPayingBuyerCount(2L);
        when(appSupplierHomeMapper.selectTradeSummary(any(), any(), any())).thenReturn(trade);

        AppSupplierSettlementSummaryDto settlement = new AppSupplierSettlementSummaryDto();
        settlement.setPayableAmount(new BigDecimal("36500"));
        settlement.setServiceFeeAmount(new BigDecimal("600"));
        when(appSupplierHomeMapper.selectSettlementSummary(any(), any(), any())).thenReturn(settlement);

        when(appSupplierHomeMapper.selectHourlyAmountTrend(any(), any(), any()))
                .thenReturn(Collections.emptyList());
        when(appSupplierHomeMapper.selectTodayTradedHeads(any(), any(), any())).thenReturn(129L);
        when(appSupplierHomeMapper.countTodayEntryEarTags(any(), any(), any())).thenReturn(30L);
        when(appSupplierHomeMapper.selectPendingPayHeads(any())).thenReturn(4L);
        when(appSupplierHomeMapper.selectActiveIsolationEarTagRows(any()))
                .thenReturn(Collections.emptyList());
        when(appSupplierHomeMapper.selectSickDeadEarTagRowsBySupplier(any()))
                .thenReturn(Collections.emptyList());

        AppSupplierHomeDashboardVo vo = service.loadDashboard(AppSupplierHomeRules.RANGE_TODAY);
        assertEquals(AppSupplierHomeRules.RANGE_TODAY, vo.getRange());
        assertEquals("今日", vo.getRangeName());
        assertEquals(AppSupplierHomeRules.TREND_GRANULARITY_HOUR, vo.getTrendGranularity());
        assertEquals(2L, vo.getTradeSummary().getOrderCount());
        assertEquals(24, vo.getAmountTrend().size());
        assertEquals(129L, vo.getYakStatus().getTradedHeads());
        assertEquals(30L, vo.getYakStatus().getEnteredPenEarTags());
        assertEquals(4L, vo.getYakStatus().getPendingPayHeads());
        assertEquals(0L, vo.getYakStatus().getIsolationEarTags());
    }
}
