123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace Jobs\Cache;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Factory;
- class CustomerCache
- {
- private $cache;
- protected $key = '';
- protected $registerSmsCountKey = 'todayCreateOrderCustomer::';
- protected $customerExpire = 10;
- protected $allCustomerNumKey = 'allCustomerNum';
- protected $lastMonthNewCustomerKey = 'lastMonthNewCustomer';
- protected $interestCustomerKey = 'interestCustomer';
- public function __construct()
- {
- $this->key = $this->registerSmsCountKey . date('Ymd', time());
- $this->cache = Factory::cache('user');
- }
-
-
- public function cacheCustomerOrderInfo($enterpriseId, $provinceCode = null, $cityCode = null, $districtCode = null, $customerId, $shopId = null)
- {
- if (empty($enterpriseId) || empty($customerId)) {
- return false;
- }
- $this->key = $this->key . '::enterprise_' . $enterpriseId;
- $this->cache->zincrby($this->key, 1, $customerId);
- $this->cache->expire($this->key, $this->customerExpire);
- if (!empty($provinceCode)) {
- $key = $this->key . '::areaCode_' . $provinceCode;
- $this->cache->zincrby($key, 1, $customerId);
- $this->cache->expire($key, $this->customerExpire);
- }
- if (!empty($cityCode)) {
- $key = $this->key . '::areaCode_' . $cityCode;
- $this->cache->zincrby($key, 1, $customerId);
- $this->cache->expire($key, $this->customerExpire);
- }
- if (!empty($districtCode)) {
- $key = $this->key . '::areaCode_' . $districtCode;
- $this->cache->zincrby($key, 1, $customerId);
- $this->cache->expire($key, $this->customerExpire);
- }
- if (!empty($shopId)) {
- $key = $this->key . '::shopId_' . $shopId;
- $this->cache->zincrby($key, 1, $customerId);
- $this->cache->expire($key, $this->customerExpire);
- }
- return true;
- }
-
- public function getTodayCustomerOrderInfo($enterpriseId, $code = null, $shopId = null)
- {
- $key = $this->key . '::enterprise_' . $enterpriseId;
- if (!empty($code)) {
- $key .= '::areaCode_' . $code;
- }
- if (!empty($shopId)) {
- $key .= '::shopId_' . $shopId;
- }
- $result = $this->cache->zrange($key, 0, -1);
- return $result ? $result : [];
- }
-
-
-
- public function incrCustomerNum($enterpriseId)
- {
- $result = $this->cache->incr($this->allCustomerNumKey . '::' . $enterpriseId);
- return $result ? true : false;
- }
-
- public function getAllCustomerNum($enterpriseId)
- {
- return $this->cache->get($this->allCustomerNumKey . '::' . $enterpriseId);
- }
-
-
-
- public function incrCustomer($customerId, $enterpriseId)
- {
- $result = $this->cache->zadd($this->lastMonthNewCustomerKey . '::' . $enterpriseId, time(), $customerId);
- return $result ? true : false;
- }
-
- public function delCustomerOfOneMonthAgo()
- {
-
- $keysList = $this->cache->keys($this->lastMonthNewCustomerKey . '*');
- $oneMonthAgoTimestamp = strtotime("-30 day");
- foreach ($keysList as $value) {
- $keyArr = explode('::', $value);
- $result = $this->cache->zremrangebyscore($this->lastMonthNewCustomerKey . '::' . $keyArr[2], 0, $oneMonthAgoTimestamp);
- if (!$result) {
- return false;
- }
- }
- return true;
- }
-
- public function delCustomerAfterPlaceOrder($customerId, $enterpriseId)
- {
- $result = $this->cache->zrem($this->lastMonthNewCustomerKey . '::' . $enterpriseId, $customerId);
- return $result ? true : false;
- }
-
-
-
- public function incrInterestCustomer($customerId, $enterpriseId)
- {
- $result = $this->cache->zadd($this->interestCustomerKey . '::' . $enterpriseId, time(), $customerId);
- return $result ? true : false;
- }
-
- public function delInterestCustomerOfSevenDaysAgo()
- {
-
- $keysList = $this->cache->keys($this->interestCustomerKey . '*');
- $SeveralDaysAgoTimestamp = strtotime("-7 day");
- foreach ($keysList as $value) {
- $keyArr = explode('::', $value);
- $result = $this->cache->zremrangebyscore($this->interestCustomerKey . '::' . $keyArr[2], 0, $SeveralDaysAgoTimestamp);
- if (!$result) {
- return false;
- }
- }
- return true;
- }
-
- public function delInterestCustomerAfterPay($customerId, $enterpriseId)
- {
- $result = $this->cache->zrem($this->interestCustomerKey . '::' . $enterpriseId, $customerId);
- return $result ? true : false;
- }
-
- }
|