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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Date;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.domain.BizYakEarTagRecord;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenEarTagDto;
import com.ruoyi.web.modules.industryservice.mapper.BizYakAssetMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizYakEarTagRecordMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("YakEarTagSyncTxService")
class YakEarTagSyncTxServiceTest
{
    @Mock
    private BizYakEarTagRecordMapper earTagRecordMapper;

    @Mock
    private BizYakAssetMapper yakAssetMapper;

    @InjectMocks
    private YakEarTagSyncTxService service;

    @Test
    @DisplayName("新增耳标温度步数记录")
    void persistEarTag_insert()
    {
        when(earTagRecordMapper.selectByExternalId(4001L)).thenReturn(null);
        when(earTagRecordMapper.insertBizYakEarTagRecord(any())).thenReturn(1);

        OpenEarTagDto dto = sampleDto();
        assertEquals(true, service.persistEarTag(dto, new Date()));

        ArgumentCaptor<BizYakEarTagRecord> captor = ArgumentCaptor.forClass(BizYakEarTagRecord.class);
        verify(earTagRecordMapper).insertBizYakEarTagRecord(captor.capture());
        BizYakEarTagRecord row = captor.getValue();
        assertEquals(4001L, row.getExternalId());
        assertEquals("YAK20250001", row.getEarTagNumber());
        assertEquals("YAK20250001", row.getYakNo());
        assertEquals(0, row.getActualTemperature().compareTo(new java.math.BigDecimal("38.6")));
        assertEquals(Integer.valueOf(1280), row.getSteps());
    }

    @Test
    @DisplayName("更新已有耳标记录")
    void persistEarTag_update()
    {
        BizYakEarTagRecord existing = new BizYakEarTagRecord();
        existing.setId(10L);
        existing.setCreateTime(new Date(1_000L));
        when(earTagRecordMapper.selectByExternalId(4001L)).thenReturn(existing);

        assertFalse(service.persistEarTag(sampleDto(), new Date()));
        verify(earTagRecordMapper).updateBizYakEarTagRecordFromSync(any());
    }

    @Test
    @DisplayName("缺少 external id 拒绝")
    void persistEarTag_missingId()
    {
        OpenEarTagDto dto = sampleDto();
        dto.setId(null);
        assertThrows(ServiceException.class, () -> service.persistEarTag(dto, new Date()));
    }

    private static OpenEarTagDto sampleDto()
    {
        OpenEarTagDto dto = new OpenEarTagDto();
        dto.setId(4001L);
        dto.setCattleNo("YAK20250001");
        dto.setEarTagNumber("YAK20250001");
        dto.setActualTemperature("38.6");
        dto.setTemperature("15.2");
        dto.setSteps("1280");
        dto.setReportTime("2026-05-20 09:15:00");
        return dto;
    }
}
