package com.ylx.order.service.impl;

import com.ylx.order.domain.vo.MerchantCompositeScoreVO;
import com.ylx.order.mapper.TCommentUserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;

import java.math.BigDecimal;

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

public class TCommentUserServiceImplTest {

    @Test
    public void getMerchantCompositeScoreDividesCommentScoreSumByOrderCount() {
        TCommentUserMapper mapper = mock(TCommentUserMapper.class);
        MerchantCompositeScoreVO statistic = new MerchantCompositeScoreVO();
        statistic.setScoreSum(new BigDecimal("18.6"));
        statistic.setOrderCount(4L);
        when(mapper.selectMerchantScoreStatistics(10)).thenReturn(statistic);

        TCommentUserServiceImpl service = new TCommentUserServiceImpl();
        ReflectionTestUtils.setField(service, "tCommentUserMapper", mapper);

        MerchantCompositeScoreVO result = service.getMerchantCompositeScoreByMerchantId(10);

        assertEquals(10, result.getMerchantId());
        assertEquals(new BigDecimal("18.6"), result.getScoreSum());
        assertEquals(4L, result.getOrderCount());
        assertEquals(new BigDecimal("4.7"), result.getComprehensiveScore());
    }

    @Test
    public void getMerchantCompositeScoreReturnsZeroWhenOrderCountIsZero() {
        TCommentUserMapper mapper = mock(TCommentUserMapper.class);
        MerchantCompositeScoreVO statistic = new MerchantCompositeScoreVO();
        statistic.setScoreSum(new BigDecimal("12.0"));
        statistic.setOrderCount(0L);
        when(mapper.selectMerchantScoreStatistics(10)).thenReturn(statistic);

        TCommentUserServiceImpl service = new TCommentUserServiceImpl();
        ReflectionTestUtils.setField(service, "tCommentUserMapper", mapper);

        MerchantCompositeScoreVO result = service.getMerchantCompositeScoreByMerchantId(10);

        assertEquals(new BigDecimal("12.0"), result.getScoreSum());
        assertEquals(0L, result.getOrderCount());
        assertEquals(new BigDecimal("0.0"), result.getComprehensiveScore());
    }
}
