StaffRelation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 StaffRelation extends Command
  9. {
  10. /**
  11. * 任务名称
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'staff_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. return 0;
  42. } catch (\Throwable $th) {
  43. // 记录错误信息
  44. Log::error('staff_relation',$th->getMessage());
  45. return 0;
  46. }
  47. }
  48. /**
  49. * 客户关系同步
  50. *
  51. * */
  52. public function sync_user(Model $Model)
  53. {
  54. // 更新维度
  55. $source = 'staff_relation';
  56. // 同步关系的任务
  57. $task = $Model->getOneBySource($source);
  58. // 返回结果
  59. if( !$task['id'] ) return ['error'=>'无执行任务'];
  60. // 获取列表
  61. $userList = OpenApi::getUserList($task['limit'],$task['offset'],$task['last_time']);
  62. // 最后的更新时间
  63. $lastTime = 0;
  64. // 放到队列执行
  65. foreach ($userList['user_list'] as $key=>$value) {
  66. // 获取最后更新时间
  67. $lastTime = $value['updated_at'];
  68. // 外部联系人
  69. $value = ['id'=>$value['id']];
  70. // 发送到队列执行,上锁成功则发送到队列执行
  71. if( $Model->lockSyncExtidMark($value['id']) ) WeiBanSyncJobs::dispatch($value);
  72. // 重组下数据
  73. $userList['user_list'][$key] = $value;
  74. }
  75. // 存在用户列表的话,更新OR新增
  76. if( $userList['user_list'] ) {
  77. // 更新下一页偏移量
  78. $task['offset'] = $task['offset'] + $task['limit'];
  79. // 同步数量
  80. $task['sync_total'] = $task['sync_total'] + count($userList['user_list']);
  81. }
  82. // 更新任务总数
  83. if( $userList['total'] ) {
  84. // 更新任务总数
  85. $task['total'] = $userList['total'];
  86. // 偏移量大于总数,更新任务状态
  87. if( $task['offset'] >= $userList['total'] ) {
  88. // 状态结束
  89. $task['status'] = 1;
  90. // 最后的修改时间
  91. $task['last_time'] = $lastTime - 1;
  92. }
  93. }
  94. // 修改任务情况
  95. $Model->edit($task['id'],$task);
  96. }
  97. }