package com.ruoyi.warehouse.service.impl;

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.warehouse.domain.OutRecord;
import com.ruoyi.warehouse.domain.StockInfo;
import com.ruoyi.warehouse.mapper.StockInfoMapper;
import com.ruoyi.warehouse.service.IOutRecordService;
import com.ruoyi.warehouse.service.IStockInfoService;

@Service
public class StockInfoServiceImpl implements IStockInfoService
{
    @Autowired
    private StockInfoMapper stockInfoMapper;

    @Autowired
    private IOutRecordService outRecordService;

    @Override
    public List<StockInfo> selectStockInfoList(StockInfo stockInfo)
    {
        return stockInfoMapper.selectStockInfoList(stockInfo);
    }

    @Override
    public StockInfo selectStockInfoById(Long id)
    {
        return stockInfoMapper.selectStockInfoById(id);
    }

    @Override
    public int outStock(OutRecord outRecord)
    {
        StockInfo stockInfo = stockInfoMapper.selectStockInfoById(outRecord.getId());
        if (stockInfo == null)
        {
            throw new ServiceException("库存记录不存在");
        }
        outRecord.setId(null);
        outRecord.setAreaId(stockInfo.getAreaId());
        outRecord.setProductId(stockInfo.getProductId());
        return outRecordService.insertOutRecord(outRecord);
    }
}
