123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace JinDouYun\Cache;
- use Mall\Framework\Cache\Redis;
- use Mall\Framework\Core\ErrorCode;
- 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';
- protected $customerDataKey = 'customerInfoCache';
- protected $customerDataByUserCenterIdKey ='customerDataByUserCenterId';
- 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 setCustomerNum(int $enterpriseId,int $num)
- {
- $result = $this->cache->set($this->allCustomerNumKey.'::'.$enterpriseId,$num);
- return $result ? true : false;
- }
-
- 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 getNewCustomerNum($enterpriseId) {
- $result = $this->cache->zcard($this->lastMonthNewCustomerKey.'::'.$enterpriseId);
- return $result ? $result : 0;
- }
-
- 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 getInterestCustomerNum($enterpriseId) {
- $result = $this->cache->zcard($this->interestCustomerKey.'::'.$enterpriseId);
- return $result ? $result : 0;
- }
-
- public function delInterestCustomerAfterPay($customerId, $enterpriseId) {
- $result = $this->cache->zrem($this->interestCustomerKey.'::'.$enterpriseId, $customerId);
- return $result ? true : false;
- }
-
-
- public function cacheCustomerData($enterpriseId, $customerId, $customerData)
- {
- return $this->cache->hset($this->customerDataKey.'::'.$enterpriseId, $customerId, json_encode($customerData));
- }
-
- public function getCustomerData($enterpriseId,$customerId)
- {
- $customerData = $this->cache->hget($this->customerDataKey.'::'.$enterpriseId, $customerId);
- if( $customerData ){
- return json_decode($customerData, true);
- }
- }
-
- public function delCustomerData($enterpriseId,$customerId)
- {
- return $this->cache->hdel($this->customerDataKey.'::'.$enterpriseId, $customerId);
- }
-
- public function cacheCustomerUserData($enterpriseId, $userCenterId, $customerData)
- {
- return $this->cache->hset($this->customerDataByUserCenterIdKey.'::'.$enterpriseId, $userCenterId, json_encode($customerData));
- }
-
- public function getCustomerUserData($enterpriseId,$userCenterId)
- {
- $customerData = $this->cache->hget($this->customerDataByUserCenterIdKey.'::'.$enterpriseId, $userCenterId);
- if( $customerData ){
- return json_decode($customerData, true);
- }
- }
-
- public function delCustomerUserData($enterpriseId,$userCenterId)
- {
- return $this->cache->hdel($this->customerDataByUserCenterIdKey.'::'.$enterpriseId, $userCenterId);
- }
- }
|