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.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.AppSupplierOrderMapper;
import com.ruoyi.web.modules.trading.mapper.BizTradeOrderLineMapper;
import com.ruoyi.web.modules.trading.support.AppSupplierOrderRules;
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("AppSupplierOrderServiceImpl")
class AppSupplierOrderServiceImplTest
{
    @Mock
    private AppSupplierSessionSupport appSupplierSessionSupport;

    @Mock
    private AppSupplierOrderMapper appSupplierOrderMapper;

    @Mock
    private BizTradeOrderLineMapper bizTradeOrderLineMapper;

    @InjectMocks
    private AppSupplierOrderServiceImpl service;

    @Test
    @DisplayName("列表含明细")
    void selectOrderList()
    {
        when(appSupplierSessionSupport.requireSupplierId()).thenReturn(3L);
        BizTradeOrder order = new BizTradeOrder();
        order.setId(10L);
        order.setOrderStatus("0");
        when(appSupplierOrderMapper.selectSupplierOrderList(eq(3L), eq("0")))
                .thenReturn(Collections.singletonList(order));
        BizTradeOrderLine line = new BizTradeOrderLine();
        line.setOrderId(10L);
        line.setEarTag("E101");
        when(bizTradeOrderLineMapper.selectLineListByOrderIds(any()))
                .thenReturn(Collections.singletonList(line));

        BizTradeOrder got = service.selectOrderList(AppSupplierOrderRules.STATUS_TAB_PENDING).get(0);
        assertEquals(1, got.getLineList().size());
        assertEquals("E101", got.getLineList().get(0).getEarTag());
    }

    @Test
    @DisplayName("无权订单")
    void orderNotFound()
    {
        when(appSupplierSessionSupport.requireSupplierId()).thenReturn(3L);
        when(appSupplierOrderMapper.selectSupplierOrderById(3L, 99L)).thenReturn(null);
        assertThrows(ServiceException.class, () -> service.selectOrderById(99L));
    }
}
