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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.web.modules.industryservice.domain.BizYakPhysioSeries;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenEarTagDto;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenEnvMonitoringDto;

@DisplayName("YakAssetPhysioSupport")
class YakAssetPhysioSupportTest
{
    @Test
    @DisplayName("取上报时间最大的耳标记录")
    void findLatestEarTag()
    {
        OpenEarTagDto older = earTag("ET001", "12.0", "100", "2026-05-19 10:00:00");
        OpenEarTagDto newer = earTag("ET001", "13.5", "200", "2026-05-20 09:15:00");
        OpenEarTagDto latest = YakAssetPhysioSupport.findLatestEarTag(Arrays.asList(older, newer));
        assertNotNull(latest);
        assertEquals(new BigDecimal("13.5"), YakAssetPhysioSupport.convertedTemperature(latest));
        assertEquals(200, YakAssetPhysioSupport.parseSteps(latest.getSteps()));
    }

    @Test
    @DisplayName("合并耳标与环境曲线")
    void mergeChartSeries()
    {
        List<OpenEarTagDto> earTags = Arrays.asList(
                earTag("ET001", "12.0", "100", "2026-05-19 10:00:00"),
                earTag("ET001", "13.0", "120", "2026-05-20 09:00:00"));
        List<OpenEnvMonitoringDto> envRows = Arrays.asList(env(18.5, "2026-05-20 10:00:00"));
        List<BizYakPhysioSeries> chart = YakAssetPhysioSupport.mergeChartSeries(earTags, envRows);
        assertEquals(3, chart.size());
        assertEquals(new BigDecimal("12.0"), chart.get(0).getBodyTemp());
        assertEquals(new BigDecimal("13.0"), chart.get(1).getBodyTemp());
        assertEquals(new BigDecimal("18.5"), chart.get(2).getEnvTemp());
        assertNull(chart.get(2).getBodyTemp());
    }

    private static OpenEarTagDto earTag(String no, String temp, String steps, String reportTime)
    {
        OpenEarTagDto dto = new OpenEarTagDto();
        dto.setEarTagNumber(no);
        dto.setTemperature(temp);
        dto.setSteps(steps);
        dto.setReportTime(reportTime);
        return dto;
    }

    private static OpenEnvMonitoringDto env(double temp, String monitoringTime)
    {
        OpenEnvMonitoringDto dto = new OpenEnvMonitoringDto();
        dto.setTemperature(temp);
        dto.setMonitoringTime(monitoringTime);
        return dto;
    }
}
