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

import java.math.BigDecimal;
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.prosperity.domain.BizCommonProsperityAchievement;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityAchievementMapper;
import com.ruoyi.web.modules.prosperity.ProsperityAchievementTestSamples;

/**
 * GTFY-CG-UT-002、009～012
 */
@ExtendWith(MockitoExtension.class)
@DisplayName("BizCommonProsperityAchievementServiceImpl")
class BizCommonProsperityAchievementServiceImplTest
{

    @Mock
    private BizCommonProsperityAchievementMapper achievementMapper;

    @InjectMocks
    private BizCommonProsperityAchievementServiceImpl service;

    @Test
    @DisplayName("GTFY-CG-UT-002 统计范围唯一-新增")
    void ut002_insertDuplicatePeriod()
    {
        BizCommonProsperityAchievement row = ProsperityAchievementTestSamples.sample();
        when(achievementMapper.countByStatPeriod("2026-01", null)).thenReturn(1);
        row.setStatPeriod("2026-01");
        assertThrows(ServiceException.class, () -> service.insertBizCommonProsperityAchievement(row));
        verify(achievementMapper, never()).insertBizCommonProsperityAchievement(any());
    }

    @Test
    @DisplayName("GTFY-CG-UT-009 新增落库")
    void ut009_insert()
    {
        BizCommonProsperityAchievement row = ProsperityAchievementTestSamples.sample();
        when(achievementMapper.countByStatPeriod(row.getStatPeriod(), null)).thenReturn(0);
        when(achievementMapper.insertBizCommonProsperityAchievement(any())).thenReturn(1);
        assertEquals(1, service.insertBizCommonProsperityAchievement(row));
        verify(achievementMapper).insertBizCommonProsperityAchievement(any());
    }

    @Test
    @DisplayName("GTFY-CG-UT-010 修改改月冲突")
    void ut010_updatePeriodConflict()
    {
        BizCommonProsperityAchievement db = ProsperityAchievementTestSamples.sample();
        db.setId(1L);
        db.setStatPeriod("2026-01");
        when(achievementMapper.selectBizCommonProsperityAchievementById(1L)).thenReturn(db);
        when(achievementMapper.countByStatPeriod("2026-02", 1L)).thenReturn(1);
        BizCommonProsperityAchievement in = ProsperityAchievementTestSamples.sample();
        in.setId(1L);
        in.setStatPeriod("2026-02");
        assertThrows(ServiceException.class, () -> service.updateBizCommonProsperityAchievement(in));
        verify(achievementMapper, never()).updateBizCommonProsperityAchievement(any());
    }

    @Test
    @DisplayName("GTFY-CG-UT-011 修改同月保留")
    void ut011_updateSamePeriod()
    {
        BizCommonProsperityAchievement db = ProsperityAchievementTestSamples.sample();
        db.setId(1L);
        db.setStatPeriod("2026-01");
        when(achievementMapper.selectBizCommonProsperityAchievementById(1L)).thenReturn(db);
        when(achievementMapper.countByStatPeriod("2026-01", 1L)).thenReturn(0);
        when(achievementMapper.updateBizCommonProsperityAchievement(any())).thenReturn(1);
        BizCommonProsperityAchievement in = ProsperityAchievementTestSamples.sample();
        in.setId(1L);
        in.setStatPeriod("2026-01");
        in.setCollectiveEconomyIncome(new BigDecimal("200"));
        assertEquals(1, service.updateBizCommonProsperityAchievement(in));
    }

    @Test
    @DisplayName("GTFY-CG-UT-012 删除记录")
    void ut012_delete()
    {
        when(achievementMapper.deleteBizCommonProsperityAchievementByIds(any())).thenReturn(1);
        assertEquals(1, service.deleteBizCommonProsperityAchievementByIds(new Long[] { 9L }));
        verify(achievementMapper).deleteBizCommonProsperityAchievementByIds(any());
    }
}
