TrafficTrends.php 2.5 KB
<?php

namespace App\Models\Bside\Statistics;

use App\Models\Base;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Bside\Statistics\TrafficTrends
 *
 * @property int $id
 * @property string|null $day 统计当天日期
 * @property int|null $pvnum 当天的访问次数PV
 * @property int|null $ipnum 当天的独立访问IP数量
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static Builder|TrafficTrends newModelQuery()
 * @method static Builder|TrafficTrends newQuery()
 * @method static Builder|TrafficTrends query()
 * @method static Builder|TrafficTrends whereCreatedAt($value)
 * @method static Builder|TrafficTrends whereDay($value)
 * @method static Builder|TrafficTrends whereId($value)
 * @method static Builder|TrafficTrends whereIpnum($value)
 * @method static Builder|TrafficTrends wherePvnum($value)
 * @method static Builder|TrafficTrends whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class TrafficTrends extends Base
{
    protected $table = 'gl_traffic_trends';

    /**
     * 根据时间获取数据
     * @param string|null $date 日期,格式:Y-m-d
     * @return TrafficTrends[]|Builder[]|Collection
     */
    public function getDaysLists(string $date = null)
    {
        $query = $this->query();
        if ($date != null && is_array($date)) {
            $query->whereIn('day', $date);
        } else {
            $query->where('day', $date);
        }
        return $query->get();
    }

    /**
     * @param string|null $date 日期,格式:Y-m-d
     * @return TrafficTrends|Builder|Model|object|null
     */
    public function getDay(string $date = null)
    {
        return $this->query()->where('day', $date ?? date('Y-m-d'))->first();
    }

    /**
     * 获取当月的IP|PV数量
     * @param string|null $date 日期,格式:Y-m
     * @return Builder[]|Collection
     */
    public function getMonthsLists(string $date = null)
    {
        if (!is_null($date)) {
            $dd    = explode('-', $date);
            $year  = $dd[0];
            $month = $dd[1];
        } else {
            $year  = date('Y');
            $month = date('m');
        }
        return $this->query()
            ->selectRaw('day as date, pvnum as pv_count, ipnum as ip_count')
            ->whereYear('day', '=', $year)
            ->whereMonth('day', '=', $month)
            ->orderBy('day', 'asc')
            ->get();
    }
}