<?php
/**
公众号用户迁移
 */

namespace App\Console\Commands\Tool;

use Log;
use Illuminate\Console\Command;
use App\Modules\User\Services\UserService;
use App\Modules\OfficialAccount\Services\ForceSubscribeService;
use DB;

class OfficialAccountUserTranser extends Command
{
    /**
     * 执行命令   Tool:OfficialAccountUserTranser  {distribution_channel_id} {from_appid} {to_appid}
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Tool:OfficialAccountUserTranser {distribution_channel_id} {from_appid} {to_appid}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '公众号用户数据迁移';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        ini_set('memory_limit', '1024M');
        print_r('memory_used:'.memory_get_usage()."\n");
        print_r("======公众号用户数据迁移【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
        
        $distribution_channel_id = $this->argument('distribution_channel_id');
        $from_appid = $loop = $this->argument('from_appid');
        $to_appid = $this->argument('to_appid');
        
        \Log::info('OfficialAccountUserTranser_start:distribution_channel_id:'.$distribution_channel_id.' from_appid:'.$from_appid.' to_appid:'.$to_appid);
        $next_openid = '';
        $skip = $loop = 0;
        while ( $loop <= 50) {
        	$loop++;
        	$users = array();
        	// 全服务号粉丝发，要记录next_openid
        	if($loop > 1 && empty($next_openid)){
        		\Log::info('OfficialAccountUserTranse_break,loop:'.$loop.' skip:'.$skip);
        		break;
        	}
        	$user_res = ForceSubscribeService::getFullOfficialAccountUsers($to_appid,$next_openid);
        	$users = $user_res['openids'];
        	$next_openid = $user_res['next_openid'];

        	\Log::info('loop'.$loop.' user_num:'.count($users).' next_openid：'.$next_openid);
        	//                         \Log::info($users);
        	$skip += count($users);
        	if(count($users)>0){
        		for ($i=0; $i < count($users); $i++) {
        			$openid = isset($users[$i]['openid'])?$users[$i]['openid']:'';
        			$userinfo = ForceSubscribeService::getUserInfo($to_appid, $openid);
        			$nickname = isset($userinfo['nickname'])?$userinfo['nickname']:'';
        			\Log::info('openid:'.$openid.' nickname:'.$nickname);
        			// 
        			if(empty($nickname)) continue;
        			$origin_user = UserService::getUserByNickAndChannelId($distribution_channel_id,$nickname);
        			if(!empty($origin_user)){
        				\Log::info('distribution_channel_id:'.$distribution_channel_id.' to_appid:'.$to_appid.' openid:'.$openid.' uid:'.$origin_user->id);
        				ForceSubscribeService::updateSubscribeUserTransfer($distribution_channel_id,$origin_user->id,$from_appid,$to_appid,$openid);
        			}
        		}
        	}
        }
       
        \Log::info('OfficialAccountUserTranser_end loop:'.$loop.' $skip:'.$skip);
        
        print_r("======公众号用户数据迁移 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
        print_r('memory_used:'.memory_get_usage()."\n");
    }
}