TagRelation.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Facades\Servers\WeiBan\OpenApi;
  5. use App\Models\WeiBan\Sync as Model;
  6. use App\Jobs\WeiBanSync as WeiBanSyncJobs;
  7. use App\Facades\Servers\Logs\Log;
  8. class TagRelation extends Command
  9. {
  10. /**
  11. * 任务名称
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'tag_relation';
  16. /**
  17. * 任务描述
  18. *
  19. * @var string
  20. */
  21. protected $description = '微伴标签数据同步任务';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. try {
  39. // 数据结果
  40. $this->sync_user(new Model());
  41. // 返回结果
  42. return 0;
  43. } catch (\Throwable $th) {
  44. // 记录错误信息
  45. Log::error('tag_relation',$th->getMessage());
  46. // 返回结果
  47. return 0;
  48. }
  49. }
  50. /**
  51. * 客户关系同步
  52. *
  53. * */
  54. public function sync_user(Model $Model)
  55. {
  56. // 更新维度
  57. $source = 'tag_relation';
  58. // 同步关系的任务
  59. $task = $Model->getOneBySource($source);
  60. // 返回结果
  61. if( !$task['id'] ) return ['error'=>'无执行任务'];
  62. // 获取列表
  63. $userList = OpenApi::getUserList($task['limit'],$task['offset'],$task['last_time'],$source);
  64. // 放到队列执行
  65. foreach ($userList['user_list'] as $key=>$value) {
  66. // 外部联系人
  67. $value = ['id'=>$value['id']];
  68. // 发送到队列执行,上锁成功则发送到队列执行
  69. if( $Model->lockSyncExtidMark($value['id']) ) WeiBanSyncJobs::dispatch($value);
  70. // 重组下数据
  71. $userList['user_list'][$key] = $value;
  72. }
  73. // 存在用户列表的话,更新OR新增
  74. if( $userList['user_list'] ) {
  75. // 更新下一页偏移量
  76. $task['offset'] = $task['offset'] + $task['limit'];
  77. // 同步数量
  78. $task['sync_total'] = $task['sync_total'] + count($userList['user_list']);
  79. }
  80. // 更新任务总数
  81. if( $userList['total'] ) {
  82. // 更新任务总数
  83. $task['total'] = $userList['total'];
  84. // 偏移量大于总数,更新任务状态
  85. if( $task['offset'] >= $userList['total'] ) {
  86. // 状态结束
  87. $task['status'] = 1;
  88. // 最后的修改时间
  89. $task['last_time'] = time() - 1;
  90. }
  91. }
  92. // 修改任务情况
  93. $Model->edit($task['id'],$task);
  94. }
  95. }