PriceCache.Class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace JinDouYun\Cache;
  3. use Mall\Framework\Cache\Redis;
  4. use Mall\Framework\Factory;
  5. /**
  6. * 价格缓存
  7. * Class PriceCache
  8. * @package JinDouYun\Cache
  9. */
  10. class PriceCache
  11. {
  12. /**
  13. * 全国价缓存key
  14. * @var string
  15. */
  16. private static $nationSalePriceTableKey = 'nationSalePrice::';
  17. /**
  18. * 客户调价信息缓存
  19. * @var string
  20. */
  21. private static $customerAdjTableKey = 'customerAdj::';
  22. /**
  23. * 客户类型调价信息
  24. * @var string
  25. */
  26. private static $customerTypeAdiTableKey = 'customerTypeAdi::';
  27. /**
  28. * @var string 价格波动
  29. */
  30. private static $priceTrend = 'priceTrend_';
  31. /**
  32. * @var integer
  33. */
  34. private $onlineEnterpriseId;
  35. /**
  36. * @var Redis
  37. */
  38. private $cache;
  39. /**
  40. * PriceCache constructor.
  41. * @param $enterpriseId
  42. * @throws \Exception
  43. */
  44. public function __construct($enterpriseId)
  45. {
  46. $this->onlineEnterpriseId = $enterpriseId;
  47. $this->cache = Factory::cache('mapping');
  48. }
  49. /**********缓存NationSalePrice数据***************/
  50. /**
  51. *
  52. * 缓存全国价 goodsId=>$value
  53. * @param int $goodsId
  54. * @param $value
  55. * @return bool|int
  56. */
  57. public function setNationSalePrice(int $goodsId, $value)
  58. {
  59. return $this->cache->hset(self::$nationSalePriceTableKey . $this->onlineEnterpriseId, $goodsId, json_encode($value));
  60. }
  61. /**
  62. *
  63. * 获取全国价 goodsId=>$value
  64. * @param int $goodsId
  65. * @return mixed
  66. */
  67. public function getNationSalePrice(int $goodsId)
  68. {
  69. $result = $this->cache->hget(self::$nationSalePriceTableKey . $this->onlineEnterpriseId, $goodsId);
  70. if (empty($result)) {
  71. return [];
  72. }
  73. $result = json_decode($result, true);
  74. return $result;
  75. }
  76. /**
  77. * 删除全国价 goodsId=>$value
  78. * @param int $goodsId
  79. * @return bool|int
  80. */
  81. public function delNationSalePrice(int $goodsId)
  82. {
  83. return $this->cache->hdel(self::$nationSalePriceTableKey . $this->onlineEnterpriseId, $goodsId);
  84. }
  85. /**********缓存客户调价信息数据***************/
  86. /**
  87. * 缓存客户类型价格信息
  88. * filed 客户id_商品id
  89. * @param int $goodsId
  90. * @param int $customerId
  91. * @param $value
  92. * @return bool|int
  93. */
  94. public function setCustomerAdj(int $goodsId, int $customerId, $value)
  95. {
  96. return $this->cache->hset(self::$customerAdjTableKey . $this->onlineEnterpriseId, $customerId . '_' . $goodsId, json_encode($value));
  97. }
  98. /**
  99. * 获取客户调价信息
  100. * @param int $goodsId
  101. * @param int $customerId
  102. * @return array|mixed|string
  103. */
  104. public function getCustomerAdj(int $goodsId, int $customerId)
  105. {
  106. $result = $this->cache->hget(self::$customerAdjTableKey . $this->onlineEnterpriseId, $customerId . '_' . $goodsId);
  107. if (empty($result)) {
  108. return [];
  109. }
  110. $result = json_decode($result, true);
  111. return $result;
  112. }
  113. /**
  114. * 删除客户调价信息
  115. * @param int $goodsId
  116. * @param int $customerId
  117. * @return bool|int
  118. */
  119. public function delCustomerAdj(int $goodsId,int $customerId)
  120. {
  121. return $this->cache->hdel(self::$customerAdjTableKey . $this->onlineEnterpriseId, $customerId . '_' . $goodsId);
  122. }
  123. /*****缓存客户类型调价信息*******/
  124. /**
  125. * 缓存客户类型调价信息
  126. * @param int $goodsId
  127. * @param int $customerType
  128. * @param $value
  129. * @return bool|int
  130. */
  131. public function setCustomerTypeAdj(int $goodsId, int $customerType, $value)
  132. {
  133. return $this->cache->hset(self::$customerTypeAdiTableKey.$this->onlineEnterpriseId,$customerType.'_'.$goodsId,json_encode($value));
  134. }
  135. /**
  136. * 获取客户类型调价信息
  137. * @param int $goodsId
  138. * @param int $customerType
  139. * @return array|mixed|string
  140. */
  141. public function getCustomerTypeAdj(int $goodsId, int $customerType)
  142. {
  143. $result = $this->cache->hget(self::$customerTypeAdiTableKey . $this->onlineEnterpriseId, $customerType . '_' . $goodsId);
  144. if (empty($result)) {
  145. return [];
  146. }
  147. $result = json_decode($result, true);
  148. return $result;
  149. }
  150. /**
  151. * 删除客户类型调价信息
  152. * @param int $goodsId
  153. * @param int $customerType
  154. * @return bool|int
  155. */
  156. public function delCustomerTypeAdj(int $goodsId, int $customerType)
  157. {
  158. return $this->cache->hdel(self::$customerTypeAdiTableKey . $this->onlineEnterpriseId, $customerType . '_' . $goodsId);
  159. }
  160. /**
  161. * Doc: (des="缓存价格波动数据")
  162. * User: XMing
  163. * Date: 2021/3/11
  164. * Time: 12:02 下午
  165. * @param int $goodsId
  166. * @param string $value
  167. * @param int $ttl
  168. * @return bool|mixed
  169. */
  170. public function setPriceTrend(int $goodsId,string $value,int $ttl = 60): bool
  171. {
  172. return $this->cache->set(self::$priceTrend . $this->onlineEnterpriseId.'_'.$goodsId,$value,$ttl);
  173. }
  174. /**
  175. * Doc: (des="获取缓存价格波动数据")
  176. * User: XMing
  177. * Date: 2021/3/11
  178. * Time: 12:16 下午
  179. * @param int $goodsId
  180. */
  181. public function getPriceTrend(int $goodsId)
  182. {
  183. return [];
  184. $result = $this->cache->get(self::$priceTrend . $this->onlineEnterpriseId.'_'.$goodsId);
  185. if (empty($result)){
  186. return [];
  187. }
  188. return $result;
  189. }
  190. }