TrafficStatistics.php 6.0 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\TrafficStatistics
 *
 * @property int $id
 * @property int|null $type 类型:
 * 1 - 访问来源
 * 2 - 地域分布
 * 3 - 受访页面
 * 4 - 访问终端
 * 5 - 流量趋势
 * @property int|null $year 年
 * @property int|null $month 月
 * @property string|null $top 访问来源|国家|页面TOP15 - 百分比 - json格式
 * @property int|null $innum 询盘量
 * @property float|null $conversion 询盘转化率
 * @property int|null $pvnum 浏览量
 * @property int|null $ipnum 访客量
 * @property int|null $pcnum PC端访问量
 * @property int|null $mbnum 移动端访问量
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static Builder|TrafficStatistics newModelQuery()
 * @method static Builder|TrafficStatistics newQuery()
 * @method static Builder|TrafficStatistics query()
 * @method static Builder|TrafficStatistics whereConversion($value)
 * @method static Builder|TrafficStatistics whereCreatedAt($value)
 * @method static Builder|TrafficStatistics whereId($value)
 * @method static Builder|TrafficStatistics whereInnum($value)
 * @method static Builder|TrafficStatistics whereIpnum($value)
 * @method static Builder|TrafficStatistics whereMbnum($value)
 * @method static Builder|TrafficStatistics whereMonth($value)
 * @method static Builder|TrafficStatistics wherePageview($value)
 * @method static Builder|TrafficStatistics wherePcnum($value)
 * @method static Builder|TrafficStatistics wherePvnum($value)
 * @method static Builder|TrafficStatistics whereTop($value)
 * @method static Builder|TrafficStatistics whereType($value)
 * @method static Builder|TrafficStatistics whereUpdatedAt($value)
 * @method static Builder|TrafficStatistics whereUserSessions($value)
 * @method static Builder|TrafficStatistics whereYear($value)
 * @mixin \Eloquent
 */
class TrafficStatistics extends Base
{
    protected $table = 'gl_traffic_statistics';

    /** @var int 访问来源 */
    const TYPE_SOURCE = 1;
    /** @var int 地域分布 */
    const TYPE_DISTRIBUTION = 2;
    /** @var int 受访页面 */
    const TYPE_PAGE = 3;
    /** @var int 访问终端 */
    const TYPE_TERMINAL = 4;
    /** @var int 流量趋势 */
    const TYPE_TREND = 5;
    /** @var string[] 类型对应字段 */
    const FIELDS     = [
        self::TYPE_SOURCE       => ['pvnum', 'ipnum', 'top'],
        self::TYPE_DISTRIBUTION => ['pvnum', 'ipnum', 'top'],
        self::TYPE_PAGE         => ['pvnum', 'ipnum', 'top'],
        self::TYPE_TERMINAL     => ['pvnum', 'ipnum', 'top', 'pcnum', 'mbnum'],
        self::TYPE_TREND        => ['pvnum', 'ipnum', 'top', 'innum'],
    ];
    /** @var string[] 数据库字段对应返回字段 */
    const KEY_VALUE  = [
        'pvnum' => 'pv_count',
        'ipnum' => 'ip_count',
        'pcnum' => 'pc_count',
        'mbnum' => 'mobile_count',
        'innum' => 'in_count',
        'top'   => 'lists'
    ];

    /**
     * 根据类型获取字段
     * @param int $type
     * @return string[]
     */
    public function selectFields(int $type = self::TYPE_SOURCE)
    {
        return self::FIELDS[$type] ?? [];
    }

    /**
     * 根据类型获取月份列表数据
     * @param int $type
     * @param $date
     * @return TrafficStatistics[]|Builder[]|Collection
     */
    public function getMonthsLists(int $type = self::TYPE_SOURCE, $date = null)
    {
        $query = $this->query()->where('type', $type);
        if ($date != null) {
            if (is_array($date)) {
                $years = $months = [];
                foreach ($date as $value) {
                    $dd    = explode('-', $value);
                    $year  = $dd[0];
                    $month = $dd[1];
                    if (!in_array($year, $years)) {
                        $years[] = $year;
                    }
                    if (!in_array($month, $months)) {
                        $months[] = $month;
                    }
                }
                $query->whereIn('year', $years)
                    ->whereIn('month', $months);
            } else {
                $dd    = explode('-', $date);
                $year  = $dd[0];
                $month = $dd[1];
                $query->where('year', $year)
                    ->where('month', $month);
            }
        } else {
            $query->where('year', date('Y'))
                ->where('month', date('m'));
        }
        return $query->get();
    }

    /**
     * 根据类型获取单条月份数据
     * @param int $type
     * @param $year
     * @param $month
     * @return TrafficStatistics|Builder|Model|object|null
     */
    public function getMonth(int $type = self::TYPE_SOURCE, $year = null, $month = null)
    {
        $year  = $year ?? date('Y');
        $month = $month ?? date('m');
        return $this->query()->where('type', $type)
            ->where('year', $year)
            ->where('month', $month)
            ->first();
    }

    /**
     * 根据类型获取数据
     * @param int $type
     * @param null $year
     * @param null $month
     * @return TrafficStatistics|Builder|Model|object|null
     */
    public function getData(int $type = self::TYPE_SOURCE, $year = null, $month = null)
    {
        $key_value = self::KEY_VALUE;
        $field_arr = [];
        foreach ($this->selectFields($type) as $field) {
            $field_arr[] = "{$field} as {$key_value[$field]}";
        }
        $query = $this->query();
        if ($field_arr) {
            $query->selectRaw(implode(',', $field_arr));
        }
        return $query->where('type', $type)
            ->where('year', $year)
            ->where('month', $month)
            ->first();
    }

    /**
     * 格式化数据
     * @param $value
     * @return mixed
     */
    public function getListsAttribute($value)
    {
        return json_decode($value);
    }
}