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

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("BizEntryIsolationMapper.xml")
class BizEntryIsolationMapperXmlTest
{
    @Test
    @DisplayName("列表筛选与排序")
    void listSql() throws Exception
    {
        String xml = readMapperXml("mapper/trading/BizEntryIsolationMapper.xml");
        assertTrue(xml.contains("e.del_flag = '0'"));
        assertTrue(xml.contains("date(e.entry_time) &gt;= #{entryDateStart}"));
        assertTrue(xml.contains("e.cert_no like concat('%', #{certNo}, '%')"));
        assertTrue(xml.contains("e.supplier_id = #{supplierId}"));
        assertTrue(xml.contains("order by e.entry_time desc, e.id desc"));
        assertTrue(xml.contains("pen_alloc_count"));
    }

    @Test
    @DisplayName("分圈明细 SQL")
    void allocSql() throws Exception
    {
        String xml = readMapperXml("mapper/trading/BizEntryIsolationAllocMapper.xml");
        assertTrue(xml.contains("delete from biz_entry_isolation_alloc"));
        assertTrue(xml.contains("batchInsertAlloc"));
        assertTrue(xml.contains("pen_code"));
    }

    private static String readMapperXml(String path)
    {
        try (InputStream in = BizEntryIsolationMapperXmlTest.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);
        }
    }
}
