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.Mockito.verify;
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 org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.trading.SupplierSettlementTestSamples;
import com.ruoyi.web.modules.trading.domain.BizSupplierSettlement;
import com.ruoyi.web.modules.trading.domain.BizTradeOrder;
import com.ruoyi.web.modules.trading.mapper.BizSupplierSettlementMapper;
import com.ruoyi.web.modules.trading.mapper.BizTradeOrderMapper;
import com.ruoyi.web.modules.trading.support.SettlementRules;
import com.ruoyi.web.modules.trading.support.SettlementValidation;
import com.ruoyi.web.modules.trading.support.TradeOrderRules;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("BizSupplierSettlementServiceImpl")
class BizSupplierSettlementServiceImplTest
{
    @Mock
    private BizSupplierSettlementMapper bizSupplierSettlementMapper;

    @Mock
    private BizTradeOrderMapper bizTradeOrderMapper;

    @Mock
    private SettlementValidation settlementValidation;

    @InjectMocks
    private BizSupplierSettlementServiceImpl service;

    @Test
    @DisplayName("ZCZX-GYSJSGL-UT-014 完成结算成功")
    void ut014_completeSettleSuccess()
    {
        BizSupplierSettlement pending = SupplierSettlementTestSamples.pendingRow();
        when(bizSupplierSettlementMapper.selectBizSupplierSettlementById(SupplierSettlementTestSamples.SETTLEMENT_ID))
                .thenReturn(pending);
        when(bizTradeOrderMapper.selectBizTradeOrderById(SupplierSettlementTestSamples.ORDER_ID))
                .thenReturn(SupplierSettlementTestSamples.completedOrder());
        when(bizSupplierSettlementMapper.updateCompleteSettle(any())).thenReturn(1);

        BizSupplierSettlement result = service.completeSettle(
                SupplierSettlementTestSamples.settleRequest(), "tester");

        assertEquals(SettlementRules.SETTLEMENT_STATUS_COMPLETED, result.getSettlementStatus());
        assertEquals("已完成", result.getSettlementStatusName());
        verify(bizSupplierSettlementMapper).updateCompleteSettle(any());
        verify(bizTradeOrderMapper).selectBizTradeOrderById(SupplierSettlementTestSamples.ORDER_ID);
    }

    @Test
    @DisplayName("ZCZX-GYSJSGL-UT-015 非待结算拒绝")
    void ut015_notPending()
    {
        when(bizSupplierSettlementMapper.selectBizSupplierSettlementById(SupplierSettlementTestSamples.SETTLEMENT_ID))
                .thenReturn(SupplierSettlementTestSamples.completedRow());
        ServiceException ex = assertThrows(ServiceException.class,
                () -> service.completeSettle(SupplierSettlementTestSamples.settleRequest(), "tester"));
        assertEquals(SettlementRules.MSG_NOT_PENDING, ex.getMessage());
    }

    @Test
    @DisplayName("ZCZX-GYSJSGL-UT-016 关联订单非已完成")
    void ut016_orderNotCompleted()
    {
        BizSupplierSettlement pending = SupplierSettlementTestSamples.pendingRow();
        BizTradeOrder order = SupplierSettlementTestSamples.completedOrder();
        order.setOrderStatus(TradeOrderRules.ORDER_STATUS_PENDING);
        when(bizSupplierSettlementMapper.selectBizSupplierSettlementById(SupplierSettlementTestSamples.SETTLEMENT_ID))
                .thenReturn(pending);
        when(bizTradeOrderMapper.selectBizTradeOrderById(SupplierSettlementTestSamples.ORDER_ID))
                .thenReturn(order);
        ServiceException ex = assertThrows(ServiceException.class,
                () -> service.completeSettle(SupplierSettlementTestSamples.settleRequest(), "tester"));
        assertEquals(SettlementRules.MSG_ORDER_INVALID, ex.getMessage());
    }

    @Test
    @DisplayName("ZCZX-GYSJSGL-UT-018 并发更新失败")
    void ut018_updateConflict()
    {
        when(bizSupplierSettlementMapper.selectBizSupplierSettlementById(SupplierSettlementTestSamples.SETTLEMENT_ID))
                .thenReturn(SupplierSettlementTestSamples.pendingRow());
        when(bizTradeOrderMapper.selectBizTradeOrderById(SupplierSettlementTestSamples.ORDER_ID))
                .thenReturn(SupplierSettlementTestSamples.completedOrder());
        when(bizSupplierSettlementMapper.updateCompleteSettle(any())).thenReturn(0);
        ServiceException ex = assertThrows(ServiceException.class,
                () -> service.completeSettle(SupplierSettlementTestSamples.settleRequest(), "tester"));
        assertEquals(SettlementRules.MSG_ALREADY_COMPLETED, ex.getMessage());
    }

    @Test
    @DisplayName("ZCZX-GYSJSGL-UT-021 详情收费方式中文")
    void ut021_detailFeeMethodName()
    {
        when(bizSupplierSettlementMapper.selectBizSupplierSettlementById(SupplierSettlementTestSamples.SETTLEMENT_ID))
                .thenReturn(SupplierSettlementTestSamples.pendingRow());
        BizSupplierSettlement row = service.selectBizSupplierSettlementById(SupplierSettlementTestSamples.SETTLEMENT_ID);
        assertEquals("按头计费", row.getFeeMethodName());
        assertEquals("待结算", row.getSettlementStatusName());
    }
}
