CustomerCache.Class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * 客户统计部分埋点
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/11/14
  7. * Time: 11:58
  8. */
  9. namespace JinDouYun\Cache;
  10. use Mall\Framework\Cache\Redis;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use Mall\Framework\Factory;
  14. class CustomerCache {
  15. /**
  16. * @var Redis
  17. */
  18. private $cache;
  19. protected $key = '';
  20. protected $registerSmsCountKey = 'todayCreateOrderCustomer::'; //今日下单用户key
  21. protected $customerExpire = 10;//过期时间为一天
  22. protected $allCustomerNumKey = 'allCustomerNum';//全部客户数量key
  23. protected $lastMonthNewCustomerKey = 'lastMonthNewCustomer';//新增客户数量:最近30天新增,且没有成功付款的客户 key
  24. protected $interestCustomerKey = 'interestCustomer';//兴趣人群数量:近7天有加购行为,但没有成功付款的客户
  25. protected $customerDataKey = 'customerInfoCache'; // 客户信息数据缓存
  26. protected $customerDataByUserCenterIdKey ='customerDataByUserCenterId';//指定用户信息缓存
  27. public function __construct()
  28. {
  29. $this->key = $this->registerSmsCountKey . date('Ymd', time());
  30. $this->cache = Factory::cache('user');
  31. }
  32. /***************************** 今日下单用户相关 start **********************************/
  33. /**
  34. * 下单时调用此方法,添加一条客户下单信息 todayCreateOrderCustomer::20190101:0001:0002:0003
  35. * @param $enterpriseId
  36. * @param $provinceCode
  37. * @param $cityCode
  38. * @param $districtCode
  39. * @param $customerId
  40. * @param $shopId
  41. * @return bool|ResultWrapper
  42. */
  43. public function cacheCustomerOrderInfo($enterpriseId, $provinceCode = null, $cityCode = null, $districtCode = null, $customerId, $shopId = null) {
  44. if (empty($enterpriseId) || empty($customerId)) {
  45. return false;
  46. }
  47. $this->key = $this->key.'::enterprise_'.$enterpriseId;
  48. $this->cache->zincrby($this->key, 1, $customerId);//存全国
  49. $this->cache->expire($this->key, $this->customerExpire);
  50. if(!empty($provinceCode)) {
  51. $key = $this->key . '::areaCode_'.$provinceCode;
  52. $this->cache->zincrby($key, 1, $customerId);//存省
  53. $this->cache->expire($key, $this->customerExpire);
  54. }
  55. if(!empty($cityCode)) {
  56. $key = $this->key . '::areaCode_'.$cityCode;
  57. $this->cache->zincrby($key, 1, $customerId);//存市
  58. $this->cache->expire($key, $this->customerExpire);
  59. }
  60. if(!empty($districtCode)) {
  61. $key = $this->key . '::areaCode_'.$districtCode;
  62. $this->cache->zincrby($key, 1, $customerId);//存区
  63. $this->cache->expire($key, $this->customerExpire);
  64. }
  65. if(!empty($shopId)) {
  66. $key = $this->key . '::shopId_'.$shopId;
  67. $this->cache->zincrby($key, 1, $customerId);
  68. $this->cache->expire($key, $this->customerExpire);
  69. }
  70. return true;
  71. }
  72. /**
  73. * 今日下单用户统计,按地区搜索
  74. * @param $enterpriseId 企业id
  75. * code不传则查的是全国的客户下单信息
  76. * @param $code
  77. * @param $shopId
  78. * @return array
  79. */
  80. public function getTodayCustomerOrderInfo($enterpriseId, $code = null, $shopId = null) {
  81. $key = $this->key.'::enterprise_'.$enterpriseId;
  82. if(!empty($code)) {
  83. $key .= '::areaCode_' . $code;
  84. }
  85. if(!empty($shopId)) {
  86. $key .= '::shopId_' . $shopId;
  87. }
  88. $result = $this->cache->zrange($key, 0, -1);
  89. return $result ? $result : [];
  90. }
  91. /***************************** 今日下单用户相关 end **********************************/
  92. /***************************** 全部客户数量相关 start **********************************/
  93. /**
  94. * @param int $enterpriseId
  95. * @param int $num
  96. * @return bool
  97. */
  98. public function setCustomerNum(int $enterpriseId,int $num)
  99. {
  100. $result = $this->cache->set($this->allCustomerNumKey.'::'.$enterpriseId,$num);
  101. return $result ? true : false;
  102. }
  103. /**
  104. * 增加客户数量:每次客户审核通过调用此方法
  105. * @param $enterpriseId
  106. * @return bool
  107. */
  108. public function incrCustomerNum($enterpriseId) {
  109. $result = $this->cache->incr($this->allCustomerNumKey.'::'.$enterpriseId);
  110. return $result ? true : false;
  111. }
  112. /**
  113. * 获取当前客户总数
  114. * @param $enterpriseId
  115. * @return bool
  116. */
  117. public function getAllCustomerNum($enterpriseId) {
  118. return $this->cache->get($this->allCustomerNumKey.'::'.$enterpriseId);
  119. }
  120. /***************************** 全部客户数量相关 end **********************************/
  121. /***************************** 新增客户数量相关 start **********************************/
  122. /**
  123. * 新增客户 customerId score存时间戳 审核用户时调用此方法,近一个月注册的用户
  124. * @param $customerId
  125. * @param $enterpriseId
  126. * @return bool
  127. */
  128. public function incrCustomer($customerId, $enterpriseId) {
  129. $result = $this->cache->zadd($this->lastMonthNewCustomerKey.'::'.$enterpriseId, time(), $customerId);
  130. return $result ? true : false;
  131. }
  132. /**
  133. * 获取新增客户数
  134. * @param $enterpriseId
  135. * @return int
  136. */
  137. public function getNewCustomerNum($enterpriseId) {
  138. $result = $this->cache->zcard($this->lastMonthNewCustomerKey.'::'.$enterpriseId);
  139. return $result ? $result : 0;
  140. }
  141. /**
  142. * 删除所有企业注册超过30天的用户
  143. * @return bool true代表有数据被删除 false相反
  144. */
  145. public function delCustomerOfOneMonthAgo() {
  146. //获取所有key ['lastMonthNewCustomer::12','lastMonthNewCustomer::11']
  147. $keysList = $this->cache->keys($this->lastMonthNewCustomerKey.'*');
  148. $oneMonthAgoTimestamp = strtotime("-30 day");
  149. foreach ($keysList as $value) {
  150. $keyArr = explode('::', $value);
  151. $result = $this->cache->zremrangebyscore($this->lastMonthNewCustomerKey.'::'.$keyArr[2], 0, $oneMonthAgoTimestamp);
  152. if(!$result) {
  153. return false;//没有数据可删除也返回false
  154. }
  155. }
  156. return true;
  157. }
  158. /**
  159. * 客户下单后删除客户信息
  160. * @param $customerId
  161. * @param $enterpriseId
  162. * @return bool
  163. */
  164. public function delCustomerAfterPlaceOrder($customerId, $enterpriseId) {
  165. $result = $this->cache->zrem($this->lastMonthNewCustomerKey.'::'.$enterpriseId, $customerId);
  166. return $result ? true : false;
  167. }
  168. /***************************** 新增客户数量相关 end **********************************/
  169. /***************************** 兴趣人群数量 : 近7天加购行为 start **********************************/
  170. /**
  171. * 加入购物车 customerId score存时间戳 注册用户时调用此方法
  172. * @param $customerId
  173. * @param $enterpriseId
  174. * @return bool
  175. */
  176. public function incrInterestCustomer($customerId, $enterpriseId) {
  177. $result = $this->cache->zadd($this->interestCustomerKey.'::'.$enterpriseId, time(), $customerId);
  178. return $result ? true : false;
  179. }
  180. /**
  181. * 删除所有企业加购超过7天的用户
  182. * @return bool true代表有数据被删除 false相反
  183. */
  184. public function delInterestCustomerOfSevenDaysAgo() {
  185. //获取所有key ['lastMonthNewCustomer::12','lastMonthNewCustomer::11']
  186. $keysList = $this->cache->keys($this->interestCustomerKey.'*');
  187. $SeveralDaysAgoTimestamp = strtotime("-7 day");
  188. foreach ($keysList as $value) {
  189. $keyArr = explode('::', $value);
  190. $result = $this->cache->zremrangebyscore($this->interestCustomerKey.'::'.$keyArr[2], 0, $SeveralDaysAgoTimestamp);
  191. if(!$result) {
  192. return false;//没有数据可删除也返回false
  193. }
  194. }
  195. return true;
  196. }
  197. /**
  198. * 获取兴趣人群数
  199. * @param $enterpriseId
  200. * @return int
  201. */
  202. public function getInterestCustomerNum($enterpriseId) {
  203. $result = $this->cache->zcard($this->interestCustomerKey.'::'.$enterpriseId);
  204. return $result ? $result : 0;
  205. }
  206. /**
  207. * 客户成功付款后删除信息
  208. * @param $customerId
  209. * @param $enterpriseId
  210. * @return bool
  211. */
  212. public function delInterestCustomerAfterPay($customerId, $enterpriseId) {
  213. $result = $this->cache->zrem($this->interestCustomerKey.'::'.$enterpriseId, $customerId);
  214. return $result ? true : false;
  215. }
  216. /***************************** 近7天加购行为 end **********************************/
  217. /**
  218. * 客户信息缓存
  219. */
  220. public function cacheCustomerData($enterpriseId, $customerId, $customerData)
  221. {
  222. return $this->cache->hset($this->customerDataKey.'::'.$enterpriseId, $customerId, json_encode($customerData));
  223. }
  224. /**
  225. * 获取客户信息
  226. */
  227. public function getCustomerData($enterpriseId,$customerId)
  228. {
  229. $customerData = $this->cache->hget($this->customerDataKey.'::'.$enterpriseId, $customerId);
  230. if( $customerData ){
  231. return json_decode($customerData, true);
  232. }
  233. }
  234. /**
  235. * 删除客户缓存
  236. */
  237. public function delCustomerData($enterpriseId,$customerId)
  238. {
  239. return $this->cache->hdel($this->customerDataKey.'::'.$enterpriseId, $customerId);
  240. }
  241. /**
  242. * 指定客户信息缓存
  243. */
  244. public function cacheCustomerUserData($enterpriseId, $userCenterId, $customerData)
  245. {
  246. return $this->cache->hset($this->customerDataByUserCenterIdKey.'::'.$enterpriseId, $userCenterId, json_encode($customerData));
  247. }
  248. /**
  249. * 获取指定客户信息
  250. */
  251. public function getCustomerUserData($enterpriseId,$userCenterId)
  252. {
  253. $customerData = $this->cache->hget($this->customerDataByUserCenterIdKey.'::'.$enterpriseId, $userCenterId);
  254. if( $customerData ){
  255. return json_decode($customerData, true);
  256. }
  257. }
  258. /**
  259. * 删除指定客户缓存
  260. */
  261. public function delCustomerUserData($enterpriseId,$userCenterId)
  262. {
  263. return $this->cache->hdel($this->customerDataByUserCenterIdKey.'::'.$enterpriseId, $userCenterId);
  264. }
  265. }