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