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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
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.architecture.domain.BizBreedingStandard;
import com.ruoyi.web.modules.architecture.mapper.BizBreedingStandardMapper;
import com.ruoyi.web.modules.architecture.mapper.BizEquipmentWorkStandardMapper;
import com.ruoyi.web.modules.architecture.mapper.BizFeedingStandardMapper;
import com.ruoyi.web.modules.architecture.mapper.BizGrowthSlaughterStandardMapper;
import com.ruoyi.web.modules.industrypolicy.mapper.BizIndustryPolicyPublicityMapper;
import com.ruoyi.web.modules.industrypolicy.mapper.BizSubsidyMatterMapper;
import com.ruoyi.web.modules.industrypolicy.mapper.BizSubsidyProjectMapper;
import com.ruoyi.web.modules.innovation.mapper.BizTechProjectGuideMapper;
import com.ruoyi.web.modules.news.FarmingNewsTestSamples;
import com.ruoyi.web.modules.news.domain.FarmingNewsQuery;
import com.ruoyi.web.modules.news.mapper.FarmingNewsMapper;
import com.ruoyi.web.modules.news.service.impl.FarmingNewsServiceImpl;
import com.ruoyi.web.modules.news.support.FarmingNewsRules;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityProjectMapper;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityProjectOpImageMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("养殖资讯 Service")
class FarmingNewsServiceImplTest
{
    @Mock
    private FarmingNewsMapper farmingNewsMapper;

    @Mock
    private BizBreedingStandardMapper bizBreedingStandardMapper;

    @Mock
    private BizFeedingStandardMapper bizFeedingStandardMapper;

    @Mock
    private BizEquipmentWorkStandardMapper bizEquipmentWorkStandardMapper;

    @Mock
    private BizGrowthSlaughterStandardMapper bizGrowthSlaughterStandardMapper;

    @Mock
    private BizTechProjectGuideMapper bizTechProjectGuideMapper;

    @Mock
    private BizIndustryPolicyPublicityMapper bizIndustryPolicyPublicityMapper;

    @Mock
    private BizSubsidyProjectMapper bizSubsidyProjectMapper;

    @Mock
    private BizSubsidyMatterMapper bizSubsidyMatterMapper;

    @Mock
    private BizCommonProsperityProjectMapper bizCommonProsperityProjectMapper;

    @Mock
    private BizCommonProsperityProjectOpImageMapper bizCommonProsperityProjectOpImageMapper;

    @InjectMocks
    private FarmingNewsServiceImpl farmingNewsService;

    @Test
    @DisplayName("按 type 查询并映射列表项")
    void selectList_mapsVo()
    {
        when(farmingNewsMapper.selectFarmingNewsList(any(FarmingNewsQuery.class)))
                .thenReturn(Collections.singletonList(FarmingNewsTestSamples.sampleRow()));
        assertEquals(1, farmingNewsService.selectFarmingNewsList(FarmingNewsTestSamples.listQuery()).size());
    }

    @Test
    @DisplayName("无数据返回空列表")
    void selectList_empty()
    {
        when(farmingNewsMapper.selectFarmingNewsList(any(FarmingNewsQuery.class)))
                .thenReturn(Collections.emptyList());
        assertTrue(farmingNewsService.selectFarmingNewsList(FarmingNewsTestSamples.listQuery()).isEmpty());
    }

    @Test
    @DisplayName("详情按 type 查养殖标准")
    void selectById_breeding()
    {
        when(bizBreedingStandardMapper.selectBizBreedingStandardById(1001L))
                .thenReturn(FarmingNewsTestSamples.sampleBreedingDetail());
        Object detail = farmingNewsService.selectFarmingNewsById(1001L, FarmingNewsTestSamples.TYPE_BREEDING);
        assertTrue(detail instanceof BizBreedingStandard);
        assertEquals("繁育作业标准示例", ((BizBreedingStandard) detail).getStandardName());
    }

    @Test
    @DisplayName("详情未发布抛异常")
    void selectById_notPublished()
    {
        BizBreedingStandard draft = FarmingNewsTestSamples.sampleBreedingDetail();
        draft.setPublishStatus(0);
        when(bizBreedingStandardMapper.selectBizBreedingStandardById(1001L)).thenReturn(draft);
        ServiceException ex = assertThrows(ServiceException.class,
                () -> farmingNewsService.selectFarmingNewsById(1001L, FarmingNewsTestSamples.TYPE_BREEDING));
        assertEquals(FarmingNewsRules.NOT_FOUND_MSG, ex.getMessage());
    }
}
