<?php
/**
 * 缓存企业与小程序数据
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/12/9
 * Time: 16:31
 */

namespace JinDouYun\Cache;


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

/**
 * Class SystemCache
 * @package JinDouYun\Cache
 */
class SystemCache
{
    private static $enterpriseRelationWxAppIdKey = 'enterpriseRelationWxAppIdMapping';

    private static $paymentKey = 'paymentMapping';//支付配置

    private static $byteDanceSettingKey = 'byteDanceSettingMapping';//字节跳动配置

    private static $printNumKey = 'printNum';//打印次数

    private static $uriRelNameKey = 'uriRelName';//url=>name

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

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

    /**
     * @param $data
     * @param $enterpriseId
     * @return mixed
     */
    public function cacheEnterpriseRelationWxAppId($data, $enterpriseId)
    {
        $result = $this->cache->hset(self::$enterpriseRelationWxAppIdKey, $enterpriseId, $data);
        return $result;
    }

    /**
     * 获取企业授权的小程序信息
     * @param $enterpriseId
     * @return string
     */
    public function getAppIdByEnterpriseId($enterpriseId)
    {
        $result = $this->cache->hget(self::$enterpriseRelationWxAppIdKey, $enterpriseId);
        if (!$result) {
            return '';
        }
        return $result;
    }

    /**
     * 缓存支付配置
     * @param $data
     * @param $enterpriseId
     * @return mixed
     */
    public function cachePayment($data, $enterpriseId)
    {
        if (is_array($data)) {
            $data = json_encode($data);
        }
        $result = $this->cache->hset(self::$paymentKey, $enterpriseId, $data);
        return $result;
    }

    /**
     * @param $enterpriseId
     * @param string $type
     * @return array|mixed
     */
    public function getPayment($enterpriseId)
    {
        $result = $this->cache->hget(self::$paymentKey, $enterpriseId);
        if (!$result) {
            return [];
        }
        if (!empty($result)) {
            $result = json_decode($result, true);
        }
        return $result;
    }

    /**
     * 字节跳动设置
     * @param string $data
     * @param int $enterpriseId
     * @return bool|int
     */
    public function cacheByteDanceSetting(string $data, int $enterpriseId)
    {
        return $this->cache->hset(self::$byteDanceSettingKey, $enterpriseId, $data);
    }

    /**
     * 获取字节跳动设置
     * @param int $enterpriseId
     * @return string
     */
    public function getByteDanceSetting(int $enterpriseId)
    {
        $result = $this->cache->hget(self::$byteDanceSettingKey, $enterpriseId);
        if (!$result) {
            return '';
        }
        return $result;
    }

    /**
     * Doc: (des="获取打印次数")
     * User: XMing
     * Date: 2020/11/14
     * Time: 11:48 上午
     * @param int $enterpriseId
     * @param $no
     * @return int
     */
    public function getObjectPrintNum(int $enterpriseId,$no)
    {
        return $this->cache->zscore(self::$printNumKey.'::'.$enterpriseId,$no);
    }

    /**
     * Doc: (des="删除打印次数")
     * User: XMing
     * Date: 2020/11/14
     * Time: 12:05 下午
     * @param int $enterpriseId
     * @param $no
     * @return bool
     */
    public function delObjectPrintNum(int $enterpriseId,$no)
    {
        return $this->cache->zrem(self::$printNumKey.'::'.$enterpriseId,$no);
    }



    /**
     * Doc: (des="设置打印次数")
     * User: XMing
     * Date: 2020/11/14
     * Time: 11:49 上午
     * @param int $enterpriseId
     * @param $no
     * @param int $value
     * @return bool
     */
    public function setObjectPrintNum(int $enterpriseId,$no,$value)
    {
        return $this->cache->zadd(self::$printNumKey.'::'.$enterpriseId,$value,$no);
    }

    /**
     * Doc: (des="混存url与name")
     * User: XMing
     * Date: 2020/11/14
     * Time: 6:47 下午
     * @param string $uri
     * @param string $name
     */
    public function setUriRelName(string $uri,string $name)
    {
        return $this->cache->hset(self::$uriRelNameKey,strtolower($uri),$name);
    }

    /**
     * Doc: (des="删除uri")
     * User: XMing
     * Date: 2020/11/14
     * Time: 6:51 下午
     * @param string $uri
     */
    public function delUriRelName(string $uri)
    {
        return $this->cache->hdel(self::$uriRelNameKey,strtolower($uri));
    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2020/11/14
     * Time: 6:53 下午
     * @param string $uri
     */
    public function getUriRelName(string $uri)
    {
        $name = $this->cache->hget(self::$uriRelNameKey,strtolower($uri));
        if ($name == false){
            return '';
        }
        return $name;
    }
}