123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Facades\Servers\WeiBan\OpenApi;
- use App\Models\WeiBan\Sync as Model;
- use App\Jobs\WeiBanSync as WeiBanSyncJobs;
- use App\Facades\Servers\Logs\Log;
- class TagRelation extends Command
- {
- /**
- * 任务名称
- *
- * @var string
- */
- protected $signature = 'tag_relation';
- /**
- * 任务描述
- *
- * @var string
- */
- protected $description = '微伴标签数据同步任务';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function handle()
- {
- try {
- // 数据结果
- $this->sync_user(new Model());
- // 返回结果
- return 0;
- } catch (\Throwable $th) {
- // 记录错误信息
- Log::error('tag_relation',$th->getMessage());
- // 返回结果
- return 0;
- }
- }
-
- /**
- * 客户关系同步
- *
- * */
- public function sync_user(Model $Model)
- {
- // 更新维度
- $source = 'tag_relation';
- // 同步关系的任务
- $task = $Model->getOneBySource($source);
- // 返回结果
- if( !$task['id'] ) return ['error'=>'无执行任务'];
- // 获取列表
- $userList = OpenApi::getUserList($task['limit'],$task['offset'],$task['last_time']);
- // 最后的更新时间
- $lastTime = 0;
- // 放到队列执行
- foreach ($userList['user_list'] as $key=>$value) {
- // 获取最后更新时间
- $lastTime = $value['updated_at'];
- // 外部联系人
- $value = ['id'=>$value['id']];
- // 发送到队列执行,上锁成功则发送到队列执行
- if( $Model->lockSyncExtidMark($value['id']) ) WeiBanSyncJobs::dispatch($value);
- // 重组下数据
- $userList['user_list'][$key] = $value;
- }
- // 存在用户列表的话,更新OR新增
- if( $userList['user_list'] ) {
- // 更新下一页偏移量
- $task['offset'] = $task['offset'] + $task['limit'];
- // 同步数量
- $task['sync_total'] = $task['sync_total'] + count($userList['user_list']);
- }
- // 更新任务总数
- if( $userList['total'] ) {
- // 更新任务总数
- $task['total'] = $userList['total'];
- // 偏移量大于总数,更新任务状态
- if( $task['offset'] >= $userList['total'] ) {
- // 状态结束
- $task['status'] = 1;
- // 最后的修改时间
- $task['last_time'] = $lastTime - 1;
- }
- }
- // 修改任务情况
- $Model->edit($task['id'],$task);
- }
- }
|