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

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.cart.constant.CartConstants;
import com.ruoyi.web.modules.cart.dto.CartItemRowDTO;
import com.ruoyi.web.modules.category.facade.ICategoryFacade;
import com.ruoyi.web.modules.goods.constant.GoodsConstants;
import com.ruoyi.web.modules.store.constant.ShopConstants;

/**
 * 购物车失效判定
 */
public final class CartInvalidSupport
{
    private CartInvalidSupport()
    {
    }

    public static String resolveInvalidType(CartItemRowDTO row, ICategoryFacade categoryFacade)
    {
        if (ShopConstants.DEL_DELETED.equals(row.getShopDelFlag()))
        {
            return CartConstants.INVALID_SHOP_DELETED;
        }
        if (row.getGoodsId() == null || GoodsConstants.DEL_DELETED.equals(row.getGoodsDelFlag())
                || row.getGoodsName() == null)
        {
            return CartConstants.INVALID_GOODS_DELETED;
        }
        if (!GoodsConstants.STATUS_ON_SALE.equals(row.getGoodsStatus()))
        {
            return CartConstants.INVALID_OFF_SHELF;
        }
        if (ShopConstants.STATUS_CLOSED.equals(row.getShopStatus()))
        {
            return CartConstants.INVALID_SHOP_CLOSED;
        }
        if (row.getCategoryId() == null || !categoryFacade.isCategoryVisible(row.getCategoryId()))
        {
            return CartConstants.INVALID_CATEGORY_HIDDEN;
        }
        Integer stock = row.getStock();
        Integer quantity = row.getQuantity();
        if (stock == null || stock <= 0 || quantity == null || quantity > stock)
        {
            return CartConstants.INVALID_OUT_OF_STOCK;
        }
        return CartConstants.INVALID_NONE;
    }

    public static String resolveInvalidMsg(String invalidType, String purchaseReason)
    {
        if (CartConstants.INVALID_NONE.equals(invalidType))
        {
            return null;
        }
        if (StringUtils.isNotEmpty(purchaseReason))
        {
            return purchaseReason;
        }
        if (CartConstants.INVALID_OUT_OF_STOCK.equals(invalidType))
        {
            return CartConstants.MSG_INVALID_OUT_OF_STOCK;
        }
        if (CartConstants.INVALID_OFF_SHELF.equals(invalidType))
        {
            return CartConstants.MSG_INVALID_OFF_SHELF;
        }
        if (CartConstants.INVALID_SHOP_CLOSED.equals(invalidType))
        {
            return CartConstants.MSG_INVALID_SHOP_CLOSED;
        }
        if (CartConstants.INVALID_CATEGORY_HIDDEN.equals(invalidType))
        {
            return CartConstants.MSG_INVALID_CATEGORY_HIDDEN;
        }
        if (CartConstants.INVALID_GOODS_DELETED.equals(invalidType))
        {
            return CartConstants.MSG_INVALID_GOODS_DELETED;
        }
        if (CartConstants.INVALID_SHOP_DELETED.equals(invalidType))
        {
            return CartConstants.MSG_INVALID_SHOP_DELETED;
        }
        return CartConstants.MSG_INVALID_OFF_SHELF;
    }

    public static boolean isPurchasable(String invalidType)
    {
        return CartConstants.INVALID_NONE.equals(invalidType);
    }
}
