package com.ruoyi.web.modules.hub.service.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.never;
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.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.hub.domain.BizDataSendRecord;
import com.ruoyi.web.modules.hub.mapper.BizDataSendRecordMapper;
import com.ruoyi.web.modules.hub.support.DataSendRecordRules;

@ExtendWith(MockitoExtension.class)
@DisplayName("BizDataSendRecordServiceImpl")
class BizDataSendRecordServiceImplTest
{

    @Mock
    private BizDataSendRecordMapper mapper;

    @InjectMocks
    private BizDataSendRecordServiceImpl service;

    @Test
    @DisplayName("新增落库 del_flag=0")
    void insert()
    {
        when(mapper.insertBizDataSendRecord(any())).thenReturn(1);
        BizDataSendRecord row = sampleRow();
        assertEquals(1, service.insertBizDataSendRecord(row));
        verify(mapper).insertBizDataSendRecord(argThat(r ->
                DataSendRecordRules.DEL_FLAG_NORMAL.equals(r.getDelFlag())
                        && "牧业产值月报".equals(r.getSendContent())));
    }

    @Test
    @DisplayName("修改全字段")
    void update()
    {
        BizDataSendRecord db = sampleRow();
        db.setId(1L);
        when(mapper.selectBizDataSendRecordById(1L)).thenReturn(db);
        when(mapper.updateBizDataSendRecord(any())).thenReturn(1);
        BizDataSendRecord in = sampleRow();
        in.setId(1L);
        in.setReceiverAddress("https://new.example.com/receive");
        assertEquals(1, service.updateBizDataSendRecord(in));
        verify(mapper).updateBizDataSendRecord(any());
    }

    @Test
    @DisplayName("记录不存在禁止修改")
    void updateNotFound()
    {
        when(mapper.selectBizDataSendRecordById(99L)).thenReturn(null);
        BizDataSendRecord in = sampleRow();
        in.setId(99L);
        assertThrows(ServiceException.class, () -> service.updateBizDataSendRecord(in));
        verify(mapper, never()).updateBizDataSendRecord(any());
    }

    @Test
    @DisplayName("逻辑删除")
    void remove()
    {
        BizDataSendRecord db = sampleRow();
        db.setId(2L);
        when(mapper.selectBizDataSendRecordByIdAny(2L)).thenReturn(db);
        when(mapper.logicDeleteBizDataSendRecordByIds(any(), any())).thenReturn(1);
        assertEquals(1, service.deleteBizDataSendRecordByIds(new Long[] { 2L }, "tester"));
        verify(mapper).logicDeleteBizDataSendRecordByIds(any(), any());
    }

    @Test
    @DisplayName("列表日期区间非法不调 Mapper")
    void listInvalidDateRange()
    {
        BizDataSendRecord query = new BizDataSendRecord();
        query.setStartDate("2025-12-01");
        query.setEndDate("2025-01-01");
        assertThrows(ServiceException.class, () -> service.selectBizDataSendRecordList(query));
        verify(mapper, never()).selectBizDataSendRecordList(any());
    }

    private static BizDataSendRecord sampleRow()
    {
        BizDataSendRecord row = new BizDataSendRecord();
        row.setSendTime(new Date());
        row.setSendContent("牧业产值月报");
        row.setReceiverName("县级政务平台");
        row.setReceiverAddress("https://api.example.com/v1/receive");
        return row;
    }
}
