<?php
/**
 * Created by PhpStorm.
 * User: wxj
 * Date: 2020-03-24
 * Time: 15:11
 */

namespace JinDouYun\Cache;

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

class TempSaveCache
{
    private $cache;
    protected $vipCardTemporarySaveKey = 'vipCardTempSave';//会员卡暂存
    protected $goodsTemporarySaveKey = 'goodsTempSave';//商品暂存
    protected $customerTempSaveKey = 'customerTempSave';//客户暂存
    protected $shopTempSaveKey = 'shopTempSave';//商铺暂存

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

    /**
     * 会员卡暂存
     * @param $enterpriseId
     * @param $userCenterId
     * @param $data
     * @return ResultWrapper
     */
    public function saveVipCard($enterpriseId, $userCenterId, $data)
    {
        if (empty($data)) {
            return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
        }
        return $this->cache->hset($this->vipCardTemporarySaveKey . '::' . $enterpriseId, $userCenterId, json_encode($data));
    }

    /**
     * 获取会员卡暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     * @return boolean
     */
    public function getVipCard($enterpriseId, $userCenterId)
    {
        $data = $this->cache->hget($this->vipCardTemporarySaveKey . '::' . $enterpriseId, $userCenterId);
        return $data ? json_decode($data, true) : [];
    }

    /**
     * 删除会员卡暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     */
    public function delVipCard($enterpriseId, $userCenterId)
    {
        $result = $this->cache->hdel($this->vipCardTemporarySaveKey . '::' . $enterpriseId, $userCenterId);
    }

    /**
     * 客户暂存
     * @param $enterpriseId
     * @param $userCenterId
     * @param $data
     * @return ResultWrapper
     */
    public function saveCustomer($enterpriseId, $userCenterId, $data)
    {
        if (empty($data)) {
            return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
        }
        return $this->cache->hset($this->customerTempSaveKey . '::' . $enterpriseId, $userCenterId, json_encode($data));
    }

    /**
     * 获取客户暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     * @return boolean
     */
    public function getCustomer($enterpriseId, $userCenterId)
    {
        $data = $this->cache->hget($this->customerTempSaveKey . '::' . $enterpriseId, $userCenterId);
        return $data ? json_decode($data, true) : [];
    }

    /**
     * 删除客户暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     */
    public function delCustomer($enterpriseId, $userCenterId)
    {
        $result = $this->cache->hdel($this->customerTempSaveKey . '::' . $enterpriseId, $userCenterId);
    }

    /**
     * 商铺暂存
     * @param $enterpriseId
     * @param $userCenterId
     * @param $data
     * @return ResultWrapper
     */
    public function saveShop($enterpriseId, $userCenterId, $data)
    {
        if (empty($data)) {
            return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
        }
        return $this->cache->hset($this->shopTempSaveKey . '::' . $enterpriseId, $userCenterId, json_encode($data));
    }

    /**
     * 获取商铺暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     * @return boolean
     */
    public function getShop($enterpriseId, $userCenterId)
    {
        $data = $this->cache->hget($this->shopTempSaveKey . '::' . $enterpriseId, $userCenterId);
        return $data ? json_decode($data, true) : [];
    }

    /**
     * 删除商铺暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     */
    public function delShop($enterpriseId, $userCenterId)
    {
        $result = $this->cache->hdel($this->shopTempSaveKey . '::' . $enterpriseId, $userCenterId);
    }

    /**
     * 商品暂存
     * @param $enterpriseId
     * @param $userCenterId
     * @param $data
     * @return ResultWrapper
     */
    public function saveGoods($enterpriseId, $userCenterId, $data)
    {
        if (empty($data)) {
            return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
        }
        return $this->cache->hset($this->goodsTemporarySaveKey . '::' . $enterpriseId, $userCenterId, json_encode($data));
    }

    /**
     * 获取商品暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     * @return boolean
     */
    public function getGoods($enterpriseId, $userCenterId)
    {
        $data = $this->cache->hget($this->goodsTemporarySaveKey . '::' . $enterpriseId, $userCenterId);
        return $data ? json_decode($data, true) : [];
    }

    /**
     * 删除商品暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     */
    public function delGoods($enterpriseId, $userCenterId)
    {
        $result = $this->cache->hdel($this->goodsTemporarySaveKey . '::' . $enterpriseId, $userCenterId);
    }
}