<?php
/**
 * (品牌,分类,sku 信息映射)
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/11/28
 * Time: 11:46
 */

namespace JinDouYun\Cache;

use Mall\Framework\Cache\Redis;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Factory;
use think\Exception;

class GoodsBasicRelevant
{
    /**
     * @var Redis
     */
    private $cache;

    private $onlineEnterpriseId;

    private static $categoryKey = 'categoryIdRelationName';

    private static $brandKey = 'brandIdRelationName';

    private static $skuKey = 'skuIdRelationNameHash';

    private static $basicKey = 'basicIdRelationName';

    private static $skuField = 'skuId';

    private static $warehouseIdKey = 'WarehouseIdRelationName';

    private static $ShopIdKey = 'ShopIdRelationName';


    public function __construct($enterpriseId)
    {
        $this->onlineEnterpriseId = $enterpriseId;
        $this->cache = Factory::cache('mapping');
    }

    /******************************************category start****************************************************************/

    /**
     * 缓存 name => id
     * @param $name
     * @param $categoryId
     * @return mixed
     */
    public function cacheCategoryIdRelationName($name, $categoryId)
    {
        $result = $this->cache->zadd(self::$categoryKey . '::' . $this->onlineEnterpriseId, $categoryId, $name);
        if ($result) {
            return ResultWrapper::success('success');
        } else {
            return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
        }
    }

    /**
     * 通过id获取集合中的name
     * @param $categoryId
     * @return array|string
     */
    public function getNameByCategoryId($categoryId)
    {
        $result = $this->cache->zrangebyscore(self::$categoryKey . '::' . $this->onlineEnterpriseId, $categoryId, $categoryId);
        if (!$result) {
            return '';
        }
        if (is_array($result)){
            return  empty($result) ? '' : array_shift($result);
        }
        return $result;
    }

    /**
     * 返回成员member的score值
     */
    public function getCategoryIdByName($name)
    {
        $result = $this->cache->zscore(self::$categoryKey . '::' . $this->onlineEnterpriseId, $name);
        if (!$result) {
            return '';
        }
        return $result;
    }

    /**
     * 移除指定元素
     * @param $categoryId
     * @return mixed
     */
    public function deleteCategoryKeyById($categoryId)
    {
        $result = $this->cache->zremrangebyscore(self::$categoryKey . '::' . $this->onlineEnterpriseId, $categoryId, $categoryId);
        return $result;
    }

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

    /**
     * 缓存 name => id
     * @param $name
     * @param $brandId
     * @return mixed
     */
    public function cacheBrandIdRelationName($name, $brandId)
    {
        $result = $this->cache->zadd(self::$brandKey . '::' . $this->onlineEnterpriseId, $brandId, $name);
        if ($result) {
            return ResultWrapper::success('success');
        } else {
            return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
        }
    }

    /**
     * 通过id获取集合中的name
     * @param $brandId
     * @return string|array
     */
    public function getNameByBrandId($brandId)
    {
        $result = $this->cache->zrangebyscore(self::$brandKey . '::' . $this->onlineEnterpriseId, $brandId, $brandId);
        if (!$result) {
            return '';
        }
        if (is_array($result)){
            return empty($result) ? '' : array_shift($result);
        }
        return $result;
    }

    /**
     * 移除指定元素
     * @param $brandId
     * @return mixed
     */
    public function deleteBrandKeyById($brandId)
    {
        $result = $this->cache->zremrangebyscore(self::$brandKey . '::' . $this->onlineEnterpriseId, $brandId, $brandId);
        return $result;
    }

    /********************************************sku start**************************************************************/
    /**
     * sku缓存数据
     * @param $name
     * @param $skuId
     * @return mixed
     */
    public function cacheSkuIdRelationName($name, $skuId)
    {
        $result = $this->cache->hset(self::$skuKey.'::'.$this->onlineEnterpriseId,self::$skuField.'::'.$skuId,$name);
        return $result;
    }

    /**
     * 根据skuId获取名称
     * @param $skuId
     * @return mixed
     */
    public function getNameBySkuId($skuId)
    {
        $result = $this->cache->hget(self::$skuKey.'::'.$this->onlineEnterpriseId,self::$skuField.'::'.$skuId);
        if (!$result){
            return '';
        }
        return $result;
    }

    /**
     * 移除指定元素
     * @param $skuId
     * @return mixed
     */
    public function deleteSkuKeyById($skuId)
    {
        $result = $this->cache->zremrangebyscore(self::$skuKey . '::' . $this->onlineEnterpriseId, $skuId, $skuId);
        return $result;
    }

    /***********************************************basic start*********************************************************/
    /**
     * Model/Goods/BasicAndSkuCache 中调用了
     * todo(方法名称不符合规范。。。。。。。。。。)
     * mapping
     * 缓存 data => id
     * @param $data
     * @param $basicId
     * @return ResultWrapper
     */
    public function cacheBasicIdRelationName(array $data,int $basicId)
    {
        $data = gzcompress(serialize($data));
        $result = $this->cache->hset(self::$basicKey . '::' . $this->onlineEnterpriseId, $basicId, $data);
        if ($result) {
            return ResultWrapper::success('添加缓存成功');
        } else {
            return ResultWrapper::fail('添加缓存失败', ErrorCode::$redisWriteError);
        }
    }

    /**
     * $fields
     * true : 返回所有字段信息(array)
     * field : 返回对应字段值 (string)
     *
     * todo(方法名称不符合规范。。。。。。。。。。)
     * 根据商品id,获取商品信息
     * @param $basicId
     * @param string $fields
     * @return array|string
     */
    public function getNameByBasicId(int $basicId, $fields = 'title')
    {
        $result = $this->cache->hget(self::$basicKey . '::' . $this->onlineEnterpriseId, $basicId);
        if (!$result) {
            if($fields === true || $fields == 'images') return [];
            return '';
        }
        $result = unserialize(zlib_decode($result));
        $result['images'] = is_array($result['images']) ? $result['images'] : json_decode($result['images'], true);
        $result['extends'] = empty($result['extends']) ? [] : json_decode($result['extends'],true);
        if($fields === true) return $result;
        if (isset($result[$fields])){
            return $result[$fields];
        }
        return '';
    }

    /**
     * @param $basicId
     * @return mixed
     */
    public function deleteBasicKeyById(int $basicId)
    {
        return $this->cache->hdel(self::$basicKey . '::' . $this->onlineEnterpriseId, $basicId);
    }

    /***************************************仓库数据 缓存
     * 增加
     * @param $warehouseId
     * @param $warehouseData
     * @return ResultWrapper
     */
    public function cacheWarehouseIdRelationName($warehouseId,$warehouseData)
    {
        $data = json_encode($warehouseData);
        $result = $this->cache->hset(self::$warehouseIdKey . '::' . $this->onlineEnterpriseId, $warehouseId, $data);
        if (!$result) {
            return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
        }
        return ResultWrapper::success('success');
    }

    /**
     * 查询
     * @param $warehouseId
     * @param bool $type
     * @return mixed|string
     */
    public function getNameByWarehouseId($warehouseId, $type = false)
    {
        $result = $this->cache->hget(self::$warehouseIdKey.'::'.$this->onlineEnterpriseId, $warehouseId);
        if (!$result) return '';
        if($type){
            return json_decode($result, true);
        }
        return json_decode($result, true)['warehouseName'];
    }

    /**
     * 删除
     * @param $warehouseId
     */
    public function delNameByWarehouseId($warehouseId)
    {
        $this->cache->hdel(self::$warehouseIdKey.'::'.$this->onlineEnterpriseId, $warehouseId);
    }

    /***************************************商铺
     *
     * 添加
     * @param $ShopId
     * @param $warehouseData
     * @return ResultWrapper
     */
    public function cacheShopIdRelationName($ShopId,$warehouseData)
    {
        $data = json_encode($warehouseData);
        $result = $this->cache->hset(self::$ShopIdKey . '::' . $this->onlineEnterpriseId, $ShopId, $data);
        if (!$result) {
            return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
        }
        return ResultWrapper::success('success');
    }

    /**
     * 查询
     * @param $ShopId
     * @param bool $type
     * @return mixed|string
     */
    public function getNameByShopId($ShopId, $type = false)
    {
        $result = $this->cache->hget(self::$ShopIdKey.'::'.$this->onlineEnterpriseId, $ShopId);
        if (!$result) return '';
        if($type){
            return json_decode($result, true);
        }
        return json_decode($result, true)['name'];
    }

    /**
     * 删除
     * @param $ShopId
     */
    public function delNameByShopId($ShopId)
    {
        $this->cache->hdel(self::$ShopIdKey.'::'.$this->onlineEnterpriseId, $ShopId);
    }
}