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); } }