package com.ruoyi.web.modules.technology.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("BizOnlineTrainingCourseMapper.xml")
class BizOnlineTrainingCourseMapperXmlTest
{

    @Test
    @DisplayName("纳入条件与列表筛选排序")
    void listSql()
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("resource_type = '004008'"));
        assertTrue(xml.contains("publish_status = 1"));
        assertTrue(xml.contains("del_flag = '0'"));
        assertTrue(xml.contains("resource_name like concat('%', #{courseTitle}, '%')"));
        assertTrue(xml.contains("course_topic = #{courseTopic}"));
        assertTrue(xml.contains("order by create_time desc, id desc"));
        int listStart = xml.indexOf("selectOnlineTrainingCourseList");
        int detailStart = xml.indexOf("selectOnlineTrainingCourseById");
        String listSection = xml.substring(listStart, detailStart);
        assertFalse(listSection.contains("video_file_url"));
        assertFalse(listSection.contains("cover_file"));
    }

    @Test
    @DisplayName("详情含视频封面")
    void detailSql()
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("selectOnlineTrainingCourseById"));
        assertTrue(xml.contains("video_file_url"));
        assertTrue(xml.contains("cover_file_url"));
    }

    @Test
    @DisplayName("主题 DISTINCT")
    void courseTopicsSql()
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("selectDistinctCourseTopics"));
        assertTrue(xml.contains("biz_information_category"));
        assertTrue(xml.contains("parent_code = '005'"));
        assertTrue(xml.contains("category_type = '8'"));
        assertTrue(xml.contains("category_code"));
        assertTrue(xml.contains("category_name"));
    }

    private static String readMapperXml()
    {
        try (InputStream in = BizOnlineTrainingCourseMapperXmlTest.class.getClassLoader()
                .getResourceAsStream("mapper/technology/BizOnlineTrainingCourseMapper.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);
        }
    }
}
