xhUserByMonthService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace common\services;
  3. use common\components\stringUtil;
  4. use Yii;
  5. use common\models\xhUserByMonth;
  6. use common\components\dict;
  7. class xhUserByMonthService {
  8. /**
  9. * 添加与更新
  10. */
  11. public static function replace($sjId,$userNum,$merchantAsset)
  12. {
  13. $year = date("Y");
  14. $month = date("n");
  15. $monthUser = self::getByIds($sjId, $year,$month);
  16. $totalNum = $merchantAsset['totalFans'];
  17. if(empty($monthUser)){
  18. $createTime = date("Y-m-d H:i:s");
  19. $data = ['year'=>$year,'month'=>$month,'riseNum'=>$userNum,'sjId'=>$sjId,'createTime'=>$createTime,'totalNum'=>$totalNum];
  20. self::add($data);
  21. }else{
  22. $newRiseNum = stringUtil::calcAdd($monthUser['riseNum'], $userNum);
  23. $data = ['riseNum'=>$newRiseNum,'totalNum'=>$totalNum];
  24. self::updateByIds($sjId, $year,$month, $data);
  25. }
  26. }
  27. public static function updateByIds($sjId, $year,$month,$data)
  28. {
  29. xhUserByMonth::updateByCondition(['year'=>$year,'month'=>$month,'sjId'=>$sjId], $data);
  30. self::getByIds($sjId, $year, $month);
  31. $cacheKey = dict::getCacheKey('xhUserByMonth').'_'.$sjId.'_'.$year.'_'.$month;
  32. $params = [$cacheKey];
  33. if(!empty($data)){
  34. foreach($data as $key => $val){
  35. $params[] = $key;
  36. $params[] = $val;
  37. }
  38. }
  39. Yii::$app->redis->executeCommand('HMSET',$params);
  40. return self::getByIds($sjId, $year,$month);
  41. }
  42. public static function add($data)
  43. {
  44. $return = xhUserByMonth::add($data);
  45. $sjId = $return['sjId'];
  46. $year = $return['year'];
  47. $month = $return['month'];
  48. $cacheKey = dict::getCacheKey('xhUserByMonth').'_'.$sjId.'_'.$year.'_'.$month;
  49. $params = [$cacheKey];
  50. if(!empty($return)){
  51. foreach($return as $key => $val){
  52. $params[] = $key;
  53. $params[] = $val;
  54. }
  55. }
  56. Yii::$app->redis->executeCommand('HMSET',$params);
  57. return $return;
  58. }
  59. public static function getByIds($sjId, $year,$month)
  60. {
  61. $cacheKey = dict::getCacheKey('xhUserByMonth').'_'.$sjId.'_'.$year.'_'.$month;
  62. $monthUser = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  63. if(!empty($monthUser)){
  64. $arr = [];
  65. $i = 0;
  66. $x = 0;
  67. foreach($monthUser as $key => $val){
  68. $i++;
  69. if($i%2 == 0){
  70. $arr[$x]['value'] = $val;
  71. $x++;
  72. }else{
  73. $arr[$x]['key'] = $val;
  74. }
  75. }
  76. $monthUserArr = [];
  77. foreach($arr as $key => $val){
  78. $monthUserArr[$val['key']] = $val['value'];
  79. }
  80. unset($monthUserArr['info_already_get_by_sql']);
  81. return $monthUserArr;
  82. }
  83. $monthUser = xhUserByMonth::find()->where(['sjId'=>$sjId,'year'=>$year,'month'=>$month])->asArray()->one();
  84. $params = [$cacheKey];
  85. $params[] = 'info_already_get_by_sql';
  86. $params[] = 'ok';
  87. if(!empty($monthUser)){
  88. foreach($monthUser as $key => $val){
  89. $params[] = $key;
  90. $params[] = $val;
  91. }
  92. }
  93. Yii::$app->redis->executeCommand('HMSET',$params);
  94. return $monthUser;
  95. }
  96. /**
  97. * 获取本月粉丝数
  98. */
  99. public static function getMonthUser($sjId,$year,$month)
  100. {
  101. $monthUser = self::getByIds($sjId, $year, $month);
  102. $num = 0;
  103. if(!empty($monthUser)){
  104. $num = $monthUser['riseNum'];
  105. }
  106. return $num;
  107. }
  108. }