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

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.trading.mapper.BizIsolationPatrolMapper;
import com.ruoyi.web.modules.trading.mapper.BizMarketEntryEarTagMapper;
import com.ruoyi.web.modules.trading.mapper.BizTradeOrderLineMapper;

/**
 * 交易订单耳标池：（M 进栏）− B（巡查病死）− C（他单占用）。
 */
@Component
public class TradeOrderEarTagResolver
{
    @Autowired
    private BizMarketEntryEarTagMapper bizMarketEntryEarTagMapper;

    @Autowired
    private BizIsolationPatrolMapper bizIsolationPatrolMapper;

    @Autowired
    private BizTradeOrderLineMapper bizTradeOrderLineMapper;

    /**
     * /supplierEarTags：仅按 supplierId 查询 biz_market_entry_ear_tag。
     */
    public List<String> listSupplierEarTagOptions(Long supplierId)
    {
        if (supplierId == null)
        {
            return Collections.emptyList();
        }
        List<String> rows = bizMarketEntryEarTagMapper.selectEarTagsBySupplierIdOnly(supplierId);
        List<String> result = new ArrayList<>();
        if (rows != null)
        {
            for (String tag : rows)
            {
                if (StringUtils.isNotEmpty(tag))
                {
                    result.add(tag.trim());
                }
            }
        }
        return result;
    }

    public List<String> listEligible(Long supplierId, Long excludeOrderId)
    {
        if (supplierId == null)
        {
            return Collections.emptyList();
        }
        Set<String> pool = loadPoolM(supplierId);
        pool.removeAll(loadPoolB());
        Set<String> occupied = loadOccupied(excludeOrderId);
        List<String> result = new ArrayList<>();
        for (String tag : pool)
        {
            if (StringUtils.isNotEmpty(tag) && !occupied.contains(tag))
            {
                result.add(tag);
            }
        }
        Collections.sort(result);
        return result;
    }

    public Set<String> loadOccupiedEarTags(Long excludeOrderId)
    {
        List<String> rows = bizTradeOrderLineMapper.selectOccupiedEarTags(excludeOrderId);
        Set<String> occupied = new LinkedHashSet<>();
        if (rows != null)
        {
            for (String tag : rows)
            {
                if (StringUtils.isNotEmpty(tag))
                {
                    occupied.add(tag.trim());
                }
            }
        }
        return occupied;
    }

    private Set<String> loadPoolM(Long supplierId)
    {
        List<String> rows = bizMarketEntryEarTagMapper.selectDistinctEarTagsBySupplierId(supplierId);
        Set<String> tags = new LinkedHashSet<>();
        if (rows != null)
        {
            for (String tag : rows)
            {
                if (StringUtils.isNotEmpty(tag))
                {
                    tags.add(tag.trim());
                }
            }
        }
        return tags;
    }

    private Set<String> loadPoolB()
    {
        List<String> rows = bizIsolationPatrolMapper.selectSickDeadEarTagRows();
        Set<String> tags = new LinkedHashSet<>();
        if (rows == null)
        {
            return tags;
        }
        for (String raw : rows)
        {
            if (StringUtils.isEmpty(raw))
            {
                continue;
            }
            tags.addAll(EarTagCodec.parse(raw));
        }
        return tags;
    }

    private Set<String> loadOccupied(Long excludeOrderId)
    {
        return loadOccupiedEarTags(excludeOrderId);
    }
}
