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

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ruoyi.web.modules.trading.domain.BizSupplierSettlement;
import com.ruoyi.web.modules.trading.domain.BizTradeOrder;
import com.ruoyi.web.modules.trading.domain.BizTradeOrderLine;
import com.ruoyi.web.modules.trading.mapper.BizTradeOrderLineMapper;

/**
 * 供应商小程序订单/结算单明细组装。
 */
public final class AppSupplierOrderEnrichSupport
{
    private AppSupplierOrderEnrichSupport()
    {
    }

    public static void enrichTradeOrders(List<BizTradeOrder> orders, BizTradeOrderLineMapper lineMapper)
    {
        if (orders == null || orders.isEmpty())
        {
            return;
        }
        List<Long> orderIds = new ArrayList<>();
        for (BizTradeOrder order : orders)
        {
            if (order == null || order.getId() == null)
            {
                continue;
            }
            orderIds.add(order.getId());
            order.setOrderStatusName(TradeOrderRules.orderStatusName(order.getOrderStatus()));
            if (order.getSettlementStatus() != null)
            {
                order.setSettlementStatusName(SettlementRules.settlementStatusName(order.getSettlementStatus()));
            }
        }
        attachLinesToOrders(orders, orderIds, lineMapper);
    }

    public static void enrichSettlements(
            List<BizSupplierSettlement> settlements, BizTradeOrderLineMapper lineMapper)
    {
        if (settlements == null || settlements.isEmpty())
        {
            return;
        }
        List<Long> orderIds = new ArrayList<>();
        for (BizSupplierSettlement row : settlements)
        {
            if (row == null)
            {
                continue;
            }
            row.setSettlementStatusName(SettlementRules.settlementStatusName(row.getSettlementStatus()));
            if (row.getOrderId() != null)
            {
                orderIds.add(row.getOrderId());
            }
        }
        Map<Long, List<BizTradeOrderLine>> lineMap = loadLineMap(orderIds, lineMapper);
        for (BizSupplierSettlement row : settlements)
        {
            if (row == null || row.getOrderId() == null)
            {
                row.setLineList(Collections.emptyList());
                continue;
            }
            List<BizTradeOrderLine> lines = lineMap.get(row.getOrderId());
            row.setLineList(lines != null ? lines : Collections.emptyList());
        }
    }

    private static void attachLinesToOrders(
            List<BizTradeOrder> orders, List<Long> orderIds, BizTradeOrderLineMapper lineMapper)
    {
        if (orderIds.isEmpty())
        {
            return;
        }
        Map<Long, List<BizTradeOrderLine>> lineMap = loadLineMap(orderIds, lineMapper);
        for (BizTradeOrder order : orders)
        {
            if (order != null && order.getId() != null)
            {
                List<BizTradeOrderLine> lines = lineMap.get(order.getId());
                order.setLineList(lines != null ? lines : Collections.emptyList());
            }
        }
    }

    private static Map<Long, List<BizTradeOrderLine>> loadLineMap(
            List<Long> orderIds, BizTradeOrderLineMapper lineMapper)
    {
        Map<Long, List<BizTradeOrderLine>> lineMap = new HashMap<>();
        if (orderIds == null || orderIds.isEmpty())
        {
            return lineMap;
        }
        List<BizTradeOrderLine> lines = lineMapper.selectLineListByOrderIds(orderIds);
        if (lines == null)
        {
            return lineMap;
        }
        for (BizTradeOrderLine line : lines)
        {
            if (line == null || line.getOrderId() == null)
            {
                continue;
            }
            lineMap.computeIfAbsent(line.getOrderId(), k -> new ArrayList<>()).add(line);
        }
        return lineMap;
    }
}
