<?php
/**
 * 缓存基础商品数据
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/11/25
 * Time: 12:05
 */

namespace JinDouYun\Cache;

use Mall\Framework\Cache\Redis;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Factory;

/**
 * todo(这个类中有很多没用的缓存,之后清理)
 * Class GoodsBasicCache
 * @package JinDouYun\Cache
 */
class GoodsBasicCache
{
    /**
     * @var Redis
     */
    private $cache;

    private $onlineEnterpriseId;

    protected $categoryKey = 'categoryAndBasicNum';

    protected $brandKey = 'brandAndBasicNum';

    /**
     * GoodsBasicCache constructor.
     * @param $enterpriseId
     * @throws \Exception
     */
    public function __construct($enterpriseId)
    {
        $this->onlineEnterpriseId = $enterpriseId;
        $this->cache = Factory::cache('default');
    }

    /**
     * @param $categoryId
     * @param $num
     * @return ResultWrapper
     */
    public function cacheCategoryRelationGoodsBasicNum($categoryId, $num)
    {
        $result = $this->cache->zadd($this->categoryKey.'::'.$this->onlineEnterpriseId, $num, $categoryId);
        return $result;
    }

    /**
     * @param $categoryId
     * @return mixed
     */
    public function categoryKeyScoreIncr($categoryId)
    {
        $result = $this->cache->zincrby($this->categoryKey.'::'.$this->onlineEnterpriseId, 1, $categoryId);
        return $result;
    }

    /**
     * @param $categoryId
     * @return mixed
     */
    public function categoryKeyScoreDecr($categoryId)
    {
        $result = $this->cache->zincrby($this->categoryKey.'::'.$this->onlineEnterpriseId, -1, $categoryId);
        return $result;
    }

    /**
     * @param $categoryId
     * @return int
     */
    public function getCategoryGoodsBasicNum($categoryId)
    {
        $result = $this->cache->zscore($this->categoryKey.'::'.$this->onlineEnterpriseId, $categoryId);
        return $result;
    }

    /**********************************************brand start*****************************************************************/

    /**
     * @param $brandId
     * @param $num
     * @return mixed
     */
    public function cacheBrandRelationGoodsBasicNum($brandId,$num)
    {
        $result = $this->cache->zadd($this->brandKey.'::'.$this->onlineEnterpriseId, $num, $brandId);
        if ($result == false){
            return 0;
        }
        return $result;
    }


    /**
     * @param $brandId
     * @return mixed
     */
    public function brandKeyScoreIncr($brandId)
    {
        $result = $this->cache->zincrby($this->brandKey.'::'.$this->onlineEnterpriseId, 1, $brandId);
        return $result;
    }

    /**
     * @param $brandId
     * @return mixed
     */
    public function brandKeyScoreDecr($brandId)
    {
        $result = $this->cache->zincrby($this->brandKey.'::'.$this->onlineEnterpriseId, -1, $brandId);
        return $result;
    }

    /**
     * @param $brandId
     * @return mixed
     */
    public function getBrandGoodsBasicNum($brandId)
    {
        $result = $this->cache->zscore($this->brandKey.'::'.$this->onlineEnterpriseId, $brandId);
        return $result;
    }
}