package com.ruoyi.web.modules.trading.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.trading.domain.BizTradeOrder;
import com.ruoyi.web.modules.trading.domain.BizTradeOrderLine;
import com.ruoyi.web.modules.trading.mapper.AppDistributorOrderMapper;
import com.ruoyi.web.modules.trading.mapper.BizTradeOrderLineMapper;
import com.ruoyi.web.modules.trading.service.IAppDistributorOrderService;
import com.ruoyi.web.modules.trading.support.AppDistributorOrderRules;
import com.ruoyi.web.modules.trading.support.AppDistributorOrderSupport;
import com.ruoyi.web.modules.trading.support.AppDistributorSessionSupport;
import com.ruoyi.web.modules.trading.support.TradeOrderRules;

/**
 * 承销商小程序订单。
 */
@Service
public class AppDistributorOrderServiceImpl implements IAppDistributorOrderService
{
    @Autowired
    private AppDistributorSessionSupport appDistributorSessionSupport;

    @Autowired
    private AppDistributorOrderMapper appDistributorOrderMapper;

    @Autowired
    private BizTradeOrderLineMapper bizTradeOrderLineMapper;

    @Override
    public List<BizTradeOrder> selectOrderList(String statusTab)
    {
        String tab = AppDistributorOrderSupport.normalizeStatusTab(statusTab);
        Long distributorId = appDistributorSessionSupport.requireDistributorId();
        List<BizTradeOrder> list = appDistributorOrderMapper.selectDistributorOrderList(
                distributorId, AppDistributorOrderSupport.resolveOrderStatus(tab));
        enrichOrders(list);
        return list;
    }

    @Override
    public BizTradeOrder selectOrderById(Long orderId)
    {
        Long distributorId = appDistributorSessionSupport.requireDistributorId();
        BizTradeOrder row = appDistributorOrderMapper.selectDistributorOrderById(distributorId, orderId);
        if (row == null)
        {
            throw new ServiceException(AppDistributorOrderRules.MSG_ORDER_NOT_FOUND);
        }
        enrichOrders(Collections.singletonList(row));
        return row;
    }

    private void enrichOrders(List<BizTradeOrder> orders)
    {
        if (orders == null || orders.isEmpty())
        {
            return;
        }
        List<Long> orderIds = new ArrayList<>();
        for (BizTradeOrder order : orders)
        {
            if (order != null && order.getId() != null)
            {
                orderIds.add(order.getId());
                order.setOrderStatusName(TradeOrderRules.orderStatusName(order.getOrderStatus()));
            }
        }
        if (orderIds.isEmpty())
        {
            return;
        }
        List<BizTradeOrderLine> lines = bizTradeOrderLineMapper.selectLineListByOrderIds(orderIds);
        Map<Long, List<BizTradeOrderLine>> lineMap = new HashMap<>();
        if (lines != null)
        {
            for (BizTradeOrderLine line : lines)
            {
                if (line == null || line.getOrderId() == null)
                {
                    continue;
                }
                lineMap.computeIfAbsent(line.getOrderId(), k -> new ArrayList<>()).add(line);
            }
        }
        for (BizTradeOrder order : orders)
        {
            if (order != null && order.getId() != null)
            {
                List<BizTradeOrderLine> orderLines = lineMap.get(order.getId());
                order.setLineList(orderLines != null ? orderLines : Collections.emptyList());
            }
        }
    }
}
