package com.ruoyi.ccdi.project.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectRiskModelPeopleQueryDTO;
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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class CcdiProjectOverviewMapperRiskModelPeopleTest {

    @Test
    void shouldExposeRiskModelPeoplePageMapperMethod() throws Exception {
        Method method = CcdiProjectOverviewMapper.class.getMethod(
            "selectRiskModelPeoplePage",
            Page.class,
            CcdiProjectRiskModelPeopleQueryDTO.class
        );

        assertEquals(Page.class, method.getReturnType());
        assertEquals(Page.class, method.getParameterTypes()[0]);
        assertEquals(CcdiProjectRiskModelPeopleQueryDTO.class, method.getParameterTypes()[1]);
    }

    @Test
    void shouldDefineRiskModelPeopleSqlForAnyAllKeywordDeptAndScopedCollections() throws Exception {
        String xml = Files.readString(Path.of("src/main/resources/mapper/ccdi/project/CcdiProjectOverviewMapper.xml"));

        assertTrue(xml.contains("<select id=\"selectRiskModelPeoplePage\""));
        assertTrue(xml.contains("query.modelCodes != null and query.modelCodes.size() > 0"));
        assertTrue(xml.contains("query.matchMode == 'ALL'"));
        assertFalse(xml.contains("#{query.modelCodes.size}"));
        assertTrue(xml.contains("find_in_set(#{modelCode}, result.model_codes_csv)"));
        assertTrue(xml.contains("<bind name=\"projectId\" value=\"query.projectId\"/>"));
        assertTrue(xml.contains("result.staff_name like concat('%', trim(#{query.keyword}), '%')"));
        assertTrue(xml.contains("result.staff_code like concat('%', trim(#{query.keyword}), '%')"));
        assertTrue(xml.contains("result.dept_id = #{query.deptId}"));
        assertTrue(xml.contains("select=\"selectRiskModelNamesByScope\""));
        assertTrue(xml.contains("select=\"selectRiskHitTagsByScope\""));
        assertTrue(xml.contains("model_hit_summary_json"));
        assertTrue(xml.contains("hit_rules_json"));
        assertTrue(xml.contains("json_extract("));
        assertTrue(xml.contains(".modelCode"));
        assertTrue(xml.contains(".modelName"));
        assertTrue(xml.contains(".ruleCode"));
        assertTrue(xml.contains(".riskLevel"));
        assertTrue(xml.contains("as model_code"));
        assertTrue(xml.contains("as model_name"));
        assertTrue(xml.contains("group by json_unquote(json_extract(result.hit_rules_json, concat('$[', idx.idx, '].modelCode')))"));
    }
}
