1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- declare(strict_types=1);
- namespace App\Model\Arts;
- use App\Master\Enum\RedisKeyEnum;
- use App\Model\Model;
- use App\Utils\RedisUtil;
- class LiveRoomLogLikeModel extends Model
- {
- /**
- * The table associated with the model.
- *
- * @var ?string
- */
- protected ?string $table = 'live_room_log_like';
- protected ?string $dateFormat = 'U';
- public bool $timestamps = false;
- protected int $is_status_search = 1;// 是否使用 1=是 0=否 默认使用 status = 1 筛选
- protected int $is_delete_search = 0;// 是否使用 1=是 0=否 默认使用 is_delete = 0 筛选
- /**
- * 默认查询字段
- *
- * @var array|string[]
- */
- public array $select = [
- '*'
- ];
- /**
- * 记录用户点赞数量
- * @param int $user_id
- * @param int $room_id
- * @param string $room_no
- * @param string $session
- * @param int $num
- * @return bool|int
- */
- public static function likes(int $user_id, int $room_id, string $room_no, string $session, int $num = 1)
- {
- if (!$info = self::query()->where(['user_id' => $user_id, 'room_id' => $room_id, 'room_no' => $room_no, 'session' => $session])->first()) {
- $res = self::query()->insert(['user_id' => $user_id, 'room_id' => $room_id, 'room_no' => $room_no, 'session' => $session, 'like' => $num]);
- } else {
- $res = self::query()->where('id', $info['id'])->increment('like', $num);
- }
- // redis 排行榜集合
- RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_LIST,$room_no)->zIncrBy($num,$user_id);
- return $res;
- }
- /**
- * 观众排行榜
- * @param string $room_no
- * @param int $user_id 主播ID
- * @param int $num
- * @return array
- * @throws \Exception
- */
- public function getTopList(string $room_no, int $user_id, int $num = 50)
- {
- $onList = RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_LIST, $room_no)->zRevRange(0, $num);
- $key = array_search($user_id, $onList);
- if ($key !== false) {
- unset($onList[$key]);
- } else if (count($onList) > $num) {
- // 如果没有主播自己,且数量大于 num 则需要去掉最后一个
- array_pop($onList);
- }
- $model = new UserModel();
- $list = $model->getList(params: ['ids' => $onList,'list_rows' => $num],select: ['id','nickname','avatar']);
- $list = array_columns($list,'id,nickname,avatar','id');
- $top = [];
- foreach ($onList as $key => $val) {
- if (isset($list[$val][0])){
- $top[] = [
- 'id' => (int)$val,
- 'nickname' => $list[$val][0]['nickname'],
- 'avatar' => cdn_url($list[$val][0]['avatar']),
- 'chat_id' => im_prefix($list[$val][0]['id']),
- 'rank' => $key + 1
- ];
- }
- }
- return $top;
- }
- // 开播日志
- public function user()
- {
- return $this->hasOne(UserModel::class, 'id', 'user_id');
- }
- }
|