<?php namespace App\Console\Commands\Channel; use App\Modules\OfficialAccount\Models\OfficialAccount; use Illuminate\Console\Command; use Redis; class SiteTitle extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'SetSiteTitle'; /** * The console command description. * * @var string */ protected $description = '设置站点名称'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $channel_ids = $this->getChannelIds(); foreach ($channel_ids as $channel_id) { $key = sprintf('channel:setting:%s', $channel_id); $hkey = 'title'; $count = $this->getChannelOfficialAccountCount($channel_id); if ($count > 1) { Redis::hDel($key, $hkey); } else if ($count == 1) { $official_account = $this->getOfficialAccount($channel_id); Redis::hSet($key, $hkey, $official_account->nickname); } } } private function getChannelIds() { return OfficialAccount::select('distribution_channel_id')->groupBy('distribution_channel_id')->pluck('distribution_channel_id')->all(); } private function getOfficialAccount(int $channel_id) { return OfficialAccount::where('distribution_channel_id', $channel_id)->first(); } private function getChannelOfficialAccountCount(int $channel_id) { return OfficialAccount::where('distribution_channel_id', $channel_id)->count(); } }