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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

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.architecture.domain.AppMedicationWithdrawalCalculateVo;
import com.ruoyi.web.modules.architecture.domain.BizWithdrawalPeriodStandard;
import com.ruoyi.web.modules.architecture.mapper.AppMedicationDrugMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("AppMedicationWithdrawalServiceImpl")
class AppMedicationWithdrawalServiceImplTest
{
    @Mock
    private AppMedicationDrugMapper appMedicationDrugMapper;

    @InjectMocks
    private AppMedicationWithdrawalServiceImpl service;

    @Test
    @DisplayName("停药日期加休药期天数")
    void calculate()
    {
        BizWithdrawalPeriodStandard row = new BizWithdrawalPeriodStandard();
        row.setDrugName("青霉素");
        row.setWithdrawalDays(7);
        when(appMedicationDrugMapper.selectWithdrawalByDrugName("青霉素")).thenReturn(row);

        AppMedicationWithdrawalCalculateVo vo = service.calculate("青霉素", "2025-06-01");
        assertEquals("2025-06-01", vo.getStopDate());
        assertEquals(7, vo.getWithdrawalDays());
        assertEquals("2025-06-08", vo.getEndDate());
    }

    @Test
    @DisplayName("停药日期格式错误")
    void invalidStopDate()
    {
        assertThrows(ServiceException.class, () -> service.calculate("青霉素", "2025/06/01"));
    }

    @Test
    @DisplayName("未找到药物")
    void drugNotFound()
    {
        when(appMedicationDrugMapper.selectWithdrawalByDrugName("未知")).thenReturn(null);
        assertThrows(ServiceException.class, () -> service.calculate("未知", "2025-06-01"));
    }
}
