package com.ruoyi.web.modules.trading.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Collections;
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;
import com.ruoyi.web.modules.trading.mapper.BizIsolationPatrolMapper;
import com.ruoyi.web.modules.trading.mapper.BizMarketEntryEarTagMapper;
import com.ruoyi.web.modules.trading.mapper.BizTradeOrderLineMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("TradeOrderEarTagResolver")
class TradeOrderEarTagResolverTest
{
    @Mock
    private BizMarketEntryEarTagMapper bizMarketEntryEarTagMapper;

    @Mock
    private BizIsolationPatrolMapper bizIsolationPatrolMapper;

    @Mock
    private BizTradeOrderLineMapper bizTradeOrderLineMapper;

    @InjectMocks
    private TradeOrderEarTagResolver resolver;

    @Test
    @DisplayName("supplierEarTags 仅子表 supplier_id")
    void supplierEarTagsOnlyTable()
    {
        when(bizMarketEntryEarTagMapper.selectEarTagsBySupplierIdOnly(1L))
                .thenReturn(Arrays.asList("E101", "E102", "E103"));
        assertEquals(Arrays.asList("E101", "E102", "E103"), resolver.listSupplierEarTagOptions(1L));
    }

    @Test
    @DisplayName("ZCZX-JYDDGL-UT-014 保存校验 M−B−C")
    void ut014_pool()
    {
        when(bizMarketEntryEarTagMapper.selectDistinctEarTagsBySupplierId(1L))
                .thenReturn(Arrays.asList("E101", "E102", "E103"));
        when(bizIsolationPatrolMapper.selectSickDeadEarTagRows())
                .thenReturn(Collections.singletonList("E102"));
        when(bizTradeOrderLineMapper.selectOccupiedEarTags(null))
                .thenReturn(Collections.singletonList("E103"));
        assertEquals(Arrays.asList("E101"), resolver.listEligible(1L, null));
    }

    @Test
    @DisplayName("ZCZX-JYDDGL-UT-010 编辑释放本单占用")
    void ut010_excludeOrder()
    {
        when(bizMarketEntryEarTagMapper.selectDistinctEarTagsBySupplierId(1L))
                .thenReturn(Arrays.asList("E101", "E103"));
        when(bizIsolationPatrolMapper.selectSickDeadEarTagRows()).thenReturn(Collections.emptyList());
        when(bizTradeOrderLineMapper.selectOccupiedEarTags(2001L))
                .thenReturn(Collections.emptyList());
        assertTrue(resolver.listEligible(1L, 2001L).contains("E103"));
    }

    @Test
    @DisplayName("ZCZX-JYDDGL-UT-017 M 为空")
    void ut017_emptyM()
    {
        when(bizMarketEntryEarTagMapper.selectDistinctEarTagsBySupplierId(2L))
                .thenReturn(Collections.emptyList());
        when(bizIsolationPatrolMapper.selectSickDeadEarTagRows()).thenReturn(Collections.emptyList());
        when(bizTradeOrderLineMapper.selectOccupiedEarTags(null)).thenReturn(Collections.emptyList());
        assertTrue(resolver.listEligible(2L, null).isEmpty());
    }

    @Test
    @DisplayName("ZCZX-JYDDGL-UT-016 他单占用")
    void ut016_occupied()
    {
        when(bizMarketEntryEarTagMapper.selectDistinctEarTagsBySupplierId(1L))
                .thenReturn(Arrays.asList("E101", "E103"));
        when(bizIsolationPatrolMapper.selectSickDeadEarTagRows()).thenReturn(Collections.emptyList());
        when(bizTradeOrderLineMapper.selectOccupiedEarTags(null))
                .thenReturn(Collections.singletonList("E103"));
        assertFalse(resolver.listEligible(1L, null).contains("E103"));
    }
}
