package com.ruoyi.web.modules.stock.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.web.modules.stock.domain.BizStockLog;
import com.ruoyi.web.modules.stock.dto.StockLogQuery;
import com.ruoyi.web.modules.stock.mapper.BizStockLogMapper;
import com.ruoyi.web.modules.stock.service.IStockLogService;
import com.ruoyi.web.modules.stock.support.StockSupport;
import com.ruoyi.web.modules.stock.vo.StockLogListRowVO;

@Service
public class StockLogServiceImpl implements IStockLogService
{
    @Autowired
    private BizStockLogMapper stockLogMapper;

    @Autowired
    private StockSupport stockSupport;

    @Override
    public void insertLog(BizStockLog log)
    {
        stockLogMapper.insert(log);
    }

    @Override
    public List<StockLogListRowVO> selectList(Long shopId, StockLogQuery query)
    {
        List<StockLogListRowVO> rows = stockLogMapper.selectList(shopId, query);
        enrichReasonText(rows);
        return rows;
    }

    @Override
    public List<StockLogListRowVO> selectByGoodsId(Long shopId, Long goodsId)
    {
        if (goodsId == null)
        {
            return Collections.emptyList();
        }
        List<StockLogListRowVO> rows = stockLogMapper.selectByGoodsId(shopId, goodsId);
        enrichReasonText(rows);
        return rows;
    }

    private void enrichReasonText(List<StockLogListRowVO> rows)
    {
        if (rows == null || rows.isEmpty())
        {
            return;
        }
        for (StockLogListRowVO row : rows)
        {
            row.setChangeReasonText(stockSupport.changeReasonText(row.getChangeReason()));
        }
    }
}
