<?php
/**
 维护渠道菜单
 */
namespace App\Console\Commands\Tool;

use App\Modules\OfficialAccount\Models\OfficialAccount;
use App\Modules\OfficialAccount\Services\CustomMsgService;
use App\Modules\OfficialAccount\Services\OfficialAccountService;
use Log;
use Illuminate\Console\Command;
use App\Modules\Channel\Models\Channel;
use DB;

class MaintainMenus extends Command
{
    /**
     * nohup php artisan Tool:MaintainMenus --distribution_channel_id="-1" &
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Tool:MaintainMenus {--appid=}  {--account=} ';

    /**
     * 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"));
        
        $appid = $this->option('appid');
        $account = $this->option('account');

        if(empty($appid) && empty($account)){
        	\Log::info('MaintainMenus_param_is_null:'.$appid);
        	exit();
        }
        
        $appids = [];
        if(empty($account)){
        	 $appids[] = $appid;
        }else{
        	$appids = OfficialAccountService::get_account_appids($account);
        }
        
        \Log::info('MaintainMenus_start:appids_num:'.count($appids));
        \Log::info($appids);
        
        foreach($appids as $key=>$appid){
        	\Log::info('MaintainMenus_one_start:'.$appid);
        	OfficialAccountService::update_official_account_menu($appid);
        }
       
        \Log::info('MaintainMenus_end');
        
        print_r("======维护菜单 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
        print_r('memory_used:'.memory_get_usage()."\n");
    }
}