CustomerCache.Class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * 客户统计部分埋点
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/11/14
  7. * Time: 11:58
  8. */
  9. namespace Jobs\Cache;
  10. use Mall\Framework\Core\ResultWrapper;
  11. use Mall\Framework\Factory;
  12. class CustomerCache
  13. {
  14. private $cache;
  15. protected $key = '';
  16. protected $registerSmsCountKey = 'todayCreateOrderCustomer::'; //今日下单用户key
  17. protected $customerExpire = 10;//过期时间为一天
  18. protected $allCustomerNumKey = 'allCustomerNum';//全部客户数量key
  19. protected $lastMonthNewCustomerKey = 'lastMonthNewCustomer';//新增客户数量:最近30天新增,且没有成功付款的客户 key
  20. protected $interestCustomerKey = 'interestCustomer';//兴趣人群数量:近7天有加购行为,但没有成功付款的客户
  21. public function __construct()
  22. {
  23. $this->key = $this->registerSmsCountKey . date('Ymd', time());
  24. $this->cache = Factory::cache('user');
  25. }
  26. /***************************** 今日下单用户相关 start **********************************/
  27. /**
  28. * 下单时调用此方法,添加一条客户下单信息 todayCreateOrderCustomer::20190101:0001:0002:0003
  29. * @param $enterpriseId
  30. * @param $provinceCode
  31. * @param $cityCode
  32. * @param $districtCode
  33. * @param $customerId
  34. * @param $shopId
  35. * @return bool|ResultWrapper
  36. */
  37. public function cacheCustomerOrderInfo($enterpriseId, $provinceCode = null, $cityCode = null, $districtCode = null, $customerId, $shopId = null)
  38. {
  39. if (empty($enterpriseId) || empty($customerId)) {
  40. return false;
  41. }
  42. $this->key = $this->key . '::enterprise_' . $enterpriseId;
  43. $this->cache->zincrby($this->key, 1, $customerId);//存全国
  44. $this->cache->expire($this->key, $this->customerExpire);
  45. if (!empty($provinceCode)) {
  46. $key = $this->key . '::areaCode_' . $provinceCode;
  47. $this->cache->zincrby($key, 1, $customerId);//存省
  48. $this->cache->expire($key, $this->customerExpire);
  49. }
  50. if (!empty($cityCode)) {
  51. $key = $this->key . '::areaCode_' . $cityCode;
  52. $this->cache->zincrby($key, 1, $customerId);//存市
  53. $this->cache->expire($key, $this->customerExpire);
  54. }
  55. if (!empty($districtCode)) {
  56. $key = $this->key . '::areaCode_' . $districtCode;
  57. $this->cache->zincrby($key, 1, $customerId);//存区
  58. $this->cache->expire($key, $this->customerExpire);
  59. }
  60. if (!empty($shopId)) {
  61. $key = $this->key . '::shopId_' . $shopId;
  62. $this->cache->zincrby($key, 1, $customerId);
  63. $this->cache->expire($key, $this->customerExpire);
  64. }
  65. return true;
  66. }
  67. /**
  68. * 今日下单用户统计,按地区搜索
  69. * @param $enterpriseId 企业id
  70. * code不传则查的是全国的客户下单信息
  71. * @param $code
  72. * @param $shopId
  73. * @return array
  74. */
  75. public function getTodayCustomerOrderInfo($enterpriseId, $code = null, $shopId = null)
  76. {
  77. $key = $this->key . '::enterprise_' . $enterpriseId;
  78. if (!empty($code)) {
  79. $key .= '::areaCode_' . $code;
  80. }
  81. if (!empty($shopId)) {
  82. $key .= '::shopId_' . $shopId;
  83. }
  84. $result = $this->cache->zrange($key, 0, -1);
  85. return $result ? $result : [];
  86. }
  87. /***************************** 今日下单用户相关 end **********************************/
  88. /***************************** 全部客户数量相关 start **********************************/
  89. /**
  90. * 增加客户数量:每次客户注册调用此方法
  91. * @param $enterpriseId
  92. * @return bool
  93. */
  94. public function incrCustomerNum($enterpriseId)
  95. {
  96. $result = $this->cache->incr($this->allCustomerNumKey . '::' . $enterpriseId);
  97. return $result ? true : false;
  98. }
  99. /**
  100. * 获取当前客户总数
  101. * @param $enterpriseId
  102. * @return bool
  103. */
  104. public function getAllCustomerNum($enterpriseId)
  105. {
  106. return $this->cache->get($this->allCustomerNumKey . '::' . $enterpriseId);
  107. }
  108. /***************************** 全部客户数量相关 end **********************************/
  109. /***************************** 新增客户数量相关 start **********************************/
  110. /**
  111. * 新增客户 customerId score存时间戳 注册用户时调用此方法
  112. * @param $customerId
  113. * @param $enterpriseId
  114. * @return bool
  115. */
  116. public function incrCustomer($customerId, $enterpriseId)
  117. {
  118. $result = $this->cache->zadd($this->lastMonthNewCustomerKey . '::' . $enterpriseId, time(), $customerId);
  119. return $result ? true : false;
  120. }
  121. /**
  122. * 删除所有企业注册超过30天的用户
  123. * @return bool true代表有数据被删除 false相反
  124. */
  125. public function delCustomerOfOneMonthAgo()
  126. {
  127. //获取所有key ['lastMonthNewCustomer::12','lastMonthNewCustomer::11']
  128. $keysList = $this->cache->keys($this->lastMonthNewCustomerKey . '*');
  129. $oneMonthAgoTimestamp = strtotime("-30 day");
  130. foreach ($keysList as $value) {
  131. $keyArr = explode('::', $value);
  132. $result = $this->cache->zremrangebyscore($this->lastMonthNewCustomerKey . '::' . $keyArr[2], 0, $oneMonthAgoTimestamp);
  133. if (!$result) {
  134. return false;//没有数据可删除也返回false
  135. }
  136. }
  137. return true;
  138. }
  139. /**
  140. * 客户下单后删除客户信息
  141. * @param $customerId
  142. * @param $enterpriseId
  143. * @return bool
  144. */
  145. public function delCustomerAfterPlaceOrder($customerId, $enterpriseId)
  146. {
  147. $result = $this->cache->zrem($this->lastMonthNewCustomerKey . '::' . $enterpriseId, $customerId);
  148. return $result ? true : false;
  149. }
  150. /***************************** 新增客户数量相关 end **********************************/
  151. /***************************** 兴趣人群数量 : 近7天加购行为 start **********************************/
  152. /**
  153. * 加入购物车 customerId score存时间戳 注册用户时调用此方法
  154. * @param $customerId
  155. * @param $enterpriseId
  156. * @return bool
  157. */
  158. public function incrInterestCustomer($customerId, $enterpriseId)
  159. {
  160. $result = $this->cache->zadd($this->interestCustomerKey . '::' . $enterpriseId, time(), $customerId);
  161. return $result ? true : false;
  162. }
  163. /**
  164. * 删除所有企业加购超过7天的用户
  165. * @return bool true代表有数据被删除 false相反
  166. */
  167. public function delInterestCustomerOfSevenDaysAgo()
  168. {
  169. //获取所有key ['lastMonthNewCustomer::12','lastMonthNewCustomer::11']
  170. $keysList = $this->cache->keys($this->interestCustomerKey . '*');
  171. $SeveralDaysAgoTimestamp = strtotime("-7 day");
  172. foreach ($keysList as $value) {
  173. $keyArr = explode('::', $value);
  174. $result = $this->cache->zremrangebyscore($this->interestCustomerKey . '::' . $keyArr[2], 0, $SeveralDaysAgoTimestamp);
  175. if (!$result) {
  176. return false;//没有数据可删除也返回false
  177. }
  178. }
  179. return true;
  180. }
  181. /**
  182. * 客户成功付款后删除信息
  183. * @param $customerId
  184. * @param $enterpriseId
  185. * @return bool
  186. */
  187. public function delInterestCustomerAfterPay($customerId, $enterpriseId)
  188. {
  189. $result = $this->cache->zrem($this->interestCustomerKey . '::' . $enterpriseId, $customerId);
  190. return $result ? true : false;
  191. }
  192. /***************************** 近7天加购行为 end **********************************/
  193. }