| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace common\services;
- use common\components\stringUtil;
- use Yii;
- use common\models\xhUserByMonth;
- use common\components\dict;
- class xhUserByMonthService {
- /**
- * 添加与更新
- */
- public static function replace($sjId,$userNum,$merchantAsset)
- {
- $year = date("Y");
- $month = date("n");
- $monthUser = self::getByIds($sjId, $year,$month);
- $totalNum = $merchantAsset['totalFans'];
- if(empty($monthUser)){
- $createTime = date("Y-m-d H:i:s");
- $data = ['year'=>$year,'month'=>$month,'riseNum'=>$userNum,'sjId'=>$sjId,'createTime'=>$createTime,'totalNum'=>$totalNum];
- self::add($data);
- }else{
- $newRiseNum = stringUtil::calcAdd($monthUser['riseNum'], $userNum);
- $data = ['riseNum'=>$newRiseNum,'totalNum'=>$totalNum];
- self::updateByIds($sjId, $year,$month, $data);
- }
- }
- public static function updateByIds($sjId, $year,$month,$data)
- {
- xhUserByMonth::updateByCondition(['year'=>$year,'month'=>$month,'sjId'=>$sjId], $data);
- self::getByIds($sjId, $year, $month);
- $cacheKey = dict::getCacheKey('xhUserByMonth').'_'.$sjId.'_'.$year.'_'.$month;
- $params = [$cacheKey];
- if(!empty($data)){
- foreach($data as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return self::getByIds($sjId, $year,$month);
- }
- public static function add($data)
- {
- $return = xhUserByMonth::add($data);
- $sjId = $return['sjId'];
- $year = $return['year'];
- $month = $return['month'];
- $cacheKey = dict::getCacheKey('xhUserByMonth').'_'.$sjId.'_'.$year.'_'.$month;
- $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, $year,$month)
- {
- $cacheKey = dict::getCacheKey('xhUserByMonth').'_'.$sjId.'_'.$year.'_'.$month;
- $monthUser = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($monthUser)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($monthUser as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $monthUserArr = [];
- foreach($arr as $key => $val){
- $monthUserArr[$val['key']] = $val['value'];
- }
- unset($monthUserArr['info_already_get_by_sql']);
- return $monthUserArr;
- }
- $monthUser = xhUserByMonth::find()->where(['sjId'=>$sjId,'year'=>$year,'month'=>$month])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';
- if(!empty($monthUser)){
- foreach($monthUser as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $monthUser;
- }
- /**
- * 获取本月粉丝数
- */
- public static function getMonthUser($sjId,$year,$month)
- {
- $monthUser = self::getByIds($sjId, $year, $month);
- $num = 0;
- if(!empty($monthUser)){
- $num = $monthUser['riseNum'];
- }
- return $num;
- }
- }
|