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