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

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("BizMarketEntryMapper.xml")
class BizMarketEntryMapperXmlTest
{
    @Test
    @DisplayName("主表列表 SQL 与子表聚合")
    void listSql() throws Exception
    {
        String main = readMapperXml("mapper/trading/BizMarketEntryMapper.xml");
        assertTrue(main.contains("e.del_flag = '0'"));
        assertTrue(main.contains("biz_market_entry_ear_tag"));
        assertTrue(main.contains("group_concat"));
        assertTrue(main.contains("order by e.entry_time desc, e.id desc"));
        assertFalse(main.contains("quantity, ear_tags, del_flag"));
    }

    @Test
    @DisplayName("耳标子表 SQL")
    void earTagSql() throws Exception
    {
        String child = readMapperXml("mapper/trading/BizMarketEntryEarTagMapper.xml");
        assertTrue(child.contains("biz_market_entry_ear_tag"));
        assertTrue(child.contains("entry_id"));
        assertTrue(child.contains("supplier_id"));
        assertTrue(child.contains("batchInsertEarTag"));
    }

    private static String readMapperXml(String path)
    {
        try (InputStream in = BizMarketEntryMapperXmlTest.class.getClassLoader().getResourceAsStream(path))
        {
            if (in == null)
            {
                throw new IllegalStateException("mapper xml not found: " + path);
            }
            return IOUtils.toString(in, StandardCharsets.UTF_8);
        }
        catch (Exception ex)
        {
            throw new RuntimeException(ex);
        }
    }
}
