package com.ruoyi.ccdi.project.mapper;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

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

class CcdiBankTagResultMapperXmlTest {

    private static final String RESOURCE = "mapper/ccdi/project/CcdiBankTagResultMapper.xml";

    @Test
    void deleteByProjectAndOptionalModel_shouldRenderScopedDelete() throws Exception {
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(RESOURCE)) {
            String xml = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
            assertTrue(xml.contains("delete from ccdi_bank_statement_tag_result"));
            assertTrue(xml.contains("project_id = #{projectId}"));
            assertTrue(xml.contains("model_code = #{modelCode}"));
        }
    }

    @Test
    void mapper_shouldExposeStatementTagQueryForBankStatementDetails() {
        Method method = Arrays.stream(CcdiBankTagResultMapper.class.getDeclaredMethods())
                .filter(item -> "selectStatementTagsByProjectAndStatementIds".equals(item.getName()))
                .findFirst()
                .orElse(null);

        assertNotNull(method, "应提供按项目和流水ID批量查询异常标签的方法");
        assertTrue(method.getParameterCount() == 4, "标签批量查询应接收模型和预警类型范围");
    }

    @Test
    void xml_shouldDefineStatementTagQueryForBankStatementDetails() throws Exception {
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(RESOURCE)) {
            String xml = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);

            assertTrue(xml.contains("selectStatementTagsByProjectAndStatementIds"), xml);
            assertTrue(xml.contains("bank_statement_id IN"), xml);
            assertTrue(xml.contains("project_id = #{projectId}"), xml);
            assertTrue(xml.contains("model_code = #{modelCode}"), xml);
            assertTrue(xml.contains("left(model_code, 9) != 'EXTERNAL_'"), xml);
            assertTrue(xml.contains("left(model_code, 9) = 'EXTERNAL_'"), xml);
        }
    }
}
