StaffRelation.php 2.7 KB

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