OrderJobs.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Facades\Servers\Wechat\OpenWork;
  13. use App\Models\OpenWork\job\Records;
  14. use Illuminate\Support\Facades\DB;
  15. /**
  16. * 订单同步队列
  17. * @author
  18. * @version 1.0
  19. * @date 2025-04-14
  20. */
  21. class OrderJobs implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. protected $order_data_info;
  25. protected $Records;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(array $order_data_info)
  32. {
  33. //
  34. $this->order_data_info = $order_data_info;
  35. }
  36. public function getCorpId()
  37. {
  38. return $this->order_data_info['authCorpId'] ?? null;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. //
  48. $corpId = $this->order_data_info['authCorpId'];
  49. (new DbService())->getConnectionNameByCorpId($corpId);
  50. // 创建任务记录
  51. $this->Records = Records::create([
  52. 'job_id' => $this->order_data_info['authCorpId'] . '_OrderJobs',
  53. 'name' => static::class,
  54. 'payload' => json_encode($this->order_data_info),
  55. 'status' => 'processing',
  56. 'started_at' => now()
  57. ]);
  58. $authCorpId = $this->order_data_info['authCorpId'];
  59. $startTime = $this->order_data_info['startTime'];
  60. $endTime = $this->order_data_info['endTime'];
  61. $cursor = $this->order_data_info['cursor'];
  62. $limit = $this->order_data_info['limit'];
  63. try {
  64. // 获取实例,
  65. $app = OpenWork::getApp();
  66. // 数据结果
  67. $result = $app->license_order->list($authCorpId, $startTime, $endTime, $cursor, $limit);
  68. // 如果获取结果失败
  69. if (!$result) return json_send(['code' => 'error', 'msg' => '获取失败']);
  70. // 错误提示
  71. if ($result['errcode']) return json_send(['code' => 'error', 'msg' => OpenWork::getErrmsg($result['errcode'])]);
  72. //同步本地订单数据
  73. $sync_status = (new LicenseOrderService())->handle_order_data($authCorpId, $result['order_list']);
  74. // 列表结果
  75. $next_cursor = $result['next_cursor'];
  76. if ($next_cursor != '' && $sync_status && count($result['order_list']) > 0) {
  77. //添加队列处理订单数据
  78. $order_data_info = [
  79. 'authCorpId' => $authCorpId,
  80. 'startTime' => $startTime,
  81. 'endTime' => $endTime,
  82. 'cursor' => $next_cursor,
  83. 'limit' => $limit
  84. ];
  85. $queueName = $authCorpId.'_OrderJobs';
  86. OrderJobs::dispatch($order_data_info);//如果还有数据继续执行同步
  87. }
  88. //删除任务记录
  89. $this->Records->delete();
  90. } catch (\Exception $e) {
  91. // 失败处理...
  92. if ($this->Records) {
  93. $this->Records->delete();
  94. }
  95. Log::info('job_error', '同步订单账号激活码失败-异常原因',['data'=>$this->order_data_info,'error' => $e->getMessage()]);
  96. }
  97. }
  98. public function failed(\Throwable $exception)
  99. {
  100. Log::info('job_error', 'OrderJobs彻底失败', ['data'=>$this->order_data_info,'error' => $exception]);
  101. // 失败处理...
  102. if ($this->Records) {
  103. $this->Records->delete();
  104. }
  105. }
  106. }