<?php
/**
 * 商户cache添加
 * Created by PhpStorm.
 * User: haoren
 * Date: 2020/04/09
 * Time: 16:00
 */

namespace JinDouYun\Cache;

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

class MerchantCache
{
    private $cache;

    private $enterpriseId;

    private $userCenterId;

    private $cacheKey = 'Merchant';

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

    /**---------------------------------------------商户------------------------------------------------**/

    /**
     * 添加
     * @param $merchantId
     * @param $merchantData
     * @return bool
     */
    public function addMerchant($merchantId, $merchantData)
    {
        $data = json_encode($merchantData);
        $result = $this->cache->hset($this->cacheKey, $merchantId, $data);
        if(!$result){
            return false;
        }
        return true;
    }

    /**
     * 查询
     * @param $merchantId
     * @param $key
     * @return array
     */
    public function getMerchant($merchantId, $key = false)
    {
        $result = $this->cache->hget($this->cacheKey, $merchantId);
        if (!$result) return [];
        $returnData = json_decode($result, true);
        if($key){
            return $returnData[$key];
        }
        return $returnData;
    }

    /**
     * 删除
     * @param $merchantId
     */
    public function delMerchant($merchantId = false)
    {
        if($merchantId){
            $this->cache->hdel($this->cacheKey, $merchantId);
        }else{
            $this->cache->del($this->cacheKey);
        }
    }

}