TagRelation.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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']);
  64. // 最后的更新时间
  65. $lastTime = 0;
  66. // 放到队列执行
  67. foreach ($userList['user_list'] as $key=>$value) {
  68. // 获取最后更新时间
  69. $lastTime = $value['updated_at'];
  70. // 外部联系人
  71. $value = ['id'=>$value['id']];
  72. // 发送到队列执行,上锁成功则发送到队列执行
  73. if( $Model->lockSyncExtidMark($value['id']) ) WeiBanSyncJobs::dispatch($value);
  74. // 重组下数据
  75. $userList['user_list'][$key] = $value;
  76. }
  77. // 存在用户列表的话,更新OR新增
  78. if( $userList['user_list'] ) {
  79. // 更新下一页偏移量
  80. $task['offset'] = $task['offset'] + $task['limit'];
  81. // 同步数量
  82. $task['sync_total'] = $task['sync_total'] + count($userList['user_list']);
  83. }
  84. // 更新任务总数
  85. if( $userList['total'] ) {
  86. // 更新任务总数
  87. $task['total'] = $userList['total'];
  88. // 偏移量大于总数,更新任务状态
  89. if( $task['offset'] >= $userList['total'] ) {
  90. // 状态结束
  91. $task['status'] = 1;
  92. // 最后的修改时间
  93. $task['last_time'] = $lastTime - 1;
  94. }
  95. }
  96. // 修改任务情况
  97. $Model->edit($task['id'],$task);
  98. }
  99. }