xhUserByDayService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\models\xhUserByDay;
  5. use common\components\dict;
  6. use common\components\stringUtil;
  7. class xhUserByDayService {
  8. /**
  9. * 添加与更新
  10. */
  11. public static function replace($sjId,$riseNum,$merchantAsset)
  12. {
  13. $time = strtotime(date("Y-m-d"));
  14. $dayUser = self::getByIds($sjId, $time);
  15. $totalFans = $merchantAsset['totalFans'];
  16. if(empty($dayUser)){
  17. $createTime = date("Y-m-d H:i:s");
  18. $data = ['time'=>$time,'riseNum'=>$riseNum,'sjId'=>$sjId,'createTime'=>$createTime,'totalNum'=>$totalFans];
  19. self::add($data);
  20. }else{
  21. $newRiseNum = stringUtil::calcAdd($dayUser['riseNum'], $riseNum);
  22. $data = ['riseNum'=>$newRiseNum,'totalNum'=>$totalFans];
  23. self::updateByIds($sjId, $time, $data);
  24. }
  25. }
  26. public static function updateByIds($sjId, $time,$data)
  27. {
  28. xhUserByDay::updateByCondition(['time'=>$time,'sjId'=>$sjId], $data);
  29. self::getByIds($sjId, $time);
  30. $cacheKey = dict::getCacheKey('xhUserByDay').'_'.$sjId.'_'.$time;
  31. $params = [$cacheKey];
  32. if(!empty($data)){
  33. foreach($data as $key => $val){
  34. $params[] = $key;
  35. $params[] = $val;
  36. }
  37. }
  38. Yii::$app->redis->executeCommand('HMSET',$params);
  39. return self::getByIds($sjId, $time);
  40. }
  41. public static function add($data)
  42. {
  43. $return = xhUserByDay::add($data);
  44. $sjId = $return['sjId'];
  45. $time = $return['time'];
  46. $cacheKey = dict::getCacheKey('xhUserByDay').'_'.$sjId.'_'.$time;
  47. $params = [$cacheKey];
  48. if(!empty($return)){
  49. foreach($return as $key => $val){
  50. $params[] = $key;
  51. $params[] = $val;
  52. }
  53. }
  54. Yii::$app->redis->executeCommand('HMSET',$params);
  55. return $return;
  56. }
  57. public static function getByIds($sjId,$time)
  58. {
  59. $cacheKey = dict::getCacheKey('xhUserByDay').'_'.$sjId.'_'.$time;
  60. $dayUser = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  61. if(!empty($dayUser)){
  62. $arr = [];
  63. $i = 0;
  64. $x = 0;
  65. foreach($dayUser as $key => $val){
  66. $i++;
  67. if($i%2 == 0){
  68. $arr[$x]['value'] = $val;
  69. $x++;
  70. }else{
  71. $arr[$x]['key'] = $val;
  72. }
  73. }
  74. $dayUserArr = [];
  75. foreach($arr as $key => $val){
  76. $dayUserArr[$val['key']] = $val['value'];
  77. }
  78. unset($dayUserArr['info_already_get_by_sql']);
  79. return $dayUserArr;
  80. }
  81. $dayUser = xhUserByDay::find()->where(['sjId' => $sjId,'time'=>$time])->asArray()->one();
  82. $params = [$cacheKey];
  83. $params[] = 'info_already_get_by_sql';
  84. $params[] = 'ok';
  85. if(!empty($dayUser)){
  86. foreach($dayUser as $key => $val){
  87. $params[] = $key;
  88. $params[] = $val;
  89. }
  90. }
  91. Yii::$app->redis->executeCommand('HMSET',$params);
  92. return $dayUser;
  93. }
  94. public static function getTodayUser($sjId,$time)
  95. {
  96. $dayUser = self::getByIds($sjId, $time);
  97. $num = 0;
  98. if(!empty($dayUser)){
  99. $num = $dayUser['riseNum'];
  100. }
  101. return $num;
  102. }
  103. /**
  104. * 取最近的粉丝变化情况
  105. */
  106. public static function getLatestNum($sjId,$num=14)
  107. {
  108. $arr = [];
  109. $dateList = [];
  110. $numList = [];
  111. for($i=$num;$i>=0;$i--){
  112. $time = strtotime(date("Y-m-d")) - $i*86400;
  113. $date = date("n/j",$time);
  114. $user = self::getByIds($sjId, $time);
  115. $fansNum = 0;
  116. if(!empty($user)){
  117. $fansNum = $user['riseNum'];
  118. }
  119. $arr[$date] = $fansNum;
  120. $dateList[] = $date;
  121. $numList[] = $fansNum;
  122. }
  123. return ['list'=>$arr,'date'=>$dateList,'num'=>$numList];
  124. }
  125. }