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.BizSupplierSettlement;
import com.ruoyi.web.modules.trading.mapper.AppSupplierSettlementMapper;
import com.ruoyi.web.modules.trading.mapper.BizTradeOrderLineMapper;
import com.ruoyi.web.modules.trading.service.IAppSupplierSettlementService;
import com.ruoyi.web.modules.trading.support.AppSupplierOrderEnrichSupport;
import com.ruoyi.web.modules.trading.support.AppSupplierOrderRules;
import com.ruoyi.web.modules.trading.support.AppSupplierSessionSupport;

/**
 * 供应商小程序待结算单。
 */
@Service
public class AppSupplierSettlementServiceImpl implements IAppSupplierSettlementService
{
    @Autowired
    private AppSupplierSessionSupport appSupplierSessionSupport;

    @Autowired
    private AppSupplierSettlementMapper appSupplierSettlementMapper;

    @Autowired
    private BizTradeOrderLineMapper bizTradeOrderLineMapper;

    @Override
    public List<BizSupplierSettlement> selectPendingList()
    {
        Long supplierId = appSupplierSessionSupport.requireSupplierId();
        List<BizSupplierSettlement> list = appSupplierSettlementMapper.selectPendingSettlementList(supplierId);
        AppSupplierOrderEnrichSupport.enrichSettlements(list, bizTradeOrderLineMapper);
        return list;
    }

    @Override
    public BizSupplierSettlement selectPendingById(Long settlementId)
    {
        Long supplierId = appSupplierSessionSupport.requireSupplierId();
        BizSupplierSettlement row = appSupplierSettlementMapper.selectPendingSettlementById(
                supplierId, settlementId);
        if (row == null)
        {
            throw new ServiceException(AppSupplierOrderRules.MSG_SETTLEMENT_NOT_FOUND);
        }
        AppSupplierOrderEnrichSupport.enrichSettlements(Collections.singletonList(row), bizTradeOrderLineMapper);
        return row;
    }
}
