SportGameOrderJob.php 3.1 KB

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