StatisticsSource.php 1.7 KB
<?php

namespace App\Console\Commands\Bside\Statistics;

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

class StatisticsSource extends Command
{
    public $error = 0;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'statistics_source';

    /**
     * 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();
        if (!empty($date)) {
            foreach ($date as $value) {
                echo $this->statistics_source($value);
            }
        }
        echo $this->statistics_source();
    }

    /**
     * 统计当天流量趋势数据
     * @param string|null $date
     * @return int|mixed
     * @throws Throwable
     */
    protected function statistics_source(string $date = null)
    {
        // 获取当天的IP|PV数量
        $customerVisit = new CustomerVisit();
        $top           = json_encode($customerVisit->getUrlCount($date) ?? []);
        $type          = TrafficStatistics::TYPE_SOURCE;
        $res           = $this->logic->save(compact('top', 'type'), $type, $date);
        if (!$res['status']) {
            $this->info($res['msg']);
            $this->error++;
        }
        $this->info($res['msg']);
        return $this->error;
    }
}