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

import java.text.ParseException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import com.ruoyi.web.modules.trading.IsolationPatrolTestSamples;
import com.ruoyi.web.modules.trading.domain.BizIsolationPatrol;
import com.ruoyi.web.modules.trading.mapper.BizIsolationPatrolMapper;
import com.ruoyi.web.modules.trading.support.IsolationPatrolRules;
import com.ruoyi.web.modules.trading.support.PatrolValidation;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("BizIsolationPatrolServiceImpl")
class BizIsolationPatrolServiceImplTest
{
    @Mock
    private BizIsolationPatrolMapper bizIsolationPatrolMapper;

    @Mock
    private PatrolValidation patrolValidation;

    @InjectMocks
    private BizIsolationPatrolServiceImpl service;

    @BeforeEach
    void stubValidation()
    {
        doAnswer(invocation -> {
            BizIsolationPatrol row = invocation.getArgument(0);
            row.setPenCode("01");
            if (row.getSickDeadQty() != null && row.getSickDeadQty() == 0)
            {
                row.setEarTags(null);
            }
            return null;
        }).when(patrolValidation).prepareAndValidateForSave(any());
    }

    @Test
    @DisplayName("ZCZX-GLXCDJ-UT-017 新增落库")
    void ut017_insert() throws ParseException
    {
        BizIsolationPatrol row = IsolationPatrolTestSamples.normalSaveRow();
        when(bizIsolationPatrolMapper.insertBizIsolationPatrol(any())).thenReturn(1);
        service.insertBizIsolationPatrol(row);
        ArgumentCaptor<BizIsolationPatrol> captor = ArgumentCaptor.forClass(BizIsolationPatrol.class);
        verify(bizIsolationPatrolMapper).insertBizIsolationPatrol(captor.capture());
        assertEquals(IsolationPatrolRules.DEL_FLAG_NORMAL, captor.getValue().getDelFlag());
        assertEquals("01", captor.getValue().getPenCode());
    }

    @Test
    @DisplayName("ZCZX-GLXCDJ-UT-018 数量为0耳标空")
    void ut018_insertQtyZero() throws ParseException
    {
        BizIsolationPatrol row = IsolationPatrolTestSamples.normalSaveRow();
        when(bizIsolationPatrolMapper.insertBizIsolationPatrol(any())).thenReturn(1);
        service.insertBizIsolationPatrol(row);
        ArgumentCaptor<BizIsolationPatrol> captor = ArgumentCaptor.forClass(BizIsolationPatrol.class);
        verify(bizIsolationPatrolMapper).insertBizIsolationPatrol(captor.capture());
        assertNull(captor.getValue().getEarTags());
    }

    @Test
    @DisplayName("ZCZX-GLXCDJ-UT-020 逻辑删除")
    void ut020_delete()
    {
        BizIsolationPatrol db = new BizIsolationPatrol();
        db.setId(IsolationPatrolTestSamples.PATROL_ID);
        db.setDelFlag(IsolationPatrolRules.DEL_FLAG_NORMAL);
        when(bizIsolationPatrolMapper.selectBizIsolationPatrolByIdAny(IsolationPatrolTestSamples.PATROL_ID))
                .thenReturn(db);
        when(bizIsolationPatrolMapper.logicDeleteBizIsolationPatrolByIds(any(), eq("tester"))).thenReturn(1);
        service.deleteBizIsolationPatrolByIds(new Long[] { IsolationPatrolTestSamples.PATROL_ID }, "tester");
        verify(bizIsolationPatrolMapper).logicDeleteBizIsolationPatrolByIds(
                new Long[] { IsolationPatrolTestSamples.PATROL_ID }, "tester");
    }

    @Test
    @DisplayName("详情解析耳标列表")
    void detailEarTagList() throws ParseException
    {
        BizIsolationPatrol row = IsolationPatrolTestSamples.persistedRow();
        row.setEarTags("E001,E002");
        when(bizIsolationPatrolMapper.selectBizIsolationPatrolById(IsolationPatrolTestSamples.PATROL_ID))
                .thenReturn(row);
        BizIsolationPatrol result = service.selectBizIsolationPatrolById(IsolationPatrolTestSamples.PATROL_ID);
        assertEquals(2, result.getEarTagList().size());
    }

    @Test
    @DisplayName("数量为0详情耳标空")
    void detailNoEarTags() throws ParseException
    {
        BizIsolationPatrol row = IsolationPatrolTestSamples.persistedRow();
        row.setEarTags(null);
        when(bizIsolationPatrolMapper.selectBizIsolationPatrolById(IsolationPatrolTestSamples.PATROL_ID))
                .thenReturn(row);
        BizIsolationPatrol result = service.selectBizIsolationPatrolById(IsolationPatrolTestSamples.PATROL_ID);
        assertNull(row.getEarTags());
        assertEquals(0, result.getEarTagList().size());
    }
}
