GoodsBasicRelevant.Class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * (品牌,分类,sku 信息映射)
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/28
  7. * Time: 11:46
  8. */
  9. namespace JinDouYun\Cache;
  10. use Mall\Framework\Cache\Redis;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use Mall\Framework\Factory;
  14. use think\Exception;
  15. class GoodsBasicRelevant
  16. {
  17. /**
  18. * @var Redis
  19. */
  20. private $cache;
  21. private $onlineEnterpriseId;
  22. private static $categoryKey = 'categoryIdRelationName';
  23. private static $brandKey = 'brandIdRelationName';
  24. private static $skuKey = 'skuIdRelationNameHash';
  25. private static $basicKey = 'basicIdRelationName';
  26. private static $skuField = 'skuId';
  27. private static $warehouseIdKey = 'WarehouseIdRelationName';
  28. private static $ShopIdKey = 'ShopIdRelationName';
  29. public function __construct($enterpriseId)
  30. {
  31. $this->onlineEnterpriseId = $enterpriseId;
  32. $this->cache = Factory::cache('mapping');
  33. }
  34. /******************************************category start****************************************************************/
  35. /**
  36. * 缓存 name => id
  37. * @param $name
  38. * @param $categoryId
  39. * @return mixed
  40. */
  41. public function cacheCategoryIdRelationName($name, $categoryId)
  42. {
  43. $result = $this->cache->zadd(self::$categoryKey . '::' . $this->onlineEnterpriseId, $categoryId, $name);
  44. if ($result) {
  45. return ResultWrapper::success('success');
  46. } else {
  47. return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
  48. }
  49. }
  50. /**
  51. * 通过id获取集合中的name
  52. * @param $categoryId
  53. * @return array|string
  54. */
  55. public function getNameByCategoryId($categoryId)
  56. {
  57. $result = $this->cache->zrangebyscore(self::$categoryKey . '::' . $this->onlineEnterpriseId, $categoryId, $categoryId);
  58. if (!$result) {
  59. return '';
  60. }
  61. if (is_array($result)){
  62. return empty($result) ? '' : array_shift($result);
  63. }
  64. return $result;
  65. }
  66. /**
  67. * 返回成员member的score值
  68. */
  69. public function getCategoryIdByName($name)
  70. {
  71. $result = $this->cache->zscore(self::$categoryKey . '::' . $this->onlineEnterpriseId, $name);
  72. if (!$result) {
  73. return '';
  74. }
  75. return $result;
  76. }
  77. /**
  78. * 移除指定元素
  79. * @param $categoryId
  80. * @return mixed
  81. */
  82. public function deleteCategoryKeyById($categoryId)
  83. {
  84. $result = $this->cache->zremrangebyscore(self::$categoryKey . '::' . $this->onlineEnterpriseId, $categoryId, $categoryId);
  85. return $result;
  86. }
  87. /************************************************brand start**********************************************************/
  88. /**
  89. * 缓存 name => id
  90. * @param $name
  91. * @param $brandId
  92. * @return mixed
  93. */
  94. public function cacheBrandIdRelationName($name, $brandId)
  95. {
  96. $result = $this->cache->zadd(self::$brandKey . '::' . $this->onlineEnterpriseId, $brandId, $name);
  97. if ($result) {
  98. return ResultWrapper::success('success');
  99. } else {
  100. return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
  101. }
  102. }
  103. /**
  104. * 通过id获取集合中的name
  105. * @param $brandId
  106. * @return string|array
  107. */
  108. public function getNameByBrandId($brandId)
  109. {
  110. $result = $this->cache->zrangebyscore(self::$brandKey . '::' . $this->onlineEnterpriseId, $brandId, $brandId);
  111. if (!$result) {
  112. return '';
  113. }
  114. if (is_array($result)){
  115. return empty($result) ? '' : array_shift($result);
  116. }
  117. return $result;
  118. }
  119. /**
  120. * 移除指定元素
  121. * @param $brandId
  122. * @return mixed
  123. */
  124. public function deleteBrandKeyById($brandId)
  125. {
  126. $result = $this->cache->zremrangebyscore(self::$brandKey . '::' . $this->onlineEnterpriseId, $brandId, $brandId);
  127. return $result;
  128. }
  129. /********************************************sku start**************************************************************/
  130. /**
  131. * sku缓存数据
  132. * @param $name
  133. * @param $skuId
  134. * @return mixed
  135. */
  136. public function cacheSkuIdRelationName($name, $skuId)
  137. {
  138. $result = $this->cache->hset(self::$skuKey.'::'.$this->onlineEnterpriseId,self::$skuField.'::'.$skuId,$name);
  139. return $result;
  140. }
  141. /**
  142. * 根据skuId获取名称
  143. * @param $skuId
  144. * @return mixed
  145. */
  146. public function getNameBySkuId($skuId)
  147. {
  148. $result = $this->cache->hget(self::$skuKey.'::'.$this->onlineEnterpriseId,self::$skuField.'::'.$skuId);
  149. if (!$result){
  150. return '';
  151. }
  152. return $result;
  153. }
  154. /**
  155. * 移除指定元素
  156. * @param $skuId
  157. * @return mixed
  158. */
  159. public function deleteSkuKeyById($skuId)
  160. {
  161. $result = $this->cache->zremrangebyscore(self::$skuKey . '::' . $this->onlineEnterpriseId, $skuId, $skuId);
  162. return $result;
  163. }
  164. /***********************************************basic start*********************************************************/
  165. /**
  166. * Model/Goods/BasicAndSkuCache 中调用了
  167. * todo(方法名称不符合规范。。。。。。。。。。)
  168. * mapping
  169. * 缓存 data => id
  170. * @param $data
  171. * @param $basicId
  172. * @return ResultWrapper
  173. */
  174. public function cacheBasicIdRelationName(array $data,int $basicId)
  175. {
  176. $data = gzcompress(serialize($data));
  177. $result = $this->cache->hset(self::$basicKey . '::' . $this->onlineEnterpriseId, $basicId, $data);
  178. if ($result) {
  179. return ResultWrapper::success('添加缓存成功');
  180. } else {
  181. return ResultWrapper::fail('添加缓存失败', ErrorCode::$redisWriteError);
  182. }
  183. }
  184. /**
  185. * $fields
  186. * true : 返回所有字段信息(array)
  187. * field : 返回对应字段值 (string)
  188. *
  189. * todo(方法名称不符合规范。。。。。。。。。。)
  190. * 根据商品id,获取商品信息
  191. * @param $basicId
  192. * @param string $fields
  193. * @return array|string
  194. */
  195. public function getNameByBasicId(int $basicId, $fields = 'title')
  196. {
  197. $result = $this->cache->hget(self::$basicKey . '::' . $this->onlineEnterpriseId, $basicId);
  198. if (!$result) {
  199. if($fields === true || $fields == 'images') return [];
  200. return '';
  201. }
  202. $result = unserialize(zlib_decode($result));
  203. $result['images'] = is_array($result['images']) ? $result['images'] : json_decode($result['images'], true);
  204. $result['extends'] = empty($result['extends']) ? [] : json_decode($result['extends'],true);
  205. if($fields === true) return $result;
  206. if (isset($result[$fields])){
  207. return $result[$fields];
  208. }
  209. return '';
  210. }
  211. /**
  212. * @param $basicId
  213. * @return mixed
  214. */
  215. public function deleteBasicKeyById(int $basicId)
  216. {
  217. return $this->cache->hdel(self::$basicKey . '::' . $this->onlineEnterpriseId, $basicId);
  218. }
  219. /***************************************仓库数据 缓存
  220. * 增加
  221. * @param $warehouseId
  222. * @param $warehouseData
  223. * @return ResultWrapper
  224. */
  225. public function cacheWarehouseIdRelationName($warehouseId,$warehouseData)
  226. {
  227. $data = json_encode($warehouseData);
  228. $result = $this->cache->hset(self::$warehouseIdKey . '::' . $this->onlineEnterpriseId, $warehouseId, $data);
  229. if (!$result) {
  230. return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
  231. }
  232. return ResultWrapper::success('success');
  233. }
  234. /**
  235. * 查询
  236. * @param $warehouseId
  237. * @param bool $type
  238. * @return mixed|string
  239. */
  240. public function getNameByWarehouseId($warehouseId, $type = false)
  241. {
  242. $result = $this->cache->hget(self::$warehouseIdKey.'::'.$this->onlineEnterpriseId, $warehouseId);
  243. if (!$result) return '';
  244. if($type){
  245. return json_decode($result, true);
  246. }
  247. return json_decode($result, true)['warehouseName'];
  248. }
  249. /**
  250. * 删除
  251. * @param $warehouseId
  252. */
  253. public function delNameByWarehouseId($warehouseId)
  254. {
  255. $this->cache->hdel(self::$warehouseIdKey.'::'.$this->onlineEnterpriseId, $warehouseId);
  256. }
  257. /***************************************商铺
  258. *
  259. * 添加
  260. * @param $ShopId
  261. * @param $warehouseData
  262. * @return ResultWrapper
  263. */
  264. public function cacheShopIdRelationName($ShopId,$warehouseData)
  265. {
  266. $data = json_encode($warehouseData);
  267. $result = $this->cache->hset(self::$ShopIdKey . '::' . $this->onlineEnterpriseId, $ShopId, $data);
  268. if (!$result) {
  269. return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
  270. }
  271. return ResultWrapper::success('success');
  272. }
  273. /**
  274. * 查询
  275. * @param $ShopId
  276. * @param bool $type
  277. * @return mixed|string
  278. */
  279. public function getNameByShopId($ShopId, $type = false)
  280. {
  281. $result = $this->cache->hget(self::$ShopIdKey.'::'.$this->onlineEnterpriseId, $ShopId);
  282. if (!$result) return '';
  283. if($type){
  284. return json_decode($result, true);
  285. }
  286. return json_decode($result, true)['name'];
  287. }
  288. /**
  289. * 删除
  290. * @param $ShopId
  291. */
  292. public function delNameByShopId($ShopId)
  293. {
  294. $this->cache->hdel(self::$ShopIdKey.'::'.$this->onlineEnterpriseId, $ShopId);
  295. }
  296. }