LiveRoomLogLikeModel.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Master\Enum\RedisKeyEnum;
  5. use App\Model\Model;
  6. use App\Utils\RedisUtil;
  7. class LiveRoomLogLikeModel extends Model
  8. {
  9. /**
  10. * The table associated with the model.
  11. *
  12. * @var ?string
  13. */
  14. protected ?string $table = 'live_room_log_like';
  15. protected ?string $dateFormat = 'U';
  16. public bool $timestamps = false;
  17. protected int $is_status_search = 1;// 是否使用 1=是 0=否 默认使用 status = 1 筛选
  18. protected int $is_delete_search = 0;// 是否使用 1=是 0=否 默认使用 is_delete = 0 筛选
  19. /**
  20. * 默认查询字段
  21. *
  22. * @var array|string[]
  23. */
  24. public array $select = [
  25. '*'
  26. ];
  27. /**
  28. * 记录用户点赞数量
  29. * @param int $user_id
  30. * @param int $room_id
  31. * @param string $room_no
  32. * @param string $session
  33. * @param int $num
  34. * @return bool|int
  35. */
  36. public static function likes(int $user_id, int $room_id, string $room_no, string $session, int $num = 1)
  37. {
  38. if (!$info = self::query()->where(['user_id' => $user_id, 'room_id' => $room_id, 'room_no' => $room_no, 'session' => $session])->first()) {
  39. $res = self::query()->insert(['user_id' => $user_id, 'room_id' => $room_id, 'room_no' => $room_no, 'session' => $session, 'like' => $num]);
  40. } else {
  41. $res = self::query()->where('id', $info['id'])->increment('like', $num);
  42. }
  43. // redis 排行榜集合
  44. RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_LIST,$room_no)->zIncrBy($num,$user_id);
  45. return $res;
  46. }
  47. /**
  48. * 观众排行榜
  49. * @param string $room_no
  50. * @param int $user_id 主播ID
  51. * @param int $num
  52. * @return array
  53. * @throws \Exception
  54. */
  55. public function getTopList(string $room_no, int $user_id, int $num = 50)
  56. {
  57. $onList = RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_LIST, $room_no)->zRevRange(0, $num);
  58. $key = array_search($user_id, $onList);
  59. if ($key !== false) {
  60. unset($onList[$key]);
  61. } else if (count($onList) > $num) {
  62. // 如果没有主播自己,且数量大于 num 则需要去掉最后一个
  63. array_pop($onList);
  64. }
  65. $model = new UserModel();
  66. $list = $model->getList(params: ['ids' => $onList,'list_rows' => $num],select: ['id','nickname','avatar']);
  67. $list = array_columns($list,'id,nickname,avatar','id');
  68. $top = [];
  69. foreach ($onList as $key => $val) {
  70. if (isset($list[$val][0])){
  71. $top[] = [
  72. 'id' => (int)$val,
  73. 'nickname' => $list[$val][0]['nickname'],
  74. 'avatar' => cdn_url($list[$val][0]['avatar']),
  75. 'chat_id' => im_prefix($list[$val][0]['id']),
  76. 'rank' => $key + 1
  77. ];
  78. }
  79. }
  80. return $top;
  81. }
  82. // 开播日志
  83. public function user()
  84. {
  85. return $this->hasOne(UserModel::class, 'id', 'user_id');
  86. }
  87. }