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

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

import java.util.Arrays;
import java.util.Collections;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.trading.domain.BizTradeOrder;
import com.ruoyi.web.modules.trading.domain.BizTradeOrderLine;
import com.ruoyi.web.modules.trading.mapper.AppDistributorOrderMapper;
import com.ruoyi.web.modules.trading.mapper.BizTradeOrderLineMapper;
import com.ruoyi.web.modules.trading.support.AppDistributorOrderRules;
import com.ruoyi.web.modules.trading.support.AppDistributorSessionSupport;
import com.ruoyi.web.modules.trading.support.TradeOrderRules;
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("AppDistributorOrderServiceImpl")
class AppDistributorOrderServiceImplTest
{
    @Mock
    private AppDistributorSessionSupport appDistributorSessionSupport;

    @Mock
    private AppDistributorOrderMapper appDistributorOrderMapper;

    @Mock
    private BizTradeOrderLineMapper bizTradeOrderLineMapper;

    @InjectMocks
    private AppDistributorOrderServiceImpl service;

    @Test
    @DisplayName("列表含明细")
    void listWithLines()
    {
        when(appDistributorSessionSupport.requireDistributorId()).thenReturn(8L);
        BizTradeOrder order = new BizTradeOrder();
        order.setId(1L);
        order.setOrderStatus(TradeOrderRules.ORDER_STATUS_PENDING);
        when(appDistributorOrderMapper.selectDistributorOrderList(eq(8L), eq(null)))
                .thenReturn(Collections.singletonList(order));
        BizTradeOrderLine line = new BizTradeOrderLine();
        line.setOrderId(1L);
        line.setEarTag("E101");
        when(bizTradeOrderLineMapper.selectLineListByOrderIds(any())).thenReturn(Collections.singletonList(line));

        BizTradeOrder got = service.selectOrderList(AppDistributorOrderRules.STATUS_TAB_ALL).get(0);
        assertEquals("待支付", got.getOrderStatusName());
        assertEquals(1, got.getLineList().size());
        assertEquals("E101", got.getLineList().get(0).getEarTag());
    }

    @Test
    @DisplayName("详情不存在")
    void detailNotFound()
    {
        when(appDistributorSessionSupport.requireDistributorId()).thenReturn(8L);
        when(appDistributorOrderMapper.selectDistributorOrderById(8L, 99L)).thenReturn(null);
        assertThrows(ServiceException.class, () -> service.selectOrderById(99L));
    }
}
