package com.ruoyi.web.modules.farming.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("BizTechResourceMapper.xml")
class BizTechResourceMapperXmlTest
{

    @Test
    @DisplayName("列表筛选与排序")
    void listSql() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("resource_type = #{resourceType}"));
        assertTrue(xml.contains("resource_type = '004005'"));
        assertTrue(xml.contains("resource_name like concat('%', #{resourceName}, '%')"));
        assertTrue(xml.contains("offline_time is null"));
        assertTrue(xml.contains("offline_time is not null"));
        assertTrue(xml.contains("order by create_time desc, id desc"));
        assertTrue(xml.contains("del_flag = '0'"));
        assertTrue(xml.contains("biz_tech_resource"));
        assertTrue(xml.contains("expert_rating"));
    }

    @Test
    @DisplayName("专家账号绑定 SQL")
    void expertResourceByUserSql() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("selectExpertResourceBySysUserId"));
        assertTrue(xml.contains("sys_user_id = #{sysUserId}"));
        assertTrue(xml.contains("account_assigned = 1"));
        assertTrue(xml.contains("countBySysUserId"));
    }

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