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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
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.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.web.modules.epidemic.RiskFactorWeightTestSamples;
import com.ruoyi.web.modules.epidemic.domain.dto.RiskFactorWeightSaveRequest;
import com.ruoyi.web.modules.epidemic.mapper.BizRiskFactorWeightMapper;
import com.ruoyi.web.modules.epidemic.support.RiskFactorWeightRules;

/**
 * FXYZ-UT-008～012
 */
@ExtendWith(MockitoExtension.class)
@DisplayName("BizRiskFactorWeightServiceImpl")
class BizRiskFactorWeightServiceImplTest
{

    @Mock
    private BizRiskFactorWeightMapper riskFactorWeightMapper;

    @InjectMocks
    private BizRiskFactorWeightServiceImpl service;

    @Test
    @DisplayName("FXYZ-UT-008 首次保存 insert")
    void ut008_insert()
    {
        when(riskFactorWeightMapper.selectBizRiskFactorWeightById(RiskFactorWeightRules.SINGLETON_ID)).thenReturn(null);
        when(riskFactorWeightMapper.insertBizRiskFactorWeight(any())).thenReturn(1);
        assertEquals(1, service.saveConfig(RiskFactorWeightTestSamples.uniformRequest(), "tester"));
        verify(riskFactorWeightMapper).insertBizRiskFactorWeight(argThat(r ->
                r.getId() == RiskFactorWeightRules.SINGLETON_ID
                        && "tester".equals(r.getCreateBy())));
        verify(riskFactorWeightMapper, never()).updateBizRiskFactorWeight(any());
    }

    @Test
    @DisplayName("FXYZ-UT-009 再次保存 update")
    void ut009_update()
    {
        when(riskFactorWeightMapper.selectBizRiskFactorWeightById(RiskFactorWeightRules.SINGLETON_ID))
                .thenReturn(RiskFactorWeightTestSamples.dbRow());
        when(riskFactorWeightMapper.updateBizRiskFactorWeight(any())).thenReturn(1);
        RiskFactorWeightSaveRequest req = RiskFactorWeightTestSamples.request(
                new BigDecimal("0.3"),
                new BigDecimal("0.25"),
                new BigDecimal("0.2"),
                new BigDecimal("0.15"),
                new BigDecimal("0.1"));
        assertEquals(1, service.saveConfig(req, "tester"));
        verify(riskFactorWeightMapper).updateBizRiskFactorWeight(argThat(r ->
                r.getWeightQuarantineIntensity().compareTo(new BigDecimal("0.2000")) == 0
                        && r.getWeightBreedingDensity().compareTo(new BigDecimal("0.3000")) == 0));
        verify(riskFactorWeightMapper, never()).insertBizRiskFactorWeight(any());
    }

    @Test
    @DisplayName("FXYZ-UT-011 查询单例")
    void ut011_getConfig()
    {
        when(riskFactorWeightMapper.selectBizRiskFactorWeightById(RiskFactorWeightRules.SINGLETON_ID))
                .thenReturn(RiskFactorWeightTestSamples.dbRow());
        com.ruoyi.web.modules.epidemic.domain.vo.RiskFactorWeightVo vo = service.getCurrentConfig();
        assertEquals(new BigDecimal("0.2000"), vo.getBreedingDensityWeight());
        assertEquals(new BigDecimal("1.0000"), vo.getWeightSum());
    }

    @Test
    @DisplayName("FXYZ-UT-012 无记录查询返回 null")
    void ut012_getConfigEmpty()
    {
        when(riskFactorWeightMapper.selectBizRiskFactorWeightById(RiskFactorWeightRules.SINGLETON_ID)).thenReturn(null);
        assertNull(service.getCurrentConfig());
    }
}
