123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <?php
- /**
- * (品牌,分类,sku 信息映射)
- * Created by PhpStorm.
- * User: XiaoMing
- * Date: 2019/11/28
- * Time: 11:46
- */
- namespace JinDouYun\Cache;
- use Mall\Framework\Cache\Redis;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Factory;
- use think\Exception;
- class GoodsBasicRelevant
- {
- /**
- * @var Redis
- */
- private $cache;
- private $onlineEnterpriseId;
- private static $categoryKey = 'categoryIdRelationName';
- private static $brandKey = 'brandIdRelationName';
- private static $skuKey = 'skuIdRelationNameHash';
- private static $basicKey = 'basicIdRelationName';
- private static $skuField = 'skuId';
- private static $warehouseIdKey = 'WarehouseIdRelationName';
- private static $ShopIdKey = 'ShopIdRelationName';
- public function __construct($enterpriseId)
- {
- $this->onlineEnterpriseId = $enterpriseId;
- $this->cache = Factory::cache('mapping');
- }
- /******************************************category start****************************************************************/
- /**
- * 缓存 name => id
- * @param $name
- * @param $categoryId
- * @return mixed
- */
- public function cacheCategoryIdRelationName($name, $categoryId)
- {
- $result = $this->cache->zadd(self::$categoryKey . '::' . $this->onlineEnterpriseId, $categoryId, $name);
- if ($result) {
- return ResultWrapper::success('success');
- } else {
- return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
- }
- }
- /**
- * 通过id获取集合中的name
- * @param $categoryId
- * @return array|string
- */
- public function getNameByCategoryId($categoryId)
- {
- $result = $this->cache->zrangebyscore(self::$categoryKey . '::' . $this->onlineEnterpriseId, $categoryId, $categoryId);
- if (!$result) {
- return '';
- }
- if (is_array($result)){
- return empty($result) ? '' : array_shift($result);
- }
- return $result;
- }
- /**
- * 返回成员member的score值
- */
- public function getCategoryIdByName($name)
- {
- $result = $this->cache->zscore(self::$categoryKey . '::' . $this->onlineEnterpriseId, $name);
- if (!$result) {
- return '';
- }
- return $result;
- }
- /**
- * 移除指定元素
- * @param $categoryId
- * @return mixed
- */
- public function deleteCategoryKeyById($categoryId)
- {
- $result = $this->cache->zremrangebyscore(self::$categoryKey . '::' . $this->onlineEnterpriseId, $categoryId, $categoryId);
- return $result;
- }
- /************************************************brand start**********************************************************/
- /**
- * 缓存 name => id
- * @param $name
- * @param $brandId
- * @return mixed
- */
- public function cacheBrandIdRelationName($name, $brandId)
- {
- $result = $this->cache->zadd(self::$brandKey . '::' . $this->onlineEnterpriseId, $brandId, $name);
- if ($result) {
- return ResultWrapper::success('success');
- } else {
- return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
- }
- }
- /**
- * 通过id获取集合中的name
- * @param $brandId
- * @return string|array
- */
- public function getNameByBrandId($brandId)
- {
- $result = $this->cache->zrangebyscore(self::$brandKey . '::' . $this->onlineEnterpriseId, $brandId, $brandId);
- if (!$result) {
- return '';
- }
- if (is_array($result)){
- return empty($result) ? '' : array_shift($result);
- }
- return $result;
- }
- /**
- * 移除指定元素
- * @param $brandId
- * @return mixed
- */
- public function deleteBrandKeyById($brandId)
- {
- $result = $this->cache->zremrangebyscore(self::$brandKey . '::' . $this->onlineEnterpriseId, $brandId, $brandId);
- return $result;
- }
- /********************************************sku start**************************************************************/
- /**
- * sku缓存数据
- * @param $name
- * @param $skuId
- * @return mixed
- */
- public function cacheSkuIdRelationName($name, $skuId)
- {
- $result = $this->cache->hset(self::$skuKey.'::'.$this->onlineEnterpriseId,self::$skuField.'::'.$skuId,$name);
- return $result;
- }
- /**
- * 根据skuId获取名称
- * @param $skuId
- * @return mixed
- */
- public function getNameBySkuId($skuId)
- {
- $result = $this->cache->hget(self::$skuKey.'::'.$this->onlineEnterpriseId,self::$skuField.'::'.$skuId);
- if (!$result){
- return '';
- }
- return $result;
- }
- /**
- * 移除指定元素
- * @param $skuId
- * @return mixed
- */
- public function deleteSkuKeyById($skuId)
- {
- $result = $this->cache->zremrangebyscore(self::$skuKey . '::' . $this->onlineEnterpriseId, $skuId, $skuId);
- return $result;
- }
- /***********************************************basic start*********************************************************/
- /**
- * Model/Goods/BasicAndSkuCache 中调用了
- * todo(方法名称不符合规范。。。。。。。。。。)
- * mapping
- * 缓存 data => id
- * @param $data
- * @param $basicId
- * @return ResultWrapper
- */
- public function cacheBasicIdRelationName(array $data,int $basicId)
- {
- $data = gzcompress(serialize($data));
- $result = $this->cache->hset(self::$basicKey . '::' . $this->onlineEnterpriseId, $basicId, $data);
- if ($result) {
- return ResultWrapper::success('添加缓存成功');
- } else {
- return ResultWrapper::fail('添加缓存失败', ErrorCode::$redisWriteError);
- }
- }
- /**
- * $fields
- * true : 返回所有字段信息(array)
- * field : 返回对应字段值 (string)
- *
- * todo(方法名称不符合规范。。。。。。。。。。)
- * 根据商品id,获取商品信息
- * @param $basicId
- * @param string $fields
- * @return array|string
- */
- public function getNameByBasicId(int $basicId, $fields = 'title')
- {
- $result = $this->cache->hget(self::$basicKey . '::' . $this->onlineEnterpriseId, $basicId);
- if (!$result) {
- if($fields === true || $fields == 'images') return [];
- return '';
- }
- $result = unserialize(zlib_decode($result));
- $result['images'] = is_array($result['images']) ? $result['images'] : json_decode($result['images'], true);
- $result['extends'] = empty($result['extends']) ? [] : json_decode($result['extends'],true);
- if($fields === true) return $result;
- if (isset($result[$fields])){
- return $result[$fields];
- }
- return '';
- }
- /**
- * @param $basicId
- * @return mixed
- */
- public function deleteBasicKeyById(int $basicId)
- {
- return $this->cache->hdel(self::$basicKey . '::' . $this->onlineEnterpriseId, $basicId);
- }
- /***************************************仓库数据 缓存
- * 增加
- * @param $warehouseId
- * @param $warehouseData
- * @return ResultWrapper
- */
- public function cacheWarehouseIdRelationName($warehouseId,$warehouseData)
- {
- $data = json_encode($warehouseData);
- $result = $this->cache->hset(self::$warehouseIdKey . '::' . $this->onlineEnterpriseId, $warehouseId, $data);
- if (!$result) {
- return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
- }
- return ResultWrapper::success('success');
- }
- /**
- * 查询
- * @param $warehouseId
- * @param bool $type
- * @return mixed|string
- */
- public function getNameByWarehouseId($warehouseId, $type = false)
- {
- $result = $this->cache->hget(self::$warehouseIdKey.'::'.$this->onlineEnterpriseId, $warehouseId);
- if (!$result) return '';
- if($type){
- return json_decode($result, true);
- }
- return json_decode($result, true)['warehouseName'];
- }
- /**
- * 删除
- * @param $warehouseId
- */
- public function delNameByWarehouseId($warehouseId)
- {
- $this->cache->hdel(self::$warehouseIdKey.'::'.$this->onlineEnterpriseId, $warehouseId);
- }
- /***************************************商铺
- *
- * 添加
- * @param $ShopId
- * @param $warehouseData
- * @return ResultWrapper
- */
- public function cacheShopIdRelationName($ShopId,$warehouseData)
- {
- $data = json_encode($warehouseData);
- $result = $this->cache->hset(self::$ShopIdKey . '::' . $this->onlineEnterpriseId, $ShopId, $data);
- if (!$result) {
- return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
- }
- return ResultWrapper::success('success');
- }
- /**
- * 查询
- * @param $ShopId
- * @param bool $type
- * @return mixed|string
- */
- public function getNameByShopId($ShopId, $type = false)
- {
- $result = $this->cache->hget(self::$ShopIdKey.'::'.$this->onlineEnterpriseId, $ShopId);
- if (!$result) return '';
- if($type){
- return json_decode($result, true);
- }
- return json_decode($result, true)['name'];
- }
- /**
- * 删除
- * @param $ShopId
- */
- public function delNameByShopId($ShopId)
- {
- $this->cache->hdel(self::$ShopIdKey.'::'.$this->onlineEnterpriseId, $ShopId);
- }
- }
|