OrderAccountJobs.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Jobs\OpenWork\License;
  3. use App\Servers\DB\DbService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use App\Servers\OpenWork\Corp\LicenseOrderService;
  11. use App\Facades\Servers\Logs\Log;
  12. use App\Models\OpenWork\job\Records;
  13. use Illuminate\Support\Facades\DB;
  14. /**
  15. * 订单账号激活码同步队列
  16. * @author 唐远望
  17. * @version 1.0
  18. * @date 2025-04-14
  19. */
  20. class OrderAccountJobs implements ShouldQueue
  21. {
  22. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  23. protected $request_account_data;
  24. protected $Records;
  25. /**
  26. * Create a new job instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct(array $request_account_data)
  31. {
  32. //
  33. $this->request_account_data = $request_account_data;
  34. }
  35. public function getCorpId()
  36. {
  37. return $this->request_account_data['corpid'] ?? null;
  38. }
  39. /**
  40. * Execute the job.
  41. *
  42. * @return void
  43. */
  44. public function handle()
  45. {
  46. $corpId = $this->request_account_data['corpid'];
  47. (new DbService())->getConnectionNameByCorpId($corpId);
  48. // 创建任务记录
  49. $this->Records = Records::create([
  50. 'job_id' => $this->request_account_data['order_id'] . '_OrderAccountJobs',
  51. 'name' => static::class,
  52. 'payload' => json_encode($this->request_account_data),
  53. 'status' => 'processing',
  54. 'started_at' => now()
  55. ]);
  56. $order_id = $this->request_account_data['order_id'];
  57. $cursor = $this->request_account_data['cursor'];
  58. $limit = $this->request_account_data['limit'];
  59. try {
  60. $result = (new LicenseOrderService())->request_handle_order_data($order_id, $cursor, $limit);
  61. if (!$result) {
  62. Log::info('job_error', '同步订单账号激活码失败-订单ID:',['error' => $this->request_account_data]);
  63. }
  64. if ($result['errcode']) {
  65. Log::info('job_error', '同步订单账号激活码失败-错误原因', ['data'=>$this->request_account_data,'error' => $result]);
  66. }
  67. $this->Records->delete();
  68. } catch (\Exception $e) {
  69. // 失败处理...
  70. if ($this->Records) {
  71. $this->Records->delete();
  72. }
  73. Log::info('job_error', '同步订单账号激活码失败-异常原因',['data'=>$this->request_account_data,'error' => $e->getMessage()]);
  74. }
  75. }
  76. public function failed(\Throwable $exception)
  77. {
  78. Log::info('job_error', 'OrderAccountJobs彻底失败', ['data'=>$this->request_account_data,'error' => $exception]);
  79. // 失败处理...
  80. if ($this->Records) {
  81. $this->Records->delete();
  82. }
  83. }
  84. }