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

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.math.BigDecimal;
import java.util.Collections;

import com.github.pagehelper.Page;
import com.ruoyi.framework.web.exception.GlobalExceptionHandler;
import com.ruoyi.web.modules.trading.domain.BizTradeOrder;
import com.ruoyi.web.modules.trading.domain.BizTradeOrderLine;
import com.ruoyi.web.modules.trading.service.IAppDistributorOrderService;
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.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@ExtendWith(MockitoExtension.class)
@DisplayName("承销商订单移动端 HTTP MockMvc")
class BizAppDistributorOrderControllerApiTest
{
    @Mock
    private IAppDistributorOrderService appDistributorOrderService;

    @InjectMocks
    private BizAppDistributorOrderController controller;

    @Test
    @DisplayName("GET /app/distributor/order/list")
    void list() throws Exception
    {
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .setControllerAdvice(new GlobalExceptionHandler())
                .build();

        BizTradeOrder order = new BizTradeOrder();
        order.setId(1L);
        order.setOrderNo("20260526002");
        order.setOrderStatusName("待支付");
        order.setTotalAmount(new BigDecimal("53089"));
        BizTradeOrderLine line = new BizTradeOrderLine();
        line.setEarTag("E101");
        order.setLineList(Collections.singletonList(line));

        Page<BizTradeOrder> page = new Page<>();
        page.add(order);
        when(appDistributorOrderService.selectOrderList(eq("ALL"))).thenReturn(page);

        mockMvc.perform(get("/app/distributor/order/list").param("statusTab", "ALL"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].orderNo").value("20260526002"))
                .andExpect(jsonPath("$.rows[0].lineList[0].earTag").value("E101"));
    }
}
