package com.ruoyi.web.modules.industrypolicy.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.industrypolicy.domain.BizIndustryPolicyPublicity;
import com.ruoyi.web.modules.industrypolicy.mapper.BizIndustryPolicyPublicityMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("BizIndustryPolicyPublicityServiceImpl")
class BizIndustryPolicyPublicityServiceImplTest
{

    @Mock
    private BizIndustryPolicyPublicityMapper mapper;

    @InjectMocks
    private BizIndustryPolicyPublicityServiceImpl service;

    @Test
    @DisplayName("ZCZX-XC-UT-012 新增初始未发布")
    void ut012_insert()
    {
        when(mapper.insertBizIndustryPolicyPublicity(any())).thenAnswer(inv -> {
            BizIndustryPolicyPublicity r = inv.getArgument(0);
            r.setId(1L);
            return 1;
        });
        assertEquals(1, service.insertBizIndustryPolicyPublicity(sampleRow()));
        verify(mapper).insertBizIndustryPolicyPublicity(argThat(r ->
                r.getPublishStatus() == 0 && r.getPublishTime() == null));
    }

    @Test
    @DisplayName("ZCZX-XC-UT-013 已发布禁止修改")
    void ut013_updatePublished()
    {
        BizIndustryPolicyPublicity db = sampleRow();
        db.setId(9L);
        db.setPublishStatus(1);
        when(mapper.selectBizIndustryPolicyPublicityById(9L)).thenReturn(db);
        BizIndustryPolicyPublicity in = sampleRow();
        in.setId(9L);
        in.setPolicyName("新名称");
        ServiceException ex = assertThrows(ServiceException.class,
                () -> service.updateBizIndustryPolicyPublicity(in));
        org.junit.jupiter.api.Assertions.assertTrue(ex.getMessage().contains("下架"));
        verify(mapper, never()).updateBizIndustryPolicyPublicity(any());
    }

    @Test
    @DisplayName("ZCZX-XC-UT-014 发布成功")
    void ut014_publish()
    {
        BizIndustryPolicyPublicity db = sampleRow();
        db.setId(3L);
        db.setPublishStatus(0);
        when(mapper.selectBizIndustryPolicyPublicityById(3L)).thenReturn(db);
        when(mapper.updateBizIndustryPolicyPublicity(any())).thenReturn(1);
        assertEquals(1, service.publish(3L));
        verify(mapper).updateBizIndustryPolicyPublicity(argThat(r ->
                r.getPublishStatus() == 1 && r.getPublishTime() != null));
    }

    @Test
    @DisplayName("ZCZX-XC-UT-015 下架成功")
    void ut015_offline()
    {
        BizIndustryPolicyPublicity db = sampleRow();
        db.setId(4L);
        db.setPublishStatus(1);
        when(mapper.selectBizIndustryPolicyPublicityById(4L)).thenReturn(db);
        when(mapper.updateBizIndustryPolicyPublicity(any())).thenReturn(1);
        assertEquals(1, service.offline(4L));
        verify(mapper).updateBizIndustryPolicyPublicity(argThat(r ->
                r.getPublishStatus() == 0 && r.getOfflineTime() != null));
    }

    @Test
    @DisplayName("ZCZX-XC-UT-016 未发布禁止下架")
    void ut016_offlineDraft()
    {
        BizIndustryPolicyPublicity db = sampleRow();
        db.setId(5L);
        db.setPublishStatus(0);
        when(mapper.selectBizIndustryPolicyPublicityById(5L)).thenReturn(db);
        assertThrows(ServiceException.class, () -> service.offline(5L));
    }

    @Test
    @DisplayName("ZCZX-XC-UT-017 删除未发布")
    void ut017_delete()
    {
        BizIndustryPolicyPublicity db = sampleRow();
        db.setId(21L);
        db.setPublishStatus(0);
        when(mapper.selectBizIndustryPolicyPublicityById(21L)).thenReturn(db);
        when(mapper.deleteBizIndustryPolicyPublicityByIds(any())).thenReturn(1);
        assertEquals(1, service.deleteBizIndustryPolicyPublicityByIds(new Long[] { 21L }));
    }

    @Test
    @DisplayName("ZCZX-XC-UT-018 已发布禁止删除")
    void ut018_deletePublished()
    {
        BizIndustryPolicyPublicity db = sampleRow();
        db.setId(20L);
        db.setPublishStatus(1);
        when(mapper.selectBizIndustryPolicyPublicityById(20L)).thenReturn(db);
        assertThrows(ServiceException.class, () -> service.deleteBizIndustryPolicyPublicityByIds(new Long[] { 20L }));
    }

    private static BizIndustryPolicyPublicity sampleRow()
    {
        BizIndustryPolicyPublicity r = new BizIndustryPolicyPublicity();
        r.setPolicyName("政策");
        r.setPolicyLevel(2);
        r.setIssueDate(new Date());
        r.setPolicyDesc("描述");
        return r;
    }
}
