<?php

namespace app\command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use support\think\Db;

class OpenIm extends Command
{
    protected static $defaultName = 'openim';
    protected static $defaultDescription = 'OpenIm';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addOption('action','a', InputArgument::OPTIONAL, '操作类型');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $action = $input->getOption('action');
        if(!$action){
            $output->writeln('空操作');
            return self::FAILURE;
        }
        if(method_exists($this, $action)){
            return $this->$action($input, $output);
        }
        return self::FAILURE;
    }
    private function change_user(InputInterface $input, OutputInterface $output):int{
        
        $im = new \support\OpenImSdk\Client([
            'host' => 'http://127.0.0.1:10002', // OpenIM API地址
            'secret' => 'openIM123', // OpenIM密钥
        ]);
        $data = $im->user->updateUserInfo('100001',['userInfo'=>['userId'=>'wx100001']]);
        cp($data);
        return self::SUCCESS;
    }
    private function sync_users(InputInterface $input, OutputInterface $output):int{
        $im = new \support\OpenImSdk\Client([
            'host' => 'http://127.0.0.1:10002', // OpenIM API地址
            'secret' => 'openIM123', // OpenIM密钥
        ]);
        $data = $im->user->getAllUsersUid(1,1000);
        cp($data);
        return self::SUCCESS;
        $exsit_user_ids = Db::name('user')->whereIn('id',$data['userIDs'])->column('id');
        $not_exsit_user_ids =array_diff($data['userIDs'],$exsit_user_ids);
        if(count($not_exsit_user_ids)> 0){
            //同步用户
            $res = $im->user->getUsersInfo($not_exsit_user_ids);
            $save_data = [];
            foreach($res['usersInfo'] as $k=>$_user){
                array_push($save_data,[
                    'id'    =>  $_user['userID'],
                    'nickname'    =>  $_user['nickname'],
                    'password'  =>  '123456',
                    'avatar'    =>  $_user['faceURL']
                ]);
                
                // "ex": "",
                // "createTime": 1688454168302,
                // "appMangerLevel": 18,
                // "globalRecvMsgOpt": 0
            }
            if(!empty($save_data)){
                Db::name('user')->insertAll($save_data);
            }
        }
        return self::SUCCESS;
    }
}
