StatisticsPage.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 StatisticsPage extends Command
{
    public $error = 0;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计当月受访页面数据';

    public $logic;

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

    /**
     * 定时执行
     * @return void
     */
    public function handle()
    {
        $date = $this->logic->getMonths(TrafficStatistics::TYPE_PAGE);
        if (!empty($date)) {
            foreach ($date as $value) {
                echo $this->statistics_page($value);
            }
        }
        echo $this->statistics_page();
    }

    /**
     * 统计当月受访页面数据
     * @param null $date
     * @return int|mixed
     * @throws Throwable
     */
    protected function statistics_page($date = null)
    {
        $customerVisit = new CustomerVisit();
        $top           = json_encode($customerVisit->getPageCount($date) ?? []);
        $type          = TrafficStatistics::TYPE_PAGE;
        $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;
    }
}