| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace common\services;
- use Yii;
- use common\components\dict;
- use common\components\stringUtil;
- use linslin\yii2\curl;
- use common\components\wxUtil;
- use common\components\util;
- use common\models\xhStatUserByDay;
- class xhStatUserByDayService {
- /**
- * 当天粉丝增加
- */
- public static function ChangeUser($sjId,$status=true)
- {
- $todayTime = strtotime(date("Y-m-d"));
- $numKey = $sjId.dict::getCacheKey('statUserNumByDay').$todayTime;
- if($status){
- $num = Yii::$app->redis->executeCommand('INCR', [$numKey]);//记录当天粉丝增加
- }else{
- $num = Yii::$app->redis->executeCommand('DECR', [$numKey]);//记录当天粉丝减少
- }
- }
- /**
- * 获取当天粉丝增减数
- */
- public static function getTodayNum($sjId)
- {
- $time = strtotime(date("Y-m-d"));
- $numKey = $sjId.dict::getCacheKey('statUserNumByDay').$time;
- $nowNum = Yii::$app->redis->executeCommand('GET', [$numKey]);//记录当天访问网站人数
- return !empty($nowNum) ? $nowNum : 0;
- }
- /**
- * 将昨天的粉丝增减数保存入库
- */
- public static function saveYesterdayUser($sjId)
- {
- $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
- $arr = xhStatUserByDay::getByCondition(['sjId' => $sjId,'time' => $yesTime]);
- if(!empty($arr)){
- return [];
- }
- $userKey = $sjId.dict::getCacheKey('statUserNumByDay').$yesTime;
- $num = Yii::$app->redis->executeCommand('GET', [$userKey]);
- $riseNum = !empty($num) ? $num : 0;
- $totalFans = self::getTotalFans($sjId);
- $todayFans = self::getTodayNum($sjId);
- $yesTotalNum = $totalFans - $todayFans;//计算昨天的总粉丝数
- $data = ['time' => $yesTime,'totalNum' =>$yesTotalNum,'riseNum' =>$riseNum,'sjId' => $sjId,'createTime' => date("Y-m-d H:i:s")];
- $saveData = self::add($data);
- return $saveData;
- }
-
- /**
- * 清空昨天的粉丝增减数据(包括缓存与数据库)
- */
- public static function clearYesNum($sjId)
- {
- $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
- $userKey = $sjId.dict::getCacheKey('statUserNumByDay').$yesTime;
- $yesterdayVisitNum = Yii::$app->redis->executeCommand('DEL', [$userKey]);
- xhStatUserByDay::deleteByCondition(['sjId' => $sjId,'time' => $yesTime]);
- }
- /**
- * 模拟创建昨天的粉丝
- */
- public static function imitationCreateYesNum($sjId)
- {
- echo "\n模拟创建昨天的粉丝……\n";
- $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
- $userKey = $sjId.dict::getCacheKey('statUserNumByDay').$yesTime;
- $num = rand(1,3);
- Yii::$app->redis->executeCommand('SET', [$userKey,$num]);
- echo "\n创建完成\n";
- }
- /**
- * 获取昨天增减粉丝数
- */
- public static function getYesNum($sjId)
- {
- $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
- $numKey = $sjId.dict::getCacheKey('statUserNumByDay').$yesTime;
- $nowNum = Yii::$app->redis->executeCommand('GET', [$numKey]);//记录当天访问网站人数
- return !empty($nowNum) ? $nowNum : 0;
- }
- /**
- * 添加
- */
- public static function add($data)
- {
- return xhStatUserByDay::add($data);
- }
- /**
- * 总的粉丝数
- */
- public static function getTotalFans($sjId)
- {
- $asset = xhMerchantAssetService::getBySjId($sjId);
- $totalFans = $asset['totalFans'];
- return $totalFans;
- }
- /**
- * 初始化最近N天的粉丝数据
- */
- public static function makeLatestUser($sjId)
- {
- $latestKey = dict::getCacheKey('latestUserNum').$sjId;
- Yii::$app->redis->executeCommand('DEL', [$latestKey]);
- $todayFans = self::getTodayNum($sjId);
- $rows = (new \yii\db\Query())->from('xhStatUserByDay')->where(['sjId' => $sjId])->orderBy('time ASC')->limit(19)->all();
- $data = [];
- if(!empty($rows)){
- foreach($rows as $val){
- Yii::$app->redis->executeCommand('ZADD', [$latestKey, $val['time'], $val['riseNum']]);
- $data[$val['time']] = $val['riseNum'];
- }
- }
- $time = strtotime(date("Y-m-d"));
- $data[$time] = $todayFans;
- ksort($data);
- return $data;
- }
- /**
- * 获取最近N天的粉丝数据
- */
- public static function getLatestUser($sjId)
- {
- $latestKey = dict::getCacheKey('latestUserNum').$sjId;
- $return = Yii::$app->redis->executeCommand('ZREVRANGE', [$latestKey, 0,-1, 'WITHSCORES']);//返回有序集合,从高分到低分排序
- $list = [];
- if(empty($return)){
- return self::makeLatestUser($sjId);
- }
- $i = 0;
- $arr = [];
- foreach($return as $key => $val){
- if($key%2 == 0){
- $arr[$i]['value'] = $val;
- }else{
- $arr[$i]['key'] = $val;
- $i++;
- }
- }
- foreach($arr as $key => $val){
- $list[$val['key']] = $val['value'];
- }
- $todayFans = self::getTodayNum($sjId);
- $time = strtotime(date("Y-m-d"));
- $list[$time] = $todayFans;
- ksort($list);
- return $list;
- }
-
- }
|