<?php
/**
 * 分销商cache
 * Created by PhpStorm.
 * User: haoren
 * Date: 2020/07/22
 * Time: 15:00
 */

namespace JinDouYun\Cache;


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

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

    private $enterpriseId;

    private $userCenterId;

    private static $settingCacheKey = 'CommissionSetting';

    private static $businessmanGradeCacheKey = 'BusinessmanGrade';

    private static $businessmanStatisticsCacheKey = 'BusinessmanStatistics';

    private static $businessmanSubCacheKey = 'BusinessmanSub';


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

    /**---------------------------------------------分销设置------------------------------------------------**/

    /**
     * 保存设置
     * @param $value
     * @return bool
     */
    public function saveSetting($value)
    {
        $data = json_encode($value);
        $result = $this->cache->hset(self::$settingCacheKey, $this->enterpriseId, $data);
        if (!$result) {
            return false;
        }
        return true;
    }

    /**
     * 查询设置
     * @param bool $field
     * @return mixed|string
     */
    public function getSetting($field = false)
    {
        $result = $this->cache->hget(self::$settingCacheKey, $this->enterpriseId);
        if (!$result) return [];
        $returnData = json_decode($result, true);
        if($field){
            return $returnData[$field];
        }
        return $returnData;
    }

    /**
     * 删除
     */
    public function delSetting()
    {
        $this->cache->hdel(self::$settingCacheKey, $this->enterpriseId);
    }

    /*********************************************** 分销商等级 ************************************************/
    /**
     * 添加
     * @param $userCenterId
     * @param $value $缓存的字段
     * @return mixed
     */
    public function saveBusinessmanGrade($userCenterId, $value)
    {
        $data = json_encode($value);
        return $this->cache->hset(self::$businessmanGradeCacheKey."::".$this->enterpriseId, $userCenterId, $data);
    }


    /**
     * 查询
     * @param $userCenterId
     * @return mixed
     */
    public function getBusinessmanGrade($userCenterId)
    {
        $result = $this->cache->hget(self::$businessmanGradeCacheKey."::".$this->enterpriseId, $userCenterId);
        if (!$result) return [];
        return json_decode($result, true);
    }


    /**
     * 删除
     * @param $userCenterId
     * @return mixed
     */
    public function delBusinessmanGrade($userCenterId)
    {
        if($userCenterId === true){
            return $this->cache->del(self::$businessmanGradeCacheKey."::".$this->enterpriseId);
        }
        return $this->cache->hdel(self::$businessmanGradeCacheKey."::".$this->enterpriseId, $userCenterId);
    }

    /********************************************分销商升级条件统计*********************************************/
    /**
     * 保存
     * @param $userCenterId
     * @param $value
     * @return bool
     */
    public function saveBusinessmanStatistics($userCenterId, $value)
    {
        $data = json_encode($value);
        $result = $this->cache->hset(self::$businessmanStatisticsCacheKey."::".$this->enterpriseId, $userCenterId, $data);
        if (!$result) {
            return false;
        }
        return true;
    }

    /**
     * 查询
     * @param $userCenterId
     * @return mixed
     */
    public function getBusinessmanStatistics($userCenterId)
    {
        $result = $this->cache->hget(self::$businessmanStatisticsCacheKey."::".$this->enterpriseId, $userCenterId);
        if (!$result) return [];
        return json_decode($result, true);
    }


    /**
     * 删除
     * @param $userCenterId
     * @return mixed
     */
    public function delBusinessmanStatistics($userCenterId)
    {
        if($userCenterId === true){
            return $this->cache->del(self::$businessmanStatisticsCacheKey."::".$this->enterpriseId);
        }
        return $this->cache->hdel(self::$businessmanStatisticsCacheKey."::".$this->enterpriseId, $userCenterId);
    }

    /********************************************分销商下级人数统计*********************************************/
    /**
     * 保存
     * @param $userCenterId
     * @param $value
     * @return bool
     */
    public function saveBusinessmanSub($userCenterId, $value)
    {
        $data = json_encode($value);
        return $this->cache->hset(self::$businessmanSubCacheKey."::".$this->enterpriseId, $userCenterId, $data);
    }

    /**
     * 查询
     * @param $userCenterId
     * @return mixed
     */
    public function getBusinessmanSub($userCenterId)
    {
        $result = $this->cache->hget(self::$businessmanSubCacheKey."::".$this->enterpriseId, $userCenterId);
        if (!$result) return [];
        return json_decode($result, true);
    }


    /**
     * 删除
     * @param $userCenterId
     * @return mixed
     */
    public function delBusinessmanSub($userCenterId)
    {
        if($userCenterId === true){
            return $this->cache->del(self::$businessmanSubCacheKey."::".$this->enterpriseId);
        }
        return $this->cache->hdel(self::$businessmanSubCacheKey."::".$this->enterpriseId, $userCenterId);
    }

}