package com.ruoyi.ccdi.project.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class CcdiProjectSpecialCheckExtendedTransferSqlTest {

    @Test
    void shouldExposeExtendedTransferMapperMethodsAndSqlCaliber() throws Exception {
        Class<?> mapperClass = Class.forName("com.ruoyi.ccdi.project.mapper.CcdiProjectSpecialCheckMapper");
        Class<?> queryClass = Class.forName("com.ruoyi.ccdi.project.domain.dto.CcdiProjectExtendedTransferQueryDTO");

        Method listMethod = mapperClass.getMethod("selectExtendedTransferPage", Page.class, queryClass);
        Method detailMethod = mapperClass.getMethod("selectExtendedTransferDetail", Long.class, Long.class);
        assertEquals("Page", listMethod.getReturnType().getSimpleName());
        assertEquals("CcdiProjectExtendedTransferDetailVO", detailMethod.getReturnType().getSimpleName());

        String xml = Files.readString(Path.of("src/main/resources/mapper/ccdi/project/CcdiProjectSpecialCheckMapper.xml"));
        String listSql = extractSelect(xml, "selectExtendedTransferPage");
        String detailSql = extractSelect(xml, "selectExtendedTransferDetail");

        assertTrue(listSql.contains("projectEmployeeScopeSql"));
        assertTrue(listSql.contains("name=\"projectId\" value=\"query.projectId\""));
        assertTrue(listSql.contains("ccdi_staff_transfer t"));
        assertTrue(listSql.contains("ccdi_base_staff s"));
        assertTrue(listSql.contains("scope.staff_name = s.name"));
        assertTrue(listSql.contains("transfer_date"));
        assertTrue(listSql.contains("order by t.transfer_date desc, t.create_time desc, t.id desc"));
        assertTrue(detailSql.contains("projectEmployeeScopeSql"));
        assertTrue(detailSql.contains("t.id = #{id}"));
        assertTrue(detailSql.contains("scope.staff_name = s.name"));
    }

    private String extractSelect(String xml, String selectId) {
        String start = "<select id=\"" + selectId + "\"";
        int startIndex = xml.indexOf(start);
        int endIndex = xml.indexOf("</select>", startIndex);
        return xml.substring(startIndex, endIndex);
    }
}
