package com.ruoyi.web.modules.prosperity.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;

/**
 * GTFY-CG-UT-013、014
 */
@DisplayName("BizCommonProsperityAchievementMapper.xml")
class BizCommonProsperityAchievementMapperXmlTest
{

    @Test
    @DisplayName("GTFY-CG-UT-013 列表按年筛选")
    void ut013_statYearFilter() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("stat_period like concat(#{statYear}, '-%')"));
    }

    @Test
    @DisplayName("GTFY-CG-UT-014 列表排序")
    void ut014_orderBy() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("order by stat_period desc, create_time desc, id desc"));
    }

    @Test
    @DisplayName("GTFY-NB-UT-013 年度报告按年查询")
    void nb013_selectByStatYear() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("selectByStatYear"));
        assertTrue(xml.contains("stat_period like concat(#{statYear}, '-%')"));
        assertTrue(xml.contains("order by stat_period asc"));
    }

    @Test
    @DisplayName("GTFY-NB-UT-013 可选年份 distinct")
    void nb013_distinctYears() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("selectDistinctStatYears"));
        assertTrue(xml.contains("distinct left(stat_period, 4)"));
    }

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