package com.ruoyi.loanpricing.mapper;

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

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;

class LoanPricingWorkflowMapperXmlTest
{
    @Test
    void shouldUseRetailFinalCalculateRateInWorkflowListQuery() throws IOException
    {
        ClassPathResource resource = new ClassPathResource("mapper/loanpricing/LoanPricingWorkflowMapper.xml");
        String xml = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);

        assertTrue(xml.contains("WHEN lpw.cust_type = '个人' THEN mr.final_calculate_rate"));
        assertTrue(xml.contains("WHEN lpw.cust_type = '企业' THEN mc.final_calculate_rate"));
        assertTrue(xml.contains("lpw.dept_id AS deptId"));
        assertTrue(xml.contains("lpw.create_by = #{query.dataScopeCreateBy}"));
        assertTrue(xml.contains("lpw.dept_id IN"));
        assertTrue(xml.contains("find_in_set(#{query.dataScopeDeptId}, ancestors)"));
        assertTrue(xml.contains("lpw.dept_id = #{query.deptId}"));
        assertTrue(xml.contains("SUBSTRING_INDEX(lpw.create_by, '-', -1) LIKE CONCAT('%', #{query.createBy}, '%')"));
        assertTrue(xml.contains("lpw.cust_isn LIKE CONCAT('%', #{query.custIsn}, '%')"));
        assertTrue(xml.contains("lpw.id_num LIKE CONCAT('%', #{query.custIsn}, '%')"));
        assertTrue(xml.contains("lpw.cust_name LIKE CONCAT('%', #{query.custIsn}, '%')"));
        assertTrue(xml.contains("lpw.cust_type = #{query.custType}"));
        assertTrue(xml.contains("lpw.guar_type = #{query.guarType}"));
        assertTrue(xml.contains("lpw.create_time &gt;= #{query.beginCreateTime}"));
        assertTrue(xml.contains("lpw.create_time &lt;= #{query.endCreateTime}"));
    }

    @Test
    void shouldUpdateOnlySensitiveFieldsForAnonymousMigration() throws IOException
    {
        ClassPathResource resource = new ClassPathResource("mapper/loanpricing/LoanPricingWorkflowMapper.xml");
        String xml = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
        String updateSql = xml.substring(xml.indexOf("<update id=\"updateSensitiveFieldsById\">"),
                xml.indexOf("</update>", xml.indexOf("<update id=\"updateSensitiveFieldsById\">")));

        assertTrue(updateSql.contains("cust_name = #{custName}"));
        assertTrue(updateSql.contains("id_num = #{idNum}"));
        assertTrue(updateSql.contains("WHERE id = #{id}"));
        assertFalse(updateSql.contains("update_by"));
        assertFalse(updateSql.contains("update_time"));
    }
}
