package com.cf.imes.module.system.service.funds.statistics;

import com.cf.imes.framework.common.enums.StatisticsUnit;
import com.cf.imes.framework.common.util.i18n.core.util.I18nUtils;
import com.cf.imes.framework.test.core.ut.BaseMockitoUnitTest;
import com.cf.imes.module.system.controller.admin.statistics.vo.OrgStatusGroupStatisticsReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.statistics.FundsTradeTypeGroupStatisticsDO;
import com.cf.imes.module.system.dal.mysql.incomeandexpense.IncomeExpenseDetailsMapper;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

/**
 *
 *
 * @author Gqr
 * @since 2025/12/31 15:20
 */
class FundsStatisticsServiceImplTest extends BaseMockitoUnitTest {

    @InjectMocks
    private FundsStatisticsServiceImpl fundsStatisticsService;

    @Mock
    private IncomeExpenseDetailsMapper incomeExpenseDetailsMapper;

    @Mock
    private I18nUtils i18nUtils;

    @Test
    void should_return_consumption_trend_with_zero_filling() {
        OrgStatusGroupStatisticsReqVO reqVO = new OrgStatusGroupStatisticsReqVO();
        reqVO.setUnit(StatisticsUnit.DAY.getValue());
        reqVO.setCreateTime(new LocalDate[]{
                LocalDate.now().minusDays(2),
                LocalDate.now().plusDays(1)
        });

        LocalDate now = LocalDate.now();

        List<FundsTradeTypeGroupStatisticsDO> purchaseList = new ArrayList<>();
        purchaseList.add(new FundsTradeTypeGroupStatisticsDO(new BigDecimal("100"), now.minusDays(2).format(DateTimeFormatter.ofPattern("yyyy-M-d"))));
        purchaseList.add(new FundsTradeTypeGroupStatisticsDO(new BigDecimal("400"), now.format(DateTimeFormatter.ofPattern("yyyy-M-d"))));
        when(incomeExpenseDetailsMapper.selectTotalSalesTrendGroupByCreateTime(reqVO)).thenReturn(purchaseList);

        // when query
        Map<String, Object> result =
                fundsStatisticsService.getFunsTradeTypeGroupStatistics(reqVO);

        // then
        List<?> xAxis = (List<?>) result.get("dateList");
        List<?> series = (List<?>) result.get("series");

        assertEquals(4, xAxis.size());

        Map<?, ?> seriesItem = (Map<?, ?>) series.get(0);
        BigDecimal[] data = (BigDecimal[]) seriesItem.get("data");

        assertArrayEquals(
                new BigDecimal[]{
                        new BigDecimal("100"),
                        BigDecimal.ZERO,
                        new BigDecimal("400"),
                        BigDecimal.ZERO,
                },
                data
        );
    }
}
