StaffRemark.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 StaffRemark extends Command
  9. {
  10. /**
  11. * 任务名称
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'staff_remark';
  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_remark',$th->getMessage());
  45. return 0;
  46. }
  47. }
  48. /**
  49. * 客户关系同步
  50. *
  51. * */
  52. public function sync_user(Model $Model)
  53. {
  54. // 更新维度
  55. $source = 'remark';
  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. // ['id'=>$value['id'],'name'=>$value['name'],'avatar'=>str_ireplace('http://','https://',(string)$value['avatar']),'gender'=>$value['gender'],'type'=>$value['type'],'corp_name'=>(string)$value['corp_name'],'corp_full_name'=>(string)$value['corp_full_name'],'insert_time'=>$value['created_at'],'update_time'=>$value['updated_at'],];
  67. // 发送到队列执行,上锁成功则发送到队列执行
  68. if( $Model->lockSyncExtidMark($value['id']) ) WeiBanSyncJobs::dispatch($value);
  69. // 重组下数据
  70. $userList['user_list'][$key] = $value;
  71. }
  72. // 存在用户列表的话,更新OR新增
  73. if( $userList['user_list'] ) {
  74. // 更新下一页偏移量
  75. $task['offset'] = $task['offset'] + $task['limit'];
  76. // 同步数量
  77. $task['sync_total'] = $task['sync_total'] + count($userList['user_list']);
  78. }
  79. // 更新任务总数
  80. if( $userList['total'] ) {
  81. // 更新任务总数
  82. $task['total'] = $userList['total'];
  83. // 偏移量大于总数,更新任务状态
  84. if( $task['offset'] >= $userList['total'] ) {
  85. // 状态结束
  86. $task['status'] = 1;
  87. // 最后的修改时间
  88. $task['last_time'] = time() - 1;
  89. }
  90. }
  91. // 修改任务情况
  92. $Model->edit($task['id'],$task);
  93. }
  94. }