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.BizStockOutbound;
import com.ruoyi.web.modules.stock.domain.BizStockOutboundItem;
import com.ruoyi.web.modules.stock.dto.DeleteSpecOutboundCommand;
import com.ruoyi.web.modules.stock.dto.DeleteSpecOutboundLine;
import com.ruoyi.web.modules.stock.dto.OrderDeductOutboundCommand;
import com.ruoyi.web.modules.stock.dto.OrderDeductOutboundLine;
import com.ruoyi.web.modules.stock.mapper.BizStockOutboundItemMapper;
import com.ruoyi.web.modules.stock.mapper.BizStockOutboundMapper;
import com.ruoyi.web.modules.stock.service.IStockLogService;
import com.ruoyi.web.modules.stock.service.IStockOutboundFacade;
import com.ruoyi.web.modules.stock.support.StockSupport;

@Service
public class StockOutboundFacadeImpl implements IStockOutboundFacade
{
    @Autowired
    private BizStockOutboundMapper outboundMapper;

    @Autowired
    private BizStockOutboundItemMapper outboundItemMapper;

    @Autowired
    private IStockLogService stockLogService;

    @Autowired
    private StockSupport stockSupport;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createOrderDeductOutbound(OrderDeductOutboundCommand cmd)
    {
        if (cmd == null || cmd.getLines() == null || cmd.getLines().isEmpty()
                || StringUtils.isEmpty(cmd.getOrderNo()))
        {
            return;
        }
        if (outboundMapper.selectByRefOrderNo(cmd.getOrderNo()) != null)
        {
            return;
        }
        Date now = cmd.getOutboundTime() != null ? cmd.getOutboundTime() : new Date();
        BizStockOutbound outbound = new BizStockOutbound();
        outbound.setOutboundNo(stockSupport.nextOutboundNo());
        outbound.setShopId(cmd.getShopId());
        outbound.setOutboundType(StockConstants.OUTBOUND_TYPE_ORDER_DEDUCT);
        outbound.setRefOrderNo(cmd.getOrderNo());
        outbound.setOperatorId(0L);
        outbound.setOperatorName(StockConstants.SYSTEM_OPERATOR_NAME);
        outbound.setOutboundTime(now);
        outbound.setCreateBy(StockConstants.SYSTEM_OPERATOR_NAME);
        outbound.setCreateTime(now);
        outboundMapper.insert(outbound);

        List<BizStockOutboundItem> items = new ArrayList<>();
        for (OrderDeductOutboundLine line : cmd.getLines())
        {
            BizStockOutboundItem item = new BizStockOutboundItem();
            item.setOutboundId(outbound.getOutboundId());
            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() : 0);
            items.add(item);

            StockOutboundServiceImpl.insertOutboundLog(cmd.getShopId(), line.getGoodsId(), line.getGoodsName(),
                    line.getSpecText(), StockConstants.REASON_ORDER_DEDUCT, line.getQuantity(), item.getStockBefore(),
                    item.getStockAfter(), outbound.getOutboundId(), outbound.getOutboundNo(), 0L,
                    StockConstants.SYSTEM_OPERATOR_NAME, now, stockLogService, stockSupport);
        }
        outboundItemMapper.batchInsert(items);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createDeleteSpecOutbound(DeleteSpecOutboundCommand cmd)
    {
        if (cmd == null || cmd.getLines() == null || cmd.getLines().isEmpty())
        {
            return;
        }
        Date now = cmd.getOutboundTime() != null ? cmd.getOutboundTime() : new Date();
        BizStockOutbound outbound = new BizStockOutbound();
        outbound.setOutboundNo(stockSupport.nextOutboundNo());
        outbound.setShopId(cmd.getShopId());
        outbound.setOutboundType(StockConstants.OUTBOUND_TYPE_DELETE_SPEC);
        outbound.setOperatorId(cmd.getOperatorId() != null ? cmd.getOperatorId() : 0L);
        outbound.setOperatorName(StringUtils.nvl(cmd.getOperatorName(), ""));
        outbound.setOutboundTime(now);
        outbound.setCreateBy(cmd.getOperatorName());
        outbound.setCreateTime(now);
        outboundMapper.insert(outbound);

        List<BizStockOutboundItem> items = new ArrayList<>();
        for (DeleteSpecOutboundLine line : cmd.getLines())
        {
            int stockBefore = line.getStockBefore() != null ? line.getStockBefore() : 0;
            BizStockOutboundItem item = new BizStockOutboundItem();
            item.setOutboundId(outbound.getOutboundId());
            item.setGoodsId(line.getGoodsId());
            item.setGoodsName(line.getGoodsName());
            item.setMainPic(StringUtils.nvl(line.getMainPic(), ""));
            item.setSpecText(stockSupport.normalizeSpecText(line.getSpecText()));
            item.setQuantity(0);
            item.setStockBefore(stockBefore);
            item.setStockAfter(stockBefore);
            items.add(item);

            StockOutboundServiceImpl.insertOutboundLog(cmd.getShopId(), line.getGoodsId(), line.getGoodsName(),
                    line.getSpecText(), StockConstants.REASON_DELETE_SPEC, 0, stockBefore, stockBefore,
                    outbound.getOutboundId(), outbound.getOutboundNo(), cmd.getOperatorId(), cmd.getOperatorName(), now,
                    stockLogService, stockSupport);
        }
        outboundItemMapper.batchInsert(items);
    }
}
