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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.stock.constant.StockConstants;
import com.ruoyi.web.modules.stock.domain.BizStockInbound;
import com.ruoyi.web.modules.stock.domain.BizStockInboundItem;
import com.ruoyi.web.modules.stock.domain.BizStockLog;
import com.ruoyi.web.modules.stock.dto.AutoInboundCommand;
import com.ruoyi.web.modules.stock.dto.AutoInboundLine;
import com.ruoyi.web.modules.stock.mapper.BizStockInboundItemMapper;
import com.ruoyi.web.modules.stock.mapper.BizStockInboundMapper;
import com.ruoyi.web.modules.stock.service.IStockInboundFacade;
import com.ruoyi.web.modules.stock.service.IStockLogService;
import com.ruoyi.web.modules.stock.support.StockSupport;

@Service
public class StockInboundFacadeImpl implements IStockInboundFacade
{
    @Autowired
    private BizStockInboundMapper inboundMapper;

    @Autowired
    private BizStockInboundItemMapper inboundItemMapper;

    @Autowired
    private IStockLogService stockLogService;

    @Autowired
    private StockSupport stockSupport;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createAutoInbound(AutoInboundCommand cmd)
    {
        if (cmd == null || cmd.getLines() == null || cmd.getLines().isEmpty())
        {
            return;
        }
        Date now = new Date();
        BizStockInbound inbound = new BizStockInbound();
        inbound.setInboundNo(stockSupport.nextInboundNo());
        inbound.setShopId(cmd.getShopId());
        inbound.setInboundType(StockConstants.INBOUND_TYPE_NEW_SPEC);
        inbound.setOperatorId(cmd.getOperatorId());
        inbound.setOperatorName(StringUtils.nvl(cmd.getOperatorName(), ""));
        inbound.setInboundTime(now);
        inbound.setCreateBy(cmd.getOperatorName());
        inbound.setCreateTime(now);
        inboundMapper.insert(inbound);

        List<BizStockInboundItem> items = new ArrayList<>();
        for (AutoInboundLine line : cmd.getLines())
        {
            BizStockInboundItem item = new BizStockInboundItem();
            item.setInboundId(inbound.getInboundId());
            item.setGoodsId(line.getGoodsId());
            item.setGoodsName(line.getGoodsName());
            item.setMainPic(StringUtils.nvl(line.getMainPic(), ""));
            item.setSpecText(stockSupport.normalizeSpecText(line.getSpecText()));
            item.setQuantity(line.getQuantity());
            item.setStockBefore(line.getStockBefore() != null ? line.getStockBefore() : 0);
            item.setStockAfter(line.getStockAfter() != null ? line.getStockAfter() : line.getQuantity());
            items.add(item);

            BizStockLog log = buildInboundLog(cmd.getShopId(), line.getGoodsId(), line.getGoodsName(),
                    line.getSpecText(), StockConstants.REASON_NEW_SPEC, line.getQuantity(),
                    item.getStockBefore(), item.getStockAfter(), inbound.getInboundId(), inbound.getInboundNo(),
                    cmd.getOperatorId(), cmd.getOperatorName(), now);
            stockLogService.insertLog(log);
        }
        inboundItemMapper.batchInsert(items);
    }

    private BizStockLog buildInboundLog(Long shopId, Long goodsId, String goodsName, String specText,
            String changeReason, Integer changeQty, Integer stockBefore, Integer stockAfter,
            Long refId, String refNo, Long operatorId, String operatorName, Date now)
    {
        BizStockLog log = new BizStockLog();
        log.setShopId(shopId);
        log.setGoodsId(goodsId);
        log.setGoodsName(goodsName);
        log.setSpecText(stockSupport.normalizeSpecText(specText));
        log.setDirection(StockConstants.DIRECTION_IN);
        log.setChangeReason(changeReason);
        log.setChangeQty(changeQty);
        log.setStockBefore(stockBefore);
        log.setStockAfter(stockAfter);
        log.setRefType(StockConstants.REF_TYPE_INBOUND);
        log.setRefId(refId);
        log.setRefNo(refNo);
        log.setOperatorId(operatorId);
        log.setOperatorName(StringUtils.nvl(operatorName, ""));
        log.setCreateTime(now);
        return log;
    }
}
