SportGameOrderJob.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. SportGameOrder::where('gameplay_id', '<>', $this->gameplayId)->where('status', 0)->update(['status' => 1]);
  44. $list = SportGameOrder::where('gameplay_id', $this->gameplayId)->where('status', 0)->get();
  45. foreach ($list as $item) {
  46. try {
  47. DB::beginTransaction();
  48. $item->status = 2;
  49. $item->win_amount = bcmul($item->amount, $item->odds, 2);
  50. $item->profit_and_loss = $item->win_amount - $item->amount;
  51. $item->updated_at = now();
  52. $item->save();
  53. $walletInfo = Wallet::where('member_id', $item->member_id)->first();
  54. if (!$walletInfo) continue;
  55. $before = $walletInfo->available_balance;
  56. $after = bcadd($walletInfo->available_balance, $item->win_amount, 2);
  57. $walletInfo->available_balance = $after;
  58. $walletInfo->save();
  59. FundsRecord::addData([
  60. 'change_type' => '足球游戏中奖',
  61. 'amount' => $item->win_amount,
  62. 'before_balance' => $before,
  63. 'after_balance' => $after,
  64. 'member_id' => $item->member_id,
  65. 'related_id' => $item->id,
  66. 'remark' => '足球游戏中奖',
  67. ]);
  68. DB::commit();
  69. } catch (\Exception $e) {
  70. DB::rollBack();
  71. Log::error('足球游戏订单开奖结算任务执行异常: ' . $e->getMessage());
  72. }
  73. }
  74. }
  75. // 可选:失败处理
  76. public function failed(\Throwable $exception)
  77. {
  78. Log::error('足球游戏订单开奖结算任务执行异常: ' . $exception->getMessage());
  79. }
  80. }