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

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.trading.domain.BizIsolationPen;
import com.ruoyi.web.modules.trading.mapper.BizIsolationPenMapper;
import com.ruoyi.web.modules.trading.support.IsolationPenReferenceChecker;
import com.ruoyi.web.modules.trading.support.IsolationPenRules;
import com.ruoyi.web.modules.trading.support.PenCodeGenerator;
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;

@ExtendWith(MockitoExtension.class)
@DisplayName("BizIsolationPenServiceImpl")
class BizIsolationPenServiceImplTest
{
    @Mock
    private BizIsolationPenMapper mapper;

    @Mock
    private PenCodeGenerator penCodeGenerator;

    @Mock
    private IsolationPenReferenceChecker referenceChecker;

    @InjectMocks
    private BizIsolationPenServiceImpl service;

    @Test
    @DisplayName("新增分配编号")
    void insert()
    {
        when(penCodeGenerator.allocateNextCode()).thenReturn("03");
        when(mapper.insertBizIsolationPen(any())).thenReturn(1);
        BizIsolationPen row = new BizIsolationPen();
        row.setRemark("隔离区");
        assertEquals(1, service.insertBizIsolationPen(row));
        verify(mapper).insertBizIsolationPen(argThat(r -> "03".equals(r.getPenCode())
                && IsolationPenRules.DEL_FLAG_NORMAL.equals(r.getDelFlag())));
    }

    @Test
    @DisplayName("不可改编号")
    void updateCodeBlocked()
    {
        BizIsolationPen db = new BizIsolationPen();
        db.setId(1L);
        db.setPenCode("01");
        when(mapper.selectBizIsolationPenById(1L)).thenReturn(db);
        BizIsolationPen in = new BizIsolationPen();
        in.setId(1L);
        in.setPenCode("99");
        assertThrows(ServiceException.class, () -> service.updateBizIsolationPen(in));
        verify(mapper, never()).updateBizIsolationPen(any());
    }
}
