StatisticsTrend.php 1.9 KB
<?php

namespace App\Console\Commands\Bside\Statistics;

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

class StatisticsTrend extends Command
{
    public $error = 0;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计当月流量趋势数据';

    public $logic;

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

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

    /**
     * 统计当天流量趋势数据
     * @param string|null $date
     * @return int|mixed
     * @throws GuzzleException|Throwable
     */
    protected function statistics_trend(string $date = null)
    {
        $domain        = request()->getHttpHost(); //'www.wowstainless.com';
        $sta_date      = !is_null($date) ? $date . "-01" : date('Y-m-01');
        $inquiry       = getInquiryInformation($domain, $sta_date);
        $innum         = $inquiry['count'] ?? 0;
        $top           = json_encode($inquiry['lists'] ?? []);
        $type          = TrafficStatistics::TYPE_TREND;
        $res           = $this->logic->save(compact('top', 'type', 'innum'), $type, $date, '统计当月流量趋势数据');
        if (!$res['status']) {
            $this->info($res['msg']);
            $this->error++;
        }
        $this->info($res['msg']);
        return $this->error;
    }
}