<?php

namespace JinDouYun\Cache;


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

class SkuCache
{

    private $onlineEnterpriseId;

    private static $skuKey = 'skuHash';

    private static $goodsBasicSkuKey = 'goodsBasicSku';

    /**
     * @var Redis
     */
    private $cache;

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

    /**
     * @param int $skuId
     * @param array $data
     * @return ResultWrapper
     */
    public function cacheSku(int $skuId,array $data)
    {
        $data = gzcompress(serialize($data));
        $result = $this->cache->hset(self::$skuKey. '::' . $this->onlineEnterpriseId, $skuId, $data);
        if ($result) {
            return ResultWrapper::success('添加缓存成功');
        } else {
            return ResultWrapper::fail('添加缓存失败', ErrorCode::$redisWriteError);
        }
    }

    /**
     * $fields
     * true : 返回所有字段信息（array）
     * field : 返回对应字段值 （string）
     * @param int $skuId
     * @param string $fields
     * @return array|bool|mixed|string
     */
    public function getSku(int $skuId, $fields='unitName')
    {
        $result = $this->cache->hget(self::$skuKey . '::' . $this->onlineEnterpriseId, $skuId);
        if(empty($result) || $result == false) return '';
        $result = unserialize(zlib_decode($result));
        $result['extends'] = empty($result['extends']) ? [] : json_decode($result['extends'],true);
        if($fields === true){
            return $result;
        }
        if (isset($result[$fields])){
            return $result[$fields];
        }
        return '';
    }

    /**
     * @param int $skuId
     * @return bool|int
     */
    public function delSku(int $skuId)
    {
        return $this->cache->hdel(self::$skuKey . '::' . $this->onlineEnterpriseId, $skuId);
    }

    /********************************以商品基础资料id为key*******************************/
    /**
     * 添加sku
     * @param int $goodsBasicId
     * @param array $data
     * @return ResultWrapper
     */
    public function addGoodsBasicSku(int $goodsBasicId,array $data)
    {
        $data = gzcompress(serialize($data));
        $result = $this->cache->hset(self::$goodsBasicSkuKey. '::' . $this->onlineEnterpriseId, $goodsBasicId, $data);
        if ($result) {
            return ResultWrapper::success('添加缓存成功');
        } else {
            return ResultWrapper::fail('添加缓存失败', ErrorCode::$redisWriteError);
        }
    }

    /**
     * 获取sku
     * true : 返回所有字段信息（array）
     * field : 返回对应字段值 （string）
     * @param int $goodsBasicId
     * @return array|bool|mixed|string
     */
    public function getGoodsBasicSku($goodsBasicId)
    {
        $result = $this->cache->hget(self::$goodsBasicSkuKey . '::' . $this->onlineEnterpriseId, $goodsBasicId);
        if(empty($result) || $result == false) return [];
        $result = unserialize(zlib_decode($result));
        return $result;
    }

    /**
     * 删除sku
     * @param int $goodsBasicId
     * @return bool|int
     */
    public function delGoodsBasicSku(int $goodsBasicId)
    {
        return $this->cache->hdel(self::$goodsBasicSkuKey . '::' . $this->onlineEnterpriseId, $goodsBasicId);
    }
}