package com.ruoyi.web.modules.farming.support;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.farming.TechServicePersonnelTestSamples;
import com.ruoyi.web.modules.farming.domain.BizTechServicePersonnel;
import com.ruoyi.web.modules.farming.domain.dto.TechServicePersonnelSaveRequest;
import com.ruoyi.web.modules.farming.domain.vo.TechServicePersonnelVo;

@DisplayName("TechServicePersonnelValidation")
class TechServicePersonnelValidationTest
{
    @Test
    @DisplayName("合法保存校验")
    void validSave()
    {
        assertDoesNotThrow(() -> TechServicePersonnelValidation.validateForSave(TechServicePersonnelTestSamples.sampleRequest()));
    }

    @Test
    @DisplayName("负人数校验失败")
    void negativeRejected()
    {
        TechServicePersonnelSaveRequest req = TechServicePersonnelTestSamples.sampleRequest();
        req.setEnvoyMaleCount(-1);
        assertThrows(ServiceException.class, () -> TechServicePersonnelValidation.validateForSave(req));
    }

    @Test
    @DisplayName("派生总人数：特派员男+女、事业五级合计")
    void derivedTotals()
    {
        BizTechServicePersonnel row = TechServicePersonnelValidation.toEntity(TechServicePersonnelTestSamples.sampleRequest());
        assertEquals(18, row.getEnvoyTotalCount());
        assertEquals(16, row.getAgriTotalCount());
    }

    @Test
    @DisplayName("全部为空时派生总人数为空")
    void derivedNullWhenEmpty()
    {
        TechServicePersonnelSaveRequest req = new TechServicePersonnelSaveRequest();
        BizTechServicePersonnel row = TechServicePersonnelValidation.toEntity(req);
        assertNull(row.getEnvoyTotalCount());
        assertNull(row.getAgriTotalCount());
    }

    @Test
    @DisplayName("toVo 映射派生字段")
    void toVo()
    {
        TechServicePersonnelVo vo = TechServicePersonnelValidation.toVo(TechServicePersonnelTestSamples.dbRow());
        assertEquals(18, vo.getEnvoyTotalCount());
        assertEquals(16, vo.getAgriTotalCount());
        assertEquals(10, vo.getEnvoyMaleCount());
    }
}
