package com.ruoyi.web.modules.diagnosis.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("BizServiceAppointmentMapper.xml")
class BizServiceAppointmentMapperXmlTest
{
    @Test
    @DisplayName("兽医列表与更新 SQL")
    void vetSql() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("provider_type = #{providerType}"));
        assertTrue(xml.contains("provider_id = #{providerId}"));
        assertTrue(xml.contains("appointee_name like concat('%', #{appointeeName}, '%')"));
        assertTrue(xml.contains("appoint_date &gt;= #{startDate}"));
        assertTrue(xml.contains("appoint_date &lt;= #{endDate}"));
        assertTrue(xml.contains("order by appoint_date desc, create_time desc, id desc"));
        assertTrue(xml.contains("updateConfirmVetAppointment"));
        assertTrue(xml.contains("updateCompleteVetAppointment"));
        assertTrue(xml.contains("and status = 0"));
        assertTrue(xml.contains("and status = 1"));
        assertTrue(xml.contains("reject_reason = #{rejectReason}"));
    }

    @Test
    @DisplayName("机构列表与详情 SQL")
    void orgSql() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("selectOrgAppointmentByIdAndProvider"));
        assertTrue(xml.contains("selectOrgAppointmentList"));
        assertTrue(xml.contains("updateCompleteOrgAppointment"));
        assertTrue(xml.contains("provider_type = 2"));
        assertTrue(xml.contains("and status = 5"));
    }

    @Test
    @DisplayName("专家列表与更新 SQL")
    void expertSql() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("selectExpertAppointmentByIdAndProvider"));
        assertTrue(xml.contains("selectExpertAppointmentList"));
        assertTrue(xml.contains("updateConfirmExpertAppointment"));
        assertTrue(xml.contains("updateRejectExpertAppointment"));
        assertTrue(xml.contains("updateCompleteExpertAppointment"));
        assertTrue(xml.contains("provider_type = 3"));
    }

    @Test
    @DisplayName("移动端预约写入 SQL")
    void appInsertSql() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("insertBizServiceAppointment"));
        assertTrue(xml.contains("countActiveAppointmentsOnDate"));
        assertTrue(xml.contains("countMyActiveAppointmentOnDate"));
        assertTrue(xml.contains("create_by = #{createBy}"));
        assertTrue(xml.contains("status not in (2, 3)"));
    }

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