123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- declare(strict_types=1);
- namespace App\Model\Arts;
- use App\Master\Framework\Library\Tencent\TencentIm;
- use App\Model\Model;
- use Hyperf\DbConnection\Db;
- class LiveRoomFollowModel extends Model
- {
- /**
- * The table associated with the model.
- *
- * @var ?string
- */
- protected ?string $table = 'live_room_follow';
- protected ?string $dateFormat = 'U';
- public bool $timestamps = false;
- protected int $is_status_search = 0;// 是否使用 1=是 0=否 默认使用 status = 1 筛选
- protected int $is_delete_search = 0;// 是否使用 1=是 0=否 默认使用 is_delete = 0 筛选
- /**
- * 默认查询字段
- *
- * @var array|string[]
- */
- public array $select = [
- '*'
- ];
- public function searchRoomNoAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('room_no', $value);
- }
- public function searchUserIdAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('user_id', $value);
- }
- public function searchKeywordAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->withWhereHas('user',function ($query) use($value) {
- $query->where('nickname','like',"%{$value}%");
- });
- }
- /**
- * 关注直播间
- * @param int $user_id
- * @param int $room_id
- * @param string $room_no
- * @param string $session
- * @return bool
- */
- public function follow(int $user_id,int $room_id,string $room_no,string $session): bool
- {
- if (!$info = $this->query()->where(['user_id'=>$user_id,'room_id'=>$room_id,'room_no'=>$room_no])->first()){
- $log = [
- 'user_id' => $user_id,
- 'room_id' => $room_id,
- 'room_no' => $room_no,
- 'session' => $session,
- 'status' => 1,
- 'create_time' => time(),
- ];
- if (!$this->query()->insertGetId($log)) {
- return $this->error('关注失败');
- }
- }else{
- if ($info->status == 1){
- return $this->error('已关注过了');
- }
- if (!$this->query()->where('id',$info->id)->update(['status'=>1,'session'=>$session,'create_time'=>time()])){
- return $this->error('关注失败了');
- }
- }
- return $this->success('关注成功');
- }
- /**
- * 取消关注
- * @param int $user_id
- * @param int $room_id
- * @param string $room_no
- * @return bool
- */
- public function cancelFollow(int $user_id,int $room_id,string $room_no)
- {
- if (!$info = $this->query()->where(['user_id'=>$user_id,'room_id'=>$room_id,'room_no'=>$room_no])->first()){
- return $this->error('您未关注过');
- }else{
- if ($info->status != 1){
- return $this->error('已取消关注');
- }
- if (!$this->query()->where('id',$info->id)->update(['status'=>0])){
- return $this->error('操作失败');
- }
- }
- return $this->success('取关成功');
- }
- // 开播日志
- public function log()
- {
- return $this->hasOne(LiveRoomLogModel::class, 'session', 'session');
- }
- // 用户信息
- public function user()
- {
- return $this->hasOne(UserModel::class, 'id', 'user_id');
- }
- }
|