LiveRoomFollowModel.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Master\Framework\Library\Tencent\TencentIm;
  5. use App\Model\Model;
  6. use Hyperf\DbConnection\Db;
  7. class LiveRoomFollowModel extends Model
  8. {
  9. /**
  10. * The table associated with the model.
  11. *
  12. * @var ?string
  13. */
  14. protected ?string $table = 'live_room_follow';
  15. protected ?string $dateFormat = 'U';
  16. public bool $timestamps = false;
  17. protected int $is_status_search = 0;// 是否使用 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. public function searchRoomNoAttribute($query, $value, array $params): mixed
  28. {
  29. if (empty($value)) {
  30. return $query;
  31. }
  32. return $query->where('room_no', $value);
  33. }
  34. public function searchUserIdAttribute($query, $value, array $params): mixed
  35. {
  36. if (empty($value)) {
  37. return $query;
  38. }
  39. return $query->where('user_id', $value);
  40. }
  41. public function searchKeywordAttribute($query, $value, array $params): mixed
  42. {
  43. if (empty($value)) {
  44. return $query;
  45. }
  46. return $query->withWhereHas('user',function ($query) use($value) {
  47. $query->where('nickname','like',"%{$value}%");
  48. });
  49. }
  50. /**
  51. * 关注直播间
  52. * @param int $user_id
  53. * @param int $room_id
  54. * @param string $room_no
  55. * @param string $session
  56. * @return bool
  57. */
  58. public function follow(int $user_id,int $room_id,string $room_no,string $session): bool
  59. {
  60. if (!$info = $this->query()->where(['user_id'=>$user_id,'room_id'=>$room_id,'room_no'=>$room_no])->first()){
  61. $log = [
  62. 'user_id' => $user_id,
  63. 'room_id' => $room_id,
  64. 'room_no' => $room_no,
  65. 'session' => $session,
  66. 'status' => 1,
  67. 'create_time' => time(),
  68. ];
  69. if (!$this->query()->insertGetId($log)) {
  70. return $this->error('关注失败');
  71. }
  72. }else{
  73. if ($info->status == 1){
  74. return $this->error('已关注过了');
  75. }
  76. if (!$this->query()->where('id',$info->id)->update(['status'=>1,'session'=>$session,'create_time'=>time()])){
  77. return $this->error('关注失败了');
  78. }
  79. }
  80. return $this->success('关注成功');
  81. }
  82. /**
  83. * 取消关注
  84. * @param int $user_id
  85. * @param int $room_id
  86. * @param string $room_no
  87. * @return bool
  88. */
  89. public function cancelFollow(int $user_id,int $room_id,string $room_no)
  90. {
  91. if (!$info = $this->query()->where(['user_id'=>$user_id,'room_id'=>$room_id,'room_no'=>$room_no])->first()){
  92. return $this->error('您未关注过');
  93. }else{
  94. if ($info->status != 1){
  95. return $this->error('已取消关注');
  96. }
  97. if (!$this->query()->where('id',$info->id)->update(['status'=>0])){
  98. return $this->error('操作失败');
  99. }
  100. }
  101. return $this->success('取关成功');
  102. }
  103. // 开播日志
  104. public function log()
  105. {
  106. return $this->hasOne(LiveRoomLogModel::class, 'session', 'session');
  107. }
  108. // 用户信息
  109. public function user()
  110. {
  111. return $this->hasOne(UserModel::class, 'id', 'user_id');
  112. }
  113. }