123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- /**
- * 客户统计部分埋点
- * Created by PhpStorm.
- * User: wxj
- * Date: 2019/11/14
- * Time: 11:58
- */
- namespace JinDouYun\Cache;
- use Mall\Framework\Cache\Redis;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Factory;
- class CustomerCache {
- /**
- * @var Redis
- */
- private $cache;
- protected $key = '';
- protected $registerSmsCountKey = 'todayCreateOrderCustomer::'; //今日下单用户key
- protected $customerExpire = 10;//过期时间为一天
- protected $allCustomerNumKey = 'allCustomerNum';//全部客户数量key
- protected $lastMonthNewCustomerKey = 'lastMonthNewCustomer';//新增客户数量:最近30天新增,且没有成功付款的客户 key
- protected $interestCustomerKey = 'interestCustomer';//兴趣人群数量:近7天有加购行为,但没有成功付款的客户
- protected $customerDataKey = 'customerInfoCache'; // 客户信息数据缓存
- protected $customerDataByUserCenterIdKey ='customerDataByUserCenterId';//指定用户信息缓存
- public function __construct()
- {
- $this->key = $this->registerSmsCountKey . date('Ymd', time());
- $this->cache = Factory::cache('user');
- }
- /***************************** 今日下单用户相关 start **********************************/
- /**
- * 下单时调用此方法,添加一条客户下单信息 todayCreateOrderCustomer::20190101:0001:0002:0003
- * @param $enterpriseId
- * @param $provinceCode
- * @param $cityCode
- * @param $districtCode
- * @param $customerId
- * @param $shopId
- * @return bool|ResultWrapper
- */
- 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;
- }
- /**
- * 今日下单用户统计,按地区搜索
- * @param $enterpriseId 企业id
- * code不传则查的是全国的客户下单信息
- * @param $code
- * @param $shopId
- * @return array
- */
- 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 : [];
- }
- /***************************** 今日下单用户相关 end **********************************/
- /***************************** 全部客户数量相关 start **********************************/
- /**
- * @param int $enterpriseId
- * @param int $num
- * @return bool
- */
- public function setCustomerNum(int $enterpriseId,int $num)
- {
- $result = $this->cache->set($this->allCustomerNumKey.'::'.$enterpriseId,$num);
- return $result ? true : false;
- }
- /**
- * 增加客户数量:每次客户审核通过调用此方法
- * @param $enterpriseId
- * @return bool
- */
- public function incrCustomerNum($enterpriseId) {
- $result = $this->cache->incr($this->allCustomerNumKey.'::'.$enterpriseId);
- return $result ? true : false;
- }
- /**
- * 获取当前客户总数
- * @param $enterpriseId
- * @return bool
- */
- public function getAllCustomerNum($enterpriseId) {
- return $this->cache->get($this->allCustomerNumKey.'::'.$enterpriseId);
- }
- /***************************** 全部客户数量相关 end **********************************/
- /***************************** 新增客户数量相关 start **********************************/
- /**
- * 新增客户 customerId score存时间戳 审核用户时调用此方法,近一个月注册的用户
- * @param $customerId
- * @param $enterpriseId
- * @return bool
- */
- public function incrCustomer($customerId, $enterpriseId) {
- $result = $this->cache->zadd($this->lastMonthNewCustomerKey.'::'.$enterpriseId, time(), $customerId);
- return $result ? true : false;
- }
- /**
- * 获取新增客户数
- * @param $enterpriseId
- * @return int
- */
- public function getNewCustomerNum($enterpriseId) {
- $result = $this->cache->zcard($this->lastMonthNewCustomerKey.'::'.$enterpriseId);
- return $result ? $result : 0;
- }
- /**
- * 删除所有企业注册超过30天的用户
- * @return bool true代表有数据被删除 false相反
- */
- public function delCustomerOfOneMonthAgo() {
- //获取所有key ['lastMonthNewCustomer::12','lastMonthNewCustomer::11']
- $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;//没有数据可删除也返回false
- }
- }
- return true;
- }
- /**
- * 客户下单后删除客户信息
- * @param $customerId
- * @param $enterpriseId
- * @return bool
- */
- public function delCustomerAfterPlaceOrder($customerId, $enterpriseId) {
- $result = $this->cache->zrem($this->lastMonthNewCustomerKey.'::'.$enterpriseId, $customerId);
- return $result ? true : false;
- }
- /***************************** 新增客户数量相关 end **********************************/
- /***************************** 兴趣人群数量 : 近7天加购行为 start **********************************/
- /**
- * 加入购物车 customerId score存时间戳 注册用户时调用此方法
- * @param $customerId
- * @param $enterpriseId
- * @return bool
- */
- public function incrInterestCustomer($customerId, $enterpriseId) {
- $result = $this->cache->zadd($this->interestCustomerKey.'::'.$enterpriseId, time(), $customerId);
- return $result ? true : false;
- }
- /**
- * 删除所有企业加购超过7天的用户
- * @return bool true代表有数据被删除 false相反
- */
- public function delInterestCustomerOfSevenDaysAgo() {
- //获取所有key ['lastMonthNewCustomer::12','lastMonthNewCustomer::11']
- $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;//没有数据可删除也返回false
- }
- }
- return true;
- }
- /**
- * 获取兴趣人群数
- * @param $enterpriseId
- * @return int
- */
- public function getInterestCustomerNum($enterpriseId) {
- $result = $this->cache->zcard($this->interestCustomerKey.'::'.$enterpriseId);
- return $result ? $result : 0;
- }
- /**
- * 客户成功付款后删除信息
- * @param $customerId
- * @param $enterpriseId
- * @return bool
- */
- public function delInterestCustomerAfterPay($customerId, $enterpriseId) {
- $result = $this->cache->zrem($this->interestCustomerKey.'::'.$enterpriseId, $customerId);
- return $result ? true : false;
- }
- /***************************** 近7天加购行为 end **********************************/
- /**
- * 客户信息缓存
- */
- 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);
- }
- }
|