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("union all"));
        assertTrue(xml.contains("relation.relation_cert_no as person_id"));
        assertTrue(xml.contains("inner join ccdi_base_staff current_staff"));
        assertTrue(xml.contains("spouse_staff.annual_income"));
        assertTrue(xml.contains("spouse_is_staff"));
        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("spouse_staff_asset_record_count"));
        assertTrue(listSql.contains("self_debt_record_count"));
        assertTrue(listSql.contains("spouse_staff_debt_record_count"));
        assertTrue(listSql.contains("source.self_asset_record_count = 0"));
        assertTrue(listSql.contains("source.spouse_staff_asset_record_count = 0"));
        assertTrue(listSql.contains("source.self_debt_record_count = 0"));
        assertTrue(listSql.contains("source.spouse_staff_debt_record_count = 0"));
        assertTrue(listSql.contains("asset.family_id = spouse.spouse_id_card"));
        assertTrue(listSql.contains("asset.person_id = spouse.spouse_id_card"));
        assertTrue(listSql.contains("debt.person_id = spouse.spouse_id_card"));
        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);
    }
}
