MatchService.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Services\Goodnight;
  3. use App\Events\GoodnightSuccess;
  4. use App\Http\Controllers\Core\Ding;
  5. use App\Models\Goodnight\EnterModel;
  6. use App\Services\Deed\FriendService;
  7. use App\Services\Service;
  8. class MatchService extends Service
  9. {
  10. /**
  11. * 启动匹配任务
  12. */
  13. public function run()
  14. {
  15. if (app()->environment() == 'production') {
  16. Ding::robot([
  17. 'title' => '[晚安伴侣]匹配|开始',
  18. 'text' => "匹配|开始",
  19. ]);
  20. }
  21. $es = new EnterService();
  22. $rms = new RoomService();
  23. $fs = new FriendService();
  24. // 1. 加载待匹配数据
  25. $acty = $es->getActivity("匹配");
  26. $datas = \DB::table('kdgx_goodnight_enters as te')
  27. ->join('kdgx_goodnight_user as tgu', 'te.uid', '=', 'tgu.uid')
  28. ->join('kdgx_partner_charge_user as tu', 'te.uid', '=', 'tu.uid')
  29. ->where([['te.type', '报名'], ['room_id', 0], ['activity_id', $acty->id], ['match_at', 0]])
  30. ->select('te.*', 'tgu.like_sex', 'tu.sex', 'tu.nickname')->get();
  31. // 2. 规则匹配
  32. foreach ($datas as &$data) {
  33. foreach ($datas as &$value) {
  34. if ($data->room_id > 0 || $value->room_id > 0) {
  35. continue;
  36. }
  37. if ($data->id == $value->id) {
  38. continue;
  39. }
  40. if ($data->like_sex != $value->sex || $value->like_sex != $data->sex) {
  41. continue;
  42. }
  43. try {
  44. $room_id = $rms->create([$data->id, $value->id], $acty->id);
  45. } catch (\Exception $e) {
  46. dump($e);
  47. }
  48. $data->room_id = $room_id;
  49. $value->room_id = $room_id;
  50. EnterModel::where('id', $data->id)->update(['room_id' => $room_id]);
  51. EnterModel::where('id', $value->id)->update(['room_id' => $room_id]);
  52. // 匹配成功通知
  53. $ns = new NoticeService();
  54. $ns->matchSuccess($data->uid, $value->uid);
  55. $ns->matchSuccess($value->uid, $data->uid);
  56. }
  57. if ($data->room_id == 0) {
  58. // 匹配失败通知
  59. $ns = new NoticeService();
  60. $ns->matchFail($data->uid);
  61. }
  62. EnterModel::where('id', $data->id)->update(['match_at' => time()]);
  63. }
  64. if (app()->environment() == 'production') {
  65. Ding::robot([
  66. 'title' => '[晚安伴侣]匹配|完成',
  67. 'text' => "匹配|完成",
  68. ]);
  69. }
  70. }
  71. }