package com.ruoyi.ccdi.project.mapper;

import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;

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

class CcdiProjectSpecialCheckMapperListSqlTest {

    @Test
    void shouldAggregateFamilyAssetLiabilityListByProjectEmployeeScope() throws Exception {
        String xml = Files.readString(Path.of("src/main/resources/mapper/ccdi/project/CcdiProjectSpecialCheckMapper.xml"));
        String listSql = extractSelect(xml, "selectFamilyAssetLiabilityList");

        assertTrue(listSql.contains("order by risk_level_sort desc, comparison_amount desc, staff_name asc"));
        assertTrue(xml.contains("from ccdi_bank_statement bs"));
        assertTrue(xml.contains("statement_staff.id_card = trim(bs.cret_no)"));
        assertTrue(xml.contains("bs.project_id = #{projectId}"));
        assertTrue(xml.contains("ccdi_base_staff"));
        assertTrue(xml.contains("ccdi_staff_fmy_relation"));
        assertTrue(xml.contains("relation_type = '配偶'"));
        assertTrue(xml.contains("annual_income"));
        assertTrue(xml.contains("current_value"));
        assertTrue(xml.contains("principal_balance"));
        assertTrue(listSql.contains("self_asset_record_count"));
        assertTrue(listSql.contains("self_debt_record_count"));
        assertTrue(listSql.contains("then 'MISSING_INFO'"));
        assertTrue(listSql.contains("then '缺少信息'"));
        assertTrue(listSql.contains("comparison_amount"));
        assertTrue(listSql.contains("&lt;= total_asset * 1.5"));
        assertTrue(listSql.contains("&gt; total_asset * 1.5"));
        assertTrue(listSql.contains("&lt;= total_asset * 3"));
        assertTrue(listSql.contains("&gt; total_asset * 3"));
        assertFalse(xml.contains("ccdi_project_overview_employee_result"));
    }

    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);
    }
}
