AgentUpdateChildUidsByAllParentUidsJob.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Mysql\AgentModel;
  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. class AgentUpdateChildUidsByAllParentUidsJob implements ShouldQueue
  10. {
  11. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  12. /**
  13. * 构造入参.
  14. * @var array|mixed
  15. */
  16. private $params = [];
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @return void
  21. */
  22. public function __construct($params = [])
  23. {
  24. $this->params = $params;
  25. }
  26. /**
  27. * 遍历所有上级的每个对象的字段child_uids,重新更新child_uids内容.
  28. *
  29. * @return mixed|void
  30. */
  31. public function handle()
  32. {
  33. $currUid = $this->params['uid'];
  34. $agentModel = new AgentModel();
  35. $item = AgentModel::query()->where(['uid' => $currUid])->first(['id', 'uid', 'parent_uid', 'parent_uids']);
  36. $strParentUids = $item->parent_uids;
  37. $arrParentUids = !empty($strParentUids) ? explode(',', $strParentUids) : [];
  38. if (!empty($arrParentUids)) {
  39. foreach ($arrParentUids as $parentUid) {
  40. $tmpObj = AgentModel::query()->where(['uid' => $parentUid])->first(['id', 'uid', 'child_uids']);
  41. $arrChildUids = $agentModel->findAllChildUids($parentUid);
  42. $strChildUids = !empty($arrChildUids) ? implode(',', $arrChildUids) : '';
  43. $tmpObj->child_uids = $strChildUids;
  44. $tmpObj->save();
  45. }
  46. }
  47. }
  48. /**
  49. * 任务失败的处理过程.
  50. * @param null $exception
  51. */
  52. public function fail($exception = null)
  53. {
  54. // 失败后的处理,比如发送通知.
  55. }
  56. /**
  57. * 通知.
  58. */
  59. private function notify()
  60. {
  61. // Todo 发送通知.
  62. }
  63. }