RoomModel.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace App\Models\Fpdx;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Redis;
  5. /**
  6. * App\Models\Fpdx\RoomModel
  7. *
  8. * @property int $room_id
  9. * @property int $stage_id 期
  10. * @property string $room_name 房间名
  11. * @property array $member 成员
  12. * @property int $create_time 创建时间
  13. * @property int $type 1:分配对象;2:卖室友
  14. * @property string $attach
  15. * @property string $headimgurl 房间头像
  16. * @property int $open_at 开启时间
  17. * @property int $close_at 结束时间
  18. * @property int $keep 是否升级恋爱小屋
  19. * @property \Illuminate\Support\Carbon $created_at 创建时间
  20. * @property \Illuminate\Support\Carbon $updated_at 修改时间
  21. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Fpdx\PairModel[] $pairs
  22. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel newModelQuery()
  23. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel newQuery()
  24. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel query()
  25. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereAttach($value)
  26. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereCloseAt($value)
  27. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereCreateTime($value)
  28. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereCreatedAt($value)
  29. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereHeadimgurl($value)
  30. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereKeep($value)
  31. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereMember($value)
  32. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereOpenAt($value)
  33. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereRoomId($value)
  34. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereRoomName($value)
  35. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereStageId($value)
  36. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereType($value)
  37. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\RoomModel whereUpdatedAt($value)
  38. * @mixin \Eloquent
  39. */
  40. class RoomModel extends Model
  41. {
  42. protected $table = 'fpdx_pair_rooms';
  43. public $dateFormat = 'U';
  44. protected $primaryKey = 'room_id';
  45. protected $fillable = [
  46. 'room_id',
  47. 'stage_id',
  48. 'room_name',
  49. 'member',
  50. 'create_time',
  51. 'type',
  52. 'attach',
  53. 'open_at',
  54. 'close_at',
  55. 'headimgurl'
  56. ];
  57. /**
  58. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  59. */
  60. public function pairs()
  61. {
  62. return $this->hasMany(PairModel::class, 'room_id', 'room_id')->where('type', 1);
  63. }
  64. /**
  65. * 获取房间内的成员
  66. * @param $value
  67. * @return array
  68. */
  69. public function getMemberAttribute($value)
  70. {
  71. return explode(',', $value);
  72. }
  73. /**
  74. * 房间名称
  75. * @param $value
  76. * @return string
  77. */
  78. public function getRoomNameAttribute($value)
  79. {
  80. return $value ?: "第{$this->stage_id}期的房间";
  81. }
  82. /**
  83. * 获取某人某个类型的房间数量
  84. * @param int $type
  85. * @param int $uid
  86. * @return mixed
  87. */
  88. public function getCountByType(int $type, int $uid)
  89. {
  90. return $this->where('type', $type)->whereRaw("FIND_IN_SET(?, member)", [$uid])->count();
  91. }
  92. /**
  93. * 获取房间信息
  94. * @param $room_id
  95. * @param int $uid 序参考用户[0 => 匹配对象,..., 1 => uid ]
  96. * @return mixed
  97. * @throws \ApiException
  98. */
  99. public function getRoom($room_id, $uid = 0)
  100. {
  101. $room = $this->find($room_id);
  102. if (!collect($room)->isEmpty()) {
  103. $members = $room->member;
  104. $response = array();
  105. foreach ($members as $key => $value) {
  106. if ($value != $uid) {
  107. array_push($response, $value);
  108. }
  109. }
  110. if (0 != $uid) {
  111. array_push($response, $uid);
  112. }
  113. $room->member = $response;
  114. return $room;
  115. } else {
  116. throw new \ApiException("未找到房间", 404);
  117. }
  118. }
  119. /**
  120. * 获取某人某期的房间信息
  121. * @param $stage_id
  122. * @param $uid
  123. * @return mixed
  124. * @throws \ApiException
  125. */
  126. public function getRoomByStage($stage_id, $uid)
  127. {
  128. $room = $this->where('stage_id', $stage_id)->where('type', 1)->whereRaw(
  129. 'FIND_IN_SET(?, member)',
  130. [$uid]
  131. )->first();
  132. return $this->getRoom($room->room_id, $uid);
  133. }
  134. /**
  135. * 用户是否在某个房间里
  136. * @param int $room_id
  137. * @param int $uid
  138. * @return bool
  139. */
  140. public function isInRoom(int $room_id, int $uid)
  141. {
  142. $room = $this->where('room_id', $room_id)->whereRaw("FIND_IN_SET(?,member)", [$uid])->first();
  143. if (empty($room)) {
  144. return false;
  145. } else {
  146. return true;
  147. }
  148. }
  149. /**
  150. * 获取某人的某期的房间信息
  151. * @param $stage_id
  152. * @param $uid
  153. * @return object
  154. * {
  155. * "room_id": 房间id,
  156. * "room_name": 房间名称,
  157. * "stage_id": 期数,
  158. * "uid": 用户id,
  159. * "pair_uid": 匹配对象uid,
  160. * "create_time": unix时间戳
  161. * }
  162. * @throws \ApiException
  163. */
  164. public function getRoomByStageUid($stage_id, $uid)
  165. {
  166. $room = $this->where('stage_id', $stage_id)->where('type', 1)->whereRaw(
  167. 'FIND_IN_SET(?, member)',
  168. [$uid]
  169. )->first();
  170. if (empty($room)) {
  171. throw new \ApiException("未找到房间信息", 404);
  172. }
  173. $room = json_decode(json_encode($room[0]));
  174. $room->uid = $uid;
  175. $uids = explode(',', $room->member);
  176. array_map(function ($value) use (&$room) {
  177. if ($value != $room->uid) {
  178. $room->pair_uid = $value;
  179. }
  180. }, $uids);
  181. return $room;
  182. }
  183. /**
  184. * 通过期数获取房间列表
  185. * @param int $stage_id
  186. * @return \Illuminate\Support\Collection
  187. * @throws \ApiException
  188. * @deprecated
  189. */
  190. public function roomByStage(int $stage_id)
  191. {
  192. \DB::connection()->enableQueryLog();
  193. $rooms = $this->where('stage_id', '=', $stage_id)->get();
  194. foreach ($rooms as &$room) {
  195. $result = $this->getRoom($room->room_id);
  196. if (0 == $result['code']) {
  197. $room->member = $result['data']['member'];
  198. } else {
  199. $room->member = false;
  200. }
  201. }
  202. $rooms = $rooms->where('member', '!=', false);
  203. return $rooms;
  204. }
  205. /**
  206. * 通过成员获取卖室友房间
  207. * @param int $uid1
  208. * @param int $uid2
  209. * @return array|mixed
  210. */
  211. public function getRoomByMember(int $uid1, int $uid2)
  212. {
  213. $room = $this->where('type', 2)->where('member', "{$uid1},{$uid2}")->orWhere(
  214. 'member',
  215. "{$uid2},{$uid1}"
  216. )->first();
  217. return $room;
  218. }
  219. /**
  220. * 加入房间
  221. * @param int $room_id
  222. * @param string $uid
  223. * @return bool
  224. * @throws \ApiException
  225. */
  226. public function joinRoom(int $room_id, string $uid)
  227. {
  228. $room = $this->find($room_id);
  229. if (collect($room)->isEMpty()) {
  230. throw new \ApiException("操作受到限制", 101);
  231. }
  232. // $members = explode(',', $room->member);
  233. $members = $room->member;
  234. array_push($members, $uid);
  235. $members = array_unique($members);
  236. $member = trim(implode(',', $members), ",");
  237. $room->member = $member;
  238. if ($room->save()) {
  239. Redis::zadd("session_room_{$uid}", time(), $room_id);
  240. return true;
  241. } else {
  242. return false;
  243. }
  244. }
  245. /**
  246. * 获取房主信息by房间
  247. * @param int $room_id
  248. * @return MasterModel|Model|null|object
  249. * @throws \ApiException
  250. */
  251. public function getMaster(int $room_id)
  252. {
  253. $rm = RoomMasterModel::where([
  254. ['room_id', $room_id],
  255. ['type', 1]
  256. ])->first();
  257. if (collect($rm)->isEmpty()) {
  258. throw new \ApiException("未找到资源", 404);
  259. }
  260. $masterModel = new MasterModel();
  261. $master = $masterModel->getMaster($rm->uid);
  262. return $master;
  263. }
  264. /**
  265. * 获取管理员列表集合by房间
  266. * @param int $room_id
  267. * @return \Illuminate\Support\Collection
  268. */
  269. public function getAdministrator(int $room_id)
  270. {
  271. $rm = RoomMasterModel::where('room_id', $room_id)->get();
  272. $data = $rm->pluck('uid');
  273. return $data;
  274. }
  275. /**
  276. * 判断管理权限
  277. * @param int $room_id
  278. * @param int $uid
  279. * @return bool
  280. */
  281. public function isAdministrator(int $room_id, int $uid)
  282. {
  283. $rm = RoomMasterModel::where('room_id', $room_id)->get();
  284. $data = $rm->pluck('uid');
  285. if (in_array($uid, $data->toArray())) {
  286. return true;
  287. } else {
  288. return false;
  289. }
  290. }
  291. /**
  292. * 增加房主cp数
  293. * @param int $room_id
  294. * @param int $increment
  295. * @return bool
  296. * @throws \ApiException
  297. */
  298. public function incrementCps(int $room_id, int $increment = 1)
  299. {
  300. $master = $this->getMaster($room_id);
  301. MasterModel::where('uid', $master->uid)->increment('cps', $increment);
  302. return true;
  303. }
  304. /**
  305. * 增加房主点赞数
  306. * @param int $room_id
  307. * @param int $increment
  308. * @return bool
  309. * @throws \ApiException
  310. */
  311. public function incrementThumbs(int $room_id, int $increment = 1)
  312. {
  313. $master = $this->getMaster($room_id);
  314. MasterModel::where('uid', $master->uid)->increment('thumbs', $increment);
  315. return true;
  316. }
  317. }