| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace common\services;
- use Yii;
- use common\models\xhUserByDay;
- use common\components\dict;
- use common\components\stringUtil;
- class xhUserByDayService {
- /**
- * 添加与更新
- */
- public static function replace($sjId,$riseNum,$merchantAsset)
- {
- $time = strtotime(date("Y-m-d"));
- $dayUser = self::getByIds($sjId, $time);
- $totalFans = $merchantAsset['totalFans'];
- if(empty($dayUser)){
- $createTime = date("Y-m-d H:i:s");
- $data = ['time'=>$time,'riseNum'=>$riseNum,'sjId'=>$sjId,'createTime'=>$createTime,'totalNum'=>$totalFans];
- self::add($data);
- }else{
- $newRiseNum = stringUtil::calcAdd($dayUser['riseNum'], $riseNum);
- $data = ['riseNum'=>$newRiseNum,'totalNum'=>$totalFans];
- self::updateByIds($sjId, $time, $data);
- }
- }
- public static function updateByIds($sjId, $time,$data)
- {
- xhUserByDay::updateByCondition(['time'=>$time,'sjId'=>$sjId], $data);
- self::getByIds($sjId, $time);
- $cacheKey = dict::getCacheKey('xhUserByDay').'_'.$sjId.'_'.$time;
- $params = [$cacheKey];
- if(!empty($data)){
- foreach($data as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return self::getByIds($sjId, $time);
- }
- public static function add($data)
- {
- $return = xhUserByDay::add($data);
- $sjId = $return['sjId'];
- $time = $return['time'];
- $cacheKey = dict::getCacheKey('xhUserByDay').'_'.$sjId.'_'.$time;
- $params = [$cacheKey];
- if(!empty($return)){
- foreach($return as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $return;
- }
- public static function getByIds($sjId,$time)
- {
- $cacheKey = dict::getCacheKey('xhUserByDay').'_'.$sjId.'_'.$time;
- $dayUser = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($dayUser)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($dayUser as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $dayUserArr = [];
- foreach($arr as $key => $val){
- $dayUserArr[$val['key']] = $val['value'];
- }
- unset($dayUserArr['info_already_get_by_sql']);
- return $dayUserArr;
- }
- $dayUser = xhUserByDay::find()->where(['sjId' => $sjId,'time'=>$time])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';
- if(!empty($dayUser)){
- foreach($dayUser as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $dayUser;
- }
- public static function getTodayUser($sjId,$time)
- {
- $dayUser = self::getByIds($sjId, $time);
- $num = 0;
- if(!empty($dayUser)){
- $num = $dayUser['riseNum'];
- }
- return $num;
- }
- /**
- * 取最近的粉丝变化情况
- */
- public static function getLatestNum($sjId,$num=14)
- {
- $arr = [];
- $dateList = [];
- $numList = [];
- for($i=$num;$i>=0;$i--){
- $time = strtotime(date("Y-m-d")) - $i*86400;
- $date = date("n/j",$time);
- $user = self::getByIds($sjId, $time);
- $fansNum = 0;
- if(!empty($user)){
- $fansNum = $user['riseNum'];
- }
- $arr[$date] = $fansNum;
- $dateList[] = $date;
- $numList[] = $fansNum;
- }
- return ['list'=>$arr,'date'=>$dateList,'num'=>$numList];
- }
- }
|