package com.ruoyi.web.modules.goods.facade.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.modules.category.facade.ICategoryFacade;
import com.ruoyi.web.modules.goods.constant.GoodsConstants;
import com.ruoyi.web.modules.goods.domain.BizGoods;
import com.ruoyi.web.modules.goods.facade.IGoodsPurchaseFacade;
import com.ruoyi.web.modules.goods.mapper.BizGoodsMapper;
import com.ruoyi.web.modules.goods.vo.GoodsPurchaseVO;
import com.ruoyi.web.modules.store.facade.IAgriShopFacade;

/**
 * 可购买校验
 */
@Service
public class GoodsPurchaseFacadeImpl implements IGoodsPurchaseFacade
{
    @Autowired
    private BizGoodsMapper goodsMapper;

    @Autowired
    private IAgriShopFacade shopFacade;

    @Autowired
    private ICategoryFacade categoryFacade;

    @Override
    public GoodsPurchaseVO canPurchase(Long goodsId)
    {
        GoodsPurchaseVO vo = new GoodsPurchaseVO();
        BizGoods goods = goodsMapper.selectById(goodsId);
        if (goods == null)
        {
            vo.setAllowed(false);
            vo.setReason("商品不存在");
            return vo;
        }
        if (!GoodsConstants.STATUS_ON_SALE.equals(goods.getGoodsStatus()))
        {
            vo.setAllowed(false);
            vo.setReason("商品非出售中");
            return vo;
        }
        if (goods.getStock() == null || goods.getStock() <= 0)
        {
            vo.setAllowed(false);
            vo.setReason("库存不足");
            return vo;
        }
        if (!shopFacade.isShopOpenForOrder(goods.getShopId()))
        {
            vo.setAllowed(false);
            vo.setReason("店铺已停业");
            return vo;
        }
        if (!categoryFacade.isCategoryVisible(goods.getCategoryId()))
        {
            vo.setAllowed(false);
            vo.setReason("商品分类不可见");
            return vo;
        }
        vo.setAllowed(true);
        return vo;
    }
}
