IntoRoomCommand.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Console\Commands\Pair;
  3. use App\Events\PairSuccess;
  4. use App\Models\Fpdx\ActivityModel;
  5. use App\Models\Fpdx\PairModel;
  6. use App\Models\Fpdx\RoomModel;
  7. use App\Services\Deed\FriendService;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\Redis;
  10. class IntoRoomCommand extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'pair:into:room';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '分配对象::匹配成功送入房间';
  24. private $stage_id;
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. public function handle()
  35. {
  36. $this->stage_id = ActivityModel::where('signend_time', '<=', time())
  37. ->where('close_time', '>=', time())->value('stage_id');
  38. $pairs = PairModel::where([
  39. 'stage_id' => $this->stage_id,
  40. 'room_id' => 0,
  41. 'activity_type' => '72h'
  42. ])->whereNotNull('assoc_id')
  43. ->where('partner', 1)->get();
  44. $fs = new FriendService();
  45. foreach ($pairs as /** @var PairModel $pair */ $pair) {
  46. try {
  47. \DB::beginTransaction();
  48. /** @var PairModel $other */
  49. $other = PairModel::findOrFail($pair->assoc_id);
  50. $room = RoomModel::create([
  51. 'stage_id' => $this->stage_id,
  52. 'room_name' => "第{$this->stage_id}期的房间",
  53. 'member' => implode(',', [$pair->uid, $other->uid]),
  54. 'create_time' => time(),
  55. 'type' => 1
  56. ]);
  57. $pair->room_id = $room->room_id;
  58. $pair->save();
  59. $other->room_id = $room->room_id;
  60. $other->save();
  61. Redis::zadd("session_room_{$pair->uid}", [$room->room_id => time()]);
  62. Redis::zadd("session_room_{$other->uid}", [$room->room_id => time()]);
  63. Redis::sadd("fpdx_pairs_{$pair->uid}", [$other->uid]);
  64. Redis::sadd("fpdx_pairs_{$other->uid}", [$pair->uid]);
  65. event(new PairSuccess($pair->uid, $other->uid, $room->room_id));
  66. \DB::commit();
  67. } catch (\Exception $e) {
  68. \DB::rollBack();
  69. app('sentry')->captureException($e);
  70. \Log::warning("创建房间失败:【{$pair->id}】|【{$pair->assoc_id}】");
  71. }
  72. }
  73. // 结束钉钉通知
  74. \Curl::to("http://api.deep.fenpeiduixiang.com/api/ding/roomstate/end/{$this->stage_id}")->get();
  75. }
  76. }