<?php

namespace JinDouYun\Cache;

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

/**
 * 价格缓存
 * Class PriceCache
 * @package JinDouYun\Cache
 */
class PriceCache
{
    /**
     * 全国价缓存key
     * @var string
     */
    private static $nationSalePriceTableKey = 'nationSalePrice::';

    /**
     * 客户调价信息缓存
     * @var string
     */
    private static $customerAdjTableKey = 'customerAdj::';

    /**
     * 客户类型调价信息
     * @var string
     */
    private static $customerTypeAdiTableKey = 'customerTypeAdi::';

    /**
     * @var string 价格波动
     */
    private static $priceTrend = 'priceTrend_';

    /**
     * @var integer
     */
    private $onlineEnterpriseId;

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

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

    /**********缓存NationSalePrice数据***************/
    /**
     *
     * 缓存全国价 goodsId=>$value
     * @param int $goodsId
     * @param $value
     * @return bool|int
     */
    public function setNationSalePrice(int $goodsId, $value)
    {
        return $this->cache->hset(self::$nationSalePriceTableKey . $this->onlineEnterpriseId, $goodsId, json_encode($value));
    }

    /**
     *
     * 获取全国价 goodsId=>$value
     * @param int $goodsId
     * @return mixed
     */
    public function getNationSalePrice(int $goodsId)
    {
        $result = $this->cache->hget(self::$nationSalePriceTableKey . $this->onlineEnterpriseId, $goodsId);
        if (empty($result)) {
            return [];
        }
        $result = json_decode($result, true);
        return $result;
    }

    /**
     * 删除全国价 goodsId=>$value
     * @param int $goodsId
     * @return bool|int
     */
    public function delNationSalePrice(int $goodsId)
    {
        return $this->cache->hdel(self::$nationSalePriceTableKey . $this->onlineEnterpriseId, $goodsId);
    }

    /**********缓存客户调价信息数据***************/

    /**
     * 缓存客户类型价格信息
     * filed 客户id_商品id
     * @param int $goodsId
     * @param int $customerId
     * @param $value
     * @return bool|int
     */
    public function setCustomerAdj(int $goodsId, int $customerId, $value)
    {
        return $this->cache->hset(self::$customerAdjTableKey . $this->onlineEnterpriseId, $customerId . '_' . $goodsId, json_encode($value));
    }

    /**
     * 获取客户调价信息
     * @param int $goodsId
     * @param int $customerId
     * @return array|mixed|string
     */
    public function getCustomerAdj(int $goodsId, int $customerId)
    {
        $result = $this->cache->hget(self::$customerAdjTableKey . $this->onlineEnterpriseId, $customerId . '_' . $goodsId);
        if (empty($result)) {
            return [];
        }
        $result = json_decode($result, true);
        return $result;
    }

    /**
     * 删除客户调价信息
     * @param int $goodsId
     * @param int $customerId
     * @return bool|int
     */
    public function delCustomerAdj(int $goodsId,int $customerId)
    {
        return $this->cache->hdel(self::$customerAdjTableKey . $this->onlineEnterpriseId, $customerId . '_' . $goodsId);
    }

    /*****缓存客户类型调价信息*******/
    /**
     * 缓存客户类型调价信息
     * @param int $goodsId
     * @param int $customerType
     * @param $value
     * @return bool|int
     */
    public function setCustomerTypeAdj(int $goodsId, int $customerType, $value)
    {
        return $this->cache->hset(self::$customerTypeAdiTableKey.$this->onlineEnterpriseId,$customerType.'_'.$goodsId,json_encode($value));
    }

    /**
     * 获取客户类型调价信息
     * @param int $goodsId
     * @param int $customerType
     * @return array|mixed|string
     */
    public function getCustomerTypeAdj(int $goodsId, int $customerType)
    {
        $result = $this->cache->hget(self::$customerTypeAdiTableKey . $this->onlineEnterpriseId, $customerType . '_' . $goodsId);
        if (empty($result)) {
            return [];
        }
        $result = json_decode($result, true);
        return $result;
    }

    /**
     * 删除客户类型调价信息
     * @param int $goodsId
     * @param int $customerType
     * @return bool|int
     */
    public function delCustomerTypeAdj(int $goodsId, int $customerType)
    {
        return $this->cache->hdel(self::$customerTypeAdiTableKey . $this->onlineEnterpriseId, $customerType . '_' . $goodsId);
    }

    /**
     * Doc: (des="缓存价格波动数据")
     * User: XMing
     * Date: 2021/3/11
     * Time: 12:02 下午
     * @param int $goodsId
     * @param string $value
     * @param int $ttl
     * @return bool|mixed
     */
    public function setPriceTrend(int $goodsId,string $value,int $ttl = 60): bool
    {
        return $this->cache->set(self::$priceTrend . $this->onlineEnterpriseId.'_'.$goodsId,$value,$ttl);
    }

    /**
     * Doc: (des="获取缓存价格波动数据")
     * User: XMing
     * Date: 2021/3/11
     * Time: 12:16 下午
     * @param int $goodsId
     */
    public function getPriceTrend(int $goodsId)
    {
        return [];
        $result = $this->cache->get(self::$priceTrend . $this->onlineEnterpriseId.'_'.$goodsId);
        if (empty($result)){
            return [];
        }
        return $result;
    }
}