RoomService.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Services\Goodnight;
  3. use App\Models\Goodnight\EnterModel;
  4. use App\Models\Goodnight\RoomMemberModel;
  5. use App\Models\Goodnight\RoomModel;
  6. use App\Models\Goodnight\UserModel;
  7. use App\Services\Service;
  8. class RoomService extends Service
  9. {
  10. /**
  11. * 创建房间
  12. * @param int $activity_id
  13. * @param array $member 报名id的数组
  14. * @return int $room_id
  15. */
  16. public function create(array $member, int $activity_id)
  17. {
  18. $enters = EnterModel::whereIn('id', $member)->where('activity_id', $activity_id)->get();
  19. // 创建房间
  20. $room = RoomModel::create([
  21. 'activity_id' => $activity_id,
  22. "name" => date('Y-m-d') . "晚安聊天室",
  23. "number" => $enters->count(),
  24. ]);
  25. // 入房
  26. foreach ($enters as $enter) {
  27. $voice = UserModel::where('uid', $enter->uid)->value('voice');
  28. RoomMemberModel::create([
  29. 'uid' => $enter->uid,
  30. 'room_id' => $room->id,
  31. 'day1_voice' => $voice,
  32. ]);
  33. $enter->room_id = $room->id;
  34. $enter->save();
  35. }
  36. return $room->id;
  37. }
  38. }