StatisticsTerminal.php 1.9 KB
<?php

namespace App\Console\Commands\Bside\Statistics;

use App\Models\Bside\Statistics\TrafficStatistics;
use App\Models\CustomerVisit\CustomerVisit;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Throwable;

class StatisticsTerminal extends Command
{
    public $error = 0;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'statistics_terminal';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计当月访问终端数据';

    public $logic;

    public function __construct()
    {
        parent::__construct();
        $this->logic = new Logic();
    }

    /**
     * @return void
     * @throws Throwable
     */
    public function handle()
    {
        $date = $this->logic->getMonths(TrafficStatistics::TYPE_TERMINAL);
        if (!empty($date)) {
            foreach ($date as $value) {
                echo $this->statistics_terminal($value);
            }
        }
        echo $this->statistics_terminal();
    }

    /**
     * 统计当月访问终端数据
     * @param string|null $date
     * @return int|mixed
     * @throws Throwable
     */
    protected function statistics_terminal(string $date = null)
    {
        $customerVisit = new CustomerVisit();
        $pcnum         = $customerVisit->getTerminalPcCount($date);
        $mbnum         = $customerVisit->getTerminalMobileCount($date);
        $top           = json_encode($customerVisit->getCountryCount($date) ?? []);
        $type          = TrafficStatistics::TYPE_TERMINAL;
        $res           = $this->logic->save(compact('top', 'type', 'pcnum', 'mbnum'), $type, $date, '统计当月访问终端数据');
        if (!$res['status']) {
            $this->info($res['msg']);
            $this->error++;
        }
        $this->info($res['msg']);
        return $this->error;
    }
}