package com.ruoyi.web.modules.stock.support;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.goods.domain.BizGoodsAttr;
import com.ruoyi.web.modules.goods.mapper.BizGoodsAttrMapper;
import com.ruoyi.web.modules.stock.constant.StockConstants;
import com.ruoyi.web.modules.stock.mapper.BizStockInboundMapper;
import com.ruoyi.web.modules.stock.mapper.BizStockOutboundMapper;
import com.ruoyi.web.modules.template.constant.AttrTemplateConstants;

/**
 * 库存模块工具
 */
@Component
public class StockSupport
{
    @Autowired
    private BizStockInboundMapper inboundMapper;

    @Autowired
    private BizStockOutboundMapper outboundMapper;

    @Autowired
    private BizGoodsAttrMapper goodsAttrMapper;

    public String nextInboundNo()
    {
        String prefix = "IN" + new SimpleDateFormat("yyyyMMdd").format(new Date());
        String max = inboundMapper.selectMaxInboundNoByPrefix(prefix);
        int seq = 1;
        if (StringUtils.isNotEmpty(max) && max.length() > prefix.length())
        {
            try
            {
                seq = Integer.parseInt(max.substring(prefix.length())) + 1;
            }
            catch (NumberFormatException ignored)
            {
                seq = 1;
            }
        }
        return prefix + String.format("%06d", seq);
    }

    public String nextOutboundNo()
    {
        String prefix = "OUT" + new SimpleDateFormat("yyyyMMdd").format(new Date());
        String max = outboundMapper.selectMaxOutboundNoByPrefix(prefix);
        int seq = 1;
        if (StringUtils.isNotEmpty(max) && max.length() > prefix.length())
        {
            try
            {
                seq = Integer.parseInt(max.substring(prefix.length())) + 1;
            }
            catch (NumberFormatException ignored)
            {
                seq = 1;
            }
        }
        return prefix + String.format("%06d", seq);
    }

    public String inboundTypeText(String inboundType)
    {
        if (StockConstants.INBOUND_TYPE_NEW_SPEC.equals(inboundType))
        {
            return "新增商品规格";
        }
        if (StockConstants.INBOUND_TYPE_PURCHASE.equals(inboundType))
        {
            return "采购入库";
        }
        if (StockConstants.INBOUND_TYPE_RETURN.equals(inboundType))
        {
            return "退货入库";
        }
        if (StockConstants.INBOUND_TYPE_OTHER.equals(inboundType))
        {
            return "其他入库";
        }
        return inboundType;
    }

    public String changeReasonForInboundType(String inboundType)
    {
        if (StockConstants.INBOUND_TYPE_NEW_SPEC.equals(inboundType))
        {
            return StockConstants.REASON_NEW_SPEC;
        }
        if (StockConstants.INBOUND_TYPE_PURCHASE.equals(inboundType))
        {
            return StockConstants.REASON_PURCHASE_IN;
        }
        if (StockConstants.INBOUND_TYPE_RETURN.equals(inboundType))
        {
            return StockConstants.REASON_RETURN_IN;
        }
        if (StockConstants.INBOUND_TYPE_OTHER.equals(inboundType))
        {
            return StockConstants.REASON_OTHER_IN;
        }
        return StockConstants.REASON_OTHER_IN;
    }

    public String outboundTypeText(String outboundType)
    {
        if (StockConstants.OUTBOUND_TYPE_ORDER_DEDUCT.equals(outboundType))
        {
            return "下单扣减";
        }
        if (StockConstants.OUTBOUND_TYPE_DELETE_SPEC.equals(outboundType))
        {
            return "删除产品规格";
        }
        if (StockConstants.OUTBOUND_TYPE_MANUAL_ORDER.equals(outboundType))
        {
            return "订单出库";
        }
        if (StockConstants.OUTBOUND_TYPE_EXPIRED.equals(outboundType))
        {
            return "过期出库";
        }
        if (StockConstants.OUTBOUND_TYPE_OTHER.equals(outboundType))
        {
            return "其他出库";
        }
        return outboundType;
    }

    public String changeReasonForOutboundType(String outboundType)
    {
        if (StockConstants.OUTBOUND_TYPE_ORDER_DEDUCT.equals(outboundType))
        {
            return StockConstants.REASON_ORDER_DEDUCT;
        }
        if (StockConstants.OUTBOUND_TYPE_DELETE_SPEC.equals(outboundType))
        {
            return StockConstants.REASON_DELETE_SPEC;
        }
        if (StockConstants.OUTBOUND_TYPE_MANUAL_ORDER.equals(outboundType))
        {
            return StockConstants.REASON_MANUAL_ORDER_OUT;
        }
        if (StockConstants.OUTBOUND_TYPE_EXPIRED.equals(outboundType))
        {
            return StockConstants.REASON_EXPIRED_OUT;
        }
        if (StockConstants.OUTBOUND_TYPE_OTHER.equals(outboundType))
        {
            return StockConstants.REASON_OTHER_OUT;
        }
        return StockConstants.REASON_OTHER_OUT;
    }

    public String changeReasonText(String changeReason)
    {
        if (StringUtils.isEmpty(changeReason))
        {
            return "";
        }
        if (StockConstants.REASON_NEW_SPEC.equals(changeReason))
        {
            return "新增商品规格";
        }
        if (StockConstants.REASON_PURCHASE_IN.equals(changeReason))
        {
            return "采购入库";
        }
        if (StockConstants.REASON_RETURN_IN.equals(changeReason))
        {
            return "退货入库";
        }
        if (StockConstants.REASON_OTHER_IN.equals(changeReason))
        {
            return "其他入库";
        }
        if (StockConstants.REASON_ORDER_CANCEL.equals(changeReason))
        {
            return "取消订单返库存";
        }
        if (StockConstants.REASON_STOCK_EDIT.equals(changeReason))
        {
            return "库存编辑";
        }
        if (StockConstants.REASON_ORDER_DEDUCT.equals(changeReason))
        {
            return "下单扣减库存";
        }
        if (StockConstants.REASON_DELETE_SPEC.equals(changeReason))
        {
            return "删除产品规格";
        }
        if (StockConstants.REASON_MANUAL_ORDER_OUT.equals(changeReason))
        {
            return "订单出库";
        }
        if (StockConstants.REASON_EXPIRED_OUT.equals(changeReason))
        {
            return "过期出库";
        }
        if (StockConstants.REASON_OTHER_OUT.equals(changeReason))
        {
            return "其他出库";
        }
        return changeReason;
    }

    public String buildSpecText(Long goodsId)
    {
        if (goodsId == null)
        {
            return null;
        }
        List<BizGoodsAttr> rows = goodsAttrMapper.selectByGoodsId(goodsId);
        if (rows == null || rows.isEmpty())
        {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (BizGoodsAttr row : rows)
        {
            if (!AttrTemplateConstants.ITEM_TYPE_SPEC.equals(row.getAttrType()))
            {
                continue;
            }
            if (sb.length() > 0)
            {
                sb.append(" / ");
            }
            sb.append(row.getItemName()).append(':').append(row.getValueText());
        }
        return sb.length() == 0 ? null : sb.toString();
    }

    public String normalizeSpecText(String specText)
    {
        return StringUtils.isEmpty(specText) ? "—" : specText;
    }

    public String adjustTypeText(String adjustType)
    {
        if (StockConstants.ADJUST_TYPE_INCREASE.equals(adjustType))
        {
            return "增加库存";
        }
        if (StockConstants.ADJUST_TYPE_DECREASE.equals(adjustType))
        {
            return "减少库存";
        }
        if (StockConstants.ADJUST_TYPE_OVERRIDE.equals(adjustType))
        {
            return "覆盖库存";
        }
        return adjustType;
    }
}
