package com.ruoyi.web.modules.industryservice.support;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Date;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;

@DisplayName("YakAssetValidation")
class YakAssetValidationTest
{
    @Test
    @DisplayName("ZCZX-MYNZDA-UT-002 非法资产状态")
    void ut002_assetStatus()
    {
        assertDoesNotThrow(() -> YakAssetValidation.validateAssetStatus(1));
        assertDoesNotThrow(() -> YakAssetValidation.validateAssetStatus(4));
        assertThrows(ServiceException.class, () -> YakAssetValidation.validateAssetStatus(0));
        assertThrows(ServiceException.class, () -> YakAssetValidation.validateAssetStatus(5));
    }

    @Test
    @DisplayName("ZCZX-MYNZDA-UT-003 月龄计算")
    void ut003_ageMonths()
    {
        int months = YakAssetValidation.resolveAgeMonths(
                Date.valueOf("2024-01-15"),
                Date.valueOf("2025-05-19"));
        assertEquals(16, months);
    }

    @Test
    @DisplayName("ZCZX-MYNZDA-UT-004 关键字 trim")
    void ut004_keywordTrim()
    {
        assertEquals("YAK", YakAssetValidation.trimKeyword("  YAK  "));
        assertEquals(null, YakAssetValidation.trimKeyword("   "));
    }

    @Test
    @DisplayName("ZCZX-MYNZDA-UT-011 生理天数")
    void ut011_physioDays()
    {
        assertDoesNotThrow(() -> YakAssetValidation.validatePhysioDays(30));
        assertThrows(ServiceException.class, () -> YakAssetValidation.validatePhysioDays(0));
    }

    @Test
    @DisplayName("ZCZX-MYNZDA-UT-017 Mapper 列表约定")
    void ut017_mapperXml() throws Exception
    {
        String path = "src/main/resources/mapper/industryservice/BizYakAssetMapper.xml";
        String xml = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
        assertTrueContains(xml, "del_flag = '0'");
        assertTrueContains(xml, "status_change_date desc");
        assertTrueContains(xml, "yak_no like");
        assertTrueContains(xml, "ear_tag_number like");
        assertTrueContains(xml, "ear_tag_number");
    }

    private static void assertTrueContains(String haystack, String needle)
    {
        if (!haystack.contains(needle))
        {
            throw new AssertionError("expected to contain: " + needle);
        }
    }
}
