package com.ruoyi.web.modules.trading.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import com.ruoyi.web.modules.trading.domain.BizGradeWeightConfig;
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.trading.GradeWeightConfigTestSamples;
import com.ruoyi.web.modules.trading.TradeOrderTestSamples;
import com.ruoyi.web.modules.trading.domain.BizTradeOrderLine;
import com.ruoyi.web.modules.trading.mapper.BizGradeWeightConfigMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("TradeOrderGradeResolver")
class TradeOrderGradeResolverTest
{
    @Mock
    private BizGradeWeightConfigMapper bizGradeWeightConfigMapper;

    @InjectMocks
    private TradeOrderGradeResolver resolver;

    @Test
    @DisplayName("重量区间匹配等级编码")
    void resolveByWeight()
    {
        List<BizGradeWeightConfig> configs = GradeWeightConfigTestSamples.standardList();
        assertEquals("A", TradeOrderGradeResolver.resolveGradeCode(new BigDecimal("140"), configs));
        assertEquals("B", TradeOrderGradeResolver.resolveGradeCode(new BigDecimal("120.5"), configs));
        assertEquals("B", TradeOrderGradeResolver.resolveGradeCode(new BigDecimal("139.99"), configs));
        assertEquals("C", TradeOrderGradeResolver.resolveGradeCode(new BigDecimal("100"), configs));
        assertEquals("D", TradeOrderGradeResolver.resolveGradeCode(new BigDecimal("99.99"), configs));
        assertEquals(null, TradeOrderGradeResolver.resolveGradeCode(new BigDecimal("1000"), configs));
    }

    @Test
    @DisplayName("assignGrades 写入明细 gradeCode 与 gradeName")
    void assignGrades()
    {
        when(bizGradeWeightConfigMapper.selectAllOrderBySort())
                .thenReturn(GradeWeightConfigTestSamples.standardList());
        BizTradeOrderLine line = TradeOrderTestSamples.line("E101", "120.5", "8000");
        resolver.assignGrades(Collections.singletonList(line));
        assertEquals("B", line.getGradeCode());
        assertEquals("二级", line.getGradeName());
    }

    @Test
    @DisplayName("无匹配配置时失败")
    void noMatchThrows()
    {
        when(bizGradeWeightConfigMapper.selectAllOrderBySort())
                .thenReturn(GradeWeightConfigTestSamples.standardList());
        BizTradeOrderLine line = TradeOrderTestSamples.line("E101", "1000", "8000");
        assertThrows(ServiceException.class, () -> resolver.assignGrades(Collections.singletonList(line)));
    }
}
