package com.ruoyi.info.collection.service;

import com.ruoyi.info.collection.domain.dto.CcdiStaffFmyRelationAddDTO;
import com.ruoyi.info.collection.service.impl.CcdiStaffFmyRelationImportServiceImpl;
import org.junit.jupiter.api.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;

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

class CcdiStaffFmyRelationImportServiceImplTest {

    private final CcdiStaffFmyRelationImportServiceImpl service = new CcdiStaffFmyRelationImportServiceImpl();

    @Test
    void validateRelationData_shouldRejectNegativeAnnualIncome() throws Exception {
        RuntimeException exception = assertThrows(RuntimeException.class,
                () -> invokeValidateRelationData(buildDto(new BigDecimal("-1.00"))));

        assertEquals("家庭成员年收入不能为负数", exception.getMessage());
    }

    @Test
    void validateRelationData_shouldRejectAnnualIncomeWithMoreThanTwoDecimals() throws Exception {
        RuntimeException exception = assertThrows(RuntimeException.class,
                () -> invokeValidateRelationData(buildDto(new BigDecimal("12.345"))));

        assertEquals("家庭成员年收入最多保留2位小数", exception.getMessage());
    }

    private void invokeValidateRelationData(CcdiStaffFmyRelationAddDTO dto) throws Exception {
        Method method = CcdiStaffFmyRelationImportServiceImpl.class
                .getDeclaredMethod("validateRelationData", CcdiStaffFmyRelationAddDTO.class);
        method.setAccessible(true);
        try {
            method.invoke(service, dto);
        } catch (InvocationTargetException ex) {
            Throwable targetException = ex.getTargetException();
            if (targetException instanceof RuntimeException runtimeException) {
                throw runtimeException;
            }
            throw ex;
        }
    }

    private CcdiStaffFmyRelationAddDTO buildDto(BigDecimal annualIncome) {
        CcdiStaffFmyRelationAddDTO dto = new CcdiStaffFmyRelationAddDTO();
        dto.setPersonId("320101199001010014");
        dto.setRelationType("配偶");
        dto.setRelationName("李四");
        dto.setRelationCertType("护照");
        dto.setRelationCertNo("A123456789");
        dto.setAnnualIncome(annualIncome);
        return dto;
    }
}
