xhStatUserByDayService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\dict;
  5. use common\components\stringUtil;
  6. use linslin\yii2\curl;
  7. use common\components\wxUtil;
  8. use common\components\util;
  9. use common\models\xhStatUserByDay;
  10. class xhStatUserByDayService {
  11. /**
  12. * 当天粉丝增加
  13. */
  14. public static function ChangeUser($sjId,$status=true)
  15. {
  16. $todayTime = strtotime(date("Y-m-d"));
  17. $numKey = $sjId.dict::getCacheKey('statUserNumByDay').$todayTime;
  18. if($status){
  19. $num = Yii::$app->redis->executeCommand('INCR', [$numKey]);//记录当天粉丝增加
  20. }else{
  21. $num = Yii::$app->redis->executeCommand('DECR', [$numKey]);//记录当天粉丝减少
  22. }
  23. }
  24. /**
  25. * 获取当天粉丝增减数
  26. */
  27. public static function getTodayNum($sjId)
  28. {
  29. $time = strtotime(date("Y-m-d"));
  30. $numKey = $sjId.dict::getCacheKey('statUserNumByDay').$time;
  31. $nowNum = Yii::$app->redis->executeCommand('GET', [$numKey]);//记录当天访问网站人数
  32. return !empty($nowNum) ? $nowNum : 0;
  33. }
  34. /**
  35. * 将昨天的粉丝增减数保存入库
  36. */
  37. public static function saveYesterdayUser($sjId)
  38. {
  39. $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
  40. $arr = xhStatUserByDay::getByCondition(['sjId' => $sjId,'time' => $yesTime]);
  41. if(!empty($arr)){
  42. return [];
  43. }
  44. $userKey = $sjId.dict::getCacheKey('statUserNumByDay').$yesTime;
  45. $num = Yii::$app->redis->executeCommand('GET', [$userKey]);
  46. $riseNum = !empty($num) ? $num : 0;
  47. $totalFans = self::getTotalFans($sjId);
  48. $todayFans = self::getTodayNum($sjId);
  49. $yesTotalNum = $totalFans - $todayFans;//计算昨天的总粉丝数
  50. $data = ['time' => $yesTime,'totalNum' =>$yesTotalNum,'riseNum' =>$riseNum,'sjId' => $sjId,'createTime' => date("Y-m-d H:i:s")];
  51. $saveData = self::add($data);
  52. return $saveData;
  53. }
  54. /**
  55. * 清空昨天的粉丝增减数据(包括缓存与数据库)
  56. */
  57. public static function clearYesNum($sjId)
  58. {
  59. $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
  60. $userKey = $sjId.dict::getCacheKey('statUserNumByDay').$yesTime;
  61. $yesterdayVisitNum = Yii::$app->redis->executeCommand('DEL', [$userKey]);
  62. xhStatUserByDay::deleteByCondition(['sjId' => $sjId,'time' => $yesTime]);
  63. }
  64. /**
  65. * 模拟创建昨天的粉丝
  66. */
  67. public static function imitationCreateYesNum($sjId)
  68. {
  69. echo "\n模拟创建昨天的粉丝……\n";
  70. $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
  71. $userKey = $sjId.dict::getCacheKey('statUserNumByDay').$yesTime;
  72. $num = rand(1,3);
  73. Yii::$app->redis->executeCommand('SET', [$userKey,$num]);
  74. echo "\n创建完成\n";
  75. }
  76. /**
  77. * 获取昨天增减粉丝数
  78. */
  79. public static function getYesNum($sjId)
  80. {
  81. $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
  82. $numKey = $sjId.dict::getCacheKey('statUserNumByDay').$yesTime;
  83. $nowNum = Yii::$app->redis->executeCommand('GET', [$numKey]);//记录当天访问网站人数
  84. return !empty($nowNum) ? $nowNum : 0;
  85. }
  86. /**
  87. * 添加
  88. */
  89. public static function add($data)
  90. {
  91. return xhStatUserByDay::add($data);
  92. }
  93. /**
  94. * 总的粉丝数
  95. */
  96. public static function getTotalFans($sjId)
  97. {
  98. $asset = xhMerchantAssetService::getBySjId($sjId);
  99. $totalFans = $asset['totalFans'];
  100. return $totalFans;
  101. }
  102. /**
  103. * 初始化最近N天的粉丝数据
  104. */
  105. public static function makeLatestUser($sjId)
  106. {
  107. $latestKey = dict::getCacheKey('latestUserNum').$sjId;
  108. Yii::$app->redis->executeCommand('DEL', [$latestKey]);
  109. $todayFans = self::getTodayNum($sjId);
  110. $rows = (new \yii\db\Query())->from('xhStatUserByDay')->where(['sjId' => $sjId])->orderBy('time ASC')->limit(19)->all();
  111. $data = [];
  112. if(!empty($rows)){
  113. foreach($rows as $val){
  114. Yii::$app->redis->executeCommand('ZADD', [$latestKey, $val['time'], $val['riseNum']]);
  115. $data[$val['time']] = $val['riseNum'];
  116. }
  117. }
  118. $time = strtotime(date("Y-m-d"));
  119. $data[$time] = $todayFans;
  120. ksort($data);
  121. return $data;
  122. }
  123. /**
  124. * 获取最近N天的粉丝数据
  125. */
  126. public static function getLatestUser($sjId)
  127. {
  128. $latestKey = dict::getCacheKey('latestUserNum').$sjId;
  129. $return = Yii::$app->redis->executeCommand('ZREVRANGE', [$latestKey, 0,-1, 'WITHSCORES']);//返回有序集合,从高分到低分排序
  130. $list = [];
  131. if(empty($return)){
  132. return self::makeLatestUser($sjId);
  133. }
  134. $i = 0;
  135. $arr = [];
  136. foreach($return as $key => $val){
  137. if($key%2 == 0){
  138. $arr[$i]['value'] = $val;
  139. }else{
  140. $arr[$i]['key'] = $val;
  141. $i++;
  142. }
  143. }
  144. foreach($arr as $key => $val){
  145. $list[$val['key']] = $val['value'];
  146. }
  147. $todayFans = self::getTodayNum($sjId);
  148. $time = strtotime(date("Y-m-d"));
  149. $list[$time] = $todayFans;
  150. ksort($list);
  151. return $list;
  152. }
  153. }