package com.ruoyi.web.modules.industryservice.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.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
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.industryservice.YakDiseaseWarningTestSamples;
import com.ruoyi.web.modules.industryservice.domain.BizYakDiseaseWarning;
import com.ruoyi.web.modules.industryservice.mapper.BizYakAssetMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizYakDiseaseWarningMapper;
import com.ruoyi.web.modules.industryservice.service.YakDiseaseWarningSyncService;
import com.ruoyi.web.modules.industryservice.support.YakDiseaseWarningRules;

@ExtendWith(MockitoExtension.class)
@DisplayName("BizYakDiseaseWarningServiceImpl")
class BizYakDiseaseWarningServiceImplTest
{
    @Mock
    private BizYakDiseaseWarningMapper warningMapper;

    @Mock
    private BizYakAssetMapper bizYakAssetMapper;

    @Mock
    private YakDiseaseWarningSyncService syncService;

    @InjectMocks
    private BizYakDiseaseWarningServiceImpl service;

    @Test
    @DisplayName("ZCZX-MYNBJY-UT-015 牧场数据权限")
    void ut015_listWithScope()
    {
        BizYakDiseaseWarning query = new BizYakDiseaseWarning();
        query.setAllowedPastureIds(Collections.singletonList(YakDiseaseWarningTestSamples.PASTURE_P1));
        when(warningMapper.selectBizYakDiseaseWarningList(any()))
                .thenReturn(Collections.singletonList(YakDiseaseWarningTestSamples.recentWarning()));
        service.selectBizYakDiseaseWarningList(query);
        verify(warningMapper).selectBizYakDiseaseWarningList(any());
    }

    @Test
    @DisplayName("ZCZX-MYNBJY-UT-017 越权详情")
    void ut017_forbiddenDetail()
    {
        when(warningMapper.selectBizYakDiseaseWarningById(YakDiseaseWarningTestSamples.OTHER_WARNING_ID))
                .thenReturn(YakDiseaseWarningTestSamples.otherPastureWarning());
        ServiceException ex = assertThrows(ServiceException.class,
                () -> service.selectBizYakDiseaseWarningById(YakDiseaseWarningTestSamples.OTHER_WARNING_ID,
                        Collections.singletonList(YakDiseaseWarningTestSamples.PASTURE_P1)));
        assertEquals(YakDiseaseWarningRules.MSG_FORBIDDEN, ex.getMessage());
    }

    @Test
    @DisplayName("ZCZX-MYNBJY-UT-016 记录不存在")
    void ut016_notFound()
    {
        when(warningMapper.selectBizYakDiseaseWarningById(999L)).thenReturn(null);
        assertThrows(ServiceException.class, () -> service.selectBizYakDiseaseWarningById(999L, null));
    }

    @Test
    @DisplayName("ZCZX-MYNBJY-UT-020 无牦牛编号不关联档案")
    void ut020_noYakAssetWhenYakNoEmpty()
    {
        when(warningMapper.selectBizYakDiseaseWarningById(YakDiseaseWarningTestSamples.WARNING_ID))
                .thenReturn(YakDiseaseWarningTestSamples.recentWarning());
        BizYakDiseaseWarning detail = service.selectBizYakDiseaseWarningById(YakDiseaseWarningTestSamples.WARNING_ID, null);
        verify(bizYakAssetMapper, never()).selectBizYakAssetByYakNo(any());
        org.junit.jupiter.api.Assertions.assertNull(detail.getYakAssetId());
    }
}
