CustomerJobs.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Jobs\OpenWork\External;
  3. use App\Servers\DB\DbService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Servers\OpenWork\External\ContactService;
  10. use App\Facades\Servers\Logs\Log;
  11. use App\Models\OpenWork\job\Records;
  12. /**
  13. * 成员客户信息同步队列
  14. * @version 1.0
  15. * @date 2025-04-10
  16. */
  17. class CustomerJobs implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. private $customer_data;
  21. private ContactService $contact_service;
  22. private $Records;
  23. /**
  24. * Create a new job instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct(array $customer_data)
  29. {
  30. //
  31. $this->customer_data = $customer_data;
  32. $this->contact_service = app(ContactService::class);
  33. }
  34. public function getCorpId()
  35. {
  36. return $this->customer_data['corpid'] ?? null;
  37. }
  38. /**
  39. * Execute the job.
  40. *
  41. * @return void
  42. */
  43. public function handle()
  44. {
  45. try {
  46. $corpId = $this->customer_data['corpid'];
  47. (new DbService())->getConnectionNameByCorpId($corpId);
  48. // 创建任务记录
  49. $this->Records = Records::create([
  50. 'job_id' => $this->customer_data['corpid'] . '_CustomerJobs',
  51. 'name' => static::class,
  52. 'payload' => json_encode($this->customer_data),
  53. 'status' => 'processing',
  54. 'started_at' => now()
  55. ]);
  56. //查询客户信息
  57. $corpid = $this->customer_data['corpid'];
  58. $userid_list = $this->customer_data['userid_list'];
  59. $cursor = $this->customer_data['cursor'];
  60. $limit = $this->customer_data['limit'];
  61. $result = $this->contact_service->get_external_contact_batchget($corpid, $userid_list, $cursor, $limit);
  62. //删除任务记录
  63. $this->Records->delete();
  64. // 成功处理...
  65. } catch (\Exception $e) {
  66. // 失败处理...
  67. if ($this->Records) {
  68. $this->Records->delete();
  69. }
  70. Log::info('job_error', 'CustomerJobs任务队列失败', ['data' => $this->customer_data, 'error' => $e->getMessage()]);
  71. }
  72. }
  73. public function failed(\Throwable $exception)
  74. {
  75. Log::info('job_error', 'CustomerJobs任务队列彻底失败', ['data' => $this->customer_data, 'error' => $exception->getMessage()]);
  76. // 失败处理...
  77. if ($this->Records) {
  78. $this->Records->delete();
  79. }
  80. }
  81. }