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

import java.util.ArrayList;
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.goods.domain.BizGoods;
import com.ruoyi.web.modules.goods.mapper.BizGoodsMapper;
import com.ruoyi.web.modules.stock.constant.StockConstants;
import com.ruoyi.web.modules.stock.dto.StockQueryQuery;
import com.ruoyi.web.modules.stock.service.IStockLogService;
import com.ruoyi.web.modules.stock.service.IStockQueryService;
import com.ruoyi.web.modules.stock.support.StockSupport;
import com.ruoyi.web.modules.stock.vo.StockLogListRowVO;
import com.ruoyi.web.modules.stock.vo.StockQueryListRowVO;
import com.ruoyi.web.modules.stock.vo.StockQueryLogRowVO;

@Service
public class StockQueryServiceImpl implements IStockQueryService
{
    @Autowired
    private BizGoodsMapper goodsMapper;

    @Autowired
    private IStockLogService stockLogService;

    @Autowired
    private StockSupport stockSupport;

    @Override
    public List<StockQueryListRowVO> selectList(Long shopId, StockQueryQuery query)
    {
        String keyword = query != null ? query.getKeyword() : null;
        List<StockQueryListRowVO> rows = goodsMapper.selectStockQueryList(shopId, keyword);
        if (rows == null || rows.isEmpty())
        {
            return Collections.emptyList();
        }
        for (StockQueryListRowVO row : rows)
        {
            row.setSpecText(stockSupport.normalizeSpecText(stockSupport.buildSpecText(row.getGoodsId())));
        }
        return rows;
    }

    @Override
    public List<StockQueryLogRowVO> listLogs(Long shopId, Long goodsId)
    {
        BizGoods goods = goodsMapper.selectByIdAndShop(goodsId, shopId);
        if (goods == null)
        {
            throw new ServiceException(StockConstants.MSG_INBOUND_GOODS_INVALID);
        }
        List<StockLogListRowVO> logs = stockLogService.selectByGoodsId(shopId, goodsId);
        if (logs == null || logs.isEmpty())
        {
            return Collections.emptyList();
        }
        List<StockQueryLogRowVO> result = new ArrayList<>(logs.size());
        for (StockLogListRowVO log : logs)
        {
            result.add(toLogRow(log));
        }
        return result;
    }

    private StockQueryLogRowVO toLogRow(StockLogListRowVO log)
    {
        StockQueryLogRowVO row = new StockQueryLogRowVO();
        row.setGoodsName(log.getGoodsName());
        row.setSpecText(log.getSpecText());
        row.setIoTypeText(log.getChangeReasonText());
        row.setIoQty(signedQty(log.getDirection(), log.getChangeQty()));
        row.setIoTime(log.getCreateTime());
        return row;
    }

    private Integer signedQty(String direction, Integer changeQty)
    {
        int qty = changeQty != null ? changeQty : 0;
        if (qty == 0)
        {
            return 0;
        }
        if (StockConstants.DIRECTION_OUT.equals(direction))
        {
            return -qty;
        }
        return qty;
    }
}
