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

import java.util.Collections;
import java.util.List;
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.mapper.AppSupplierOrderMapper;
import com.ruoyi.web.modules.trading.mapper.BizTradeOrderLineMapper;
import com.ruoyi.web.modules.trading.service.IAppSupplierOrderService;
import com.ruoyi.web.modules.trading.support.AppSupplierOrderEnrichSupport;
import com.ruoyi.web.modules.trading.support.AppSupplierOrderRules;
import com.ruoyi.web.modules.trading.support.AppSupplierOrderSupport;
import com.ruoyi.web.modules.trading.support.AppSupplierSessionSupport;

/**
 * 供应商小程序交易订单。
 */
@Service
public class AppSupplierOrderServiceImpl implements IAppSupplierOrderService
{
    @Autowired
    private AppSupplierSessionSupport appSupplierSessionSupport;

    @Autowired
    private AppSupplierOrderMapper appSupplierOrderMapper;

    @Autowired
    private BizTradeOrderLineMapper bizTradeOrderLineMapper;

    @Override
    public List<BizTradeOrder> selectOrderList(String statusTab)
    {
        String tab = AppSupplierOrderSupport.normalizeStatusTab(statusTab);
        Long supplierId = appSupplierSessionSupport.requireSupplierId();
        List<BizTradeOrder> list = appSupplierOrderMapper.selectSupplierOrderList(
                supplierId, AppSupplierOrderSupport.resolveOrderStatus(tab));
        AppSupplierOrderEnrichSupport.enrichTradeOrders(list, bizTradeOrderLineMapper);
        return list;
    }

    @Override
    public BizTradeOrder selectOrderById(Long orderId)
    {
        Long supplierId = appSupplierSessionSupport.requireSupplierId();
        BizTradeOrder row = appSupplierOrderMapper.selectSupplierOrderById(supplierId, orderId);
        if (row == null)
        {
            throw new ServiceException(AppSupplierOrderRules.MSG_ORDER_NOT_FOUND);
        }
        AppSupplierOrderEnrichSupport.enrichTradeOrders(Collections.singletonList(row), bizTradeOrderLineMapper);
        return row;
    }
}
