SportGameOrderJob.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Support\Facades\Log;
  9. use App\Models\SportGameOrder;
  10. use App\Models\Wallet;
  11. use App\Models\FundsRecord;
  12. use App\Services\AdminRealtime\AdminRealtime;
  13. use Illuminate\Support\Facades\DB;
  14. class SportGameOrderJob implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. /**
  18. * 任务尝试次数
  19. */
  20. public $tries = 3;
  21. /**
  22. * 任务超时时间(秒)
  23. */
  24. public $timeout = 30;
  25. public $gameplayId = 0;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(int $gameplayId)
  32. {
  33. $this->gameplayId = $gameplayId;
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. Log::error('开始执行足球游戏订单开奖结算任务: ' . now());
  43. //先把未中奖的标记为未中奖
  44. DB::transaction(function () {
  45. $query = SportGameOrder::where('gameplay_id', '<>', $this->gameplayId)->where('status', 0);
  46. $ids = (clone $query)->lockForUpdate()->pluck('id')->all();
  47. $query->update(['status' => 1]);
  48. \App\Services\Reporting\ReportContext::sportGamesSettled($ids);
  49. AdminRealtime::record('sport_game_order', $ids);
  50. });
  51. $list = SportGameOrder::where('gameplay_id', $this->gameplayId)->where('status', 0)->get();
  52. foreach ($list as $item) {
  53. try {
  54. DB::beginTransaction();
  55. $item->status = 2;
  56. $item->win_amount = bcmul($item->amount, $item->odds, 2);
  57. $item->profit_and_loss = $item->win_amount - $item->amount;
  58. $item->updated_at = now();
  59. $item->save();
  60. $walletInfo = Wallet::where('member_id', $item->member_id)->first();
  61. if (!$walletInfo) continue;
  62. $before = $walletInfo->available_balance;
  63. $after = bcadd($walletInfo->available_balance, $item->win_amount, 2);
  64. $walletInfo->available_balance = $after;
  65. $walletInfo->save();
  66. FundsRecord::addData([
  67. 'change_type' => '足球游戏中奖',
  68. 'amount' => $item->win_amount,
  69. 'before_balance' => $before,
  70. 'after_balance' => $after,
  71. 'member_id' => $item->member_id,
  72. 'related_id' => $item->id,
  73. 'remark' => '足球游戏中奖',
  74. ]);
  75. DB::commit();
  76. } catch (\Exception $e) {
  77. DB::rollBack();
  78. Log::error('足球游戏订单开奖结算任务执行异常: ' . $e->getMessage());
  79. }
  80. }
  81. }
  82. // 可选:失败处理
  83. public function failed(\Throwable $exception)
  84. {
  85. Log::error('足球游戏订单开奖结算任务执行异常: ' . $exception->getMessage());
  86. }
  87. }