PreBase.php 1.83 KB
<?php

/**
 * 商品库存
 * User: ziy
 * Date: 14-7-22
 * Time: 下午6:32
 */
class YHMCart_Hook_Orders_PreBase extends YHMCart_Hook_Abstract
{
    /**
     * @param array $package
     */
    static function run(YHMCart_Hook_Orders $package)
    {
        self::AntiXSRFFilter();
        $package = $package->getOrder();
        self::blackList($package);
        self::checkStorage($package);
    }

    /**
     * 库存验证
     * @param $package
     * @throws Exception
     */
    private static function checkStorage(array $package)
    {
        $goods_list = $package['goods_list'];
        foreach ($goods_list as $g) {
            $stock = YHMProduct_Models_Stock_Client::getStockBySku($g['product_sku']);
             $goodsInfo = YHMProduct_Models_Goods_Client::getOneByProductSkc($g['product_skc']);
            if (empty($stock) || $stock['stock_number'] < $g['buy_number']) {
                $goodsName = '';
                if (!empty($goodsInfo)) {
                    $goodsName = $goodsInfo['goods_name'];
                }
                $txt=$goodsName . '已售罄';
                throw new Exception($txt,400);
            }
            if (!empty($goodsInfo)) {
                    $goodsName = $goodsInfo['goods_name'];
                     if ($goodsInfo['status'] == 0) {
                        $txt = '商品已无效,请联系卖家';
                        throw new Exception($txt, 400);
                    }
                }       
        }
    }


    /**
     * 黑名单
     * @param array $package
     * @throws Exception
     */
    private static function blackList(array $package)
    {

    }

    #TODO 不能购买自己的商品
    private static function aa()
    {

    }

    static function AntiXSRFFilter()
    {
        Q_Core_Session::start('auth_check_submit')->__unset('auth_submit_action');
    }
}